Debian Codegraph @

src/ringct/rctSigs.cpp

raw symbols SCIP
1 // Copyright (c) 2016, Monero Research Labs
2 //
3 // Author: Shen Noether <[email protected]>
4 //
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without modification, are
8 // permitted provided that the following conditions are met:
9 //
10 // 1. Redistributions of source code must retain the above copyright notice, this list of
11 // conditions and the following disclaimer.
12 //
13 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
14 // of conditions and the following disclaimer in the documentation and/or other
15 // materials provided with the distribution.
16 //
17 // 3. Neither the name of the copyright holder nor the names of its contributors may be
18 // used to endorse or promote products derived from this software without specific
19 // prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
22 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #include "misc_log_ex.h"
32 #include "misc_language.h"
33 #include "common/perf_timer.h"
34 #include "common/threadpool.h"
35 #include "common/util.h"
36 #include "rctSigs.h"
37 #include "bulletproofs.h"
38 #include "bulletproofs_plus.h"
39 #include "cryptonote_basic/cryptonote_format_utils.h"
40 #include "cryptonote_config.h"
41
42 using namespace crypto;
43 using namespace std;
44
45 #undef MONERO_DEFAULT_LOG_CATEGORY
46 #define MONERO_DEFAULT_LOG_CATEGORY "ringct"
47
48 #define CHECK_AND_ASSERT_MES_L1(expr, ret, message) {if(!(expr)) {MCERROR("verify", message); return ret;}}
49
50 namespace
51 {
52 rct::Bulletproof make_dummy_bulletproof(const std::vector<uint64_t> &outamounts, rct::keyV &C, rct::keyV &masks)
53 {
54 const size_t n_outs = outamounts.size();
55 const rct::key I = rct::identity();
56 size_t nrl = 0;
57 while ((1u << nrl) < n_outs)
58 ++nrl;
59 nrl += 6;
60
61 C.resize(n_outs);
62 masks.resize(n_outs);
63 for (size_t i = 0; i < n_outs; ++i)
64 {
65 masks[i] = I;
66 rct::key sv8, sv;
67 sv = rct::zero();
68 sv.bytes[0] = outamounts[i] & 255;
69 sv.bytes[1] = (outamounts[i] >> 8) & 255;
70 sv.bytes[2] = (outamounts[i] >> 16) & 255;
71 sv.bytes[3] = (outamounts[i] >> 24) & 255;
72 sv.bytes[4] = (outamounts[i] >> 32) & 255;
73 sv.bytes[5] = (outamounts[i] >> 40) & 255;
74 sv.bytes[6] = (outamounts[i] >> 48) & 255;
75 sv.bytes[7] = (outamounts[i] >> 56) & 255;
76 sc_mul(sv8.bytes, sv.bytes, rct::INV_EIGHT.bytes);
77 rct::addKeys2(C[i], rct::INV_EIGHT, sv8, rct::H);
78 }
79
80 return rct::Bulletproof{rct::keyV(n_outs, I), I, I, I, I, I, I, rct::keyV(nrl, I), rct::keyV(nrl, I), I, I, I};
81 }
82
83 rct::BulletproofPlus make_dummy_bulletproof_plus(const std::vector<uint64_t> &outamounts, rct::keyV &C, rct::keyV &masks)
84 {
85 const size_t n_outs = outamounts.size();
86 const rct::key I = rct::identity();
87 size_t nrl = 0;
88 while ((1u << nrl) < n_outs)
89 ++nrl;
90 nrl += 6;
91
92 C.resize(n_outs);
93 masks.resize(n_outs);
94 for (size_t i = 0; i < n_outs; ++i)
95 {
96 masks[i] = I;
97 rct::key sv8, sv;
98 sv = rct::zero();
99 sv.bytes[0] = outamounts[i] & 255;
100 sv.bytes[1] = (outamounts[i] >> 8) & 255;
101 sv.bytes[2] = (outamounts[i] >> 16) & 255;
102 sv.bytes[3] = (outamounts[i] >> 24) & 255;
103 sv.bytes[4] = (outamounts[i] >> 32) & 255;
104 sv.bytes[5] = (outamounts[i] >> 40) & 255;
105 sv.bytes[6] = (outamounts[i] >> 48) & 255;
106 sv.bytes[7] = (outamounts[i] >> 56) & 255;
107 sc_mul(sv8.bytes, sv.bytes, rct::INV_EIGHT.bytes);
108 rct::addKeys2(C[i], rct::INV_EIGHT, sv8, rct::H);
109 }
110
111 return rct::BulletproofPlus{rct::keyV(n_outs, I), I, I, I, I, I, I, rct::keyV(nrl, I), rct::keyV(nrl, I)};
112 }
113
114 rct::clsag make_dummy_clsag(size_t ring_size)
115 {
116 const rct::key I = rct::identity();
117 const size_t n_scalars = ring_size;
118 return rct::clsag{rct::keyV(n_scalars, I), I, I, I};
119 }
120 }
121
122 namespace rct {
123 Bulletproof proveRangeBulletproof(keyV &C, keyV &masks, const std::vector<uint64_t> &amounts, epee::span<const key> sk, hw::device &hwdev)
124 {
125 CHECK_AND_ASSERT_THROW_MES(amounts.size() == sk.size(), "Invalid amounts/sk sizes");
126 masks.resize(amounts.size());
127 for (size_t i = 0; i < masks.size(); ++i)
128 masks[i] = hwdev.genCommitmentMask(sk[i]);
129 Bulletproof proof = bulletproof_PROVE(amounts, masks);
130 CHECK_AND_ASSERT_THROW_MES(proof.V.size() == amounts.size(), "V does not have the expected size");
131 C = proof.V;
132 return proof;
133 }
134
135 bool verBulletproof(const Bulletproof &proof)
136 {
137 try { return bulletproof_VERIFY(proof); }
138 // we can get deep throws from ge_frombytes_vartime if input isn't valid
139 catch (...) { return false; }
140 }
141
142 bool verBulletproof(const std::vector<const Bulletproof*> &proofs)
143 {
144 try { return bulletproof_VERIFY(proofs); }
145 // we can get deep throws from ge_frombytes_vartime if input isn't valid
146 catch (...) { return false; }
147 }
148
149 BulletproofPlus proveRangeBulletproofPlus(keyV &C, keyV &masks, const std::vector<uint64_t> &amounts, epee::span<const key> sk, hw::device &hwdev)
150 {
151 CHECK_AND_ASSERT_THROW_MES(amounts.size() == sk.size(), "Invalid amounts/sk sizes");
152 masks.resize(amounts.size());
153 for (size_t i = 0; i < masks.size(); ++i)
154 masks[i] = hwdev.genCommitmentMask(sk[i]);
155 BulletproofPlus proof = bulletproof_plus_PROVE(amounts, masks);
156 CHECK_AND_ASSERT_THROW_MES(proof.V.size() == amounts.size(), "V does not have the expected size");
157 C = proof.V;
158 return proof;
159 }
160
161 bool verBulletproofPlus(const BulletproofPlus &proof)
162 {
163 try { return bulletproof_plus_VERIFY(proof); }
164 // we can get deep throws from ge_frombytes_vartime if input isn't valid
165 catch (...) { return false; }
166 }
167
168 bool verBulletproofPlus(const std::vector<const BulletproofPlus*> &proofs)
169 {
170 try { return bulletproof_plus_VERIFY(proofs); }
171 // we can get deep throws from ge_frombytes_vartime if input isn't valid
172 catch (...) { return false; }
173 }
174
175 //Borromean (c.f. gmax/andytoshi's paper)
176 boroSig genBorromean(const key64 x, const key64 P1, const key64 P2, const bits indices) {
177 key64 L[2], alpha;
178 auto wiper = epee::misc_utils::create_scope_leave_handler([&](){memwipe(alpha, sizeof(alpha));});
179 key c;
180 int naught = 0, prime = 0, ii = 0, jj=0;
181 boroSig bb;
182 for (ii = 0 ; ii < 64 ; ii++) {
183 naught = indices[ii]; prime = (indices[ii] + 1) % 2;
184 skGen(alpha[ii]);
185 scalarmultBase(L[naught][ii], alpha[ii]);
186 if (naught == 0) {
187 skGen(bb.s1[ii]);
188 c = hash_to_scalar(L[naught][ii]);
189 addKeys2(L[prime][ii], bb.s1[ii], c, P2[ii]);
190 }
191 }
192 bb.ee = hash_to_scalar(L[1]); //or L[1]..
193 key LL, cc;
194 for (jj = 0 ; jj < 64 ; jj++) {
195 if (!indices[jj]) {
196 sc_mulsub(bb.s0[jj].bytes, x[jj].bytes, bb.ee.bytes, alpha[jj].bytes);
197 } else {
198 skGen(bb.s0[jj]);
199 addKeys2(LL, bb.s0[jj], bb.ee, P1[jj]); //different L0
200 cc = hash_to_scalar(LL);
201 sc_mulsub(bb.s1[jj].bytes, x[jj].bytes, cc.bytes, alpha[jj].bytes);
202 }
203 }
204 return bb;
205 }
206
207 //see above.
208 bool verifyBorromean(const boroSig &bb, const ge_p3 P1[64], const ge_p3 P2[64]) {
209 key64 Lv1; key chash, LL;
210 int ii = 0;
211 ge_p2 p2;
212 for (ii = 0 ; ii < 64 ; ii++) {
213 // equivalent of: addKeys2(LL, bb.s0[ii], bb.ee, P1[ii]);
214 ge_double_scalarmult_base_vartime(&p2, bb.ee.bytes, &P1[ii], bb.s0[ii].bytes);
215 ge_tobytes(LL.bytes, &p2);
216 chash = hash_to_scalar(LL);
217 // equivalent of: addKeys2(Lv1[ii], bb.s1[ii], chash, P2[ii]);
218 ge_double_scalarmult_base_vartime(&p2, chash.bytes, &P2[ii], bb.s1[ii].bytes);
219 ge_tobytes(Lv1[ii].bytes, &p2);
220 }
221 key eeComputed = hash_to_scalar(Lv1); //hash function fine
222 return equalKeys(eeComputed, bb.ee);
223 }
224
225 bool verifyBorromean(const boroSig &bb, const key64 P1, const key64 P2) {
226 ge_p3 P1_p3[64], P2_p3[64];
227 for (size_t i = 0 ; i < 64 ; ++i) {
228 CHECK_AND_ASSERT_MES_L1(ge_frombytes_vartime(&P1_p3[i], P1[i].bytes) == 0, false, "point conv failed");
229 CHECK_AND_ASSERT_MES_L1(ge_frombytes_vartime(&P2_p3[i], P2[i].bytes) == 0, false, "point conv failed");
230 }
231 return verifyBorromean(bb, P1_p3, P2_p3);
232 }
233
234 // Generate a CLSAG signature
235 // See paper by Goodell et al. (https://eprint.iacr.org/2019/654)
236 //
237 // The keys are set as follows:
238 // P[l] == p*G
239 // C[l] == z*G
240 // C[i] == C_nonzero[i] - C_offset (for hashing purposes) for all i
241 clsag CLSAG_Gen(const key &message, const keyV & P, const key & p, const keyV & C, const key & z, const keyV & C_nonzero, const key & C_offset, const unsigned int l, hw::device &hwdev) {
242 clsag sig;
243 size_t n = P.size(); // ring size
244 CHECK_AND_ASSERT_THROW_MES(n == C.size(), "Signing and commitment key vector sizes must match!");
245 CHECK_AND_ASSERT_THROW_MES(n == C_nonzero.size(), "Signing and commitment key vector sizes must match!");
246 CHECK_AND_ASSERT_THROW_MES(l < n, "Signing index out of range!");
247
248 // Key images
249 ge_p3 H_p3;
250 hash_to_p3(H_p3,P[l]);
251 key H;
252 ge_p3_tobytes(H.bytes,&H_p3);
253
254 key D;
255
256 // Initial values
257 key a;
258 key aG;
259 key aH;
260
261 hwdev.clsag_prepare(p,z,sig.I,D,H,a,aG,aH);
262
263 geDsmp I_precomp;
264 geDsmp D_precomp;
265 precomp(I_precomp.k,sig.I);
266 precomp(D_precomp.k,D);
267
268 // Offset key image
269 scalarmultKey(sig.D,D,INV_EIGHT);
270
271 // Aggregation hashes
272 keyV mu_P_to_hash(2*n+4); // domain, I, D, P, C, C_offset
273 keyV mu_C_to_hash(2*n+4); // domain, I, D, P, C, C_offset
274 sc_0(mu_P_to_hash[0].bytes);
275 memcpy(mu_P_to_hash[0].bytes,config::HASH_KEY_CLSAG_AGG_0,sizeof(config::HASH_KEY_CLSAG_AGG_0)-1);
276 sc_0(mu_C_to_hash[0].bytes);
277 memcpy(mu_C_to_hash[0].bytes,config::HASH_KEY_CLSAG_AGG_1,sizeof(config::HASH_KEY_CLSAG_AGG_1)-1);
278 for (size_t i = 1; i < n+1; ++i) {
279 mu_P_to_hash[i] = P[i-1];
280 mu_C_to_hash[i] = P[i-1];
281 }
282 for (size_t i = n+1; i < 2*n+1; ++i) {
283 mu_P_to_hash[i] = C_nonzero[i-n-1];
284 mu_C_to_hash[i] = C_nonzero[i-n-1];
285 }
286 mu_P_to_hash[2*n+1] = sig.I;
287 mu_P_to_hash[2*n+2] = sig.D;
288 mu_P_to_hash[2*n+3] = C_offset;
289 mu_C_to_hash[2*n+1] = sig.I;
290 mu_C_to_hash[2*n+2] = sig.D;
291 mu_C_to_hash[2*n+3] = C_offset;
292 key mu_P, mu_C;
293 mu_P = hash_to_scalar(mu_P_to_hash);
294 mu_C = hash_to_scalar(mu_C_to_hash);
295
296 // Initial commitment
297 keyV c_to_hash(2*n+5); // domain, P, C, C_offset, message, aG, aH
298 key c;
299 sc_0(c_to_hash[0].bytes);
300 memcpy(c_to_hash[0].bytes,config::HASH_KEY_CLSAG_ROUND,sizeof(config::HASH_KEY_CLSAG_ROUND)-1);
301 for (size_t i = 1; i < n+1; ++i)
302 {
303 c_to_hash[i] = P[i-1];
304 c_to_hash[i+n] = C_nonzero[i-1];
305 }
306 c_to_hash[2*n+1] = C_offset;
307 c_to_hash[2*n+2] = message;
308
309 c_to_hash[2*n+3] = aG;
310 c_to_hash[2*n+4] = aH;
311
312 hwdev.clsag_hash(c_to_hash,c);
313
314 size_t i;
315 i = (l + 1) % n;
316 if (i == 0)
317 copy(sig.c1, c);
318
319 // Decoy indices
320 sig.s = keyV(n);
321 key c_new;
322 key L;
323 key R;
324 key c_p; // = c[i]*mu_P
325 key c_c; // = c[i]*mu_C
326 geDsmp P_precomp;
327 geDsmp C_precomp;
328 geDsmp H_precomp;
329 ge_p3 Hi_p3;
330
331 while (i != l) {
332 sig.s[i] = skGen();
333 sc_0(c_new.bytes);
334 sc_mul(c_p.bytes,mu_P.bytes,c.bytes);
335 sc_mul(c_c.bytes,mu_C.bytes,c.bytes);
336
337 // Precompute points
338 precomp(P_precomp.k,P[i]);
339 precomp(C_precomp.k,C[i]);
340
341 // Compute L
342 addKeys_aGbBcC(L,sig.s[i],c_p,P_precomp.k,c_c,C_precomp.k);
343
344 // Compute R
345 hash_to_p3(Hi_p3,P[i]);
346 ge_dsm_precomp(H_precomp.k, &Hi_p3);
347 addKeys_aAbBcC(R,sig.s[i],H_precomp.k,c_p,I_precomp.k,c_c,D_precomp.k);
348
349 c_to_hash[2*n+3] = L;
350 c_to_hash[2*n+4] = R;
351 hwdev.clsag_hash(c_to_hash,c_new);
352 copy(c,c_new);
353
354 i = (i + 1) % n;
355 if (i == 0)
356 copy(sig.c1,c);
357 }
358
359 // Compute final scalar
360 hwdev.clsag_sign(c,a,p,z,mu_P,mu_C,sig.s[l]);
361 memwipe(&a, sizeof(key));
362
363 return sig;
364 }
365
366 clsag CLSAG_Gen(const key &message, const keyV & P, const key & p, const keyV & C, const key & z, const keyV & C_nonzero, const key & C_offset, const unsigned int l) {
367 return CLSAG_Gen(message, P, p, C, z, C_nonzero, C_offset, l, hw::get_device("default"));
368 }
369
370 // MLSAG signatures
371 // See paper by Noether (https://eprint.iacr.org/2015/1098)
372 // This generalization allows for some dimensions not to require linkability;
373 // this is used in practice for commitment data within signatures
374 // Note that using more than one linkable dimension is not recommended.
375 mgSig MLSAG_Gen(const key &message, const keyM & pk, const keyV & xx, const unsigned int index, size_t dsRows, hw::device &hwdev) {
376 mgSig rv;
377 size_t cols = pk.size();
378 CHECK_AND_ASSERT_THROW_MES(cols >= 2, "Error! What is c if cols = 1!");
379 CHECK_AND_ASSERT_THROW_MES(index < cols, "Index out of range");
380 size_t rows = pk[0].size();
381 CHECK_AND_ASSERT_THROW_MES(rows >= 1, "Empty pk");
382 for (size_t i = 1; i < cols; ++i) {
383 CHECK_AND_ASSERT_THROW_MES(pk[i].size() == rows, "pk is not rectangular");
384 }
385 CHECK_AND_ASSERT_THROW_MES(xx.size() == rows, "Bad xx size");
386 CHECK_AND_ASSERT_THROW_MES(dsRows <= rows, "Bad dsRows size");
387
388 size_t i = 0, j = 0, ii = 0;
389 key c, c_old, L, R, Hi;
390 ge_p3 Hi_p3;
391 sc_0(c_old.bytes);
392 vector<geDsmp> Ip(dsRows);
393 rv.II = keyV(dsRows);
394 keyV alpha(rows);
395 auto wiper = epee::misc_utils::create_scope_leave_handler([&](){memwipe(alpha.data(), alpha.size() * sizeof(alpha[0]));});
396 keyV aG(rows);
397 rv.ss = keyM(cols, aG);
398 keyV aHP(dsRows);
399 keyV toHash(1 + 3 * dsRows + 2 * (rows - dsRows));
400 toHash[0] = message;
401 DP("here1");
402 for (i = 0; i < dsRows; i++) {
403 toHash[3 * i + 1] = pk[index][i];
404 hash_to_p3(Hi_p3, pk[index][i]);
405 ge_p3_tobytes(Hi.bytes, &Hi_p3);
406 hwdev.mlsag_prepare(Hi, xx[i], alpha[i] , aG[i] , aHP[i] , rv.II[i]);
407 toHash[3 * i + 2] = aG[i];
408 toHash[3 * i + 3] = aHP[i];
409 precomp(Ip[i].k, rv.II[i]);
410 }
411 size_t ndsRows = 3 * dsRows; //non Double Spendable Rows (see identity chains paper)
412 for (i = dsRows, ii = 0 ; i < rows ; i++, ii++) {
413 skpkGen(alpha[i], aG[i]); //need to save alphas for later..
414 toHash[ndsRows + 2 * ii + 1] = pk[index][i];
415 toHash[ndsRows + 2 * ii + 2] = aG[i];
416 }
417
418 hwdev.mlsag_hash(toHash, c_old);
419
420
421 i = (index + 1) % cols;
422 if (i == 0) {
423 copy(rv.cc, c_old);
424 }
425 while (i != index) {
426
427 rv.ss[i] = skvGen(rows);
428 sc_0(c.bytes);
429 for (j = 0; j < dsRows; j++) {
430 addKeys2(L, rv.ss[i][j], c_old, pk[i][j]);
431 hash_to_p3(Hi_p3, pk[i][j]);
432 ge_p3_tobytes(Hi.bytes, &Hi_p3);
433 addKeys3(R, rv.ss[i][j], Hi, c_old, Ip[j].k);
434 toHash[3 * j + 1] = pk[i][j];
435 toHash[3 * j + 2] = L;
436 toHash[3 * j + 3] = R;
437 }
438 for (j = dsRows, ii = 0; j < rows; j++, ii++) {
439 addKeys2(L, rv.ss[i][j], c_old, pk[i][j]);
440 toHash[ndsRows + 2 * ii + 1] = pk[i][j];
441 toHash[ndsRows + 2 * ii + 2] = L;
442 }
443 hwdev.mlsag_hash(toHash, c);
444 copy(c_old, c);
445 i = (i + 1) % cols;
446
447 if (i == 0) {
448 copy(rv.cc, c_old);
449 }
450 }
451 hwdev.mlsag_sign(c, xx, alpha, rows, dsRows, rv.ss[index]);
452 return rv;
453 }
454
455 // MLSAG signatures
456 // See paper by Noether (https://eprint.iacr.org/2015/1098)
457 // This generalization allows for some dimensions not to require linkability;
458 // this is used in practice for commitment data within signatures
459 // Note that using more than one linkable dimension is not recommended.
460 bool MLSAG_Ver(const key &message, const keyM & pk, const mgSig & rv, size_t dsRows) {
461 size_t cols = pk.size();
462 CHECK_AND_ASSERT_MES(cols >= 2, false, "Signature must contain more than one public key");
463 size_t rows = pk[0].size();
464 CHECK_AND_ASSERT_MES(rows >= 1, false, "Bad total row number");
465 for (size_t i = 1; i < cols; ++i) {
466 CHECK_AND_ASSERT_MES(pk[i].size() == rows, false, "Bad public key matrix dimensions");
467 }
468 CHECK_AND_ASSERT_MES(rv.II.size() == dsRows, false, "Wrong number of key images present");
469 CHECK_AND_ASSERT_MES(rv.ss.size() == cols, false, "Bad scalar matrix dimensions");
470 for (size_t i = 0; i < cols; ++i) {
471 CHECK_AND_ASSERT_MES(rv.ss[i].size() == rows, false, "Bad scalar matrix dimensions");
472 }
473 CHECK_AND_ASSERT_MES(dsRows <= rows, false, "Non-double-spend rows cannot exceed total rows");
474
475 for (size_t i = 0; i < rv.ss.size(); ++i) {
476 for (size_t j = 0; j < rv.ss[i].size(); ++j) {
477 CHECK_AND_ASSERT_MES(sc_check(rv.ss[i][j].bytes) == 0, false, "Bad signature scalar");
478 }
479 }
480 CHECK_AND_ASSERT_MES(sc_check(rv.cc.bytes) == 0, false, "Bad initial signature hash");
481
482 size_t i = 0, j = 0, ii = 0;
483 key c, L, R;
484 key c_old = copy(rv.cc);
485 vector<geDsmp> Ip(dsRows);
486 for (i = 0 ; i < dsRows ; i++) {
487 CHECK_AND_ASSERT_MES(!(rv.II[i] == rct::identity()), false, "Bad key image");
488 precomp(Ip[i].k, rv.II[i]);
489 }
490 size_t ndsRows = 3 * dsRows; // number of dimensions not requiring linkability
491 keyV toHash(1 + 3 * dsRows + 2 * (rows - dsRows));
492 toHash[0] = message;
493 i = 0;
494 while (i < cols) {
495 sc_0(c.bytes);
496 for (j = 0; j < dsRows; j++) {
497 addKeys2(L, rv.ss[i][j], c_old, pk[i][j]);
498
499 // Compute R directly
500 ge_p3 hash8_p3;
501 hash_to_p3(hash8_p3, pk[i][j]);
502 ge_p2 R_p2;
503 ge_double_scalarmult_precomp_vartime(&R_p2, rv.ss[i][j].bytes, &hash8_p3, c_old.bytes, Ip[j].k);
504 ge_tobytes(R.bytes, &R_p2);
505
506 toHash[3 * j + 1] = pk[i][j];
507 toHash[3 * j + 2] = L;
508 toHash[3 * j + 3] = R;
509 }
510 for (j = dsRows, ii = 0 ; j < rows ; j++, ii++) {
511 addKeys2(L, rv.ss[i][j], c_old, pk[i][j]);
512 toHash[ndsRows + 2 * ii + 1] = pk[i][j];
513 toHash[ndsRows + 2 * ii + 2] = L;
514 }
515 c = hash_to_scalar(toHash);
516 CHECK_AND_ASSERT_MES(!(c == rct::zero()), false, "Bad signature hash");
517 copy(c_old, c);
518 i = (i + 1);
519 }
520 sc_sub(c.bytes, c_old.bytes, rv.cc.bytes);
521 return sc_isnonzero(c.bytes) == 0;
522 }
523
524
525
526 //proveRange and verRange
527 //proveRange gives C, and mask such that \sumCi = C
528 // c.f. https://eprint.iacr.org/2015/1098 section 5.1
529 // and Ci is a commitment to either 0 or 2^i, i=0,...,63
530 // thus this proves that "amount" is in [0, 2^64]
531 // mask is a such that C = aG + bH, and b = amount
532 //verRange verifies that \sum Ci = C and that each Ci is a commitment to 0 or 2^i
533 rangeSig proveRange(key & C, key & mask, const xmr_amount & amount) {
534 sc_0(mask.bytes);
535 identity(C);
536 bits b;
537 d2b(b, amount);
538 rangeSig sig;
539 key64 ai;
540 key64 CiH;
541 int i = 0;
542 for (i = 0; i < ATOMS; i++) {
543 skGen(ai[i]);
544 if (b[i] == 0) {
545 scalarmultBase(sig.Ci[i], ai[i]);
546 }
547 if (b[i] == 1) {
548 addKeys1(sig.Ci[i], ai[i], H2[i]);
549 }
550 subKeys(CiH[i], sig.Ci[i], H2[i]);
551 sc_add(mask.bytes, mask.bytes, ai[i].bytes);
552 addKeys(C, C, sig.Ci[i]);
553 }
554 sig.asig = genBorromean(ai, sig.Ci, CiH, b);
555 return sig;
556 }
557
558 //proveRange and verRange
559 //proveRange gives C, and mask such that \sumCi = C
560 // c.f. https://eprint.iacr.org/2015/1098 section 5.1
561 // and Ci is a commitment to either 0 or 2^i, i=0,...,63
562 // thus this proves that "amount" is in [0, 2^64]
563 // mask is a such that C = aG + bH, and b = amount
564 //verRange verifies that \sum Ci = C and that each Ci is a commitment to 0 or 2^i
565 bool verRange(const key & C, const rangeSig & as) {
566 try
567 {
568 PERF_TIMER(verRange);
569 ge_p3 CiH[64], asCi[64];
570 int i = 0;
571 ge_p3 Ctmp_p3 = ge_p3_identity;
572 for (i = 0; i < 64; i++) {
573 // faster equivalent of:
574 // subKeys(CiH[i], as.Ci[i], H2[i]);
575 // addKeys(Ctmp, Ctmp, as.Ci[i]);
576 ge_cached cached;
577 ge_p3 p3;
578 ge_p1p1 p1;
579 CHECK_AND_ASSERT_MES_L1(ge_frombytes_vartime(&p3, H2[i].bytes) == 0, false, "point conv failed");
580 ge_p3_to_cached(&cached, &p3);
581 CHECK_AND_ASSERT_MES_L1(ge_frombytes_vartime(&asCi[i], as.Ci[i].bytes) == 0, false, "point conv failed");
582 ge_sub(&p1, &asCi[i], &cached);
583 ge_p3_to_cached(&cached, &asCi[i]);
584 ge_p1p1_to_p3(&CiH[i], &p1);
585 ge_add(&p1, &Ctmp_p3, &cached);
586 ge_p1p1_to_p3(&Ctmp_p3, &p1);
587 }
588 key Ctmp;
589 ge_p3_tobytes(Ctmp.bytes, &Ctmp_p3);
590 if (!equalKeys(C, Ctmp))
591 return false;
592 if (!verifyBorromean(as.asig, asCi, CiH))
593 return false;
594 return true;
595 }
596 // we can get deep throws from ge_frombytes_vartime if input isn't valid
597 catch (...) { return false; }
598 }
599
600 key get_pre_mlsag_hash(const rctSig &rv, hw::device &hwdev)
601 {
602 keyV hashes;
603 hashes.reserve(3);
604 hashes.push_back(rv.message);
605 crypto::hash h;
606
607 std::stringstream ss;
608 binary_archive<true> ba(ss);
609 CHECK_AND_ASSERT_THROW_MES(!rv.mixRing.empty(), "Empty mixRing");
610 const size_t inputs = is_rct_simple(rv.type) ? rv.mixRing.size() : rv.mixRing[0].size();
611 const size_t outputs = rv.ecdhInfo.size();
612 key prehash;
613 CHECK_AND_ASSERT_THROW_MES(const_cast<rctSig&>(rv).serialize_rctsig_base(ba, inputs, outputs),
614 "Failed to serialize rctSigBase");
615 cryptonote::get_blob_hash(ss.str(), h);
616 hashes.push_back(hash2rct(h));
617
618 keyV kv;
619 if (rv.type == RCTTypeBulletproof || rv.type == RCTTypeBulletproof2 || rv.type == RCTTypeCLSAG)
620 {
621 kv.reserve((6*2+9) * rv.p.bulletproofs.size());
622 for (const auto &p: rv.p.bulletproofs)
623 {
624 // V are not hashed as they're expanded from outPk.mask
625 // (and thus hashed as part of rctSigBase above)
626 kv.push_back(p.A);
627 kv.push_back(p.S);
628 kv.push_back(p.T1);
629 kv.push_back(p.T2);
630 kv.push_back(p.taux);
631 kv.push_back(p.mu);
632 for (size_t n = 0; n < p.L.size(); ++n)
633 kv.push_back(p.L[n]);
634 for (size_t n = 0; n < p.R.size(); ++n)
635 kv.push_back(p.R[n]);
636 kv.push_back(p.a);
637 kv.push_back(p.b);
638 kv.push_back(p.t);
639 }
640 }
641 else if (rv.type == RCTTypeBulletproofPlus)
642 {
643 kv.reserve((6*2+6) * rv.p.bulletproofs_plus.size());
644 for (const auto &p: rv.p.bulletproofs_plus)
645 {
646 // V are not hashed as they're expanded from outPk.mask
647 // (and thus hashed as part of rctSigBase above)
648 kv.push_back(p.A);
649 kv.push_back(p.A1);
650 kv.push_back(p.B);
651 kv.push_back(p.r1);
652 kv.push_back(p.s1);
653 kv.push_back(p.d1);
654 for (size_t n = 0; n < p.L.size(); ++n)
655 kv.push_back(p.L[n]);
656 for (size_t n = 0; n < p.R.size(); ++n)
657 kv.push_back(p.R[n]);
658 }
659 }
660 else
661 {
662 kv.reserve((64*3+1) * rv.p.rangeSigs.size());
663 for (const auto &r: rv.p.rangeSigs)
664 {
665 for (size_t n = 0; n < 64; ++n)
666 kv.push_back(r.asig.s0[n]);
667 for (size_t n = 0; n < 64; ++n)
668 kv.push_back(r.asig.s1[n]);
669 kv.push_back(r.asig.ee);
670 for (size_t n = 0; n < 64; ++n)
671 kv.push_back(r.Ci[n]);
672 }
673 }
674 hashes.push_back(cn_fast_hash(kv));
675 hwdev.mlsag_prehash(ss.str(), inputs, outputs, hashes, rv.outPk, prehash);
676 return prehash;
677 }
678
679 //Ring-ct MG sigs
680 //Prove:
681 // c.f. https://eprint.iacr.org/2015/1098 section 4. definition 10.
682 // This does the MG sig on the "dest" part of the given key matrix, and
683 // the last row is the sum of input commitments from that column - sum output commitments
684 // this shows that sum inputs = sum outputs
685 //Ver:
686 // verifies the above sig is created corretly
687 mgSig proveRctMG(const key &message, const ctkeyM & pubs, const ctkeyV & inSk, const ctkeyV &outSk, const ctkeyV & outPk, unsigned int index, const key &txnFeeKey, hw::device &hwdev) {
688 //setup vars
689 size_t cols = pubs.size();
690 CHECK_AND_ASSERT_THROW_MES(cols >= 1, "Empty pubs");
691 size_t rows = pubs[0].size();
692 CHECK_AND_ASSERT_THROW_MES(rows >= 1, "Empty pubs");
693 for (size_t i = 1; i < cols; ++i) {
694 CHECK_AND_ASSERT_THROW_MES(pubs[i].size() == rows, "pubs is not rectangular");
695 }
696 CHECK_AND_ASSERT_THROW_MES(inSk.size() == rows, "Bad inSk size");
697 CHECK_AND_ASSERT_THROW_MES(outSk.size() == outPk.size(), "Bad outSk/outPk size");
698
699 keyV sk(rows + 1);
700 keyV tmp(rows + 1);
701 size_t i = 0, j = 0;
702 for (i = 0; i < rows + 1; i++) {
703 sc_0(sk[i].bytes);
704 identity(tmp[i]);
705 }
706 keyM M(cols, tmp);
707 //create the matrix to mg sig
708 for (i = 0; i < cols; i++) {
709 M[i][rows] = identity();
710 for (j = 0; j < rows; j++) {
711 M[i][j] = pubs[i][j].dest;
712 addKeys(M[i][rows], M[i][rows], pubs[i][j].mask); //add input commitments in last row
713 }
714 }
715 sc_0(sk[rows].bytes);
716 for (j = 0; j < rows; j++) {
717 sk[j] = copy(inSk[j].dest);
718 sc_add(sk[rows].bytes, sk[rows].bytes, inSk[j].mask.bytes); //add masks in last row
719 }
720 for (i = 0; i < cols; i++) {
721 for (size_t j = 0; j < outPk.size(); j++) {
722 subKeys(M[i][rows], M[i][rows], outPk[j].mask); //subtract output Ci's in last row
723 }
724 //subtract txn fee output in last row
725 subKeys(M[i][rows], M[i][rows], txnFeeKey);
726 }
727 for (size_t j = 0; j < outPk.size(); j++) {
728 sc_sub(sk[rows].bytes, sk[rows].bytes, outSk[j].mask.bytes); //subtract output masks in last row..
729 }
730 mgSig result = MLSAG_Gen(message, M, sk, index, rows, hwdev);
731 memwipe(sk.data(), sk.size() * sizeof(key));
732 return result;
733 }
734
735
736 //Ring-ct MG sigs Simple
737 // Simple version for when we assume only
738 // post rct inputs
739 // here pubs is a vector of (P, C) length mixin
740 // inSk is x, a_in corresponding to signing index
741 // a_out, Cout is for the output commitment
742 // index is the signing index..
743 mgSig proveRctMGSimple(const key &message, const ctkeyV & pubs, const ctkey & inSk, const key &a , const key &Cout, unsigned int index, hw::device &hwdev) {
744 //setup vars
745 size_t rows = 1;
746 size_t cols = pubs.size();
747 CHECK_AND_ASSERT_THROW_MES(cols >= 1, "Empty pubs");
748 keyV tmp(rows + 1);
749 keyV sk(rows + 1);
750 size_t i;
751 keyM M(cols, tmp);
752
753 sk[0] = copy(inSk.dest);
754 sc_sub(sk[1].bytes, inSk.mask.bytes, a.bytes);
755 for (i = 0; i < cols; i++) {
756 M[i][0] = pubs[i].dest;
757 subKeys(M[i][1], pubs[i].mask, Cout);
758 }
759 mgSig result = MLSAG_Gen(message, M, sk, index, rows, hwdev);
760 memwipe(sk.data(), sk.size() * sizeof(key));
761 return result;
762 }
763
764 clsag proveRctCLSAGSimple(const key &message, const ctkeyV &pubs, const ctkey &inSk, const key &a, const key &Cout, unsigned int index, hw::device &hwdev) {
765 //setup vars
766 size_t rows = 1;
767 size_t cols = pubs.size();
768 CHECK_AND_ASSERT_THROW_MES(cols >= 1, "Empty pubs");
769 keyV tmp(rows + 1);
770 keyV sk(rows + 1);
771 keyM M(cols, tmp);
772
773 keyV P, C, C_nonzero;
774 P.reserve(pubs.size());
775 C.reserve(pubs.size());
776 C_nonzero.reserve(pubs.size());
777 for (const ctkey &k: pubs)
778 {
779 P.push_back(k.dest);
780 C_nonzero.push_back(k.mask);
781 rct::key tmp;
782 subKeys(tmp, k.mask, Cout);
783 C.push_back(tmp);
784 }
785
786 sk[0] = copy(inSk.dest);
787 sc_sub(sk[1].bytes, inSk.mask.bytes, a.bytes);
788 clsag result = CLSAG_Gen(message, P, sk[0], C, sk[1], C_nonzero, Cout, index, hwdev);
789 memwipe(sk.data(), sk.size() * sizeof(key));
790 return result;
791 }
792
793
794 //Ring-ct MG sigs
795 //Prove:
796 // c.f. https://eprint.iacr.org/2015/1098 section 4. definition 10.
797 // This does the MG sig on the "dest" part of the given key matrix, and
798 // the last row is the sum of input commitments from that column - sum output commitments
799 // this shows that sum inputs = sum outputs
800 //Ver:
801 // verifies the above sig is created corretly
802 bool verRctMG(const mgSig &mg, const ctkeyM & pubs, const ctkeyV & outPk, const key &txnFeeKey, const key &message) {
803 PERF_TIMER(verRctMG);
804 //setup vars
805 size_t cols = pubs.size();
806 CHECK_AND_ASSERT_MES(cols >= 1, false, "Empty pubs");
807 size_t rows = pubs[0].size();
808 CHECK_AND_ASSERT_MES(rows >= 1, false, "Empty pubs");
809 for (size_t i = 1; i < cols; ++i) {
810 CHECK_AND_ASSERT_MES(pubs[i].size() == rows, false, "pubs is not rectangular");
811 }
812
813 keyV tmp(rows + 1);
814 size_t i = 0, j = 0;
815 for (i = 0; i < rows + 1; i++) {
816 identity(tmp[i]);
817 }
818 keyM M(cols, tmp);
819
820 //create the matrix to mg sig
821 for (j = 0; j < rows; j++) {
822 for (i = 0; i < cols; i++) {
823 M[i][j] = pubs[i][j].dest;
824 addKeys(M[i][rows], M[i][rows], pubs[i][j].mask); //add Ci in last row
825 }
826 }
827 for (i = 0; i < cols; i++) {
828 for (j = 0; j < outPk.size(); j++) {
829 subKeys(M[i][rows], M[i][rows], outPk[j].mask); //subtract output Ci's in last row
830 }
831 //subtract txn fee output in last row
832 subKeys(M[i][rows], M[i][rows], txnFeeKey);
833 }
834 return MLSAG_Ver(message, M, mg, rows);
835 }
836
837 //Ring-ct Simple MG sigs
838 //Ver:
839 //This does a simplified version, assuming only post Rct
840 //inputs
841 bool verRctMGSimple(const key &message, const mgSig &mg, const ctkeyV & pubs, const key & C) {
842 try
843 {
844 PERF_TIMER(verRctMGSimple);
845 //setup vars
846 size_t rows = 1;
847 size_t cols = pubs.size();
848 CHECK_AND_ASSERT_MES(cols >= 1, false, "Empty pubs");
849 keyV tmp(rows + 1);
850 size_t i;
851 keyM M(cols, tmp);
852 ge_p3 Cp3;
853 CHECK_AND_ASSERT_MES_L1(ge_frombytes_vartime(&Cp3, C.bytes) == 0, false, "point conv failed");
854 ge_cached Ccached;
855 ge_p3_to_cached(&Ccached, &Cp3);
856 ge_p1p1 p1;
857 //create the matrix to mg sig
858 for (i = 0; i < cols; i++) {
859 M[i][0] = pubs[i].dest;
860 ge_p3 p3;
861 CHECK_AND_ASSERT_MES_L1(ge_frombytes_vartime(&p3, pubs[i].mask.bytes) == 0, false, "point conv failed");
862 ge_sub(&p1, &p3, &Ccached);
863 ge_p1p1_to_p3(&p3, &p1);
864 ge_p3_tobytes(M[i][1].bytes, &p3);
865 }
866 //DP(C);
867 return MLSAG_Ver(message, M, mg, rows);
868 }
869 catch (...) { return false; }
870 }
871
872 bool verRctCLSAGSimple(const key &message, const clsag &sig, const ctkeyV & pubs, const key & C_offset) {
873 try
874 {
875 PERF_TIMER(verRctCLSAGSimple);
876 const size_t n = pubs.size();
877
878 // Check data
879 CHECK_AND_ASSERT_MES(n >= 1, false, "Empty pubs");
880 CHECK_AND_ASSERT_MES(n == sig.s.size(), false, "Signature scalar vector is the wrong size!");
881 for (size_t i = 0; i < n; ++i)
882 CHECK_AND_ASSERT_MES(sc_check(sig.s[i].bytes) == 0, false, "Bad signature scalar!");
883 CHECK_AND_ASSERT_MES(sc_check(sig.c1.bytes) == 0, false, "Bad signature commitment!");
884 CHECK_AND_ASSERT_MES(!(sig.I == rct::identity()), false, "Bad key image!");
885
886 // Cache commitment offset for efficient subtraction later
887 ge_p3 C_offset_p3;
888 CHECK_AND_ASSERT_MES(ge_frombytes_vartime(&C_offset_p3, C_offset.bytes) == 0, false, "point conv failed");
889 ge_cached C_offset_cached;
890 ge_p3_to_cached(&C_offset_cached, &C_offset_p3);
891
892 // Prepare key images
893 key c = copy(sig.c1);
894 key D_8 = scalarmult8(sig.D);
895 CHECK_AND_ASSERT_MES(!(D_8 == rct::identity()), false, "Bad auxiliary key image!");
896 geDsmp I_precomp;
897 geDsmp D_precomp;
898 precomp(I_precomp.k,sig.I);
899 precomp(D_precomp.k,D_8);
900
901 // Aggregation hashes
902 keyV mu_P_to_hash(2*n+4); // domain, I, D, P, C, C_offset
903 keyV mu_C_to_hash(2*n+4); // domain, I, D, P, C, C_offset
904 sc_0(mu_P_to_hash[0].bytes);
905 memcpy(mu_P_to_hash[0].bytes,config::HASH_KEY_CLSAG_AGG_0,sizeof(config::HASH_KEY_CLSAG_AGG_0)-1);
906 sc_0(mu_C_to_hash[0].bytes);
907 memcpy(mu_C_to_hash[0].bytes,config::HASH_KEY_CLSAG_AGG_1,sizeof(config::HASH_KEY_CLSAG_AGG_1)-1);
908 for (size_t i = 1; i < n+1; ++i) {
909 mu_P_to_hash[i] = pubs[i-1].dest;
910 mu_C_to_hash[i] = pubs[i-1].dest;
911 }
912 for (size_t i = n+1; i < 2*n+1; ++i) {
913 mu_P_to_hash[i] = pubs[i-n-1].mask;
914 mu_C_to_hash[i] = pubs[i-n-1].mask;
915 }
916 mu_P_to_hash[2*n+1] = sig.I;
917 mu_P_to_hash[2*n+2] = sig.D;
918 mu_P_to_hash[2*n+3] = C_offset;
919 mu_C_to_hash[2*n+1] = sig.I;
920 mu_C_to_hash[2*n+2] = sig.D;
921 mu_C_to_hash[2*n+3] = C_offset;
922 key mu_P, mu_C;
923 mu_P = hash_to_scalar(mu_P_to_hash);
924 mu_C = hash_to_scalar(mu_C_to_hash);
925
926 // Set up round hash
927 keyV c_to_hash(2*n+5); // domain, P, C, C_offset, message, L, R
928 sc_0(c_to_hash[0].bytes);
929 memcpy(c_to_hash[0].bytes,config::HASH_KEY_CLSAG_ROUND,sizeof(config::HASH_KEY_CLSAG_ROUND)-1);
930 for (size_t i = 1; i < n+1; ++i)
931 {
932 c_to_hash[i] = pubs[i-1].dest;
933 c_to_hash[i+n] = pubs[i-1].mask;
934 }
935 c_to_hash[2*n+1] = C_offset;
936 c_to_hash[2*n+2] = message;
937 key c_p; // = c[i]*mu_P
938 key c_c; // = c[i]*mu_C
939 key c_new;
940 key L;
941 key R;
942 geDsmp P_precomp;
943 geDsmp C_precomp;
944 size_t i = 0;
945 ge_p3 hash8_p3;
946 geDsmp hash_precomp;
947 ge_p3 temp_p3;
948 ge_p1p1 temp_p1;
949
950 while (i < n) {
951 sc_0(c_new.bytes);
952 sc_mul(c_p.bytes,mu_P.bytes,c.bytes);
953 sc_mul(c_c.bytes,mu_C.bytes,c.bytes);
954
955 // Precompute points for L/R
956 precomp(P_precomp.k,pubs[i].dest);
957
958 CHECK_AND_ASSERT_MES(ge_frombytes_vartime(&temp_p3, pubs[i].mask.bytes) == 0, false, "point conv failed");
959 ge_sub(&temp_p1,&temp_p3,&C_offset_cached);
960 ge_p1p1_to_p3(&temp_p3,&temp_p1);
961 ge_dsm_precomp(C_precomp.k,&temp_p3);
962
963 // Compute L
964 addKeys_aGbBcC(L,sig.s[i],c_p,P_precomp.k,c_c,C_precomp.k);
965
966 // Compute R
967 hash_to_p3(hash8_p3,pubs[i].dest);
968 ge_dsm_precomp(hash_precomp.k, &hash8_p3);
969 addKeys_aAbBcC(R,sig.s[i],hash_precomp.k,c_p,I_precomp.k,c_c,D_precomp.k);
970
971 c_to_hash[2*n+3] = L;
972 c_to_hash[2*n+4] = R;
973 c_new = hash_to_scalar(c_to_hash);
974 CHECK_AND_ASSERT_MES(!(c_new == rct::zero()), false, "Bad signature hash");
975 copy(c,c_new);
976
977 i = i + 1;
978 }
979 sc_sub(c_new.bytes,c.bytes,sig.c1.bytes);
980 return sc_isnonzero(c_new.bytes) == 0;
981 }
982 catch (...) { return false; }
983 }
984
985
986 //These functions get keys from blockchain
987 //replace these when connecting blockchain
988 //getKeyFromBlockchain grabs a key from the blockchain at "reference_index" to mix with
989 //populateFromBlockchain creates a keymatrix with "mixin" columns and one of the columns is inPk
990 // the return value are the key matrix, and the index where inPk was put (random).
991 void getKeyFromBlockchain(ctkey & a, size_t reference_index) {
992 a.mask = pkGen();
993 a.dest = pkGen();
994 }
995
996 //These functions get keys from blockchain
997 //replace these when connecting blockchain
998 //getKeyFromBlockchain grabs a key from the blockchain at "reference_index" to mix with
999 //populateFromBlockchain creates a keymatrix with "mixin" + 1 columns and one of the columns is inPk
1000 // the return value are the key matrix, and the index where inPk was put (random).
1001 tuple<ctkeyM, xmr_amount> populateFromBlockchain(ctkeyV inPk, int mixin) {
1002 int rows = inPk.size();
1003 ctkeyM rv(mixin + 1, inPk);
1004 int index = randXmrAmount(mixin);
1005 int i = 0, j = 0;
1006 for (i = 0; i <= mixin; i++) {
1007 if (i != index) {
1008 for (j = 0; j < rows; j++) {
1009 getKeyFromBlockchain(rv[i][j], (size_t)randXmrAmount);
1010 }
1011 }
1012 }
1013 return make_tuple(rv, index);
1014 }
1015
1016 //These functions get keys from blockchain
1017 //replace these when connecting blockchain
1018 //getKeyFromBlockchain grabs a key from the blockchain at "reference_index" to mix with
1019 //populateFromBlockchain creates a keymatrix with "mixin" columns and one of the columns is inPk
1020 // the return value are the key matrix, and the index where inPk was put (random).
1021 xmr_amount populateFromBlockchainSimple(ctkeyV & mixRing, const ctkey & inPk, int mixin) {
1022 int index = randXmrAmount(mixin);
1023 int i = 0;
1024 for (i = 0; i <= mixin; i++) {
1025 if (i != index) {
1026 getKeyFromBlockchain(mixRing[i], (size_t)randXmrAmount(1000));
1027 } else {
1028 mixRing[i] = inPk;
1029 }
1030 }
1031 return index;
1032 }
1033
1034 //RingCT protocol
1035 //genRct:
1036 // creates an rctSig with all data necessary to verify the rangeProofs and that the signer owns one of the
1037 // columns that are claimed as inputs, and that the sum of inputs = sum of outputs.
1038 // Also contains masked "amount" and "mask" so the receiver can see how much they received
1039 //verRct:
1040 // verifies that all signatures (rangeProogs, MG sig, sum inputs = outputs) are correct
1041 //decodeRct: (c.f. https://eprint.iacr.org/2015/1098 section 5.1.1)
1042 // uses the attached ecdh info to find the amounts represented by each output commitment
1043 // must know the destination private key to find the correct amount, else will return a random number
1044 // Note: For txn fees, the last index in the amounts vector should contain that
1045 // Thus the amounts vector will be "one" longer than the destinations vectort
1046 rctSig genRct(const key &message, const ctkeyV & inSk, const keyV & destinations, const vector<xmr_amount> & amounts, const ctkeyM &mixRing, const keyV &amount_keys, unsigned int index, ctkeyV &outSk, const RCTConfig &rct_config, hw::device &hwdev) {
1047 CHECK_AND_ASSERT_THROW_MES(amounts.size() == destinations.size() || amounts.size() == destinations.size() + 1, "Different number of amounts/destinations");
1048 CHECK_AND_ASSERT_THROW_MES(amount_keys.size() == destinations.size(), "Different number of amount_keys/destinations");
1049 CHECK_AND_ASSERT_THROW_MES(index < mixRing.size(), "Bad index into mixRing");
1050 for (size_t n = 0; n < mixRing.size(); ++n) {
1051 CHECK_AND_ASSERT_THROW_MES(mixRing[n].size() == inSk.size(), "Bad mixRing size");
1052 }
1053 CHECK_AND_ASSERT_THROW_MES(inSk.size() < 2, "genRct is not suitable for 2+ rings");
1054
1055 rctSig rv;
1056 rv.type = RCTTypeFull;
1057 rv.message = message;
1058 rv.outPk.resize(destinations.size());
1059 rv.p.rangeSigs.resize(destinations.size());
1060 rv.ecdhInfo.resize(destinations.size());
1061
1062 size_t i = 0;
1063 keyV masks(destinations.size()); //sk mask..
1064 outSk.resize(destinations.size());
1065 for (i = 0; i < destinations.size(); i++) {
1066 //add destination to sig
1067 rv.outPk[i].dest = copy(destinations[i]);
1068 //compute range proof
1069 rv.p.rangeSigs[i] = proveRange(rv.outPk[i].mask, outSk[i].mask, amounts[i]);
1070 #ifdef DBG
1071 CHECK_AND_ASSERT_THROW_MES(verRange(rv.outPk[i].mask, rv.p.rangeSigs[i]), "verRange failed on newly created proof");
1072 #endif
1073 //mask amount and mask
1074 rv.ecdhInfo[i].mask = copy(outSk[i].mask);
1075 rv.ecdhInfo[i].amount = d2h(amounts[i]);
1076 hwdev.ecdhEncode(rv.ecdhInfo[i], amount_keys[i], rv.type == RCTTypeBulletproof2 || rv.type == RCTTypeCLSAG || rv.type == RCTTypeBulletproofPlus);
1077 }
1078
1079 //set txn fee
1080 if (amounts.size() > destinations.size())
1081 {
1082 rv.txnFee = amounts[destinations.size()];
1083 }
1084 else
1085 {
1086 rv.txnFee = 0;
1087 }
1088 key txnFeeKey = scalarmultH(d2h(rv.txnFee));
1089
1090 rv.mixRing = mixRing;
1091 rv.p.MGs.push_back(proveRctMG(get_pre_mlsag_hash(rv, hwdev), rv.mixRing, inSk, outSk, rv.outPk, index, txnFeeKey,hwdev));
1092 return rv;
1093 }
1094
1095 rctSig genRct(const key &message, const ctkeyV & inSk, const ctkeyV & inPk, const keyV & destinations, const vector<xmr_amount> & amounts, const keyV &amount_keys, const int mixin, const RCTConfig &rct_config, hw::device &hwdev) {
1096 unsigned int index;
1097 ctkeyM mixRing;
1098 ctkeyV outSk;
1099 tie(mixRing, index) = populateFromBlockchain(inPk, mixin);
1100 return genRct(message, inSk, destinations, amounts, mixRing, amount_keys, index, outSk, rct_config, hwdev);
1101 }
1102
1103 //RCT simple
1104 //for post-rct only
1105 rctSig genRctSimple(const key &message, const ctkeyV & inSk, const keyV & destinations, const vector<xmr_amount> &inamounts, const vector<xmr_amount> &outamounts, xmr_amount txnFee, const ctkeyM & mixRing, const keyV &amount_keys, const std::vector<unsigned int> & index, ctkeyV &outSk, const RCTConfig &rct_config, hw::device &hwdev) {
1106 const bool bulletproof_or_plus = rct_config.range_proof_type > RangeProofBorromean;
1107 CHECK_AND_ASSERT_THROW_MES(inamounts.size() > 0, "Empty inamounts");
1108 CHECK_AND_ASSERT_THROW_MES(inamounts.size() == inSk.size(), "Different number of inamounts/inSk");
1109 CHECK_AND_ASSERT_THROW_MES(outamounts.size() == destinations.size(), "Different number of amounts/destinations");
1110 CHECK_AND_ASSERT_THROW_MES(amount_keys.size() == destinations.size(), "Different number of amount_keys/destinations");
1111 CHECK_AND_ASSERT_THROW_MES(index.size() == inSk.size(), "Different number of index/inSk");
1112 CHECK_AND_ASSERT_THROW_MES(mixRing.size() == inSk.size(), "Different number of mixRing/inSk");
1113 for (size_t n = 0; n < mixRing.size(); ++n) {
1114 CHECK_AND_ASSERT_THROW_MES(index[n] < mixRing[n].size(), "Bad index into mixRing");
1115 }
1116
1117 rctSig rv;
1118 if (bulletproof_or_plus)
1119 {
1120 switch (rct_config.bp_version)
1121 {
1122 case 0:
1123 case 4:
1124 rv.type = RCTTypeBulletproofPlus;
1125 break;
1126 case 3:
1127 rv.type = RCTTypeCLSAG;
1128 break;
1129 case 2:
1130 rv.type = RCTTypeBulletproof2;
1131 break;
1132 case 1:
1133 rv.type = RCTTypeBulletproof;
1134 break;
1135 default:
1136 ASSERT_MES_AND_THROW("Unsupported BP version: " << rct_config.bp_version);
1137 }
1138 }
1139 else
1140 rv.type = RCTTypeSimple;
1141
1142 rv.message = message;
1143 rv.outPk.resize(destinations.size());
1144 if (!bulletproof_or_plus)
1145 rv.p.rangeSigs.resize(destinations.size());
1146 rv.ecdhInfo.resize(destinations.size());
1147
1148 size_t i;
1149 keyV masks(destinations.size()); //sk mask..
1150 outSk.resize(destinations.size());
1151 for (i = 0; i < destinations.size(); i++) {
1152
1153 //add destination to sig
1154 rv.outPk[i].dest = copy(destinations[i]);
1155 //compute range proof
1156 if (!bulletproof_or_plus)
1157 rv.p.rangeSigs[i] = proveRange(rv.outPk[i].mask, outSk[i].mask, outamounts[i]);
1158 #ifdef DBG
1159 if (!bulletproof_or_plus)
1160 CHECK_AND_ASSERT_THROW_MES(verRange(rv.outPk[i].mask, rv.p.rangeSigs[i]), "verRange failed on newly created proof");
1161 #endif
1162 }
1163
1164 rv.p.bulletproofs.clear();
1165 rv.p.bulletproofs_plus.clear();
1166 if (bulletproof_or_plus)
1167 {
1168 const bool plus = is_rct_bulletproof_plus(rv.type);
1169 size_t n_amounts = outamounts.size();
1170 size_t amounts_proved = 0;
1171 if (rct_config.range_proof_type == RangeProofPaddedBulletproof)
1172 {
1173 rct::keyV C, masks;
1174 if (hwdev.get_mode() == hw::device::TRANSACTION_CREATE_FAKE)
1175 {
1176 // use a fake bulletproof for speed
1177 if (plus)
1178 rv.p.bulletproofs_plus.push_back(make_dummy_bulletproof_plus(outamounts, C, masks));
1179 else
1180 rv.p.bulletproofs.push_back(make_dummy_bulletproof(outamounts, C, masks));
1181 }
1182 else
1183 {
1184 const epee::span<const key> keys{&amount_keys[0], amount_keys.size()};
1185 if (plus)
1186 rv.p.bulletproofs_plus.push_back(proveRangeBulletproofPlus(C, masks, outamounts, keys, hwdev));
1187 else
1188 rv.p.bulletproofs.push_back(proveRangeBulletproof(C, masks, outamounts, keys, hwdev));
1189 #ifdef DBG
1190 if (plus)
1191 CHECK_AND_ASSERT_THROW_MES(verBulletproofPlus(rv.p.bulletproofs_plus.back()), "verBulletproofPlus failed on newly created proof");
1192 else
1193 CHECK_AND_ASSERT_THROW_MES(verBulletproof(rv.p.bulletproofs.back()), "verBulletproof failed on newly created proof");
1194 #endif
1195 }
1196 for (i = 0; i < outamounts.size(); ++i)
1197 {
1198 rv.outPk[i].mask = rct::scalarmult8(C[i]);
1199 outSk[i].mask = masks[i];
1200 }
1201 }
1202 else while (amounts_proved < n_amounts)
1203 {
1204 size_t batch_size = 1;
1205 if (rct_config.range_proof_type == RangeProofMultiOutputBulletproof)
1206 while (batch_size * 2 + amounts_proved <= n_amounts && batch_size * 2 <= (plus ? BULLETPROOF_PLUS_MAX_OUTPUTS : BULLETPROOF_MAX_OUTPUTS))
1207 batch_size *= 2;
1208 rct::keyV C, masks;
1209 std::vector<uint64_t> batch_amounts(batch_size);
1210 for (i = 0; i < batch_size; ++i)
1211 batch_amounts[i] = outamounts[i + amounts_proved];
1212 if (hwdev.get_mode() == hw::device::TRANSACTION_CREATE_FAKE)
1213 {
1214 // use a fake bulletproof for speed
1215 if (plus)
1216 rv.p.bulletproofs_plus.push_back(make_dummy_bulletproof_plus(batch_amounts, C, masks));
1217 else
1218 rv.p.bulletproofs.push_back(make_dummy_bulletproof(batch_amounts, C, masks));
1219 }
1220 else
1221 {
1222 const epee::span<const key> keys{&amount_keys[amounts_proved], batch_size};
1223 if (plus)
1224 rv.p.bulletproofs_plus.push_back(proveRangeBulletproofPlus(C, masks, batch_amounts, keys, hwdev));
1225 else
1226 rv.p.bulletproofs.push_back(proveRangeBulletproof(C, masks, batch_amounts, keys, hwdev));
1227 #ifdef DBG
1228 if (plus)
1229 CHECK_AND_ASSERT_THROW_MES(verBulletproofPlus(rv.p.bulletproofs_plus.back()), "verBulletproofPlus failed on newly created proof");
1230 else
1231 CHECK_AND_ASSERT_THROW_MES(verBulletproof(rv.p.bulletproofs.back()), "verBulletproof failed on newly created proof");
1232 #endif
1233 }
1234 for (i = 0; i < batch_size; ++i)
1235 {
1236 rv.outPk[i + amounts_proved].mask = rct::scalarmult8(C[i]);
1237 outSk[i + amounts_proved].mask = masks[i];
1238 }
1239 amounts_proved += batch_size;
1240 }
1241 }
1242
1243 key sumout = zero();
1244 for (i = 0; i < outSk.size(); ++i)
1245 {
1246 sc_add(sumout.bytes, outSk[i].mask.bytes, sumout.bytes);
1247
1248 //mask amount and mask
1249 rv.ecdhInfo[i].mask = copy(outSk[i].mask);
1250 rv.ecdhInfo[i].amount = d2h(outamounts[i]);
1251 hwdev.ecdhEncode(rv.ecdhInfo[i], amount_keys[i], rv.type == RCTTypeBulletproof2 || rv.type == RCTTypeCLSAG || rv.type == RCTTypeBulletproofPlus);
1252 }
1253
1254 //set txn fee
1255 rv.txnFee = txnFee;
1256 // TODO: unused ??
1257 // key txnFeeKey = scalarmultH(d2h(rv.txnFee));
1258 rv.mixRing = mixRing;
1259 keyV &pseudoOuts = bulletproof_or_plus ? rv.p.pseudoOuts : rv.pseudoOuts;
1260 pseudoOuts.resize(inamounts.size());
1261 if (is_rct_clsag(rv.type))
1262 rv.p.CLSAGs.resize(inamounts.size());
1263 else
1264 rv.p.MGs.resize(inamounts.size());
1265 key sumpouts = zero(); //sum pseudoOut masks
1266 keyV a(inamounts.size());
1267 for (i = 0 ; i < inamounts.size() - 1; i++) {
1268 skGen(a[i]);
1269 sc_add(sumpouts.bytes, a[i].bytes, sumpouts.bytes);
1270 genC(pseudoOuts[i], a[i], inamounts[i]);
1271 }
1272 sc_sub(a[i].bytes, sumout.bytes, sumpouts.bytes);
1273 genC(pseudoOuts[i], a[i], inamounts[i]);
1274 DP(pseudoOuts[i]);
1275
1276 key full_message = get_pre_mlsag_hash(rv,hwdev);
1277
1278 for (i = 0 ; i < inamounts.size(); i++)
1279 {
1280 if (is_rct_clsag(rv.type))
1281 {
1282 if (hwdev.get_mode() == hw::device::TRANSACTION_CREATE_FAKE)
1283 rv.p.CLSAGs[i] = make_dummy_clsag(rv.mixRing[i].size());
1284 else
1285 rv.p.CLSAGs[i] = proveRctCLSAGSimple(full_message, rv.mixRing[i], inSk[i], a[i], pseudoOuts[i], index[i], hwdev);
1286 }
1287 else
1288 {
1289 rv.p.MGs[i] = proveRctMGSimple(full_message, rv.mixRing[i], inSk[i], a[i], pseudoOuts[i], index[i], hwdev);
1290 }
1291 }
1292 return rv;
1293 }
1294
1295 rctSig genRctSimple(const key &message, const ctkeyV & inSk, const ctkeyV & inPk, const keyV & destinations, const vector<xmr_amount> &inamounts, const vector<xmr_amount> &outamounts, const keyV &amount_keys, xmr_amount txnFee, unsigned int mixin, const RCTConfig &rct_config, hw::device &hwdev) {
1296 std::vector<unsigned int> index;
1297 index.resize(inPk.size());
1298 ctkeyM mixRing;
1299 ctkeyV outSk;
1300 mixRing.resize(inPk.size());
1301 for (size_t i = 0; i < inPk.size(); ++i) {
1302 mixRing[i].resize(mixin+1);
1303 index[i] = populateFromBlockchainSimple(mixRing[i], inPk[i], mixin);
1304 }
1305 return genRctSimple(message, inSk, destinations, inamounts, outamounts, txnFee, mixRing, amount_keys, index, outSk, rct_config, hwdev);
1306 }
1307
1308 //RingCT protocol
1309 //genRct:
1310 // creates an rctSig with all data necessary to verify the rangeProofs and that the signer owns one of the
1311 // columns that are claimed as inputs, and that the sum of inputs = sum of outputs.
1312 // Also contains masked "amount" and "mask" so the receiver can see how much they received
1313 //verRct:
1314 // verifies that all signatures (rangeProogs, MG sig, sum inputs = outputs) are correct
1315 //decodeRct: (c.f. https://eprint.iacr.org/2015/1098 section 5.1.1)
1316 // uses the attached ecdh info to find the amounts represented by each output commitment
1317 // must know the destination private key to find the correct amount, else will return a random number
1318 bool verRct(const rctSig & rv, bool semantics) {
1319 PERF_TIMER(verRct);
1320 CHECK_AND_ASSERT_MES(rv.type == RCTTypeFull, false, "verRct called on non-full rctSig");
1321 if (semantics)
1322 {
1323 CHECK_AND_ASSERT_MES(rv.outPk.size() == rv.p.rangeSigs.size(), false, "Mismatched sizes of outPk and rv.p.rangeSigs");
1324 CHECK_AND_ASSERT_MES(rv.outPk.size() == rv.ecdhInfo.size(), false, "Mismatched sizes of outPk and rv.ecdhInfo");
1325 CHECK_AND_ASSERT_MES(rv.p.MGs.size() == 1, false, "full rctSig has not one MG");
1326 }
1327 else
1328 {
1329 // semantics check is early, we don't have the MGs resolved yet
1330 }
1331
1332 // some rct ops can throw
1333 try
1334 {
1335 if (semantics) {
1336 tools::threadpool& tpool = tools::threadpool::getInstanceForCompute();
1337 tools::threadpool::waiter waiter(tpool);
1338 std::deque<bool> results(rv.outPk.size(), false);
1339 DP("range proofs verified?");
1340 for (size_t i = 0; i < rv.outPk.size(); i++)
1341 tpool.submit(&waiter, [&, i] { results[i] = verRange(rv.outPk[i].mask, rv.p.rangeSigs[i]); });
1342 if (!waiter.wait())
1343 return false;
1344
1345 for (size_t i = 0; i < results.size(); ++i) {
1346 if (!results[i]) {
1347 LOG_PRINT_L1("Range proof verified failed for proof " << i);
1348 return false;
1349 }
1350 }
1351 }
1352
1353 if (!semantics) {
1354 //compute txn fee
1355 key txnFeeKey = scalarmultH(d2h(rv.txnFee));
1356 bool mgVerd = verRctMG(rv.p.MGs[0], rv.mixRing, rv.outPk, txnFeeKey, get_pre_mlsag_hash(rv, hw::get_device("default")));
1357 DP("mg sig verified?");
1358 DP(mgVerd);
1359 if (!mgVerd) {
1360 LOG_PRINT_L1("MG signature verification failed");
1361 return false;
1362 }
1363 }
1364
1365 return true;
1366 }
1367 catch (const std::exception &e)
1368 {
1369 LOG_PRINT_L1("Error in verRct: " << e.what());
1370 return false;
1371 }
1372 catch (...)
1373 {
1374 LOG_PRINT_L1("Error in verRct, but not an actual exception");
1375 return false;
1376 }
1377 }
1378
1379 //ver RingCT simple
1380 //assumes only post-rct style inputs (at least for max anonymity)
1381 bool verRctSemanticsSimple(const std::vector<const rctSig*> & rvv) {
1382 try
1383 {
1384 PERF_TIMER(verRctSemanticsSimple);
1385
1386 tools::threadpool& tpool = tools::threadpool::getInstanceForCompute();
1387 tools::threadpool::waiter waiter(tpool);
1388 std::deque<bool> results;
1389 std::vector<const Bulletproof*> bp_proofs;
1390 std::vector<const BulletproofPlus*> bpp_proofs;
1391 size_t max_non_bp_proofs = 0, offset = 0;
1392
1393 for (const rctSig *rvp: rvv)
1394 {
1395 CHECK_AND_ASSERT_MES(rvp, false, "rctSig pointer is NULL");
1396 const rctSig &rv = *rvp;
1397 CHECK_AND_ASSERT_MES(rv.type == RCTTypeSimple || rv.type == RCTTypeBulletproof || rv.type == RCTTypeBulletproof2 || rv.type == RCTTypeCLSAG || rv.type == RCTTypeBulletproofPlus,
1398 false, "verRctSemanticsSimple called on non simple rctSig");
1399 const bool bulletproof = is_rct_bulletproof(rv.type);
1400 const bool bulletproof_plus = is_rct_bulletproof_plus(rv.type);
1401 if (bulletproof || bulletproof_plus)
1402 {
1403 if (bulletproof_plus)
1404 CHECK_AND_ASSERT_MES(rv.outPk.size() == n_bulletproof_plus_amounts(rv.p.bulletproofs_plus), false, "Mismatched sizes of outPk and bulletproofs_plus");
1405 else
1406 CHECK_AND_ASSERT_MES(rv.outPk.size() == n_bulletproof_amounts(rv.p.bulletproofs), false, "Mismatched sizes of outPk and bulletproofs");
1407 if (is_rct_clsag(rv.type))
1408 {
1409 CHECK_AND_ASSERT_MES(rv.p.MGs.empty(), false, "MGs are not empty for CLSAG");
1410 CHECK_AND_ASSERT_MES(rv.p.pseudoOuts.size() == rv.p.CLSAGs.size(), false, "Mismatched sizes of rv.p.pseudoOuts and rv.p.CLSAGs");
1411 }
1412 else
1413 {
1414 CHECK_AND_ASSERT_MES(rv.p.CLSAGs.empty(), false, "CLSAGs are not empty for MLSAG");
1415 CHECK_AND_ASSERT_MES(rv.p.pseudoOuts.size() == rv.p.MGs.size(), false, "Mismatched sizes of rv.p.pseudoOuts and rv.p.MGs");
1416 }
1417 CHECK_AND_ASSERT_MES(rv.pseudoOuts.empty(), false, "rv.pseudoOuts is not empty");
1418 }
1419 else
1420 {
1421 CHECK_AND_ASSERT_MES(rv.outPk.size() == rv.p.rangeSigs.size(), false, "Mismatched sizes of outPk and rv.p.rangeSigs");
1422 CHECK_AND_ASSERT_MES(rv.pseudoOuts.size() == rv.p.MGs.size(), false, "Mismatched sizes of rv.pseudoOuts and rv.p.MGs");
1423 CHECK_AND_ASSERT_MES(rv.p.pseudoOuts.empty(), false, "rv.p.pseudoOuts is not empty");
1424 }
1425 CHECK_AND_ASSERT_MES(rv.outPk.size() == rv.ecdhInfo.size(), false, "Mismatched sizes of outPk and rv.ecdhInfo");
1426
1427 if (!bulletproof && !bulletproof_plus)
1428 max_non_bp_proofs += rv.p.rangeSigs.size();
1429 }
1430
1431 results.resize(max_non_bp_proofs);
1432 for (const rctSig *rvp: rvv)
1433 {
1434 const rctSig &rv = *rvp;
1435
1436 const bool bulletproof = is_rct_bulletproof(rv.type);
1437 const bool bulletproof_plus = is_rct_bulletproof_plus(rv.type);
1438 const keyV &pseudoOuts = bulletproof || bulletproof_plus ? rv.p.pseudoOuts : rv.pseudoOuts;
1439
1440 rct::keyV masks(rv.outPk.size());
1441 for (size_t i = 0; i < rv.outPk.size(); i++) {
1442 masks[i] = rv.outPk[i].mask;
1443 }
1444 key sumOutpks = addKeys(masks);
1445 DP(sumOutpks);
1446 const key txnFeeKey = scalarmultH(d2h(rv.txnFee));
1447 addKeys(sumOutpks, txnFeeKey, sumOutpks);
1448
1449 key sumPseudoOuts = addKeys(pseudoOuts);
1450 DP(sumPseudoOuts);
1451
1452 //check pseudoOuts vs Outs..
1453 if (!equalKeys(sumPseudoOuts, sumOutpks)) {
1454 LOG_PRINT_L1("Sum check failed");
1455 return false;
1456 }
1457
1458 if (bulletproof_plus)
1459 {
1460 for (size_t i = 0; i < rv.p.bulletproofs_plus.size(); i++)
1461 bpp_proofs.push_back(&rv.p.bulletproofs_plus[i]);
1462 }
1463 else if (bulletproof)
1464 {
1465 for (size_t i = 0; i < rv.p.bulletproofs.size(); i++)
1466 bp_proofs.push_back(&rv.p.bulletproofs[i]);
1467 }
1468 else
1469 {
1470 for (size_t i = 0; i < rv.p.rangeSigs.size(); i++)
1471 tpool.submit(&waiter, [&, i, offset] { results[i+offset] = verRange(rv.outPk[i].mask, rv.p.rangeSigs[i]); });
1472 offset += rv.p.rangeSigs.size();
1473 }
1474 }
1475 if (!bpp_proofs.empty() && !verBulletproofPlus(bpp_proofs))
1476 {
1477 LOG_PRINT_L1("Aggregate range proof verified failed");
1478 if (!waiter.wait())
1479 return false;
1480 return false;
1481 }
1482 if (!bp_proofs.empty() && !verBulletproof(bp_proofs))
1483 {
1484 LOG_PRINT_L1("Aggregate range proof verified failed");
1485 if (!waiter.wait())
1486 return false;
1487 return false;
1488 }
1489
1490 if (!waiter.wait())
1491 return false;
1492 for (size_t i = 0; i < results.size(); ++i) {
1493 if (!results[i]) {
1494 LOG_PRINT_L1("Range proof verified failed for proof " << i);
1495 return false;
1496 }
1497 }
1498
1499 return true;
1500 }
1501 // we can get deep throws from ge_frombytes_vartime if input isn't valid
1502 catch (const std::exception &e)
1503 {
1504 LOG_PRINT_L1("Error in verRctSemanticsSimple: " << e.what());
1505 return false;
1506 }
1507 catch (...)
1508 {
1509 LOG_PRINT_L1("Error in verRctSemanticsSimple, but not an actual exception");
1510 return false;
1511 }
1512 }
1513
1514 bool verRctSemanticsSimple(const rctSig & rv)
1515 {
1516 return verRctSemanticsSimple(std::vector<const rctSig*>(1, &rv));
1517 }
1518
1519 //ver RingCT simple
1520 //assumes only post-rct style inputs (at least for max anonymity)
1521 bool verRctNonSemanticsSimple(const rctSig & rv) {
1522 try
1523 {
1524 PERF_TIMER(verRctNonSemanticsSimple);
1525
1526 CHECK_AND_ASSERT_MES(rv.type == RCTTypeSimple || rv.type == RCTTypeBulletproof || rv.type == RCTTypeBulletproof2 || rv.type == RCTTypeCLSAG || rv.type == RCTTypeBulletproofPlus,
1527 false, "verRctNonSemanticsSimple called on non simple rctSig");
1528 const bool bulletproof = is_rct_bulletproof(rv.type);
1529 const bool bulletproof_plus = is_rct_bulletproof_plus(rv.type);
1530 // semantics check is early, and mixRing/MGs aren't resolved yet
1531 if (bulletproof || bulletproof_plus)
1532 CHECK_AND_ASSERT_MES(rv.p.pseudoOuts.size() == rv.mixRing.size(), false, "Mismatched sizes of rv.p.pseudoOuts and mixRing");
1533 else
1534 CHECK_AND_ASSERT_MES(rv.pseudoOuts.size() == rv.mixRing.size(), false, "Mismatched sizes of rv.pseudoOuts and mixRing");
1535
1536 const size_t threads = std::max(rv.outPk.size(), rv.mixRing.size());
1537
1538 std::deque<bool> results(threads);
1539 tools::threadpool& tpool = tools::threadpool::getInstanceForCompute();
1540 tools::threadpool::waiter waiter(tpool);
1541
1542 const keyV &pseudoOuts = bulletproof || bulletproof_plus ? rv.p.pseudoOuts : rv.pseudoOuts;
1543
1544 const key message = get_pre_mlsag_hash(rv, hw::get_device("default"));
1545
1546 results.clear();
1547 results.resize(rv.mixRing.size());
1548 for (size_t i = 0 ; i < rv.mixRing.size() ; i++) {
1549 tpool.submit(&waiter, [&, i] {
1550 if (is_rct_clsag(rv.type))
1551 results[i] = verRctCLSAGSimple(message, rv.p.CLSAGs[i], rv.mixRing[i], pseudoOuts[i]);
1552 else
1553 results[i] = verRctMGSimple(message, rv.p.MGs[i], rv.mixRing[i], pseudoOuts[i]);
1554 });
1555 }
1556 if (!waiter.wait())
1557 return false;
1558
1559 for (size_t i = 0; i < results.size(); ++i) {
1560 if (!results[i]) {
1561 LOG_PRINT_L1("verRctMGSimple/verRctCLSAGSimple failed for input " << i);
1562 return false;
1563 }
1564 }
1565
1566 return true;
1567 }
1568 // we can get deep throws from ge_frombytes_vartime if input isn't valid
1569 catch (const std::exception &e)
1570 {
1571 LOG_PRINT_L1("Error in verRctNonSemanticsSimple: " << e.what());
1572 return false;
1573 }
1574 catch (...)
1575 {
1576 LOG_PRINT_L1("Error in verRctNonSemanticsSimple, but not an actual exception");
1577 return false;
1578 }
1579 }
1580
1581 //RingCT protocol
1582 //genRct:
1583 // creates an rctSig with all data necessary to verify the rangeProofs and that the signer owns one of the
1584 // columns that are claimed as inputs, and that the sum of inputs = sum of outputs.
1585 // Also contains masked "amount" and "mask" so the receiver can see how much they received
1586 //verRct:
1587 // verifies that all signatures (rangeProogs, MG sig, sum inputs = outputs) are correct
1588 //decodeRct: (c.f. https://eprint.iacr.org/2015/1098 section 5.1.1)
1589 // uses the attached ecdh info to find the amounts represented by each output commitment
1590 // must know the destination private key to find the correct amount, else will return a random number
1591 xmr_amount decodeRct(const rctSig & rv, const key & sk, unsigned int i, key & mask, hw::device &hwdev) {
1592 CHECK_AND_ASSERT_MES(rv.type == RCTTypeFull, false, "decodeRct called on non-full rctSig");
1593 CHECK_AND_ASSERT_THROW_MES(i < rv.ecdhInfo.size(), "Bad index");
1594 CHECK_AND_ASSERT_THROW_MES(rv.outPk.size() == rv.ecdhInfo.size(), "Mismatched sizes of rv.outPk and rv.ecdhInfo");
1595
1596 //mask amount and mask
1597 ecdhTuple ecdh_info = rv.ecdhInfo[i];
1598 hwdev.ecdhDecode(ecdh_info, sk, rv.type == RCTTypeBulletproof2 || rv.type == RCTTypeCLSAG || rv.type == RCTTypeBulletproofPlus);
1599 mask = ecdh_info.mask;
1600 key amount = ecdh_info.amount;
1601 key C = rv.outPk[i].mask;
1602 DP("C");
1603 DP(C);
1604 key Ctmp;
1605 CHECK_AND_ASSERT_THROW_MES(sc_check(mask.bytes) == 0, "warning, bad ECDH mask");
1606 CHECK_AND_ASSERT_THROW_MES(sc_check(amount.bytes) == 0, "warning, bad ECDH amount");
1607 addKeys2(Ctmp, mask, amount, H);
1608 DP("Ctmp");
1609 DP(Ctmp);
1610 if (equalKeys(C, Ctmp) == false) {
1611 CHECK_AND_ASSERT_THROW_MES(false, "warning, amount decoded incorrectly, will be unable to spend");
1612 }
1613 return h2d(amount);
1614 }
1615
1616 xmr_amount decodeRct(const rctSig & rv, const key & sk, unsigned int i, hw::device &hwdev) {
1617 key mask;
1618 return decodeRct(rv, sk, i, mask, hwdev);
1619 }
1620
1621 xmr_amount decodeRctSimple(const rctSig & rv, const key & sk, unsigned int i, key &mask, hw::device &hwdev) {
1622 CHECK_AND_ASSERT_MES(rv.type == RCTTypeSimple || rv.type == RCTTypeBulletproof || rv.type == RCTTypeBulletproof2 || rv.type == RCTTypeCLSAG || rv.type == RCTTypeBulletproofPlus,
1623 false, "decodeRct called on non simple rctSig");
1624 CHECK_AND_ASSERT_THROW_MES(i < rv.ecdhInfo.size(), "Bad index");
1625 CHECK_AND_ASSERT_THROW_MES(rv.outPk.size() == rv.ecdhInfo.size(), "Mismatched sizes of rv.outPk and rv.ecdhInfo");
1626
1627 //mask amount and mask
1628 ecdhTuple ecdh_info = rv.ecdhInfo[i];
1629 hwdev.ecdhDecode(ecdh_info, sk, rv.type == RCTTypeBulletproof2 || rv.type == RCTTypeCLSAG || rv.type == RCTTypeBulletproofPlus);
1630 mask = ecdh_info.mask;
1631 key amount = ecdh_info.amount;
1632 key C = rv.outPk[i].mask;
1633 DP("C");
1634 DP(C);
1635 key Ctmp;
1636 CHECK_AND_ASSERT_THROW_MES(sc_check(mask.bytes) == 0, "warning, bad ECDH mask");
1637 CHECK_AND_ASSERT_THROW_MES(sc_check(amount.bytes) == 0, "warning, bad ECDH amount");
1638 addKeys2(Ctmp, mask, amount, H);
1639 DP("Ctmp");
1640 DP(Ctmp);
1641 if (equalKeys(C, Ctmp) == false) {
1642 CHECK_AND_ASSERT_THROW_MES(false, "warning, amount decoded incorrectly, will be unable to spend");
1643 }
1644 return h2d(amount);
1645 }
1646
1647 xmr_amount decodeRctSimple(const rctSig & rv, const key & sk, unsigned int i, hw::device &hwdev) {
1648 key mask;
1649 return decodeRctSimple(rv, sk, i, mask, hwdev);
1650 }
1651 }