| 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 | // Implements the Bulletproofs+ prover and verifier algorithms |
| 30 | // |
| 31 | // Preprint: https://eprint.iacr.org/2020/735, version 17 Jun 2020 |
| 32 | // |
| 33 | // NOTE ON NOTATION: |
| 34 | // In the signature constructions used in Monero, commitments to zero are treated as |
| 35 | // public keys against the curve group generator `G`. This means that amount |
| 36 | // commitments must use another generator `H` for values in order to show balance. |
| 37 | // The result is that the roles of `g` and `h` in the preprint are effectively swapped |
| 38 | // in this code, taking on the roles of `H` and `G`, respectively. Read carefully! |
| 39 | |
| 40 | #include <stdlib.h> |
| 41 | #include <boost/thread/mutex.hpp> |
| 42 | #include <boost/thread/lock_guard.hpp> |
| 43 | #include "misc_log_ex.h" |
| 44 | #include "span.h" |
| 45 | #include "cryptonote_config.h" |
| 46 | extern "C" |
| 47 | { |
| 48 | #include "crypto/crypto-ops.h" |
| 49 | } |
| 50 | #include "rctOps.h" |
| 51 | #include "multiexp.h" |
| 52 | #include "bulletproofs_plus.h" |
| 53 | |
| 54 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 55 | #define MONERO_DEFAULT_LOG_CATEGORY "bulletproof_plus" |
| 56 | |
| 57 | #define STRAUS_SIZE_LIMIT 232 |
| 58 | #define PIPPENGER_SIZE_LIMIT 0 |
| 59 | |
| 60 | namespace rct |
| 61 | { |
| 62 | // Vector functions |
| 63 | static rct::key vector_exponent(const rct::keyV &a, const rct::keyV &b); |
| 64 | static rct::keyV vector_of_scalar_powers(const rct::key &x, size_t n); |
| 65 | |
| 66 | // Proof bounds |
| 67 | static constexpr size_t maxN = 64; // maximum number of bits in range |
| 68 | static constexpr size_t maxM = BULLETPROOF_PLUS_MAX_OUTPUTS; // maximum number of outputs to aggregate into a single proof |
| 69 | |
| 70 | // Cached public generators |
| 71 | static ge_p3 Hi_p3[maxN*maxM], Gi_p3[maxN*maxM]; |
| 72 | static std::shared_ptr<straus_cached_data> straus_HiGi_cache; |
| 73 | static std::shared_ptr<pippenger_cached_data> pippenger_HiGi_cache; |
| 74 | |
| 75 | // Useful scalar constants |
| 76 | static const constexpr rct::key ZERO = { {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,0x00 } }; // 0 |
| 77 | static const constexpr rct::key ONE = { {0x01, 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 } }; // 1 |
| 78 | 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 } }; // 2 |
| 79 | 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 } }; // -1 |
| 80 | 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 } }; // -(8**(-1)) |
| 81 | static rct::key TWO_SIXTY_FOUR_MINUS_ONE; // 2**64 - 1 |
| 82 | |
| 83 | // Initial transcript hash |
| 84 | static rct::key initial_transcript; |
| 85 | |
| 86 | static boost::mutex init_mutex; |
| 87 | |
| 88 | // Use the generator caches to compute a multiscalar multiplication |
| 89 | static inline rct::key multiexp(const std::vector<MultiexpData> &data, size_t HiGi_size) |
| 90 | { |
| 91 | if (HiGi_size > 0) |
| 92 | { |
| 93 | static_assert(232 <= STRAUS_SIZE_LIMIT, "Straus in precalc mode can only be calculated till STRAUS_SIZE_LIMIT"); |
| 94 | 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())); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | return data.size() <= 95 ? straus(data, NULL, 0) : pippenger(data, NULL, 0, get_pippenger_c(data.size())); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // Confirm that a scalar is properly reduced |
| 103 | static inline bool is_reduced(const rct::key &scalar) |
| 104 | { |
| 105 | return sc_check(scalar.bytes) == 0; |
| 106 | } |
| 107 | |
| 108 | // Use hashed values to produce indexed public generators |
| 109 | static ge_p3 get_exponent(const rct::key &base, size_t idx) |
| 110 | { |
| 111 | std::string hashed = std::string((const char*)base.bytes, sizeof(base)) + config::HASH_KEY_BULLETPROOF_PLUS_EXPONENT + tools::get_varint_data(idx); |
| 112 | rct::key generator; |
| 113 | ge_p3 generator_p3; |
| 114 | rct::hash_to_p3(generator_p3, rct::hash2rct(crypto::cn_fast_hash(hashed.data(), hashed.size()))); |
| 115 | ge_p3_tobytes(generator.bytes, &generator_p3); |
| 116 | CHECK_AND_ASSERT_THROW_MES(!(generator == rct::identity()), "Exponent is point at infinity"); |
| 117 | return generator_p3; |
| 118 | } |
| 119 | |
| 120 | // Construct public generators |
| 121 | static void init_exponents() |
| 122 | { |
| 123 | boost::lock_guard<boost::mutex> lock(init_mutex); |
| 124 | |
| 125 | // Only needs to be done once |
| 126 | static bool init_done = false; |
| 127 | if (init_done) |
| 128 | return; |
| 129 | |
| 130 | std::vector<MultiexpData> data; |
| 131 | data.reserve(maxN*maxM*2); |
| 132 | for (size_t i = 0; i < maxN*maxM; ++i) |
| 133 | { |
| 134 | Hi_p3[i] = get_exponent(rct::H, i * 2); |
| 135 | Gi_p3[i] = get_exponent(rct::H, i * 2 + 1); |
| 136 | |
| 137 | data.push_back({rct::zero(), Gi_p3[i]}); |
| 138 | data.push_back({rct::zero(), Hi_p3[i]}); |
| 139 | } |
| 140 | |
| 141 | straus_HiGi_cache = straus_init_cache(data, STRAUS_SIZE_LIMIT); |
| 142 | pippenger_HiGi_cache = pippenger_init_cache(data, 0, PIPPENGER_SIZE_LIMIT); |
| 143 | |
| 144 | // Compute 2**64 - 1 for later use in simplifying verification |
| 145 | TWO_SIXTY_FOUR_MINUS_ONE = TWO; |
| 146 | for (size_t i = 0; i < 6; i++) |
| 147 | { |
| 148 | sc_mul(TWO_SIXTY_FOUR_MINUS_ONE.bytes, TWO_SIXTY_FOUR_MINUS_ONE.bytes, TWO_SIXTY_FOUR_MINUS_ONE.bytes); |
| 149 | } |
| 150 | sc_sub(TWO_SIXTY_FOUR_MINUS_ONE.bytes, TWO_SIXTY_FOUR_MINUS_ONE.bytes, ONE.bytes); |
| 151 | |
| 152 | // Generate the initial Fiat-Shamir transcript hash, which is constant across all proofs |
| 153 | const std::string domain_separator(config::HASH_KEY_BULLETPROOF_PLUS_TRANSCRIPT); |
| 154 | ge_p3 initial_transcript_p3; |
| 155 | rct::hash_to_p3(initial_transcript_p3, rct::hash2rct(crypto::cn_fast_hash(domain_separator.data(), domain_separator.size()))); |
| 156 | ge_p3_tobytes(initial_transcript.bytes, &initial_transcript_p3); |
| 157 | |
| 158 | init_done = true; |
| 159 | } |
| 160 | |
| 161 | // Given two scalar arrays, construct a vector pre-commitment: |
| 162 | // |
| 163 | // a = (a_0, ..., a_{n-1}) |
| 164 | // b = (b_0, ..., b_{n-1}) |
| 165 | // |
| 166 | // Outputs a_0*Gi_0 + ... + a_{n-1}*Gi_{n-1} + |
| 167 | // b_0*Hi_0 + ... + b_{n-1}*Hi_{n-1} |
| 168 | static rct::key vector_exponent(const rct::keyV &a, const rct::keyV &b) |
| 169 | { |
| 170 | CHECK_AND_ASSERT_THROW_MES(a.size() == b.size(), "Incompatible sizes of a and b"); |
| 171 | CHECK_AND_ASSERT_THROW_MES(a.size() <= maxN*maxM, "Incompatible sizes of a and maxN"); |
| 172 | |
| 173 | std::vector<MultiexpData> multiexp_data; |
| 174 | multiexp_data.reserve(a.size()*2); |
| 175 | for (size_t i = 0; i < a.size(); ++i) |
| 176 | { |
| 177 | multiexp_data.emplace_back(a[i], Gi_p3[i]); |
| 178 | multiexp_data.emplace_back(b[i], Hi_p3[i]); |
| 179 | } |
| 180 | return multiexp(multiexp_data, 2 * a.size()); |
| 181 | } |
| 182 | |
| 183 | // Helper function used to compute the L and R terms used in the inner-product round function |
| 184 | static rct::key compute_LR(size_t size, const rct::key &y, const std::vector<ge_p3> &G, size_t G0, const std::vector<ge_p3> &H, size_t H0, const rct::keyV &a, size_t a0, const rct::keyV &b, size_t b0, const rct::key &c, const rct::key &d) |
| 185 | { |
| 186 | CHECK_AND_ASSERT_THROW_MES(size + G0 <= G.size(), "Incompatible size for G"); |
| 187 | CHECK_AND_ASSERT_THROW_MES(size + H0 <= H.size(), "Incompatible size for H"); |
| 188 | CHECK_AND_ASSERT_THROW_MES(size + a0 <= a.size(), "Incompatible size for a"); |
| 189 | CHECK_AND_ASSERT_THROW_MES(size + b0 <= b.size(), "Incompatible size for b"); |
| 190 | CHECK_AND_ASSERT_THROW_MES(size <= maxN*maxM, "size is too large"); |
| 191 | |
| 192 | std::vector<MultiexpData> multiexp_data; |
| 193 | multiexp_data.resize(size*2 + 2); |
| 194 | rct::key temp; |
| 195 | for (size_t i = 0; i < size; ++i) |
| 196 | { |
| 197 | sc_mul(temp.bytes, a[a0+i].bytes, y.bytes); |
| 198 | sc_mul(multiexp_data[i*2].scalar.bytes, temp.bytes, INV_EIGHT.bytes); |
| 199 | multiexp_data[i*2].point = G[G0+i]; |
| 200 | |
| 201 | sc_mul(multiexp_data[i*2+1].scalar.bytes, b[b0+i].bytes, INV_EIGHT.bytes); |
| 202 | multiexp_data[i*2+1].point = H[H0+i]; |
| 203 | } |
| 204 | |
| 205 | sc_mul(multiexp_data[2*size].scalar.bytes, c.bytes, INV_EIGHT.bytes); |
| 206 | ge_p3 H_p3; |
| 207 | ge_frombytes_vartime(&H_p3, rct::H.bytes); |
| 208 | multiexp_data[2*size].point = H_p3; |
| 209 | |
| 210 | sc_mul(multiexp_data[2*size+1].scalar.bytes, d.bytes, INV_EIGHT.bytes); |
| 211 | ge_p3 G_p3; |
| 212 | ge_frombytes_vartime(&G_p3, rct::G.bytes); |
| 213 | multiexp_data[2*size+1].point = G_p3; |
| 214 | |
| 215 | return multiexp(multiexp_data, 0); |
| 216 | } |
| 217 | |
| 218 | // Given a scalar, construct a vector of its powers: |
| 219 | // |
| 220 | // Output (1,x,x**2,...,x**{n-1}) |
| 221 | static rct::keyV vector_of_scalar_powers(const rct::key &x, size_t n) |
| 222 | { |
| 223 | CHECK_AND_ASSERT_THROW_MES(n != 0, "Need n > 0"); |
| 224 | |
| 225 | rct::keyV res(n); |
| 226 | res[0] = rct::identity(); |
| 227 | if (n == 1) |
| 228 | return res; |
| 229 | res[1] = x; |
| 230 | for (size_t i = 2; i < n; ++i) |
| 231 | { |
| 232 | sc_mul(res[i].bytes, res[i-1].bytes, x.bytes); |
| 233 | } |
| 234 | return res; |
| 235 | } |
| 236 | |
| 237 | // Given a scalar, construct the sum of its powers from 2 to n (where n is a power of 2): |
| 238 | // |
| 239 | // Output x**2 + x**4 + x**6 + ... + x**n |
| 240 | static rct::key sum_of_even_powers(const rct::key &x, size_t n) |
| 241 | { |
| 242 | CHECK_AND_ASSERT_THROW_MES((n & (n - 1)) == 0, "Need n to be a power of 2"); |
| 243 | CHECK_AND_ASSERT_THROW_MES(n != 0, "Need n > 0"); |
| 244 | |
| 245 | rct::key x1 = copy(x); |
| 246 | sc_mul(x1.bytes, x1.bytes, x1.bytes); |
| 247 | |
| 248 | rct::key res = copy(x1); |
| 249 | while (n > 2) |
| 250 | { |
| 251 | sc_muladd(res.bytes, x1.bytes, res.bytes, res.bytes); |
| 252 | sc_mul(x1.bytes, x1.bytes, x1.bytes); |
| 253 | n /= 2; |
| 254 | } |
| 255 | |
| 256 | return res; |
| 257 | } |
| 258 | |
| 259 | // Given a scalar, return the sum of its powers from 1 to n |
| 260 | // |
| 261 | // Output x**1 + x**2 + x**3 + ... + x**n |
| 262 | static rct::key sum_of_scalar_powers(const rct::key &x, size_t n) |
| 263 | { |
| 264 | CHECK_AND_ASSERT_THROW_MES(n != 0, "Need n > 0"); |
| 265 | |
| 266 | rct::key res = ONE; |
| 267 | if (n == 1) |
| 268 | return x; |
| 269 | |
| 270 | n += 1; |
| 271 | rct::key x1 = copy(x); |
| 272 | |
| 273 | const bool is_power_of_2 = (n & (n - 1)) == 0; |
| 274 | if (is_power_of_2) |
| 275 | { |
| 276 | sc_add(res.bytes, res.bytes, x1.bytes); |
| 277 | while (n > 2) |
| 278 | { |
| 279 | sc_mul(x1.bytes, x1.bytes, x1.bytes); |
| 280 | sc_muladd(res.bytes, x1.bytes, res.bytes, res.bytes); |
| 281 | n /= 2; |
| 282 | } |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | rct::key prev = x1; |
| 287 | for (size_t i = 1; i < n; ++i) |
| 288 | { |
| 289 | if (i > 1) |
| 290 | sc_mul(prev.bytes, prev.bytes, x1.bytes); |
| 291 | sc_add(res.bytes, res.bytes, prev.bytes); |
| 292 | } |
| 293 | } |
| 294 | sc_sub(res.bytes, res.bytes, ONE.bytes); |
| 295 | |
| 296 | return res; |
| 297 | } |
| 298 | |
| 299 | // Given two scalar arrays, construct the weighted inner product against another scalar |
| 300 | // |
| 301 | // Output a_0*b_0*y**1 + a_1*b_1*y**2 + ... + a_{n-1}*b_{n-1}*y**n |
| 302 | static rct::key weighted_inner_product(const epee::span<const rct::key> &a, const epee::span<const rct::key> &b, const rct::key &y) |
| 303 | { |
| 304 | CHECK_AND_ASSERT_THROW_MES(a.size() == b.size(), "Incompatible sizes of a and b"); |
| 305 | rct::key res = rct::zero(); |
| 306 | rct::key y_power = ONE; |
| 307 | rct::key temp; |
| 308 | for (size_t i = 0; i < a.size(); ++i) |
| 309 | { |
| 310 | sc_mul(temp.bytes, a[i].bytes, b[i].bytes); |
| 311 | sc_mul(y_power.bytes, y_power.bytes, y.bytes); |
| 312 | sc_muladd(res.bytes, temp.bytes, y_power.bytes, res.bytes); |
| 313 | } |
| 314 | return res; |
| 315 | } |
| 316 | |
| 317 | static rct::key weighted_inner_product(const rct::keyV &a, const epee::span<const rct::key> &b, const rct::key &y) |
| 318 | { |
| 319 | CHECK_AND_ASSERT_THROW_MES(a.size() == b.size(), "Incompatible sizes of a and b"); |
| 320 | return weighted_inner_product(epee::to_span(a), b, y); |
| 321 | } |
| 322 | |
| 323 | // Fold inner-product point vectors |
| 324 | static void hadamard_fold(std::vector<ge_p3> &v, const rct::key &a, const rct::key &b) |
| 325 | { |
| 326 | CHECK_AND_ASSERT_THROW_MES((v.size() & 1) == 0, "Vector size should be even"); |
| 327 | const size_t sz = v.size() / 2; |
| 328 | for (size_t n = 0; n < sz; ++n) |
| 329 | { |
| 330 | ge_dsmp c[2]; |
| 331 | ge_dsm_precomp(c[0], &v[n]); |
| 332 | ge_dsm_precomp(c[1], &v[sz + n]); |
| 333 | ge_double_scalarmult_precomp_vartime2_p3(&v[n], a.bytes, c[0], b.bytes, c[1]); |
| 334 | } |
| 335 | v.resize(sz); |
| 336 | } |
| 337 | |
| 338 | // Add vectors componentwise |
| 339 | static rct::keyV vector_add(const rct::keyV &a, const rct::keyV &b) |
| 340 | { |
| 341 | CHECK_AND_ASSERT_THROW_MES(a.size() == b.size(), "Incompatible sizes of a and b"); |
| 342 | rct::keyV res(a.size()); |
| 343 | for (size_t i = 0; i < a.size(); ++i) |
| 344 | { |
| 345 | sc_add(res[i].bytes, a[i].bytes, b[i].bytes); |
| 346 | } |
| 347 | return res; |
| 348 | } |
| 349 | |
| 350 | // Add a scalar to all elements of a vector |
| 351 | static rct::keyV vector_add(const rct::keyV &a, const rct::key &b) |
| 352 | { |
| 353 | rct::keyV res(a.size()); |
| 354 | for (size_t i = 0; i < a.size(); ++i) |
| 355 | { |
| 356 | sc_add(res[i].bytes, a[i].bytes, b.bytes); |
| 357 | } |
| 358 | return res; |
| 359 | } |
| 360 | |
| 361 | // Subtract a scalar from all elements of a vector |
| 362 | static rct::keyV vector_subtract(const rct::keyV &a, const rct::key &b) |
| 363 | { |
| 364 | rct::keyV res(a.size()); |
| 365 | for (size_t i = 0; i < a.size(); ++i) |
| 366 | { |
| 367 | sc_sub(res[i].bytes, a[i].bytes, b.bytes); |
| 368 | } |
| 369 | return res; |
| 370 | } |
| 371 | |
| 372 | // Multiply a scalar by all elements of a vector |
| 373 | static rct::keyV vector_scalar(const epee::span<const rct::key> &a, const rct::key &x) |
| 374 | { |
| 375 | rct::keyV res(a.size()); |
| 376 | for (size_t i = 0; i < a.size(); ++i) |
| 377 | { |
| 378 | sc_mul(res[i].bytes, a[i].bytes, x.bytes); |
| 379 | } |
| 380 | return res; |
| 381 | } |
| 382 | |
| 383 | // Inversion helper function |
| 384 | static rct::key sm(rct::key y, int n, const rct::key &x) |
| 385 | { |
| 386 | while (n--) |
| 387 | sc_mul(y.bytes, y.bytes, y.bytes); |
| 388 | sc_mul(y.bytes, y.bytes, x.bytes); |
| 389 | return y; |
| 390 | } |
| 391 | |
| 392 | // Compute the inverse of a nonzero |
| 393 | static rct::key invert(const rct::key &x) |
| 394 | { |
| 395 | CHECK_AND_ASSERT_THROW_MES(!(x == ZERO), "Cannot invert zero!"); |
| 396 | rct::key _1, _10, _100, _11, _101, _111, _1001, _1011, _1111; |
| 397 | |
| 398 | _1 = x; |
| 399 | sc_mul(_10.bytes, _1.bytes, _1.bytes); |
| 400 | sc_mul(_100.bytes, _10.bytes, _10.bytes); |
| 401 | sc_mul(_11.bytes, _10.bytes, _1.bytes); |
| 402 | sc_mul(_101.bytes, _10.bytes, _11.bytes); |
| 403 | sc_mul(_111.bytes, _10.bytes, _101.bytes); |
| 404 | sc_mul(_1001.bytes, _10.bytes, _111.bytes); |
| 405 | sc_mul(_1011.bytes, _10.bytes, _1001.bytes); |
| 406 | sc_mul(_1111.bytes, _100.bytes, _1011.bytes); |
| 407 | |
| 408 | rct::key inv; |
| 409 | sc_mul(inv.bytes, _1111.bytes, _1.bytes); |
| 410 | |
| 411 | inv = sm(inv, 123 + 3, _101); |
| 412 | inv = sm(inv, 2 + 2, _11); |
| 413 | inv = sm(inv, 1 + 4, _1111); |
| 414 | inv = sm(inv, 1 + 4, _1111); |
| 415 | inv = sm(inv, 4, _1001); |
| 416 | inv = sm(inv, 2, _11); |
| 417 | inv = sm(inv, 1 + 4, _1111); |
| 418 | inv = sm(inv, 1 + 3, _101); |
| 419 | inv = sm(inv, 3 + 3, _101); |
| 420 | inv = sm(inv, 3, _111); |
| 421 | inv = sm(inv, 1 + 4, _1111); |
| 422 | inv = sm(inv, 2 + 3, _111); |
| 423 | inv = sm(inv, 2 + 2, _11); |
| 424 | inv = sm(inv, 1 + 4, _1011); |
| 425 | inv = sm(inv, 2 + 4, _1011); |
| 426 | inv = sm(inv, 6 + 4, _1001); |
| 427 | inv = sm(inv, 2 + 2, _11); |
| 428 | inv = sm(inv, 3 + 2, _11); |
| 429 | inv = sm(inv, 3 + 2, _11); |
| 430 | inv = sm(inv, 1 + 4, _1001); |
| 431 | inv = sm(inv, 1 + 3, _111); |
| 432 | inv = sm(inv, 2 + 4, _1111); |
| 433 | inv = sm(inv, 1 + 4, _1011); |
| 434 | inv = sm(inv, 3, _101); |
| 435 | inv = sm(inv, 2 + 4, _1111); |
| 436 | inv = sm(inv, 3, _101); |
| 437 | inv = sm(inv, 1 + 2, _11); |
| 438 | |
| 439 | return inv; |
| 440 | } |
| 441 | |
| 442 | // Invert a batch of scalars, all of which _must_ be nonzero |
| 443 | static rct::keyV invert(rct::keyV x) |
| 444 | { |
| 445 | rct::keyV scratch; |
| 446 | scratch.reserve(x.size()); |
| 447 | |
| 448 | rct::key acc = rct::identity(); |
| 449 | for (size_t n = 0; n < x.size(); ++n) |
| 450 | { |
| 451 | CHECK_AND_ASSERT_THROW_MES(!(x[n] == ZERO), "Cannot invert zero!"); |
| 452 | scratch.push_back(acc); |
| 453 | if (n == 0) |
| 454 | acc = x[0]; |
| 455 | else |
| 456 | sc_mul(acc.bytes, acc.bytes, x[n].bytes); |
| 457 | } |
| 458 | |
| 459 | acc = invert(acc); |
| 460 | |
| 461 | rct::key tmp; |
| 462 | for (int i = x.size(); i-- > 0; ) |
| 463 | { |
| 464 | sc_mul(tmp.bytes, acc.bytes, x[i].bytes); |
| 465 | sc_mul(x[i].bytes, acc.bytes, scratch[i].bytes); |
| 466 | acc = tmp; |
| 467 | } |
| 468 | |
| 469 | return x; |
| 470 | } |
| 471 | |
| 472 | // Compute the slice of a vector |
| 473 | static epee::span<const rct::key> slice(const rct::keyV &a, size_t start, size_t stop) |
| 474 | { |
| 475 | CHECK_AND_ASSERT_THROW_MES(start < a.size(), "Invalid start index"); |
| 476 | CHECK_AND_ASSERT_THROW_MES(stop <= a.size(), "Invalid stop index"); |
| 477 | CHECK_AND_ASSERT_THROW_MES(start < stop, "Invalid start/stop indices"); |
| 478 | return epee::span<const rct::key>(&a[start], stop - start); |
| 479 | } |
| 480 | |
| 481 | // Update the transcript |
| 482 | static rct::key transcript_update(rct::key &transcript, const rct::key &update_0) |
| 483 | { |
| 484 | rct::key data[2]; |
| 485 | data[0] = transcript; |
| 486 | data[1] = update_0; |
| 487 | rct::hash_to_scalar(transcript, data, sizeof(data)); |
| 488 | return transcript; |
| 489 | } |
| 490 | |
| 491 | static rct::key transcript_update(rct::key &transcript, const rct::key &update_0, const rct::key &update_1) |
| 492 | { |
| 493 | rct::key data[3]; |
| 494 | data[0] = transcript; |
| 495 | data[1] = update_0; |
| 496 | data[2] = update_1; |
| 497 | rct::hash_to_scalar(transcript, data, sizeof(data)); |
| 498 | return transcript; |
| 499 | } |
| 500 | |
| 501 | // Given a value v [0..2**N) and a mask gamma, construct a range proof |
| 502 | BulletproofPlus bulletproof_plus_PROVE(const rct::key &sv, const rct::key &gamma) |
| 503 | { |
| 504 | return bulletproof_plus_PROVE(rct::keyV(1, sv), rct::keyV(1, gamma)); |
| 505 | } |
| 506 | |
| 507 | BulletproofPlus bulletproof_plus_PROVE(uint64_t v, const rct::key &gamma) |
| 508 | { |
| 509 | return bulletproof_plus_PROVE(std::vector<uint64_t>(1, v), rct::keyV(1, gamma)); |
| 510 | } |
| 511 | |
| 512 | // Given a set of values v [0..2**N) and masks gamma, construct a range proof |
| 513 | BulletproofPlus bulletproof_plus_PROVE(const rct::keyV &sv, const rct::keyV &gamma) |
| 514 | { |
| 515 | // Sanity check on inputs |
| 516 | CHECK_AND_ASSERT_THROW_MES(sv.size() == gamma.size(), "Incompatible sizes of sv and gamma"); |
| 517 | CHECK_AND_ASSERT_THROW_MES(!sv.empty(), "sv is empty"); |
| 518 | for (const rct::key &sve: sv) |
| 519 | CHECK_AND_ASSERT_THROW_MES(is_reduced(sve), "Invalid sv input"); |
| 520 | for (const rct::key &g: gamma) |
| 521 | CHECK_AND_ASSERT_THROW_MES(is_reduced(g), "Invalid gamma input"); |
| 522 | |
| 523 | init_exponents(); |
| 524 | |
| 525 | // Useful proof bounds |
| 526 | // |
| 527 | // N: number of bits in each range (here, 64) |
| 528 | // logN: base-2 logarithm |
| 529 | // M: first power of 2 greater than or equal to the number of range proofs to aggregate |
| 530 | // logM: base-2 logarithm |
| 531 | constexpr size_t logN = 6; // log2(64) |
| 532 | constexpr size_t N = 1<<logN; |
| 533 | size_t M, logM; |
| 534 | for (logM = 0; (M = 1<<logM) <= maxM && M < sv.size(); ++logM); |
| 535 | CHECK_AND_ASSERT_THROW_MES(M <= maxM, "sv/gamma are too large"); |
| 536 | const size_t logMN = logM + logN; |
| 537 | const size_t MN = M * N; |
| 538 | |
| 539 | rct::keyV V(sv.size()); |
| 540 | rct::keyV aL(MN), aR(MN); |
| 541 | rct::keyV aL8(MN), aR8(MN); |
| 542 | rct::key temp; |
| 543 | rct::key temp2; |
| 544 | |
| 545 | // Prepare output commitments and offset by a factor of 8**(-1) |
| 546 | // |
| 547 | // This offset is applied to other group elements as well; |
| 548 | // it allows us to apply a multiply-by-8 operation in the verifier efficiently |
| 549 | // to ensure that the resulting group elements are in the prime-order point subgroup |
| 550 | // and avoid much more constly multiply-by-group-order operations. |
| 551 | for (size_t i = 0; i < sv.size(); ++i) |
| 552 | { |
| 553 | rct::key gamma8, sv8; |
| 554 | sc_mul(gamma8.bytes, gamma[i].bytes, INV_EIGHT.bytes); |
| 555 | sc_mul(sv8.bytes, sv[i].bytes, INV_EIGHT.bytes); |
| 556 | rct::addKeys2(V[i], gamma8, sv8, rct::H); |
| 557 | } |
| 558 | |
| 559 | // Decompose values |
| 560 | // |
| 561 | // Note that this effectively pads the set to a power of 2, which is required for the inner-product argument later. |
| 562 | for (size_t j = 0; j < M; ++j) |
| 563 | { |
| 564 | for (size_t i = N; i-- > 0; ) |
| 565 | { |
| 566 | if (j < sv.size() && (sv[j][i/8] & (((uint64_t)1)<<(i%8)))) |
| 567 | { |
| 568 | aL[j*N+i] = rct::identity(); |
| 569 | aL8[j*N+i] = INV_EIGHT; |
| 570 | aR[j*N+i] = aR8[j*N+i] = rct::zero(); |
| 571 | } |
| 572 | else |
| 573 | { |
| 574 | aL[j*N+i] = aL8[j*N+i] = rct::zero(); |
| 575 | aR[j*N+i] = MINUS_ONE; |
| 576 | aR8[j*N+i] = MINUS_INV_EIGHT; |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | try_again: |
| 582 | // This is a Fiat-Shamir transcript |
| 583 | rct::key transcript = copy(initial_transcript); |
| 584 | transcript = transcript_update(transcript, rct::hash_to_scalar(V)); |
| 585 | |
| 586 | // A |
| 587 | rct::key alpha = rct::skGen(); |
| 588 | rct::key pre_A = vector_exponent(aL8, aR8); |
| 589 | rct::key A; |
| 590 | sc_mul(temp.bytes, alpha.bytes, INV_EIGHT.bytes); |
| 591 | rct::addKeys(A, pre_A, rct::scalarmultBase(temp)); |
| 592 | |
| 593 | // Challenges |
| 594 | rct::key y = transcript_update(transcript, A); |
| 595 | if (y == rct::zero()) |
| 596 | { |
| 597 | MINFO("y is 0, trying again"); |
| 598 | goto try_again; |
| 599 | } |
| 600 | rct::key z = transcript = rct::hash_to_scalar(y); |
| 601 | if (z == rct::zero()) |
| 602 | { |
| 603 | MINFO("z is 0, trying again"); |
| 604 | goto try_again; |
| 605 | } |
| 606 | rct::key z_squared; |
| 607 | sc_mul(z_squared.bytes, z.bytes, z.bytes); |
| 608 | |
| 609 | // Windowed vector |
| 610 | // d[j*N+i] = z**(2*(j+1)) * 2**i |
| 611 | // |
| 612 | // We compute this iteratively in order to reduce scalar operations. |
| 613 | rct::keyV d(MN, rct::zero()); |
| 614 | d[0] = z_squared; |
| 615 | for (size_t i = 1; i < N; i++) |
| 616 | { |
| 617 | sc_mul(d[i].bytes, d[i-1].bytes, TWO.bytes); |
| 618 | } |
| 619 | |
| 620 | for (size_t j = 1; j < M; j++) |
| 621 | { |
| 622 | for (size_t i = 0; i < N; i++) |
| 623 | { |
| 624 | sc_mul(d[j*N+i].bytes, d[(j-1)*N+i].bytes, z_squared.bytes); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | rct::keyV y_powers = vector_of_scalar_powers(y, MN+2); |
| 629 | |
| 630 | // Prepare inner product terms |
| 631 | rct::keyV aL1 = vector_subtract(aL, z); |
| 632 | |
| 633 | rct::keyV aR1 = vector_add(aR, z); |
| 634 | rct::keyV d_y(MN); |
| 635 | for (size_t i = 0; i < MN; i++) |
| 636 | { |
| 637 | sc_mul(d_y[i].bytes, d[i].bytes, y_powers[MN-i].bytes); |
| 638 | } |
| 639 | aR1 = vector_add(aR1, d_y); |
| 640 | |
| 641 | rct::key alpha1 = alpha; |
| 642 | temp = ONE; |
| 643 | for (size_t j = 0; j < sv.size(); j++) |
| 644 | { |
| 645 | sc_mul(temp.bytes, temp.bytes, z_squared.bytes); |
| 646 | sc_mul(temp2.bytes, y_powers[MN+1].bytes, temp.bytes); |
| 647 | sc_muladd(alpha1.bytes, temp2.bytes, gamma[j].bytes, alpha1.bytes); |
| 648 | } |
| 649 | |
| 650 | // These are used in the inner product rounds |
| 651 | size_t nprime = MN; |
| 652 | std::vector<ge_p3> Gprime(MN); |
| 653 | std::vector<ge_p3> Hprime(MN); |
| 654 | rct::keyV aprime(MN); |
| 655 | rct::keyV bprime(MN); |
| 656 | |
| 657 | const rct::key yinv = invert(y); |
| 658 | rct::keyV yinvpow(MN); |
| 659 | yinvpow[0] = ONE; |
| 660 | for (size_t i = 0; i < MN; ++i) |
| 661 | { |
| 662 | Gprime[i] = Gi_p3[i]; |
| 663 | Hprime[i] = Hi_p3[i]; |
| 664 | if (i > 0) |
| 665 | { |
| 666 | sc_mul(yinvpow[i].bytes, yinvpow[i-1].bytes, yinv.bytes); |
| 667 | } |
| 668 | aprime[i] = aL1[i]; |
| 669 | bprime[i] = aR1[i]; |
| 670 | } |
| 671 | rct::keyV L(logMN); |
| 672 | rct::keyV R(logMN); |
| 673 | int round = 0; |
| 674 | |
| 675 | // Inner-product rounds |
| 676 | while (nprime > 1) |
| 677 | { |
| 678 | nprime /= 2; |
| 679 | |
| 680 | rct::key cL = weighted_inner_product(slice(aprime, 0, nprime), slice(bprime, nprime, bprime.size()), y); |
| 681 | rct::key cR = weighted_inner_product(vector_scalar(slice(aprime, nprime, aprime.size()), y_powers[nprime]), slice(bprime, 0, nprime), y); |
| 682 | |
| 683 | rct::key dL = rct::skGen(); |
| 684 | rct::key dR = rct::skGen(); |
| 685 | |
| 686 | L[round] = compute_LR(nprime, yinvpow[nprime], Gprime, nprime, Hprime, 0, aprime, 0, bprime, nprime, cL, dL); |
| 687 | R[round] = compute_LR(nprime, y_powers[nprime], Gprime, 0, Hprime, nprime, aprime, nprime, bprime, 0, cR, dR); |
| 688 | |
| 689 | const rct::key challenge = transcript_update(transcript, L[round], R[round]); |
| 690 | if (challenge == rct::zero()) |
| 691 | { |
| 692 | MINFO("challenge is 0, trying again"); |
| 693 | goto try_again; |
| 694 | } |
| 695 | |
| 696 | const rct::key challenge_inv = invert(challenge); |
| 697 | |
| 698 | sc_mul(temp.bytes, yinvpow[nprime].bytes, challenge.bytes); |
| 699 | hadamard_fold(Gprime, challenge_inv, temp); |
| 700 | hadamard_fold(Hprime, challenge, challenge_inv); |
| 701 | |
| 702 | sc_mul(temp.bytes, challenge_inv.bytes, y_powers[nprime].bytes); |
| 703 | aprime = vector_add(vector_scalar(slice(aprime, 0, nprime), challenge), vector_scalar(slice(aprime, nprime, aprime.size()), temp)); |
| 704 | bprime = vector_add(vector_scalar(slice(bprime, 0, nprime), challenge_inv), vector_scalar(slice(bprime, nprime, bprime.size()), challenge)); |
| 705 | |
| 706 | rct::key challenge_squared; |
| 707 | sc_mul(challenge_squared.bytes, challenge.bytes, challenge.bytes); |
| 708 | rct::key challenge_squared_inv; |
| 709 | sc_mul(challenge_squared_inv.bytes, challenge_inv.bytes, challenge_inv.bytes); |
| 710 | sc_muladd(alpha1.bytes, dL.bytes, challenge_squared.bytes, alpha1.bytes); |
| 711 | sc_muladd(alpha1.bytes, dR.bytes, challenge_squared_inv.bytes, alpha1.bytes); |
| 712 | |
| 713 | ++round; |
| 714 | } |
| 715 | |
| 716 | // Final round computations |
| 717 | rct::key r = rct::skGen(); |
| 718 | rct::key s = rct::skGen(); |
| 719 | rct::key d_ = rct::skGen(); |
| 720 | rct::key eta = rct::skGen(); |
| 721 | |
| 722 | std::vector<MultiexpData> A1_data; |
| 723 | A1_data.reserve(4); |
| 724 | A1_data.resize(4); |
| 725 | |
| 726 | sc_mul(A1_data[0].scalar.bytes, r.bytes, INV_EIGHT.bytes); |
| 727 | A1_data[0].point = Gprime[0]; |
| 728 | |
| 729 | sc_mul(A1_data[1].scalar.bytes, s.bytes, INV_EIGHT.bytes); |
| 730 | A1_data[1].point = Hprime[0]; |
| 731 | |
| 732 | sc_mul(A1_data[2].scalar.bytes, d_.bytes, INV_EIGHT.bytes); |
| 733 | ge_p3 G_p3; |
| 734 | ge_frombytes_vartime(&G_p3, rct::G.bytes); |
| 735 | A1_data[2].point = G_p3; |
| 736 | |
| 737 | sc_mul(temp.bytes, r.bytes, y.bytes); |
| 738 | sc_mul(temp.bytes, temp.bytes, bprime[0].bytes); |
| 739 | sc_mul(temp2.bytes, s.bytes, y.bytes); |
| 740 | sc_mul(temp2.bytes, temp2.bytes, aprime[0].bytes); |
| 741 | sc_add(temp.bytes, temp.bytes, temp2.bytes); |
| 742 | sc_mul(A1_data[3].scalar.bytes, temp.bytes, INV_EIGHT.bytes); |
| 743 | ge_p3 H_p3; |
| 744 | ge_frombytes_vartime(&H_p3, rct::H.bytes); |
| 745 | A1_data[3].point = H_p3; |
| 746 | |
| 747 | rct::key A1 = multiexp(A1_data, 0); |
| 748 | |
| 749 | sc_mul(temp.bytes, r.bytes, y.bytes); |
| 750 | sc_mul(temp.bytes, temp.bytes, s.bytes); |
| 751 | sc_mul(temp.bytes, temp.bytes, INV_EIGHT.bytes); |
| 752 | sc_mul(temp2.bytes, eta.bytes, INV_EIGHT.bytes); |
| 753 | rct::key B; |
| 754 | rct::addKeys2(B, temp2, temp, rct::H); |
| 755 | |
| 756 | rct::key e = transcript_update(transcript, A1, B); |
| 757 | if (e == rct::zero()) |
| 758 | { |
| 759 | MINFO("e is 0, trying again"); |
| 760 | goto try_again; |
| 761 | } |
| 762 | rct::key e_squared; |
| 763 | sc_mul(e_squared.bytes, e.bytes, e.bytes); |
| 764 | |
| 765 | rct::key r1; |
| 766 | sc_muladd(r1.bytes, aprime[0].bytes, e.bytes, r.bytes); |
| 767 | |
| 768 | rct::key s1; |
| 769 | sc_muladd(s1.bytes, bprime[0].bytes, e.bytes, s.bytes); |
| 770 | |
| 771 | rct::key d1; |
| 772 | sc_muladd(d1.bytes, d_.bytes, e.bytes, eta.bytes); |
| 773 | sc_muladd(d1.bytes, alpha1.bytes, e_squared.bytes, d1.bytes); |
| 774 | |
| 775 | return BulletproofPlus(std::move(V), A, A1, B, r1, s1, d1, std::move(L), std::move(R)); |
| 776 | } |
| 777 | |
| 778 | BulletproofPlus bulletproof_plus_PROVE(const std::vector<uint64_t> &v, const rct::keyV &gamma) |
| 779 | { |
| 780 | CHECK_AND_ASSERT_THROW_MES(v.size() == gamma.size(), "Incompatible sizes of v and gamma"); |
| 781 | |
| 782 | // vG + gammaH |
| 783 | rct::keyV sv(v.size()); |
| 784 | for (size_t i = 0; i < v.size(); ++i) |
| 785 | { |
| 786 | sv[i] = rct::d2h(v[i]); |
| 787 | } |
| 788 | return bulletproof_plus_PROVE(sv, gamma); |
| 789 | } |
| 790 | |
| 791 | struct bp_plus_proof_data_t |
| 792 | { |
| 793 | rct::key y, z, e; |
| 794 | std::vector<rct::key> challenges; |
| 795 | size_t logM, inv_offset; |
| 796 | }; |
| 797 | |
| 798 | // Given a batch of range proofs, determine if they are all valid |
| 799 | bool bulletproof_plus_VERIFY(const std::vector<const BulletproofPlus*> &proofs) |
| 800 | { |
| 801 | init_exponents(); |
| 802 | |
| 803 | const size_t logN = 6; |
| 804 | const size_t N = 1 << logN; |
| 805 | |
| 806 | // Set up |
| 807 | size_t max_length = 0; // size of each of the longest proof's inner-product vectors |
| 808 | size_t nV = 0; // number of output commitments across all proofs |
| 809 | size_t inv_offset = 0; |
| 810 | size_t max_logM = 0; |
| 811 | |
| 812 | std::vector<bp_plus_proof_data_t> proof_data; |
| 813 | proof_data.reserve(proofs.size()); |
| 814 | |
| 815 | // We'll perform only a single batch inversion across all proofs in the batch, |
| 816 | // since batch inversion requires only one scalar inversion operation. |
| 817 | std::vector<rct::key> to_invert; |
| 818 | to_invert.reserve(11 * proofs.size()); // maximal size, given the aggregation limit |
| 819 | |
| 820 | for (const BulletproofPlus *p: proofs) |
| 821 | { |
| 822 | const BulletproofPlus &proof = *p; |
| 823 | |
| 824 | // Sanity checks |
| 825 | CHECK_AND_ASSERT_MES(is_reduced(proof.r1), false, "Input scalar not in range"); |
| 826 | CHECK_AND_ASSERT_MES(is_reduced(proof.s1), false, "Input scalar not in range"); |
| 827 | CHECK_AND_ASSERT_MES(is_reduced(proof.d1), false, "Input scalar not in range"); |
| 828 | |
| 829 | CHECK_AND_ASSERT_MES(proof.V.size() >= 1, false, "V does not have at least one element"); |
| 830 | CHECK_AND_ASSERT_MES(proof.L.size() == proof.R.size(), false, "Mismatched L and R sizes"); |
| 831 | CHECK_AND_ASSERT_MES(proof.L.size() > 0, false, "Empty proof"); |
| 832 | |
| 833 | max_length = std::max(max_length, proof.L.size()); |
| 834 | nV += proof.V.size(); |
| 835 | |
| 836 | proof_data.push_back({}); |
| 837 | bp_plus_proof_data_t &pd = proof_data.back(); |
| 838 | |
| 839 | // Reconstruct the challenges |
| 840 | rct::key transcript = copy(initial_transcript); |
| 841 | transcript = transcript_update(transcript, rct::hash_to_scalar(proof.V)); |
| 842 | pd.y = transcript_update(transcript, proof.A); |
| 843 | CHECK_AND_ASSERT_MES(!(pd.y == rct::zero()), false, "y == 0"); |
| 844 | pd.z = transcript = rct::hash_to_scalar(pd.y); |
| 845 | CHECK_AND_ASSERT_MES(!(pd.z == rct::zero()), false, "z == 0"); |
| 846 | |
| 847 | // Determine the number of inner-product rounds based on proof size |
| 848 | size_t M; |
| 849 | for (pd.logM = 0; (M = 1<<pd.logM) <= maxM && M < proof.V.size(); ++pd.logM); |
| 850 | CHECK_AND_ASSERT_MES(proof.L.size() == 6+pd.logM, false, "Proof is not the expected size"); |
| 851 | max_logM = std::max(pd.logM, max_logM); |
| 852 | |
| 853 | const size_t rounds = pd.logM+logN; |
| 854 | CHECK_AND_ASSERT_MES(rounds > 0, false, "Zero rounds"); |
| 855 | |
| 856 | // The inner-product challenges are computed per round |
| 857 | pd.challenges.resize(rounds); |
| 858 | for (size_t j = 0; j < rounds; ++j) |
| 859 | { |
| 860 | pd.challenges[j] = transcript_update(transcript, proof.L[j], proof.R[j]); |
| 861 | CHECK_AND_ASSERT_MES(!(pd.challenges[j] == rct::zero()), false, "challenges[j] == 0"); |
| 862 | } |
| 863 | |
| 864 | // Final challenge |
| 865 | pd.e = transcript_update(transcript,proof.A1,proof.B); |
| 866 | CHECK_AND_ASSERT_MES(!(pd.e == rct::zero()), false, "e == 0"); |
| 867 | |
| 868 | // Batch scalar inversions |
| 869 | pd.inv_offset = inv_offset; |
| 870 | for (size_t j = 0; j < rounds; ++j) |
| 871 | to_invert.push_back(pd.challenges[j]); |
| 872 | to_invert.push_back(pd.y); |
| 873 | inv_offset += rounds + 1; |
| 874 | } |
| 875 | CHECK_AND_ASSERT_MES(max_length < 32, false, "At least one proof is too large"); |
| 876 | size_t maxMN = 1u << max_length; |
| 877 | |
| 878 | rct::key temp; |
| 879 | rct::key temp2; |
| 880 | |
| 881 | // Final batch proof data |
| 882 | std::vector<MultiexpData> multiexp_data; |
| 883 | multiexp_data.reserve(nV + (2 * (max_logM + logN) + 3) * proofs.size() + 2 * maxMN); |
| 884 | multiexp_data.resize(2 * maxMN); |
| 885 | |
| 886 | const std::vector<rct::key> inverses = invert(std::move(to_invert)); |
| 887 | to_invert.clear(); |
| 888 | |
| 889 | // Weights and aggregates |
| 890 | // |
| 891 | // The idea is to take the single multiscalar multiplication used in the verification |
| 892 | // of each proof in the batch and weight it using a random weighting factor, resulting |
| 893 | // in just one multiscalar multiplication check to zero for the entire batch. |
| 894 | // We can further simplify the verifier complexity by including common group elements |
| 895 | // only once in this single multiscalar multiplication. |
| 896 | // Common group elements' weighted scalar sums are tracked across proofs for this reason. |
| 897 | // |
| 898 | // To build a multiscalar multiplication for each proof, we use the method described in |
| 899 | // Section 6.1 of the preprint. Note that the result given there does not account for |
| 900 | // the construction of the inner-product inputs that are produced in the range proof |
| 901 | // verifier algorithm; we have done so here. |
| 902 | rct::key G_scalar = rct::zero(); |
| 903 | rct::key H_scalar = rct::zero(); |
| 904 | rct::keyV Gi_scalars(maxMN, rct::zero()); |
| 905 | rct::keyV Hi_scalars(maxMN, rct::zero()); |
| 906 | |
| 907 | int proof_data_index = 0; |
| 908 | rct::keyV challenges_cache; |
| 909 | std::vector<ge_p3> proof8_V, proof8_L, proof8_R; |
| 910 | |
| 911 | // Process each proof and add to the weighted batch |
| 912 | for (const BulletproofPlus *p: proofs) |
| 913 | { |
| 914 | const BulletproofPlus &proof = *p; |
| 915 | const bp_plus_proof_data_t &pd = proof_data[proof_data_index++]; |
| 916 | |
| 917 | CHECK_AND_ASSERT_MES(proof.L.size() == 6+pd.logM, false, "Proof is not the expected size"); |
| 918 | const size_t M = 1 << pd.logM; |
| 919 | const size_t MN = M*N; |
| 920 | |
| 921 | // Random weighting factor must be nonzero, which is exceptionally unlikely! |
| 922 | rct::key weight = ZERO; |
| 923 | while (weight == ZERO) |
| 924 | { |
| 925 | weight = rct::skGen(); |
| 926 | } |
| 927 | |
| 928 | // Rescale previously offset proof elements |
| 929 | // |
| 930 | // This ensures that all such group elements are in the prime-order subgroup. |
| 931 | proof8_V.resize(proof.V.size()); for (size_t i = 0; i < proof.V.size(); ++i) rct::scalarmult8(proof8_V[i], proof.V[i]); |
| 932 | proof8_L.resize(proof.L.size()); for (size_t i = 0; i < proof.L.size(); ++i) rct::scalarmult8(proof8_L[i], proof.L[i]); |
| 933 | proof8_R.resize(proof.R.size()); for (size_t i = 0; i < proof.R.size(); ++i) rct::scalarmult8(proof8_R[i], proof.R[i]); |
| 934 | ge_p3 proof8_A1; |
| 935 | ge_p3 proof8_B; |
| 936 | ge_p3 proof8_A; |
| 937 | rct::scalarmult8(proof8_A1, proof.A1); |
| 938 | rct::scalarmult8(proof8_B, proof.B); |
| 939 | rct::scalarmult8(proof8_A, proof.A); |
| 940 | |
| 941 | // Compute necessary powers of the y-challenge |
| 942 | rct::key y_MN = copy(pd.y); |
| 943 | rct::key y_MN_1; |
| 944 | size_t temp_MN = MN; |
| 945 | while (temp_MN > 1) |
| 946 | { |
| 947 | sc_mul(y_MN.bytes, y_MN.bytes, y_MN.bytes); |
| 948 | temp_MN /= 2; |
| 949 | } |
| 950 | sc_mul(y_MN_1.bytes, y_MN.bytes, pd.y.bytes); |
| 951 | |
| 952 | // V_j: -e**2 * z**(2*j+1) * y**(MN+1) * weight |
| 953 | rct::key e_squared; |
| 954 | sc_mul(e_squared.bytes, pd.e.bytes, pd.e.bytes); |
| 955 | |
| 956 | rct::key z_squared; |
| 957 | sc_mul(z_squared.bytes, pd.z.bytes, pd.z.bytes); |
| 958 | |
| 959 | sc_sub(temp.bytes, ZERO.bytes, e_squared.bytes); |
| 960 | sc_mul(temp.bytes, temp.bytes, y_MN_1.bytes); |
| 961 | sc_mul(temp.bytes, temp.bytes, weight.bytes); |
| 962 | for (size_t j = 0; j < proof8_V.size(); j++) |
| 963 | { |
| 964 | sc_mul(temp.bytes, temp.bytes, z_squared.bytes); |
| 965 | multiexp_data.emplace_back(temp, proof8_V[j]); |
| 966 | } |
| 967 | |
| 968 | // B: -weight |
| 969 | sc_mul(temp.bytes, MINUS_ONE.bytes, weight.bytes); |
| 970 | multiexp_data.emplace_back(temp, proof8_B); |
| 971 | |
| 972 | // A1: -weight*e |
| 973 | sc_mul(temp.bytes, temp.bytes, pd.e.bytes); |
| 974 | multiexp_data.emplace_back(temp, proof8_A1); |
| 975 | |
| 976 | // A: -weight*e*e |
| 977 | rct::key minus_weight_e_squared; |
| 978 | sc_mul(minus_weight_e_squared.bytes, temp.bytes, pd.e.bytes); |
| 979 | multiexp_data.emplace_back(minus_weight_e_squared, proof8_A); |
| 980 | |
| 981 | // G: weight*d1 |
| 982 | sc_muladd(G_scalar.bytes, weight.bytes, proof.d1.bytes, G_scalar.bytes); |
| 983 | |
| 984 | // Windowed vector |
| 985 | // d[j*N+i] = z**(2*(j+1)) * 2**i |
| 986 | rct::keyV d(MN, rct::zero()); |
| 987 | d[0] = z_squared; |
| 988 | for (size_t i = 1; i < N; i++) |
| 989 | { |
| 990 | sc_add(d[i].bytes, d[i-1].bytes, d[i-1].bytes); |
| 991 | } |
| 992 | |
| 993 | for (size_t j = 1; j < M; j++) |
| 994 | { |
| 995 | for (size_t i = 0; i < N; i++) |
| 996 | { |
| 997 | sc_mul(d[j*N+i].bytes, d[(j-1)*N+i].bytes, z_squared.bytes); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | // More efficient computation of sum(d) |
| 1002 | rct::key sum_d; |
| 1003 | sc_mul(sum_d.bytes, TWO_SIXTY_FOUR_MINUS_ONE.bytes, sum_of_even_powers(pd.z, 2*M).bytes); |
| 1004 | |
| 1005 | // H: weight*( r1*y*s1 + e**2*( y**(MN+1)*z*sum(d) + (z**2-z)*sum(y) ) ) |
| 1006 | rct::key sum_y = sum_of_scalar_powers(pd.y, MN); |
| 1007 | sc_sub(temp.bytes, z_squared.bytes, pd.z.bytes); |
| 1008 | sc_mul(temp.bytes, temp.bytes, sum_y.bytes); |
| 1009 | |
| 1010 | sc_mul(temp2.bytes, y_MN_1.bytes, pd.z.bytes); |
| 1011 | sc_mul(temp2.bytes, temp2.bytes, sum_d.bytes); |
| 1012 | sc_add(temp.bytes, temp.bytes, temp2.bytes); |
| 1013 | sc_mul(temp.bytes, temp.bytes, e_squared.bytes); |
| 1014 | sc_mul(temp2.bytes, proof.r1.bytes, pd.y.bytes); |
| 1015 | sc_mul(temp2.bytes, temp2.bytes, proof.s1.bytes); |
| 1016 | sc_add(temp.bytes, temp.bytes, temp2.bytes); |
| 1017 | sc_muladd(H_scalar.bytes, temp.bytes, weight.bytes, H_scalar.bytes); |
| 1018 | |
| 1019 | // Compute the number of rounds for the inner-product argument |
| 1020 | const size_t rounds = pd.logM+logN; |
| 1021 | CHECK_AND_ASSERT_MES(rounds > 0, false, "Zero rounds"); |
| 1022 | |
| 1023 | const rct::key *challenges_inv = &inverses[pd.inv_offset]; |
| 1024 | const rct::key yinv = inverses[pd.inv_offset + rounds]; |
| 1025 | |
| 1026 | // Compute challenge products |
| 1027 | challenges_cache.resize(1<<rounds); |
| 1028 | challenges_cache[0] = challenges_inv[0]; |
| 1029 | challenges_cache[1] = pd.challenges[0]; |
| 1030 | for (size_t j = 1; j < rounds; ++j) |
| 1031 | { |
| 1032 | const size_t slots = 1<<(j+1); |
| 1033 | for (size_t s = slots; s-- > 0; --s) |
| 1034 | { |
| 1035 | sc_mul(challenges_cache[s].bytes, challenges_cache[s/2].bytes, pd.challenges[j].bytes); |
| 1036 | sc_mul(challenges_cache[s-1].bytes, challenges_cache[s/2].bytes, challenges_inv[j].bytes); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | // Gi and Hi |
| 1041 | rct::key e_r1_w_y; |
| 1042 | sc_mul(e_r1_w_y.bytes, pd.e.bytes, proof.r1.bytes); |
| 1043 | sc_mul(e_r1_w_y.bytes, e_r1_w_y.bytes, weight.bytes); |
| 1044 | rct::key e_s1_w; |
| 1045 | sc_mul(e_s1_w.bytes, pd.e.bytes, proof.s1.bytes); |
| 1046 | sc_mul(e_s1_w.bytes, e_s1_w.bytes, weight.bytes); |
| 1047 | rct::key e_squared_z_w; |
| 1048 | sc_mul(e_squared_z_w.bytes, e_squared.bytes, pd.z.bytes); |
| 1049 | sc_mul(e_squared_z_w.bytes, e_squared_z_w.bytes, weight.bytes); |
| 1050 | rct::key minus_e_squared_z_w; |
| 1051 | sc_sub(minus_e_squared_z_w.bytes, ZERO.bytes, e_squared_z_w.bytes); |
| 1052 | rct::key minus_e_squared_w_y; |
| 1053 | sc_sub(minus_e_squared_w_y.bytes, ZERO.bytes, e_squared.bytes); |
| 1054 | sc_mul(minus_e_squared_w_y.bytes, minus_e_squared_w_y.bytes, weight.bytes); |
| 1055 | sc_mul(minus_e_squared_w_y.bytes, minus_e_squared_w_y.bytes, y_MN.bytes); |
| 1056 | for (size_t i = 0; i < MN; ++i) |
| 1057 | { |
| 1058 | rct::key g_scalar = copy(e_r1_w_y); |
| 1059 | rct::key h_scalar; |
| 1060 | |
| 1061 | // Use the binary decomposition of the index |
| 1062 | sc_muladd(g_scalar.bytes, g_scalar.bytes, challenges_cache[i].bytes, e_squared_z_w.bytes); |
| 1063 | sc_muladd(h_scalar.bytes, e_s1_w.bytes, challenges_cache[(~i) & (MN-1)].bytes, minus_e_squared_z_w.bytes); |
| 1064 | |
| 1065 | // Complete the scalar derivation |
| 1066 | sc_add(Gi_scalars[i].bytes, Gi_scalars[i].bytes, g_scalar.bytes); |
| 1067 | sc_muladd(h_scalar.bytes, minus_e_squared_w_y.bytes, d[i].bytes, h_scalar.bytes); |
| 1068 | sc_add(Hi_scalars[i].bytes, Hi_scalars[i].bytes, h_scalar.bytes); |
| 1069 | |
| 1070 | // Update iterated values |
| 1071 | sc_mul(e_r1_w_y.bytes, e_r1_w_y.bytes, yinv.bytes); |
| 1072 | sc_mul(minus_e_squared_w_y.bytes, minus_e_squared_w_y.bytes, yinv.bytes); |
| 1073 | } |
| 1074 | |
| 1075 | // L_j: -weight*e*e*challenges[j]**2 |
| 1076 | // R_j: -weight*e*e*challenges[j]**(-2) |
| 1077 | for (size_t j = 0; j < rounds; ++j) |
| 1078 | { |
| 1079 | sc_mul(temp.bytes, pd.challenges[j].bytes, pd.challenges[j].bytes); |
| 1080 | sc_mul(temp.bytes, temp.bytes, minus_weight_e_squared.bytes); |
| 1081 | multiexp_data.emplace_back(temp, proof8_L[j]); |
| 1082 | |
| 1083 | sc_mul(temp.bytes, challenges_inv[j].bytes, challenges_inv[j].bytes); |
| 1084 | sc_mul(temp.bytes, temp.bytes, minus_weight_e_squared.bytes); |
| 1085 | multiexp_data.emplace_back(temp, proof8_R[j]); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | // Verify all proofs in the weighted batch |
| 1090 | multiexp_data.emplace_back(G_scalar, rct::G); |
| 1091 | multiexp_data.emplace_back(H_scalar, rct::H); |
| 1092 | for (size_t i = 0; i < maxMN; ++i) |
| 1093 | { |
| 1094 | multiexp_data[i * 2] = {Gi_scalars[i], Gi_p3[i]}; |
| 1095 | multiexp_data[i * 2 + 1] = {Hi_scalars[i], Hi_p3[i]}; |
| 1096 | } |
| 1097 | if (!(multiexp(multiexp_data, 2 * maxMN) == rct::identity())) |
| 1098 | { |
| 1099 | MERROR("Verification failure"); |
| 1100 | return false; |
| 1101 | } |
| 1102 | |
| 1103 | return true; |
| 1104 | } |
| 1105 | |
| 1106 | bool bulletproof_plus_VERIFY(const std::vector<BulletproofPlus> &proofs) |
| 1107 | { |
| 1108 | std::vector<const BulletproofPlus*> proof_pointers; |
| 1109 | proof_pointers.reserve(proofs.size()); |
| 1110 | for (const BulletproofPlus &proof: proofs) |
| 1111 | proof_pointers.push_back(&proof); |
| 1112 | return bulletproof_plus_VERIFY(proof_pointers); |
| 1113 | } |
| 1114 | |
| 1115 | bool bulletproof_plus_VERIFY(const BulletproofPlus &proof) |
| 1116 | { |
| 1117 | std::vector<const BulletproofPlus*> proofs; |
| 1118 | proofs.push_back(&proof); |
| 1119 | return bulletproof_plus_VERIFY(proofs); |
| 1120 | } |
| 1121 | } |