Debian Codegraph @

src/ringct/bulletproofs.cc

raw symbols SCIP
1 // Copyright (c) 2017-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Adapted from Java code by Sarang Noether
30 // Paper references are to https://eprint.iacr.org/2017/1066 (revision 1 July 2018)
31
32 #include <stdlib.h>
33 #include <boost/thread/mutex.hpp>
34 #include <boost/thread/lock_guard.hpp>
35 #include "misc_log_ex.h"
36 #include "span.h"
37 #include "common/perf_timer.h"
38 #include "cryptonote_config.h"
39 extern "C"
40 {
41 #include "crypto/crypto-ops.h"
42 }
43 #include "rctOps.h"
44 #include "multiexp.h"
45 #include "bulletproofs.h"
46
47 #undef MONERO_DEFAULT_LOG_CATEGORY
48 #define MONERO_DEFAULT_LOG_CATEGORY "bulletproofs"
49
50 //#define DEBUG_BP
51
52 #if 0
53 #define PERF_TIMER_START_BP(x) PERF_TIMER_START_UNIT(x, 1000000)
54 #define PERF_TIMER_STOP_BP(x) PERF_TIMER_STOP(x)
55 #else
56 #define PERF_TIMER_START_BP(x) ((void)0)
57 #define PERF_TIMER_STOP_BP(x) ((void)0)
58 #endif
59
60 #define STRAUS_SIZE_LIMIT 232
61 #define PIPPENGER_SIZE_LIMIT 0
62
63 namespace rct
64 {
65
66 static rct::key vector_exponent(const rct::keyV &a, const rct::keyV &b);
67 static rct::keyV vector_powers(const rct::key &x, size_t n);
68 static rct::keyV vector_dup(const rct::key &x, size_t n);
69 static rct::key inner_product(const rct::keyV &a, const rct::keyV &b);
70
71 static constexpr size_t maxN = 64;
72 static constexpr size_t maxM = BULLETPROOF_MAX_OUTPUTS;
73 static ge_p3 Hi_p3[maxN*maxM], Gi_p3[maxN*maxM];
74 static std::shared_ptr<straus_cached_data> straus_HiGi_cache;
75 static std::shared_ptr<pippenger_cached_data> pippenger_HiGi_cache;
76 static const constexpr rct::key TWO = { {0x02, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 , 0x00, 0x00, 0x00,0x00 } };
77 static const constexpr rct::key MINUS_ONE = { { 0xec, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 } };
78 static const constexpr rct::key MINUS_INV_EIGHT = { { 0x74, 0xa4, 0x19, 0x7a, 0xf0, 0x7d, 0x0b, 0xf7, 0x05, 0xc2, 0xda, 0x25, 0x2b, 0x5c, 0x0b, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a } };
79 static const rct::keyV oneN = vector_dup(rct::identity(), maxN);
80 static const rct::keyV twoN = vector_powers(TWO, maxN);
81 static const rct::key ip12 = inner_product(oneN, twoN);
82 static boost::mutex init_mutex;
83
84 static inline rct::key multiexp(const std::vector<MultiexpData> &data, size_t HiGi_size)
85 {
86 if (HiGi_size > 0)
87 {
88 static_assert(232 <= STRAUS_SIZE_LIMIT, "Straus in precalc mode can only be calculated till STRAUS_SIZE_LIMIT");
89 return HiGi_size <= 232 && data.size() == HiGi_size ? straus(data, straus_HiGi_cache, 0) : pippenger(data, pippenger_HiGi_cache, HiGi_size, get_pippenger_c(data.size()));
90 }
91 else
92 return data.size() <= 95 ? straus(data, NULL, 0) : pippenger(data, NULL, 0, get_pippenger_c(data.size()));
93 }
94
95 static inline bool is_reduced(const rct::key &scalar)
96 {
97 return sc_check(scalar.bytes) == 0;
98 }
99
100 static rct::key get_exponent(const rct::key &base, size_t idx)
101 {
102 std::string hashed = std::string((const char*)base.bytes, sizeof(base)) + config::HASH_KEY_BULLETPROOF_EXPONENT + tools::get_varint_data(idx);
103 rct::key e;
104 ge_p3 e_p3;
105 rct::hash_to_p3(e_p3, rct::hash2rct(crypto::cn_fast_hash(hashed.data(), hashed.size())));
106 ge_p3_tobytes(e.bytes, &e_p3);
107 CHECK_AND_ASSERT_THROW_MES(!(e == rct::identity()), "Exponent is point at infinity");
108 return e;
109 }
110
111 static void init_exponents()
112 {
113 boost::lock_guard<boost::mutex> lock(init_mutex);
114
115 static bool init_done = false;
116 if (init_done)
117 return;
118 std::vector<MultiexpData> data;
119 data.reserve(maxN*maxM*2);
120 for (size_t i = 0; i < maxN*maxM; ++i)
121 {
122 const rct::key Hi = get_exponent(rct::H, i * 2);
123 CHECK_AND_ASSERT_THROW_MES(ge_frombytes_vartime(&Hi_p3[i], Hi.bytes) == 0, "ge_frombytes_vartime failed");
124 const rct::key Gi = get_exponent(rct::H, i * 2 + 1);
125 CHECK_AND_ASSERT_THROW_MES(ge_frombytes_vartime(&Gi_p3[i], Gi.bytes) == 0, "ge_frombytes_vartime failed");
126
127 data.push_back({rct::zero(), Gi_p3[i]});
128 data.push_back({rct::zero(), Hi_p3[i]});
129 }
130
131 straus_HiGi_cache = straus_init_cache(data, STRAUS_SIZE_LIMIT);
132 pippenger_HiGi_cache = pippenger_init_cache(data, 0, PIPPENGER_SIZE_LIMIT);
133
134 MINFO("Hi_p3/Gi_p3 cache size: " << (sizeof(Hi_p3)+sizeof(Gi_p3))/1024 << " kB");
135 MINFO("Straus cache size: " << straus_get_cache_size(straus_HiGi_cache)/1024 << " kB");
136 MINFO("Pippenger cache size: " << pippenger_get_cache_size(pippenger_HiGi_cache)/1024 << " kB");
137 size_t cache_size = straus_get_cache_size(straus_HiGi_cache) + pippenger_get_cache_size(pippenger_HiGi_cache);
138 MINFO("Total cache size: " << cache_size/1024 << "kB");
139 init_done = true;
140 }
141
142 /* Given two scalar arrays, construct a vector commitment */
143 static rct::key vector_exponent(const rct::keyV &a, const rct::keyV &b)
144 {
145 CHECK_AND_ASSERT_THROW_MES(a.size() == b.size(), "Incompatible sizes of a and b");
146 CHECK_AND_ASSERT_THROW_MES(a.size() <= maxN*maxM, "Incompatible sizes of a and maxN");
147
148 std::vector<MultiexpData> multiexp_data;
149 multiexp_data.reserve(a.size()*2);
150 for (size_t i = 0; i < a.size(); ++i)
151 {
152 multiexp_data.emplace_back(a[i], Gi_p3[i]);
153 multiexp_data.emplace_back(b[i], Hi_p3[i]);
154 }
155 return multiexp(multiexp_data, 2 * a.size());
156 }
157
158 /* Compute a custom vector-scalar commitment */
159 static rct::key cross_vector_exponent8(size_t size, const std::vector<ge_p3> &A, size_t Ao, const std::vector<ge_p3> &B, size_t Bo, const rct::keyV &a, size_t ao, const rct::keyV &b, size_t bo, const rct::keyV *scale, const ge_p3 *extra_point, const rct::key *extra_scalar)
160 {
161 CHECK_AND_ASSERT_THROW_MES(size + Ao <= A.size(), "Incompatible size for A");
162 CHECK_AND_ASSERT_THROW_MES(size + Bo <= B.size(), "Incompatible size for B");
163 CHECK_AND_ASSERT_THROW_MES(size + ao <= a.size(), "Incompatible size for a");
164 CHECK_AND_ASSERT_THROW_MES(size + bo <= b.size(), "Incompatible size for b");
165 CHECK_AND_ASSERT_THROW_MES(size <= maxN*maxM, "size is too large");
166 CHECK_AND_ASSERT_THROW_MES(!scale || size == scale->size() / 2, "Incompatible size for scale");
167 CHECK_AND_ASSERT_THROW_MES(!!extra_point == !!extra_scalar, "only one of extra point/scalar present");
168
169 std::vector<MultiexpData> multiexp_data;
170 multiexp_data.resize(size*2 + (!!extra_point));
171 for (size_t i = 0; i < size; ++i)
172 {
173 sc_mul(multiexp_data[i*2].scalar.bytes, a[ao+i].bytes, INV_EIGHT.bytes);
174 multiexp_data[i*2].point = A[Ao+i];
175 sc_mul(multiexp_data[i*2+1].scalar.bytes, b[bo+i].bytes, INV_EIGHT.bytes);
176 if (scale)
177 sc_mul(multiexp_data[i*2+1].scalar.bytes, multiexp_data[i*2+1].scalar.bytes, (*scale)[Bo+i].bytes);
178 multiexp_data[i*2+1].point = B[Bo+i];
179 }
180 if (extra_point)
181 {
182 sc_mul(multiexp_data.back().scalar.bytes, extra_scalar->bytes, INV_EIGHT.bytes);
183 multiexp_data.back().point = *extra_point;
184 }
185 return multiexp(multiexp_data, 0);
186 }
187
188 /* Given a scalar, construct a vector of powers */
189 static rct::keyV vector_powers(const rct::key &x, size_t n)
190 {
191 rct::keyV res(n);
192 if (n == 0)
193 return res;
194 res[0] = rct::identity();
195 if (n == 1)
196 return res;
197 res[1] = x;
198 for (size_t i = 2; i < n; ++i)
199 {
200 sc_mul(res[i].bytes, res[i-1].bytes, x.bytes);
201 }
202 return res;
203 }
204
205 /* Given a scalar, return the sum of its powers from 0 to n-1 */
206 static rct::key vector_power_sum(rct::key x, size_t n)
207 {
208 if (n == 0)
209 return rct::zero();
210 rct::key res = rct::identity();
211 if (n == 1)
212 return res;
213
214 const bool is_power_of_2 = (n & (n - 1)) == 0;
215 if (is_power_of_2)
216 {
217 sc_add(res.bytes, res.bytes, x.bytes);
218 while (n > 2)
219 {
220 sc_mul(x.bytes, x.bytes, x.bytes);
221 sc_muladd(res.bytes, x.bytes, res.bytes, res.bytes);
222 n /= 2;
223 }
224 }
225 else
226 {
227 rct::key prev = x;
228 for (size_t i = 1; i < n; ++i)
229 {
230 if (i > 1)
231 sc_mul(prev.bytes, prev.bytes, x.bytes);
232 sc_add(res.bytes, res.bytes, prev.bytes);
233 }
234 }
235
236 return res;
237 }
238
239 /* Given two scalar arrays, construct the inner product */
240 static rct::key inner_product(const epee::span<const rct::key> &a, const epee::span<const rct::key> &b)
241 {
242 CHECK_AND_ASSERT_THROW_MES(a.size() == b.size(), "Incompatible sizes of a and b");
243 rct::key res = rct::zero();
244 for (size_t i = 0; i < a.size(); ++i)
245 {
246 sc_muladd(res.bytes, a[i].bytes, b[i].bytes, res.bytes);
247 }
248 return res;
249 }
250
251 static rct::key inner_product(const rct::keyV &a, const rct::keyV &b)
252 {
253 return inner_product(epee::span<const rct::key>(a.data(), a.size()), epee::span<const rct::key>(b.data(), b.size()));
254 }
255
256 /* Given two scalar arrays, construct the Hadamard product */
257 static rct::keyV hadamard(const rct::keyV &a, const rct::keyV &b)
258 {
259 CHECK_AND_ASSERT_THROW_MES(a.size() == b.size(), "Incompatible sizes of a and b");
260 rct::keyV res(a.size());
261 for (size_t i = 0; i < a.size(); ++i)
262 {
263 sc_mul(res[i].bytes, a[i].bytes, b[i].bytes);
264 }
265 return res;
266 }
267
268 /* folds a curvepoint array using a two way scaled Hadamard product */
269 static void hadamard_fold(std::vector<ge_p3> &v, const rct::keyV *scale, const rct::key &a, const rct::key &b)
270 {
271 CHECK_AND_ASSERT_THROW_MES((v.size() & 1) == 0, "Vector size should be even");
272 const size_t sz = v.size() / 2;
273 for (size_t n = 0; n < sz; ++n)
274 {
275 ge_dsmp c[2];
276 ge_dsm_precomp(c[0], &v[n]);
277 ge_dsm_precomp(c[1], &v[sz + n]);
278 rct::key sa, sb;
279 if (scale) sc_mul(sa.bytes, a.bytes, (*scale)[n].bytes); else sa = a;
280 if (scale) sc_mul(sb.bytes, b.bytes, (*scale)[sz + n].bytes); else sb = b;
281 ge_double_scalarmult_precomp_vartime2_p3(&v[n], sa.bytes, c[0], sb.bytes, c[1]);
282 }
283 v.resize(sz);
284 }
285
286 /* Add two vectors */
287 static rct::keyV vector_add(const rct::keyV &a, const rct::keyV &b)
288 {
289 CHECK_AND_ASSERT_THROW_MES(a.size() == b.size(), "Incompatible sizes of a and b");
290 rct::keyV res(a.size());
291 for (size_t i = 0; i < a.size(); ++i)
292 {
293 sc_add(res[i].bytes, a[i].bytes, b[i].bytes);
294 }
295 return res;
296 }
297
298 /* Add a scalar to all elements of a vector */
299 static rct::keyV vector_add(const rct::keyV &a, const rct::key &b)
300 {
301 rct::keyV res(a.size());
302 for (size_t i = 0; i < a.size(); ++i)
303 {
304 sc_add(res[i].bytes, a[i].bytes, b.bytes);
305 }
306 return res;
307 }
308
309 /* Subtract a scalar from all elements of a vector */
310 static rct::keyV vector_subtract(const rct::keyV &a, const rct::key &b)
311 {
312 rct::keyV res(a.size());
313 for (size_t i = 0; i < a.size(); ++i)
314 {
315 sc_sub(res[i].bytes, a[i].bytes, b.bytes);
316 }
317 return res;
318 }
319
320 /* Multiply a scalar and a vector */
321 static rct::keyV vector_scalar(const epee::span<const rct::key> &a, const rct::key &x)
322 {
323 rct::keyV res(a.size());
324 for (size_t i = 0; i < a.size(); ++i)
325 {
326 sc_mul(res[i].bytes, a[i].bytes, x.bytes);
327 }
328 return res;
329 }
330
331 static rct::keyV vector_scalar(const rct::keyV &a, const rct::key &x)
332 {
333 return vector_scalar(epee::span<const rct::key>(a.data(), a.size()), x);
334 }
335
336 /* Create a vector from copies of a single value */
337 static rct::keyV vector_dup(const rct::key &x, size_t N)
338 {
339 return rct::keyV(N, x);
340 }
341
342 static rct::key sm(rct::key y, int n, const rct::key &x)
343 {
344 while (n--)
345 sc_mul(y.bytes, y.bytes, y.bytes);
346 sc_mul(y.bytes, y.bytes, x.bytes);
347 return y;
348 }
349
350 /* Compute the inverse of a scalar, the clever way */
351 static rct::key invert(const rct::key &x)
352 {
353 rct::key _1, _10, _100, _11, _101, _111, _1001, _1011, _1111;
354
355 _1 = x;
356 sc_mul(_10.bytes, _1.bytes, _1.bytes);
357 sc_mul(_100.bytes, _10.bytes, _10.bytes);
358 sc_mul(_11.bytes, _10.bytes, _1.bytes);
359 sc_mul(_101.bytes, _10.bytes, _11.bytes);
360 sc_mul(_111.bytes, _10.bytes, _101.bytes);
361 sc_mul(_1001.bytes, _10.bytes, _111.bytes);
362 sc_mul(_1011.bytes, _10.bytes, _1001.bytes);
363 sc_mul(_1111.bytes, _100.bytes, _1011.bytes);
364
365 rct::key inv;
366 sc_mul(inv.bytes, _1111.bytes, _1.bytes);
367
368 inv = sm(inv, 123 + 3, _101);
369 inv = sm(inv, 2 + 2, _11);
370 inv = sm(inv, 1 + 4, _1111);
371 inv = sm(inv, 1 + 4, _1111);
372 inv = sm(inv, 4, _1001);
373 inv = sm(inv, 2, _11);
374 inv = sm(inv, 1 + 4, _1111);
375 inv = sm(inv, 1 + 3, _101);
376 inv = sm(inv, 3 + 3, _101);
377 inv = sm(inv, 3, _111);
378 inv = sm(inv, 1 + 4, _1111);
379 inv = sm(inv, 2 + 3, _111);
380 inv = sm(inv, 2 + 2, _11);
381 inv = sm(inv, 1 + 4, _1011);
382 inv = sm(inv, 2 + 4, _1011);
383 inv = sm(inv, 6 + 4, _1001);
384 inv = sm(inv, 2 + 2, _11);
385 inv = sm(inv, 3 + 2, _11);
386 inv = sm(inv, 3 + 2, _11);
387 inv = sm(inv, 1 + 4, _1001);
388 inv = sm(inv, 1 + 3, _111);
389 inv = sm(inv, 2 + 4, _1111);
390 inv = sm(inv, 1 + 4, _1011);
391 inv = sm(inv, 3, _101);
392 inv = sm(inv, 2 + 4, _1111);
393 inv = sm(inv, 3, _101);
394 inv = sm(inv, 1 + 2, _11);
395
396 #ifdef DEBUG_BP
397 rct::key tmp;
398 sc_mul(tmp.bytes, inv.bytes, x.bytes);
399 CHECK_AND_ASSERT_THROW_MES(tmp == rct::identity(), "invert failed");
400 #endif
401 return inv;
402 }
403
404 static rct::keyV invert(rct::keyV x)
405 {
406 rct::keyV scratch;
407 scratch.reserve(x.size());
408
409 rct::key acc = rct::identity();
410 for (size_t n = 0; n < x.size(); ++n)
411 {
412 scratch.push_back(acc);
413 if (n == 0)
414 acc = x[0];
415 else
416 sc_mul(acc.bytes, acc.bytes, x[n].bytes);
417 }
418
419 acc = invert(acc);
420
421 rct::key tmp;
422 for (int i = x.size(); i-- > 0; )
423 {
424 sc_mul(tmp.bytes, acc.bytes, x[i].bytes);
425 sc_mul(x[i].bytes, acc.bytes, scratch[i].bytes);
426 acc = tmp;
427 }
428
429 return x;
430 }
431
432 /* Compute the slice of a vector */
433 static epee::span<const rct::key> slice(const rct::keyV &a, size_t start, size_t stop)
434 {
435 CHECK_AND_ASSERT_THROW_MES(start < a.size(), "Invalid start index");
436 CHECK_AND_ASSERT_THROW_MES(stop <= a.size(), "Invalid stop index");
437 CHECK_AND_ASSERT_THROW_MES(start < stop, "Invalid start/stop indices");
438 return epee::span<const rct::key>(&a[start], stop - start);
439 }
440
441 static rct::key hash_cache_mash(rct::key &hash_cache, const rct::key &mash0, const rct::key &mash1)
442 {
443 rct::key data[3];
444 data[0] = hash_cache;
445 data[1] = mash0;
446 data[2] = mash1;
447 rct::hash_to_scalar(hash_cache, data, sizeof(data));
448 return hash_cache;
449 }
450
451 static rct::key hash_cache_mash(rct::key &hash_cache, const rct::key &mash0, const rct::key &mash1, const rct::key &mash2)
452 {
453 rct::key data[4];
454 data[0] = hash_cache;
455 data[1] = mash0;
456 data[2] = mash1;
457 data[3] = mash2;
458 rct::hash_to_scalar(hash_cache, data, sizeof(data));
459 return hash_cache;
460 }
461
462 static rct::key hash_cache_mash(rct::key &hash_cache, const rct::key &mash0, const rct::key &mash1, const rct::key &mash2, const rct::key &mash3)
463 {
464 rct::key data[5];
465 data[0] = hash_cache;
466 data[1] = mash0;
467 data[2] = mash1;
468 data[3] = mash2;
469 data[4] = mash3;
470 rct::hash_to_scalar(hash_cache, data, sizeof(data));
471 return hash_cache;
472 }
473
474 /* Given a value v (0..2^N-1) and a mask gamma, construct a range proof */
475 Bulletproof bulletproof_PROVE(const rct::key &sv, const rct::key &gamma)
476 {
477 return bulletproof_PROVE(rct::keyV(1, sv), rct::keyV(1, gamma));
478 }
479
480 Bulletproof bulletproof_PROVE(uint64_t v, const rct::key &gamma)
481 {
482 return bulletproof_PROVE(std::vector<uint64_t>(1, v), rct::keyV(1, gamma));
483 }
484
485 /* Given a set of values v (0..2^N-1) and masks gamma, construct a range proof */
486 Bulletproof bulletproof_PROVE(const rct::keyV &sv, const rct::keyV &gamma)
487 {
488 CHECK_AND_ASSERT_THROW_MES(sv.size() == gamma.size(), "Incompatible sizes of sv and gamma");
489 CHECK_AND_ASSERT_THROW_MES(!sv.empty(), "sv is empty");
490 for (const rct::key &sve: sv)
491 CHECK_AND_ASSERT_THROW_MES(is_reduced(sve), "Invalid sv input");
492 for (const rct::key &g: gamma)
493 CHECK_AND_ASSERT_THROW_MES(is_reduced(g), "Invalid gamma input");
494
495 init_exponents();
496
497 PERF_TIMER_UNIT(PROVE, 1000000);
498
499 constexpr size_t logN = 6; // log2(64)
500 constexpr size_t N = 1<<logN;
501 size_t M, logM;
502 for (logM = 0; (M = 1<<logM) <= maxM && M < sv.size(); ++logM);
503 CHECK_AND_ASSERT_THROW_MES(M <= maxM, "sv/gamma are too large");
504 const size_t logMN = logM + logN;
505 const size_t MN = M * N;
506
507 rct::keyV V(sv.size());
508 rct::keyV aL(MN), aR(MN);
509 rct::keyV aL8(MN), aR8(MN);
510 rct::key tmp, tmp2;
511
512 PERF_TIMER_START_BP(PROVE_v);
513 for (size_t i = 0; i < sv.size(); ++i)
514 {
515 rct::key gamma8, sv8;
516 sc_mul(gamma8.bytes, gamma[i].bytes, INV_EIGHT.bytes);
517 sc_mul(sv8.bytes, sv[i].bytes, INV_EIGHT.bytes);
518 rct::addKeys2(V[i], gamma8, sv8, rct::H);
519 }
520 PERF_TIMER_STOP_BP(PROVE_v);
521
522 // PAPER LINES 41-42
523 PERF_TIMER_START_BP(PROVE_aLaR);
524 for (size_t j = 0; j < M; ++j)
525 {
526 for (size_t i = N; i-- > 0; )
527 {
528 if (j < sv.size() && (sv[j][i/8] & (((uint64_t)1)<<(i%8))))
529 {
530 aL[j*N+i] = rct::identity();
531 aL8[j*N+i] = INV_EIGHT;
532 aR[j*N+i] = aR8[j*N+i] = rct::zero();
533 }
534 else
535 {
536 aL[j*N+i] = aL8[j*N+i] = rct::zero();
537 aR[j*N+i] = MINUS_ONE;
538 aR8[j*N+i] = MINUS_INV_EIGHT;
539 }
540 }
541 }
542 PERF_TIMER_STOP_BP(PROVE_aLaR);
543
544 // DEBUG: Test to ensure this recovers the value
545 #ifdef DEBUG_BP
546 for (size_t j = 0; j < M; ++j)
547 {
548 uint64_t test_aL = 0, test_aR = 0;
549 for (size_t i = 0; i < N; ++i)
550 {
551 if (aL[j*N+i] == rct::identity())
552 test_aL += ((uint64_t)1)<<i;
553 if (aR[j*N+i] == rct::zero())
554 test_aR += ((uint64_t)1)<<i;
555 }
556 uint64_t v_test = 0;
557 if (j < sv.size())
558 for (int n = 0; n < 8; ++n) v_test |= (((uint64_t)sv[j][n]) << (8*n));
559 CHECK_AND_ASSERT_THROW_MES(test_aL == v_test, "test_aL failed");
560 CHECK_AND_ASSERT_THROW_MES(test_aR == v_test, "test_aR failed");
561 }
562 #endif
563
564 try_again:
565 rct::key hash_cache = rct::hash_to_scalar(V);
566
567 PERF_TIMER_START_BP(PROVE_step1);
568 // PAPER LINES 43-44
569 rct::key alpha = rct::skGen();
570 rct::key ve = vector_exponent(aL8, aR8);
571 rct::key A;
572 sc_mul(tmp.bytes, alpha.bytes, INV_EIGHT.bytes);
573 rct::addKeys(A, ve, rct::scalarmultBase(tmp));
574
575 // PAPER LINES 45-47
576 rct::keyV sL = rct::skvGen(MN), sR = rct::skvGen(MN);
577 rct::key rho = rct::skGen();
578 ve = vector_exponent(sL, sR);
579 rct::key S;
580 rct::addKeys(S, ve, rct::scalarmultBase(rho));
581 S = rct::scalarmultKey(S, INV_EIGHT);
582
583 // PAPER LINES 48-50
584 rct::key y = hash_cache_mash(hash_cache, A, S);
585 if (y == rct::zero())
586 {
587 PERF_TIMER_STOP_BP(PROVE_step1);
588 MINFO("y is 0, trying again");
589 goto try_again;
590 }
591 rct::key z = hash_cache = rct::hash_to_scalar(y);
592 if (z == rct::zero())
593 {
594 PERF_TIMER_STOP_BP(PROVE_step1);
595 MINFO("z is 0, trying again");
596 goto try_again;
597 }
598
599 // Polynomial construction by coefficients
600 // PAPER LINES 70-71
601 rct::keyV l0 = vector_subtract(aL, z);
602 const rct::keyV &l1 = sL;
603
604 rct::keyV zero_twos(MN);
605 const rct::keyV zpow = vector_powers(z, M+2);
606 for (size_t j = 0; j < M; ++j)
607 {
608 for (size_t i = 0; i < N; ++i)
609 {
610 CHECK_AND_ASSERT_THROW_MES(j+2 < zpow.size(), "invalid zpow index");
611 CHECK_AND_ASSERT_THROW_MES(i < twoN.size(), "invalid twoN index");
612 sc_mul(zero_twos[j*N+i].bytes,zpow[j+2].bytes,twoN[i].bytes);
613 }
614 }
615
616 rct::keyV r0 = vector_add(aR, z);
617 const auto yMN = vector_powers(y, MN);
618 r0 = hadamard(r0, yMN);
619 r0 = vector_add(r0, zero_twos);
620 rct::keyV r1 = hadamard(yMN, sR);
621
622 // Polynomial construction before PAPER LINE 51
623 rct::key t1_1 = inner_product(l0, r1);
624 rct::key t1_2 = inner_product(l1, r0);
625 rct::key t1;
626 sc_add(t1.bytes, t1_1.bytes, t1_2.bytes);
627 rct::key t2 = inner_product(l1, r1);
628
629 PERF_TIMER_STOP_BP(PROVE_step1);
630
631 PERF_TIMER_START_BP(PROVE_step2);
632 // PAPER LINES 52-53
633 rct::key tau1 = rct::skGen(), tau2 = rct::skGen();
634
635 rct::key T1, T2;
636 ge_p3 p3;
637 sc_mul(tmp.bytes, t1.bytes, INV_EIGHT.bytes);
638 sc_mul(tmp2.bytes, tau1.bytes, INV_EIGHT.bytes);
639 ge_double_scalarmult_base_vartime_p3(&p3, tmp.bytes, &ge_p3_H, tmp2.bytes);
640 ge_p3_tobytes(T1.bytes, &p3);
641 sc_mul(tmp.bytes, t2.bytes, INV_EIGHT.bytes);
642 sc_mul(tmp2.bytes, tau2.bytes, INV_EIGHT.bytes);
643 ge_double_scalarmult_base_vartime_p3(&p3, tmp.bytes, &ge_p3_H, tmp2.bytes);
644 ge_p3_tobytes(T2.bytes, &p3);
645
646 // PAPER LINES 54-56
647 rct::key x = hash_cache_mash(hash_cache, z, T1, T2);
648 if (x == rct::zero())
649 {
650 PERF_TIMER_STOP_BP(PROVE_step2);
651 MINFO("x is 0, trying again");
652 goto try_again;
653 }
654
655 // PAPER LINES 61-63
656 rct::key taux;
657 sc_mul(taux.bytes, tau1.bytes, x.bytes);
658 rct::key xsq;
659 sc_mul(xsq.bytes, x.bytes, x.bytes);
660 sc_muladd(taux.bytes, tau2.bytes, xsq.bytes, taux.bytes);
661 for (size_t j = 1; j <= sv.size(); ++j)
662 {
663 CHECK_AND_ASSERT_THROW_MES(j+1 < zpow.size(), "invalid zpow index");
664 sc_muladd(taux.bytes, zpow[j+1].bytes, gamma[j-1].bytes, taux.bytes);
665 }
666 rct::key mu;
667 sc_muladd(mu.bytes, x.bytes, rho.bytes, alpha.bytes);
668
669 // PAPER LINES 58-60
670 rct::keyV l = l0;
671 l = vector_add(l, vector_scalar(l1, x));
672 rct::keyV r = r0;
673 r = vector_add(r, vector_scalar(r1, x));
674 PERF_TIMER_STOP_BP(PROVE_step2);
675
676 PERF_TIMER_START_BP(PROVE_step3);
677 rct::key t = inner_product(l, r);
678
679 // DEBUG: Test if the l and r vectors match the polynomial forms
680 #ifdef DEBUG_BP
681 rct::key test_t;
682 const rct::key t0 = inner_product(l0, r0);
683 sc_muladd(test_t.bytes, t1.bytes, x.bytes, t0.bytes);
684 sc_muladd(test_t.bytes, t2.bytes, xsq.bytes, test_t.bytes);
685 CHECK_AND_ASSERT_THROW_MES(test_t == t, "test_t check failed");
686 #endif
687
688 // PAPER LINE 6
689 rct::key x_ip = hash_cache_mash(hash_cache, x, taux, mu, t);
690 if (x_ip == rct::zero())
691 {
692 PERF_TIMER_STOP_BP(PROVE_step3);
693 MINFO("x_ip is 0, trying again");
694 goto try_again;
695 }
696
697 // These are used in the inner product rounds
698 size_t nprime = MN;
699 std::vector<ge_p3> Gprime(MN);
700 std::vector<ge_p3> Hprime(MN);
701 rct::keyV aprime(MN);
702 rct::keyV bprime(MN);
703 const rct::key yinv = invert(y);
704 rct::keyV yinvpow(MN);
705 yinvpow[0] = rct::identity();
706 yinvpow[1] = yinv;
707 for (size_t i = 0; i < MN; ++i)
708 {
709 Gprime[i] = Gi_p3[i];
710 Hprime[i] = Hi_p3[i];
711 if (i > 1)
712 sc_mul(yinvpow[i].bytes, yinvpow[i-1].bytes, yinv.bytes);
713 aprime[i] = l[i];
714 bprime[i] = r[i];
715 }
716 rct::keyV L(logMN);
717 rct::keyV R(logMN);
718 int round = 0;
719 rct::keyV w(logMN); // this is the challenge x in the inner product protocol
720 PERF_TIMER_STOP_BP(PROVE_step3);
721
722 PERF_TIMER_START_BP(PROVE_step4);
723 const rct::keyV *scale = &yinvpow;
724 while (nprime > 1)
725 {
726 // PAPER LINE 20
727 nprime /= 2;
728
729 // PAPER LINES 21-22
730 PERF_TIMER_START_BP(PROVE_inner_product);
731 rct::key cL = inner_product(slice(aprime, 0, nprime), slice(bprime, nprime, bprime.size()));
732 rct::key cR = inner_product(slice(aprime, nprime, aprime.size()), slice(bprime, 0, nprime));
733 PERF_TIMER_STOP_BP(PROVE_inner_product);
734
735 // PAPER LINES 23-24
736 PERF_TIMER_START_BP(PROVE_LR);
737 sc_mul(tmp.bytes, cL.bytes, x_ip.bytes);
738 L[round] = cross_vector_exponent8(nprime, Gprime, nprime, Hprime, 0, aprime, 0, bprime, nprime, scale, &ge_p3_H, &tmp);
739 sc_mul(tmp.bytes, cR.bytes, x_ip.bytes);
740 R[round] = cross_vector_exponent8(nprime, Gprime, 0, Hprime, nprime, aprime, nprime, bprime, 0, scale, &ge_p3_H, &tmp);
741 PERF_TIMER_STOP_BP(PROVE_LR);
742
743 // PAPER LINES 25-27
744 w[round] = hash_cache_mash(hash_cache, L[round], R[round]);
745 if (w[round] == rct::zero())
746 {
747 PERF_TIMER_STOP_BP(PROVE_step4);
748 MINFO("w[round] is 0, trying again");
749 goto try_again;
750 }
751
752 // PAPER LINES 29-30
753 const rct::key winv = invert(w[round]);
754 if (nprime > 1)
755 {
756 PERF_TIMER_START_BP(PROVE_hadamard2);
757 hadamard_fold(Gprime, NULL, winv, w[round]);
758 hadamard_fold(Hprime, scale, w[round], winv);
759 PERF_TIMER_STOP_BP(PROVE_hadamard2);
760 }
761
762 // PAPER LINES 33-34
763 PERF_TIMER_START_BP(PROVE_prime);
764 aprime = vector_add(vector_scalar(slice(aprime, 0, nprime), w[round]), vector_scalar(slice(aprime, nprime, aprime.size()), winv));
765 bprime = vector_add(vector_scalar(slice(bprime, 0, nprime), winv), vector_scalar(slice(bprime, nprime, bprime.size()), w[round]));
766 PERF_TIMER_STOP_BP(PROVE_prime);
767
768 scale = NULL;
769 ++round;
770 }
771 PERF_TIMER_STOP_BP(PROVE_step4);
772
773 return Bulletproof(std::move(V), A, S, T1, T2, taux, mu, std::move(L), std::move(R), aprime[0], bprime[0], t);
774 }
775
776 Bulletproof bulletproof_PROVE(const std::vector<uint64_t> &v, const rct::keyV &gamma)
777 {
778 CHECK_AND_ASSERT_THROW_MES(v.size() == gamma.size(), "Incompatible sizes of v and gamma");
779
780 // vG + gammaH
781 PERF_TIMER_START_BP(PROVE_v);
782 rct::keyV sv(v.size());
783 for (size_t i = 0; i < v.size(); ++i)
784 {
785 sv[i] = rct::zero();
786 sv[i].bytes[0] = v[i] & 255;
787 sv[i].bytes[1] = (v[i] >> 8) & 255;
788 sv[i].bytes[2] = (v[i] >> 16) & 255;
789 sv[i].bytes[3] = (v[i] >> 24) & 255;
790 sv[i].bytes[4] = (v[i] >> 32) & 255;
791 sv[i].bytes[5] = (v[i] >> 40) & 255;
792 sv[i].bytes[6] = (v[i] >> 48) & 255;
793 sv[i].bytes[7] = (v[i] >> 56) & 255;
794 }
795 PERF_TIMER_STOP_BP(PROVE_v);
796 return bulletproof_PROVE(sv, gamma);
797 }
798
799 struct proof_data_t
800 {
801 rct::key x, y, z, x_ip;
802 std::vector<rct::key> w;
803 size_t logM, inv_offset;
804 };
805
806 /* Given a range proof, determine if it is valid
807 * This uses the method in PAPER LINES 95-105,
808 * weighted across multiple proofs in a batch
809 */
810 bool bulletproof_VERIFY(const std::vector<const Bulletproof*> &proofs)
811 {
812 init_exponents();
813
814 PERF_TIMER_START_BP(VERIFY);
815
816 const size_t logN = 6;
817 const size_t N = 1 << logN;
818
819 // sanity and figure out which proof is longest
820 size_t max_length = 0;
821 size_t nV = 0;
822 std::vector<proof_data_t> proof_data;
823 proof_data.reserve(proofs.size());
824 size_t inv_offset = 0;
825 std::vector<rct::key> to_invert;
826 to_invert.reserve(11 * proofs.size());
827 size_t max_logM = 0;
828 for (const Bulletproof *p: proofs)
829 {
830 const Bulletproof &proof = *p;
831
832 // check scalar range
833 CHECK_AND_ASSERT_MES(is_reduced(proof.taux), false, "Input scalar not in range");
834 CHECK_AND_ASSERT_MES(is_reduced(proof.mu), false, "Input scalar not in range");
835 CHECK_AND_ASSERT_MES(is_reduced(proof.a), false, "Input scalar not in range");
836 CHECK_AND_ASSERT_MES(is_reduced(proof.b), false, "Input scalar not in range");
837 CHECK_AND_ASSERT_MES(is_reduced(proof.t), false, "Input scalar not in range");
838
839 CHECK_AND_ASSERT_MES(proof.V.size() >= 1, false, "V does not have at least one element");
840 CHECK_AND_ASSERT_MES(proof.L.size() == proof.R.size(), false, "Mismatched L and R sizes");
841 CHECK_AND_ASSERT_MES(proof.L.size() > 0, false, "Empty proof");
842
843 max_length = std::max(max_length, proof.L.size());
844 nV += proof.V.size();
845
846 // Reconstruct the challenges
847 PERF_TIMER_START_BP(VERIFY_start);
848 proof_data.resize(proof_data.size() + 1);
849 proof_data_t &pd = proof_data.back();
850 rct::key hash_cache = rct::hash_to_scalar(proof.V);
851 pd.y = hash_cache_mash(hash_cache, proof.A, proof.S);
852 CHECK_AND_ASSERT_MES(!(pd.y == rct::zero()), false, "y == 0");
853 pd.z = hash_cache = rct::hash_to_scalar(pd.y);
854 CHECK_AND_ASSERT_MES(!(pd.z == rct::zero()), false, "z == 0");
855 pd.x = hash_cache_mash(hash_cache, pd.z, proof.T1, proof.T2);
856 CHECK_AND_ASSERT_MES(!(pd.x == rct::zero()), false, "x == 0");
857 pd.x_ip = hash_cache_mash(hash_cache, pd.x, proof.taux, proof.mu, proof.t);
858 CHECK_AND_ASSERT_MES(!(pd.x_ip == rct::zero()), false, "x_ip == 0");
859 PERF_TIMER_STOP_BP(VERIFY_start);
860
861 size_t M;
862 for (pd.logM = 0; (M = 1<<pd.logM) <= maxM && M < proof.V.size(); ++pd.logM);
863 CHECK_AND_ASSERT_MES(proof.L.size() == 6+pd.logM, false, "Proof is not the expected size");
864 max_logM = std::max(pd.logM, max_logM);
865
866 const size_t rounds = pd.logM+logN;
867 CHECK_AND_ASSERT_MES(rounds > 0, false, "Zero rounds");
868
869 PERF_TIMER_START_BP(VERIFY_line_21_22);
870 // The inner product challenges are computed per round
871 pd.w.resize(rounds);
872 for (size_t i = 0; i < rounds; ++i)
873 {
874 pd.w[i] = hash_cache_mash(hash_cache, proof.L[i], proof.R[i]);
875 CHECK_AND_ASSERT_MES(!(pd.w[i] == rct::zero()), false, "w[i] == 0");
876 }
877 PERF_TIMER_STOP_BP(VERIFY_line_21_22);
878
879 pd.inv_offset = inv_offset;
880 for (size_t i = 0; i < rounds; ++i)
881 to_invert.push_back(pd.w[i]);
882 to_invert.push_back(pd.y);
883 inv_offset += rounds + 1;
884 }
885 CHECK_AND_ASSERT_MES(max_length < 32, false, "At least one proof is too large");
886 size_t maxMN = 1u << max_length;
887
888 rct::key tmp;
889
890 std::vector<MultiexpData> multiexp_data;
891 multiexp_data.reserve(nV + (2 * (max_logM + logN) + 4) * proofs.size() + 2 * maxMN);
892 multiexp_data.resize(2 * maxMN);
893
894 PERF_TIMER_START_BP(VERIFY_line_24_25_invert);
895 const std::vector<rct::key> inverses = invert(std::move(to_invert));
896 to_invert.clear();
897 PERF_TIMER_STOP_BP(VERIFY_line_24_25_invert);
898
899 // setup weighted aggregates
900 rct::key z1 = rct::zero();
901 rct::key z3 = rct::zero();
902 rct::keyV m_z4(maxMN, rct::zero()), m_z5(maxMN, rct::zero());
903 rct::key m_y0 = rct::zero(), y1 = rct::zero();
904 int proof_data_index = 0;
905 rct::keyV w_cache;
906 std::vector<ge_p3> proof8_V, proof8_L, proof8_R;
907 for (const Bulletproof *p: proofs)
908 {
909 const Bulletproof &proof = *p;
910 const proof_data_t &pd = proof_data[proof_data_index++];
911
912 CHECK_AND_ASSERT_MES(proof.L.size() == 6+pd.logM, false, "Proof is not the expected size");
913 const size_t M = 1 << pd.logM;
914 const size_t MN = M*N;
915 const rct::key weight_y = rct::skGen();
916 const rct::key weight_z = rct::skGen();
917
918 // pre-multiply some points by 8
919 proof8_V.resize(proof.V.size()); for (size_t i = 0; i < proof.V.size(); ++i) rct::scalarmult8(proof8_V[i], proof.V[i]);
920 proof8_L.resize(proof.L.size()); for (size_t i = 0; i < proof.L.size(); ++i) rct::scalarmult8(proof8_L[i], proof.L[i]);
921 proof8_R.resize(proof.R.size()); for (size_t i = 0; i < proof.R.size(); ++i) rct::scalarmult8(proof8_R[i], proof.R[i]);
922 ge_p3 proof8_T1;
923 ge_p3 proof8_T2;
924 ge_p3 proof8_S;
925 ge_p3 proof8_A;
926 rct::scalarmult8(proof8_T1, proof.T1);
927 rct::scalarmult8(proof8_T2, proof.T2);
928 rct::scalarmult8(proof8_S, proof.S);
929 rct::scalarmult8(proof8_A, proof.A);
930
931 PERF_TIMER_START_BP(VERIFY_line_61);
932 sc_mulsub(m_y0.bytes, proof.taux.bytes, weight_y.bytes, m_y0.bytes);
933
934 const rct::keyV zpow = vector_powers(pd.z, M+3);
935
936 rct::key k;
937 const rct::key ip1y = vector_power_sum(pd.y, MN);
938 sc_mulsub(k.bytes, zpow[2].bytes, ip1y.bytes, rct::zero().bytes);
939 for (size_t j = 1; j <= M; ++j)
940 {
941 CHECK_AND_ASSERT_MES(j+2 < zpow.size(), false, "invalid zpow index");
942 sc_mulsub(k.bytes, zpow[j+2].bytes, ip12.bytes, k.bytes);
943 }
944 PERF_TIMER_STOP_BP(VERIFY_line_61);
945
946 PERF_TIMER_START_BP(VERIFY_line_61rl_new);
947 sc_muladd(tmp.bytes, pd.z.bytes, ip1y.bytes, k.bytes);
948 sc_sub(tmp.bytes, proof.t.bytes, tmp.bytes);
949 sc_muladd(y1.bytes, tmp.bytes, weight_y.bytes, y1.bytes);
950 for (size_t j = 0; j < proof8_V.size(); j++)
951 {
952 sc_mul(tmp.bytes, zpow[j+2].bytes, weight_y.bytes);
953 multiexp_data.emplace_back(tmp, proof8_V[j]);
954 }
955 sc_mul(tmp.bytes, pd.x.bytes, weight_y.bytes);
956 multiexp_data.emplace_back(tmp, proof8_T1);
957 rct::key xsq;
958 sc_mul(xsq.bytes, pd.x.bytes, pd.x.bytes);
959 sc_mul(tmp.bytes, xsq.bytes, weight_y.bytes);
960 multiexp_data.emplace_back(tmp, proof8_T2);
961 PERF_TIMER_STOP_BP(VERIFY_line_61rl_new);
962
963 PERF_TIMER_START_BP(VERIFY_line_62);
964 multiexp_data.emplace_back(weight_z, proof8_A);
965 sc_mul(tmp.bytes, pd.x.bytes, weight_z.bytes);
966 multiexp_data.emplace_back(tmp, proof8_S);
967 PERF_TIMER_STOP_BP(VERIFY_line_62);
968
969 // Compute the number of rounds for the inner product
970 const size_t rounds = pd.logM+logN;
971 CHECK_AND_ASSERT_MES(rounds > 0, false, "Zero rounds");
972
973 PERF_TIMER_START_BP(VERIFY_line_24_25);
974 // Compute the curvepoints from G[i] and H[i]
975 rct::key yinvpow = rct::identity();
976 rct::key ypow = rct::identity();
977
978 const rct::key *winv = &inverses[pd.inv_offset];
979 const rct::key yinv = inverses[pd.inv_offset + rounds];
980
981 // precalc
982 PERF_TIMER_START_BP(VERIFY_line_24_25_precalc);
983 w_cache.resize(1<<rounds);
984 w_cache[0] = winv[0];
985 w_cache[1] = pd.w[0];
986 for (size_t j = 1; j < rounds; ++j)
987 {
988 const size_t slots = 1<<(j+1);
989 for (size_t s = slots; s-- > 0; --s)
990 {
991 sc_mul(w_cache[s].bytes, w_cache[s/2].bytes, pd.w[j].bytes);
992 sc_mul(w_cache[s-1].bytes, w_cache[s/2].bytes, winv[j].bytes);
993 }
994 }
995 PERF_TIMER_STOP_BP(VERIFY_line_24_25_precalc);
996
997 for (size_t i = 0; i < MN; ++i)
998 {
999 rct::key g_scalar = proof.a;
1000 rct::key h_scalar;
1001 if (i == 0)
1002 h_scalar = proof.b;
1003 else
1004 sc_mul(h_scalar.bytes, proof.b.bytes, yinvpow.bytes);
1005
1006 // Convert the index to binary IN REVERSE and construct the scalar exponent
1007 sc_mul(g_scalar.bytes, g_scalar.bytes, w_cache[i].bytes);
1008 sc_mul(h_scalar.bytes, h_scalar.bytes, w_cache[(~i) & (MN-1)].bytes);
1009
1010 sc_add(g_scalar.bytes, g_scalar.bytes, pd.z.bytes);
1011 CHECK_AND_ASSERT_MES(2+i/N < zpow.size(), false, "invalid zpow index");
1012 CHECK_AND_ASSERT_MES(i%N < twoN.size(), false, "invalid twoN index");
1013 sc_mul(tmp.bytes, zpow[2+i/N].bytes, twoN[i%N].bytes);
1014 if (i == 0)
1015 {
1016 sc_add(tmp.bytes, tmp.bytes, pd.z.bytes);
1017 sc_sub(h_scalar.bytes, h_scalar.bytes, tmp.bytes);
1018 }
1019 else
1020 {
1021 sc_muladd(tmp.bytes, pd.z.bytes, ypow.bytes, tmp.bytes);
1022 sc_mulsub(h_scalar.bytes, tmp.bytes, yinvpow.bytes, h_scalar.bytes);
1023 }
1024
1025 sc_mulsub(m_z4[i].bytes, g_scalar.bytes, weight_z.bytes, m_z4[i].bytes);
1026 sc_mulsub(m_z5[i].bytes, h_scalar.bytes, weight_z.bytes, m_z5[i].bytes);
1027
1028 if (i == 0)
1029 {
1030 yinvpow = yinv;
1031 ypow = pd.y;
1032 }
1033 else if (i != MN-1)
1034 {
1035 sc_mul(yinvpow.bytes, yinvpow.bytes, yinv.bytes);
1036 sc_mul(ypow.bytes, ypow.bytes, pd.y.bytes);
1037 }
1038 }
1039
1040 PERF_TIMER_STOP_BP(VERIFY_line_24_25);
1041
1042 PERF_TIMER_START_BP(VERIFY_line_26_new);
1043 sc_muladd(z1.bytes, proof.mu.bytes, weight_z.bytes, z1.bytes);
1044 for (size_t i = 0; i < rounds; ++i)
1045 {
1046 sc_mul(tmp.bytes, pd.w[i].bytes, pd.w[i].bytes);
1047 sc_mul(tmp.bytes, tmp.bytes, weight_z.bytes);
1048 multiexp_data.emplace_back(tmp, proof8_L[i]);
1049 sc_mul(tmp.bytes, winv[i].bytes, winv[i].bytes);
1050 sc_mul(tmp.bytes, tmp.bytes, weight_z.bytes);
1051 multiexp_data.emplace_back(tmp, proof8_R[i]);
1052 }
1053 sc_mulsub(tmp.bytes, proof.a.bytes, proof.b.bytes, proof.t.bytes);
1054 sc_mul(tmp.bytes, tmp.bytes, pd.x_ip.bytes);
1055 sc_muladd(z3.bytes, tmp.bytes, weight_z.bytes, z3.bytes);
1056 PERF_TIMER_STOP_BP(VERIFY_line_26_new);
1057 }
1058
1059 // now check all proofs at once
1060 PERF_TIMER_START_BP(VERIFY_step2_check);
1061 sc_sub(tmp.bytes, m_y0.bytes, z1.bytes);
1062 multiexp_data.emplace_back(tmp, rct::G);
1063 sc_sub(tmp.bytes, z3.bytes, y1.bytes);
1064 multiexp_data.emplace_back(tmp, rct::H);
1065 for (size_t i = 0; i < maxMN; ++i)
1066 {
1067 multiexp_data[i * 2] = {m_z4[i], Gi_p3[i]};
1068 multiexp_data[i * 2 + 1] = {m_z5[i], Hi_p3[i]};
1069 }
1070 if (!(multiexp(multiexp_data, 2 * maxMN) == rct::identity()))
1071 {
1072 PERF_TIMER_STOP_BP(VERIFY_step2_check);
1073 MERROR("Verification failure");
1074 return false;
1075 }
1076 PERF_TIMER_STOP_BP(VERIFY_step2_check);
1077
1078 PERF_TIMER_STOP_BP(VERIFY);
1079 return true;
1080 }
1081
1082 bool bulletproof_VERIFY(const std::vector<Bulletproof> &proofs)
1083 {
1084 std::vector<const Bulletproof*> proof_pointers;
1085 proof_pointers.reserve(proofs.size());
1086 for (const Bulletproof &proof: proofs)
1087 proof_pointers.push_back(&proof);
1088 return bulletproof_VERIFY(proof_pointers);
1089 }
1090
1091 bool bulletproof_VERIFY(const Bulletproof &proof)
1092 {
1093 std::vector<const Bulletproof*> proofs;
1094 proofs.push_back(&proof);
1095 return bulletproof_VERIFY(proofs);
1096 }
1097
1098 }