Skip to content

Commit 6038c37

Browse files
committed
[BROKEN] Add unit test for blinding logic
1 parent aa33202 commit 6038c37

File tree

2 files changed

+372
-1
lines changed

2 files changed

+372
-1
lines changed

src/Makefile.test.include

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ BITCOIN_TESTS =\
9696
test/util_tests.cpp \
9797
test/validation_block_tests.cpp \
9898
test/versionbits_tests.cpp \
99-
test/pegin_spent_tests.cpp
99+
test/pegin_spent_tests.cpp \
100+
test/blind_tests.cpp
100101
# ELEMENTS IN THE END
101102

102103
if ENABLE_PROPERTY_TESTS

src/test/blind_tests.cpp

Lines changed: 370 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
// Copyright (c) 2013-2019 The Elements Core developers
2+
// Distributed under the MIT/X11 software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <arith_uint256.h>
6+
#include <blind.h>
7+
#include <coins.h>
8+
#include <uint256.h>
9+
#include <validation.h>
10+
11+
#include <test/test_bitcoin.h>
12+
13+
#include <boost/test/unit_test.hpp>
14+
15+
#include <secp256k1.h>
16+
17+
// For elements serialization rules
18+
struct ElementsSetup : public TestingSetup {
19+
ElementsSetup() : TestingSetup("custom") {}
20+
};
21+
22+
BOOST_FIXTURE_TEST_SUITE(blind_tests, ElementsSetup)
23+
24+
// TODO: Make deterministic blinding wrapper function, test caching more exactly
25+
26+
BOOST_AUTO_TEST_CASE(naive_blinding_test)
27+
{
28+
CKey key1;
29+
CKey key2;
30+
CKey keyDummy;
31+
32+
// Any asset id will do
33+
CAsset bitcoinID(GetRandHash());
34+
CAsset otherID(GetRandHash());
35+
CAsset unblinded_id;
36+
uint256 asset_blind;
37+
CScript op_true(OP_TRUE);
38+
std::vector<CKey> vDummy;
39+
40+
unsigned char k1[32] = {1,2,3};
41+
unsigned char k2[32] = {22,33,44};
42+
unsigned char kDummy[32] = {133,144,155};
43+
key1.Set(&k1[0], &k1[32], true);
44+
key2.Set(&k2[0], &k2[32], true);
45+
keyDummy.Set(&kDummy[0], &kDummy[32], true);
46+
CPubKey pubkey1 = key1.GetPubKey();
47+
CPubKey pubkey2 = key2.GetPubKey();
48+
CPubKey pubkeyDummy = keyDummy.GetPubKey();
49+
50+
uint256 blind3, blind4, blindDummy;
51+
52+
std::vector<CTxOut> inputs;
53+
CTxOut btc_oo(bitcoinID, 11, CScript());
54+
CTxOut btc_ooo(bitcoinID, 111, CScript());
55+
CTxOut other_fzz(otherID, 500, CScript());
56+
CTxOut blind_ozz; // Will be computed later
57+
58+
{
59+
inputs.clear();
60+
inputs.push_back(btc_oo);
61+
inputs.push_back(btc_ooo);
62+
63+
// Build a transaction that spends 2 unblinded coins (11, 111), and produces a single blinded one (100) and fee (22).
64+
CMutableTransaction tx3;
65+
tx3.vin.resize(2);
66+
tx3.vin[0].prevout.hash = ArithToUint256(1);
67+
68+
tx3.vin[0].prevout.n = 0;
69+
tx3.vin[1].prevout.hash = ArithToUint256(2);
70+
tx3.vin[1].prevout.n = 0;
71+
tx3.vout.resize(0);
72+
tx3.vout.push_back(CTxOut(bitcoinID, 100, CScript() << OP_TRUE));
73+
// Fee outputs are blank scriptpubkeys, and unblinded value/asset
74+
tx3.vout.push_back(CTxOut(bitcoinID, 22, CScript()));
75+
BOOST_CHECK(VerifyAmounts(inputs, tx3, nullptr, false));
76+
77+
// Malleate the output and check for correct handling of bad commitments
78+
// These will fail IsValid checks
79+
std::vector<unsigned char> asset_copy(tx3.vout[0].nAsset.vchCommitment);
80+
std::vector<unsigned char> value_copy(tx3.vout[0].nValue.vchCommitment);
81+
tx3.vout[0].nAsset.vchCommitment[0] = 122;
82+
BOOST_CHECK(!VerifyAmounts(inputs, tx3, nullptr, false));
83+
tx3.vout[0].nAsset.vchCommitment = asset_copy;
84+
tx3.vout[0].nValue.vchCommitment[0] = 122;
85+
BOOST_CHECK(!VerifyAmounts(inputs, tx3, nullptr, false));
86+
tx3.vout[0].nValue.vchCommitment = value_copy;
87+
88+
// Make sure null values are handled correctly
89+
tx3.vout[0].nAsset.SetNull();
90+
BOOST_CHECK(!VerifyAmounts(inputs, tx3, nullptr, false));
91+
tx3.vout[0].nAsset.vchCommitment = asset_copy;
92+
tx3.vout[0].nValue.SetNull();
93+
BOOST_CHECK(!VerifyAmounts(inputs, tx3, nullptr, false));
94+
tx3.vout[0].nValue.vchCommitment = value_copy;
95+
96+
// Bad nonce values will result in failure to deserialize
97+
tx3.vout[0].nNonce.SetNull();
98+
BOOST_CHECK(VerifyAmounts(inputs, tx3, nullptr, false));
99+
tx3.vout[0].nNonce.vchCommitment = tx3.vout[0].nValue.vchCommitment;
100+
BOOST_CHECK(!VerifyAmounts(inputs, tx3, nullptr, false));
101+
102+
// Try to blind with a single non-fee output, which fails as its blinding factor ends up being zero.
103+
std::vector<uint256> input_blinds;
104+
std::vector<uint256> input_asset_blinds;
105+
std::vector<CAsset> input_assets;
106+
std::vector<CAmount> input_amounts;
107+
std::vector<uint256> output_blinds;
108+
std::vector<uint256> output_asset_blinds;
109+
std::vector<CPubKey> output_pubkeys;
110+
input_blinds.push_back(uint256());
111+
input_blinds.push_back(uint256());
112+
input_asset_blinds.push_back(uint256());
113+
input_asset_blinds.push_back(uint256());
114+
input_assets.push_back(bitcoinID);
115+
input_assets.push_back(bitcoinID);
116+
input_amounts.push_back(11);
117+
input_amounts.push_back(111);
118+
output_pubkeys.push_back(pubkey1);
119+
output_pubkeys.push_back(CPubKey());
120+
BOOST_CHECK(BlindTransaction(input_blinds, input_asset_blinds, input_assets, input_amounts, output_blinds, output_asset_blinds, output_pubkeys, vDummy, vDummy, tx3) == 0);
121+
122+
// Add a dummy output. Must be unspendable since it's 0-valued.
123+
tx3.vout.push_back(CTxOut(bitcoinID, 0, CScript() << OP_RETURN));
124+
output_pubkeys.push_back(pubkeyDummy);
125+
BOOST_CHECK(BlindTransaction(input_blinds, input_asset_blinds, input_assets, input_amounts, output_blinds, output_asset_blinds, output_pubkeys, vDummy, vDummy, tx3) == 2);
126+
BOOST_CHECK(!tx3.vout[0].nValue.IsExplicit());
127+
BOOST_CHECK(!tx3.vout[2].nValue.IsExplicit());
128+
BOOST_CHECK(VerifyAmounts(inputs, tx3, nullptr, false));
129+
130+
CAmount unblinded_amount;
131+
BOOST_CHECK(UnblindConfidentialPair(key2, tx3.vout[0].nValue, tx3.vout[0].nAsset, tx3.vout[0].nNonce, op_true, tx3.witness.vtxoutwit[0].vchRangeproof, unblinded_amount, blind3, unblinded_id, asset_blind) == 0);
132+
// Saving unblinded_id and asset_blind for later since we need for input
133+
BOOST_CHECK(UnblindConfidentialPair(key1, tx3.vout[0].nValue, tx3.vout[0].nAsset, tx3.vout[0].nNonce, op_true, tx3.witness.vtxoutwit[0].vchRangeproof, unblinded_amount, blind3, unblinded_id, asset_blind) == 1);
134+
BOOST_CHECK(unblinded_amount == 100);
135+
BOOST_CHECK(unblinded_id == bitcoinID);
136+
CAsset temp_asset;
137+
uint256 temp_asset_blinder;
138+
BOOST_CHECK(UnblindConfidentialPair(keyDummy, tx3.vout[2].nValue, tx3.vout[2].nAsset, tx3.vout[2].nNonce, CScript() << OP_RETURN, tx3.witness.vtxoutwit[2].vchRangeproof, unblinded_amount, blindDummy, temp_asset, temp_asset_blinder) == 1);
139+
BOOST_CHECK(unblinded_amount == 0);
140+
141+
// Storing for next section
142+
BOOST_CHECK(tx3.vout[0].nValue.IsCommitment());
143+
BOOST_CHECK(tx3.vout[0].nAsset.IsCommitment());
144+
blind_ozz = tx3.vout[0];
145+
146+
tx3.vout[1].nValue = CConfidentialValue(tx3.vout[1].nValue.GetAmount() - 1);
147+
BOOST_CHECK(!VerifyAmounts(inputs, tx3, nullptr, false));
148+
}
149+
150+
{
151+
inputs.clear();
152+
inputs.push_back(btc_ooo);
153+
inputs.push_back(blind_ozz);
154+
155+
// Build a transactions that spends an unblinded (111) and blinded (100) coin, and produces only unblinded coins (impossible)
156+
CMutableTransaction tx4;
157+
tx4.vin.resize(2);
158+
tx4.vin[0].prevout.hash = ArithToUint256(2);
159+
tx4.vin[0].prevout.n = 0;
160+
tx4.vin[1].prevout.hash = ArithToUint256(3);
161+
tx4.vin[1].prevout.n = 0;
162+
tx4.vout.push_back(CTxOut(bitcoinID, 30, CScript() << OP_TRUE));
163+
tx4.vout.push_back(CTxOut(bitcoinID, 40, CScript() << OP_TRUE));
164+
tx4.vout.push_back(CTxOut(bitcoinID, 111+100-30-40, CScript()));
165+
BOOST_CHECK(!VerifyAmounts(inputs, tx4, nullptr, false)); // Spends a blinded coin with no blinded outputs to compensate.
166+
167+
std::vector<uint256> input_blinds;
168+
std::vector<uint256> input_asset_blinds;
169+
std::vector<CAsset> input_assets;
170+
std::vector<CAmount> input_amounts;
171+
std::vector<uint256> output_blinds;
172+
std::vector<uint256> output_asset_blinds;
173+
std::vector<CPubKey> output_pubkeys;
174+
input_blinds.push_back(uint256());
175+
input_blinds.push_back(blind3);
176+
input_asset_blinds.push_back(uint256());
177+
input_asset_blinds.push_back(asset_blind);
178+
input_amounts.push_back(111);
179+
input_amounts.push_back(100);
180+
input_assets.push_back(unblinded_id);
181+
input_assets.push_back(unblinded_id);
182+
output_pubkeys.push_back(CPubKey());
183+
output_pubkeys.push_back(CPubKey());
184+
output_pubkeys.push_back(CPubKey());
185+
BOOST_CHECK(BlindTransaction(input_blinds, input_asset_blinds, input_assets, input_amounts, output_blinds, output_asset_blinds, output_pubkeys, vDummy, vDummy, tx4) == 0); // Blinds nothing
186+
}
187+
188+
{
189+
inputs.clear();
190+
inputs.push_back(btc_ooo);
191+
inputs.push_back(blind_ozz);
192+
193+
// Build a transactions that spends an unblinded (111) and blinded (100) coin, and produces a blinded (30), unblinded (40), and blinded (50) coin and fee (91)
194+
CMutableTransaction tx4;
195+
tx4.vin.resize(2);
196+
tx4.vin[0].prevout.hash = ArithToUint256(2);
197+
tx4.vin[0].prevout.n = 0;
198+
tx4.vin[1].prevout.hash = ArithToUint256(3);
199+
tx4.vin[1].prevout.n = 0;
200+
tx4.vout.push_back(CTxOut(bitcoinID, 30, CScript() << OP_TRUE));
201+
tx4.vout.push_back(CTxOut(bitcoinID, 40, CScript() << OP_TRUE));
202+
tx4.vout.push_back(CTxOut(bitcoinID, 50, CScript() << OP_TRUE));
203+
// Fee
204+
tx4.vout.push_back(CTxOut(bitcoinID, 111+100-30-40-50, CScript()));
205+
BOOST_CHECK(!VerifyAmounts(inputs, tx4, nullptr, false)); // Spends a blinded coin with no blinded outputs to compensate.
206+
207+
std::vector<uint256> input_blinds;
208+
std::vector<uint256> input_asset_blinds;
209+
std::vector<CAsset> input_assets;
210+
std::vector<CAmount> input_amounts;
211+
std::vector<uint256> output_blinds;
212+
std::vector<uint256> output_asset_blinds;
213+
std::vector<CPubKey> output_pubkeys;
214+
215+
input_blinds.push_back(uint256());
216+
input_blinds.push_back(blind3);
217+
input_asset_blinds.push_back(uint256());
218+
input_asset_blinds.push_back(asset_blind);
219+
input_amounts.push_back(111);
220+
input_amounts.push_back(100);
221+
input_assets.push_back(unblinded_id);
222+
input_assets.push_back(unblinded_id);
223+
224+
output_pubkeys.push_back(pubkey2);
225+
output_pubkeys.push_back(CPubKey());
226+
output_pubkeys.push_back(pubkey2);
227+
output_pubkeys.push_back(CPubKey());
228+
229+
BOOST_CHECK(BlindTransaction(input_blinds, input_asset_blinds, input_assets, input_amounts, output_blinds, output_asset_blinds, output_pubkeys, vDummy, vDummy, tx4) == 2);
230+
BOOST_CHECK(!tx4.vout[0].nValue.IsExplicit());
231+
BOOST_CHECK(tx4.vout[1].nValue.IsExplicit());
232+
BOOST_CHECK(!tx4.vout[2].nValue.IsExplicit());
233+
// This one broken
234+
BOOST_CHECK(VerifyAmounts(inputs, tx4, nullptr, false));
235+
236+
CAmount unblinded_amount;
237+
CAsset asset_out;
238+
uint256 asset_blinder_out;
239+
BOOST_CHECK(UnblindConfidentialPair(key1, tx4.vout[0].nValue, tx4.vout[0].nAsset, tx4.vout[0].nNonce, op_true, tx4.witness.vtxoutwit[0].vchRangeproof, unblinded_amount, blind4, asset_out, asset_blinder_out) == 0);
240+
BOOST_CHECK(UnblindConfidentialPair(key2, tx4.vout[0].nValue, tx4.vout[0].nAsset, tx4.vout[0].nNonce, op_true, tx4.witness.vtxoutwit[0].vchRangeproof, unblinded_amount, blind4, asset_out, asset_blinder_out) == 1);
241+
BOOST_CHECK(unblinded_amount == 30);
242+
BOOST_CHECK(asset_out == unblinded_id);
243+
BOOST_CHECK(UnblindConfidentialPair(key2, tx4.vout[2].nValue, tx4.vout[2].nAsset, tx4.vout[2].nNonce, op_true, tx4.witness.vtxoutwit[2].vchRangeproof, unblinded_amount, blind4, asset_out, asset_blinder_out) == 1);
244+
BOOST_CHECK(asset_out == unblinded_id);
245+
BOOST_CHECK(unblinded_amount == 50);
246+
247+
// Commit to the wrong script in the rangeproof
248+
BOOST_CHECK(UnblindConfidentialPair(key2, tx4.vout[2].nValue, tx4.vout[2].nAsset, tx4.vout[2].nNonce, CScript() << OP_FALSE, tx4.witness.vtxoutwit[2].vchRangeproof, unblinded_amount, blind4, asset_out, asset_blinder_out) == 0);
249+
250+
// Make invalid public keys in nonce commitment, first of right size
251+
tx4.vout[2].nNonce.vchCommitment = std::vector<unsigned char>(33, 0);
252+
tx4.vout[2].nNonce.vchCommitment[0] = 0x03;
253+
BOOST_CHECK(UnblindConfidentialPair(key2, tx4.vout[2].nValue, tx4.vout[2].nAsset, tx4.vout[2].nNonce, op_true, tx4.witness.vtxoutwit[2].vchRangeproof, unblinded_amount, blind4, asset_out, asset_blinder_out) == 0);
254+
255+
// Next, leading byte claiming to be 33 bytes in size
256+
tx4.vout[2].nNonce.vchCommitment.resize(1);
257+
BOOST_CHECK(UnblindConfidentialPair(key2, tx4.vout[2].nValue, tx4.vout[2].nAsset, tx4.vout[2].nNonce, op_true, tx4.witness.vtxoutwit[2].vchRangeproof, unblinded_amount, blind4, asset_out, asset_blinder_out) == 0);
258+
259+
// Last, blank nonce commitment
260+
tx4.vout[2].nNonce.vchCommitment.clear();
261+
BOOST_CHECK(UnblindConfidentialPair(key2, tx4.vout[2].nValue, tx4.vout[2].nAsset, tx4.vout[2].nNonce, op_true, tx4.witness.vtxoutwit[2].vchRangeproof, unblinded_amount, blind4, asset_out, asset_blinder_out) == 0);
262+
263+
tx4.vout[3].nValue = CConfidentialValue(tx4.vout[3].nValue.GetAmount() - 1);
264+
BOOST_CHECK(!VerifyAmounts(inputs, tx4, nullptr, false));
265+
266+
// Check wallet borromean-based rangeproof results against expected args
267+
size_t proof_size = DEFAULT_RANGEPROOF_SIZE;
268+
BOOST_CHECK(tx4.witness.vtxoutwit[2].vchRangeproof.size() == proof_size);
269+
secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
270+
int exp = 0;
271+
int mantissa = 0;
272+
uint64_t min_value = 0;
273+
uint64_t max_value = 0;
274+
BOOST_CHECK(secp256k1_rangeproof_info(ctx, &exp, &mantissa, &min_value, &max_value, tx4.witness.vtxoutwit[2].vchRangeproof.data(), proof_size) == 1);
275+
BOOST_CHECK(exp == 0);
276+
BOOST_CHECK(mantissa == 36); // 36 bit default
277+
BOOST_CHECK(min_value == 1);
278+
BOOST_CHECK(max_value == 68719476736);
279+
}
280+
{
281+
inputs.clear();
282+
inputs.push_back(blind_ozz);
283+
inputs.push_back(other_fzz);
284+
285+
// Spends 100 blinded bitcoin, 500 of unblinded "other"
286+
CMutableTransaction tx5;
287+
tx5.vin.resize(0);
288+
tx5.vout.resize(0);
289+
tx5.vin.push_back(CTxIn(COutPoint(ArithToUint256(3), 0)));
290+
tx5.vin.push_back(CTxIn(COutPoint(ArithToUint256(5), 0)));
291+
tx5.vout.push_back(CTxOut(bitcoinID, 29, CScript() << OP_TRUE));
292+
tx5.vout.push_back(CTxOut(bitcoinID, 70, CScript() << OP_TRUE));
293+
tx5.vout.push_back(CTxOut(otherID, 250, CScript() << OP_TRUE));
294+
tx5.vout.push_back(CTxOut(otherID, 249, CScript() << OP_TRUE));
295+
// Fees
296+
tx5.vout.push_back(CTxOut(bitcoinID, 1, CScript()));
297+
tx5.vout.push_back(CTxOut(otherID, 1, CScript()));
298+
299+
// Blinds don't balance
300+
BOOST_CHECK(!VerifyAmounts(inputs, tx5, nullptr, false));
301+
302+
// Blinding setup stuff
303+
std::vector<uint256> input_blinds;
304+
std::vector<uint256> input_asset_blinds;
305+
std::vector<CAsset> input_assets;
306+
std::vector<CAmount> input_amounts;
307+
std::vector<uint256> output_blinds;
308+
std::vector<uint256> output_asset_blinds;
309+
std::vector<CPubKey> output_pubkeys;
310+
input_blinds.push_back(blind3);
311+
input_blinds.push_back(uint256()); //
312+
input_asset_blinds.push_back(asset_blind);
313+
input_asset_blinds.push_back(uint256());
314+
input_amounts.push_back(100);
315+
input_amounts.push_back(500);
316+
input_assets.push_back(bitcoinID);
317+
input_assets.push_back(otherID);
318+
for (unsigned int i = 0; i < 6; i++) {
319+
output_pubkeys.push_back(pubkey2);
320+
}
321+
322+
CMutableTransaction txtemp(tx5);
323+
324+
// No blinding keys for fees, bails out blinding nothing, still invalid due to imbalance
325+
BOOST_CHECK(BlindTransaction(input_blinds, input_asset_blinds, input_assets, input_amounts, output_blinds, output_asset_blinds, output_pubkeys, vDummy, vDummy, txtemp) == -1);
326+
BOOST_CHECK(!VerifyAmounts(inputs, txtemp, nullptr, false));
327+
// Last will be implied blank keys
328+
output_pubkeys.resize(4);
329+
330+
// Blind transaction, verify amounts
331+
txtemp = tx5;
332+
BOOST_CHECK(BlindTransaction(input_blinds, input_asset_blinds, input_assets, input_amounts, output_blinds, output_asset_blinds, output_pubkeys, vDummy, vDummy, txtemp) == 4);
333+
BOOST_CHECK(VerifyAmounts(inputs, txtemp, nullptr, false));
334+
335+
// Transaction may not have spendable 0-value output
336+
txtemp.vout.push_back(CTxOut(CAsset(), 0, CScript() << OP_TRUE));
337+
BOOST_CHECK(!VerifyAmounts(inputs, txtemp, nullptr, false));
338+
339+
// Create imbalance by removing fees, should still be able to blind
340+
txtemp = tx5;
341+
txtemp.vout.resize(5);
342+
BOOST_CHECK(!VerifyAmounts(inputs, txtemp, nullptr, false));
343+
txtemp.vout.resize(4);
344+
BOOST_CHECK(!VerifyAmounts(inputs, txtemp, nullptr, false));
345+
BOOST_CHECK(BlindTransaction(input_blinds, input_asset_blinds, input_assets, input_amounts, output_blinds, output_asset_blinds, output_pubkeys, vDummy, vDummy, txtemp) == 4);
346+
BOOST_CHECK(!VerifyAmounts(inputs, txtemp, nullptr, false));
347+
348+
txtemp = tx5;
349+
// Remove other input, make surjection proof impossible for 2 "otherID" outputs
350+
std::vector<uint256> t_input_blinds;
351+
std::vector<uint256> t_input_asset_blinds;
352+
std::vector<CAsset> t_input_assets;
353+
std::vector<CAmount> t_input_amounts;
354+
355+
t_input_blinds = input_blinds;
356+
t_input_asset_blinds = input_asset_blinds;
357+
t_input_assets = input_assets;
358+
t_input_amounts = input_amounts;
359+
txtemp.vin.resize(1);
360+
inputs.resize(1);
361+
t_input_blinds.resize(1);
362+
t_input_asset_blinds.resize(1);
363+
t_input_assets.resize(1);
364+
t_input_amounts.resize(1);
365+
BOOST_CHECK(!VerifyAmounts(inputs, txtemp, nullptr, false));
366+
BOOST_CHECK(BlindTransaction(t_input_blinds, t_input_asset_blinds, t_input_assets, t_input_amounts, output_blinds, output_asset_blinds, output_pubkeys, vDummy, vDummy, txtemp) == 2);
367+
BOOST_CHECK(!VerifyAmounts(inputs, txtemp, nullptr, false));
368+
}
369+
}
370+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)