| 1 | // Copyright (c) 2017-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Adapted from Python code by Sarang Noether |
| 31 | |
| 32 | #include "misc_log_ex.h" |
| 33 | #include "common/perf_timer.h" |
| 34 | extern "C" |
| 35 | { |
| 36 | #include "crypto/crypto-ops.h" |
| 37 | } |
| 38 | #include "common/aligned.h" |
| 39 | #include "rctOps.h" |
| 40 | #include "multiexp.h" |
| 41 | |
| 42 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 43 | #define MONERO_DEFAULT_LOG_CATEGORY "multiexp" |
| 44 | |
| 45 | //#define MULTIEXP_PERF(x) x |
| 46 | #define MULTIEXP_PERF(x) |
| 47 | |
| 48 | #define RAW_MEMORY_BLOCK |
| 49 | //#define ALTERNATE_LAYOUT |
| 50 | //#define TRACK_STRAUS_ZERO_IDENTITY |
| 51 | |
| 52 | // per points us for N/B points (B point bands) |
| 53 | // raw alt 128/192 4096/192 4096/4096 |
| 54 | // 0 0 52.6 71 71.2 |
| 55 | // 0 1 53.2 72.2 72.4 |
| 56 | // 1 0 52.7 67 67.1 |
| 57 | // 1 1 52.8 70.4 70.2 |
| 58 | |
| 59 | // Pippenger: |
| 60 | // 1 2 3 4 5 6 7 8 9 bestN |
| 61 | // 2 555 598 621 804 1038 1733 2486 5020 8304 1 |
| 62 | // 4 783 747 800 1006 1428 2132 3285 5185 9806 2 |
| 63 | // 8 1174 1071 1095 1286 1640 2398 3869 6378 12080 2 |
| 64 | // 16 2279 1874 1745 1739 2144 2831 4209 6964 12007 4 |
| 65 | // 32 3910 3706 2588 2477 2782 3467 4856 7489 12618 4 |
| 66 | // 64 7184 5429 4710 4368 4010 4672 6027 8559 13684 5 |
| 67 | // 128 14097 10574 8452 7297 6841 6718 8615 10580 15641 6 |
| 68 | // 256 27715 20800 16000 13550 11875 11400 11505 14090 18460 6 |
| 69 | // 512 55100 41250 31740 26570 22030 19830 20760 21380 25215 6 |
| 70 | // 1024 111520 79000 61080 49720 43080 38320 37600 35040 36750 8 |
| 71 | // 2048 219480 162680 122120 102080 83760 70360 66600 63920 66160 8 |
| 72 | // 4096 453320 323080 247240 210200 180040 150240 132440 114920 110560 9 |
| 73 | |
| 74 | // 2 4 8 16 32 64 128 256 512 1024 2048 4096 |
| 75 | // Bos Coster 858 994 1316 1949 3183 5512 9865 17830 33485 63160 124280 246320 |
| 76 | // Straus 226 341 548 980 1870 3538 7039 14490 29020 57200 118640 233640 |
| 77 | // Straus/cached 226 315 485 785 1514 2858 5753 11065 22970 45120 98880 194840 |
| 78 | // Pippenger 555 747 1071 1739 2477 4010 6718 11400 19830 35040 63920 110560 |
| 79 | |
| 80 | // Best/cached Straus Straus Straus Straus Straus Straus Straus Straus Pip Pip Pip Pip |
| 81 | // Best/uncached Straus Straus Straus Straus Straus Straus Pip Pip Pip Pip Pip Pip |
| 82 | |
| 83 | // New timings: |
| 84 | // Pippenger: |
| 85 | // 2/1 always |
| 86 | // 3/2 at ~13 |
| 87 | // 4/3 at ~29 |
| 88 | // 5/4 at ~83 |
| 89 | // 6/5 < 200 |
| 90 | // 7/6 at ~470 |
| 91 | // 8/7 at ~1180 |
| 92 | // 9/8 at ~2290 |
| 93 | // Cached Pippenger: |
| 94 | // 6/5 < 200 |
| 95 | // 7/6 at 460 |
| 96 | // 8/7 at 1180 |
| 97 | // 9/8 at 2300 |
| 98 | // |
| 99 | // Cached Straus/Pippenger cross at 232 |
| 100 | // |
| 101 | |
| 102 | namespace rct |
| 103 | { |
| 104 | |
| 105 | static inline bool operator<(const rct::key &k0, const rct::key&k1) |
| 106 | { |
| 107 | for (int n = 31; n >= 0; --n) |
| 108 | { |
| 109 | if (k0.bytes[n] < k1.bytes[n]) |
| 110 | return true; |
| 111 | if (k0.bytes[n] > k1.bytes[n]) |
| 112 | return false; |
| 113 | } |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | static inline rct::key div2(const rct::key &k) |
| 118 | { |
| 119 | rct::key res; |
| 120 | int carry = 0; |
| 121 | for (int n = 31; n >= 0; --n) |
| 122 | { |
| 123 | int new_carry = (k.bytes[n] & 1) << 7; |
| 124 | res.bytes[n] = k.bytes[n] / 2 + carry; |
| 125 | carry = new_carry; |
| 126 | } |
| 127 | return res; |
| 128 | } |
| 129 | |
| 130 | static inline rct::key pow2(size_t n) |
| 131 | { |
| 132 | CHECK_AND_ASSERT_THROW_MES(n < 256, "Invalid pow2 argument"); |
| 133 | rct::key res = rct::zero(); |
| 134 | res[n >> 3] |= 1<<(n&7); |
| 135 | return res; |
| 136 | } |
| 137 | |
| 138 | static inline int test(const rct::key &k, size_t n) |
| 139 | { |
| 140 | if (n >= 256) return 0; |
| 141 | return k[n >> 3] & (1 << (n & 7)); |
| 142 | } |
| 143 | |
| 144 | static inline void add(ge_p3 &p3, const ge_cached &other) |
| 145 | { |
| 146 | ge_p1p1 p1; |
| 147 | ge_add(&p1, &p3, &other); |
| 148 | ge_p1p1_to_p3(&p3, &p1); |
| 149 | } |
| 150 | |
| 151 | static inline void add(ge_p3 &p3, const ge_p3 &other) |
| 152 | { |
| 153 | ge_cached cached; |
| 154 | ge_p3_to_cached(&cached, &other); |
| 155 | add(p3, cached); |
| 156 | } |
| 157 | |
| 158 | rct::key bos_coster_heap_conv(std::vector<MultiexpData> data) |
| 159 | { |
| 160 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(bos_coster, 1000000)); |
| 161 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(setup, 1000000)); |
| 162 | size_t points = data.size(); |
| 163 | CHECK_AND_ASSERT_THROW_MES(points > 1, "Not enough points"); |
| 164 | std::vector<size_t> heap(points); |
| 165 | for (size_t n = 0; n < points; ++n) |
| 166 | heap[n] = n; |
| 167 | |
| 168 | auto Comp = [&](size_t e0, size_t e1) { return data[e0].scalar < data[e1].scalar; }; |
| 169 | std::make_heap(heap.begin(), heap.end(), Comp); |
| 170 | MULTIEXP_PERF(PERF_TIMER_STOP(setup)); |
| 171 | |
| 172 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(loop, 1000000)); |
| 173 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(pop, 1000000)); MULTIEXP_PERF(PERF_TIMER_PAUSE(pop)); |
| 174 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(add, 1000000)); MULTIEXP_PERF(PERF_TIMER_PAUSE(add)); |
| 175 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(sub, 1000000)); MULTIEXP_PERF(PERF_TIMER_PAUSE(sub)); |
| 176 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(push, 1000000)); MULTIEXP_PERF(PERF_TIMER_PAUSE(push)); |
| 177 | while (heap.size() > 1) |
| 178 | { |
| 179 | MULTIEXP_PERF(PERF_TIMER_RESUME(pop)); |
| 180 | std::pop_heap(heap.begin(), heap.end(), Comp); |
| 181 | size_t index1 = heap.back(); |
| 182 | heap.pop_back(); |
| 183 | std::pop_heap(heap.begin(), heap.end(), Comp); |
| 184 | size_t index2 = heap.back(); |
| 185 | heap.pop_back(); |
| 186 | MULTIEXP_PERF(PERF_TIMER_PAUSE(pop)); |
| 187 | |
| 188 | MULTIEXP_PERF(PERF_TIMER_RESUME(add)); |
| 189 | ge_cached cached; |
| 190 | ge_p3_to_cached(&cached, &data[index1].point); |
| 191 | ge_p1p1 p1; |
| 192 | ge_add(&p1, &data[index2].point, &cached); |
| 193 | ge_p1p1_to_p3(&data[index2].point, &p1); |
| 194 | MULTIEXP_PERF(PERF_TIMER_PAUSE(add)); |
| 195 | |
| 196 | MULTIEXP_PERF(PERF_TIMER_RESUME(sub)); |
| 197 | sc_sub(data[index1].scalar.bytes, data[index1].scalar.bytes, data[index2].scalar.bytes); |
| 198 | MULTIEXP_PERF(PERF_TIMER_PAUSE(sub)); |
| 199 | |
| 200 | MULTIEXP_PERF(PERF_TIMER_RESUME(push)); |
| 201 | if (!(data[index1].scalar == rct::zero())) |
| 202 | { |
| 203 | heap.push_back(index1); |
| 204 | std::push_heap(heap.begin(), heap.end(), Comp); |
| 205 | } |
| 206 | |
| 207 | heap.push_back(index2); |
| 208 | std::push_heap(heap.begin(), heap.end(), Comp); |
| 209 | MULTIEXP_PERF(PERF_TIMER_PAUSE(push)); |
| 210 | } |
| 211 | MULTIEXP_PERF(PERF_TIMER_STOP(push)); |
| 212 | MULTIEXP_PERF(PERF_TIMER_STOP(sub)); |
| 213 | MULTIEXP_PERF(PERF_TIMER_STOP(add)); |
| 214 | MULTIEXP_PERF(PERF_TIMER_STOP(pop)); |
| 215 | MULTIEXP_PERF(PERF_TIMER_STOP(loop)); |
| 216 | |
| 217 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(end, 1000000)); |
| 218 | //return rct::scalarmultKey(data[index1].point, data[index1].scalar); |
| 219 | std::pop_heap(heap.begin(), heap.end(), Comp); |
| 220 | size_t index1 = heap.back(); |
| 221 | heap.pop_back(); |
| 222 | ge_p2 p2; |
| 223 | ge_scalarmult(&p2, data[index1].scalar.bytes, &data[index1].point); |
| 224 | rct::key res; |
| 225 | ge_tobytes(res.bytes, &p2); |
| 226 | return res; |
| 227 | } |
| 228 | |
| 229 | rct::key bos_coster_heap_conv_robust(std::vector<MultiexpData> data) |
| 230 | { |
| 231 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(bos_coster, 1000000)); |
| 232 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(setup, 1000000)); |
| 233 | size_t points = data.size(); |
| 234 | CHECK_AND_ASSERT_THROW_MES(points > 0, "Not enough points"); |
| 235 | std::vector<size_t> heap; |
| 236 | heap.reserve(points); |
| 237 | for (size_t n = 0; n < points; ++n) |
| 238 | { |
| 239 | if (!(data[n].scalar == rct::zero()) && !ge_p3_is_point_at_infinity_vartime(&data[n].point)) |
| 240 | heap.push_back(n); |
| 241 | } |
| 242 | points = heap.size(); |
| 243 | if (points == 0) |
| 244 | return rct::identity(); |
| 245 | |
| 246 | auto Comp = [&](size_t e0, size_t e1) { return data[e0].scalar < data[e1].scalar; }; |
| 247 | std::make_heap(heap.begin(), heap.end(), Comp); |
| 248 | |
| 249 | if (points < 2) |
| 250 | { |
| 251 | std::pop_heap(heap.begin(), heap.end(), Comp); |
| 252 | size_t index1 = heap.back(); |
| 253 | ge_p2 p2; |
| 254 | ge_scalarmult(&p2, data[index1].scalar.bytes, &data[index1].point); |
| 255 | rct::key res; |
| 256 | ge_tobytes(res.bytes, &p2); |
| 257 | return res; |
| 258 | } |
| 259 | |
| 260 | MULTIEXP_PERF(PERF_TIMER_STOP(setup)); |
| 261 | |
| 262 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(loop, 1000000)); |
| 263 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(pop, 1000000)); MULTIEXP_PERF(PERF_TIMER_PAUSE(pop)); |
| 264 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(div, 1000000)); MULTIEXP_PERF(PERF_TIMER_PAUSE(div)); |
| 265 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(add, 1000000)); MULTIEXP_PERF(PERF_TIMER_PAUSE(add)); |
| 266 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(sub, 1000000)); MULTIEXP_PERF(PERF_TIMER_PAUSE(sub)); |
| 267 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(push, 1000000)); MULTIEXP_PERF(PERF_TIMER_PAUSE(push)); |
| 268 | while (heap.size() > 1) |
| 269 | { |
| 270 | MULTIEXP_PERF(PERF_TIMER_RESUME(pop)); |
| 271 | std::pop_heap(heap.begin(), heap.end(), Comp); |
| 272 | size_t index1 = heap.back(); |
| 273 | heap.pop_back(); |
| 274 | std::pop_heap(heap.begin(), heap.end(), Comp); |
| 275 | size_t index2 = heap.back(); |
| 276 | heap.pop_back(); |
| 277 | MULTIEXP_PERF(PERF_TIMER_PAUSE(pop)); |
| 278 | |
| 279 | ge_cached cached; |
| 280 | ge_p1p1 p1; |
| 281 | ge_p2 p2; |
| 282 | |
| 283 | MULTIEXP_PERF(PERF_TIMER_RESUME(div)); |
| 284 | while (1) |
| 285 | { |
| 286 | rct::key s1_2 = div2(data[index1].scalar); |
| 287 | if (!(data[index2].scalar < s1_2)) |
| 288 | break; |
| 289 | if (data[index1].scalar.bytes[0] & 1) |
| 290 | { |
| 291 | data.resize(data.size()+1); |
| 292 | data.back().scalar = rct::identity(); |
| 293 | data.back().point = data[index1].point; |
| 294 | heap.push_back(data.size() - 1); |
| 295 | std::push_heap(heap.begin(), heap.end(), Comp); |
| 296 | } |
| 297 | data[index1].scalar = div2(data[index1].scalar); |
| 298 | ge_p3_to_p2(&p2, &data[index1].point); |
| 299 | ge_p2_dbl(&p1, &p2); |
| 300 | ge_p1p1_to_p3(&data[index1].point, &p1); |
| 301 | } |
| 302 | MULTIEXP_PERF(PERF_TIMER_PAUSE(div)); |
| 303 | |
| 304 | MULTIEXP_PERF(PERF_TIMER_RESUME(add)); |
| 305 | ge_p3_to_cached(&cached, &data[index1].point); |
| 306 | ge_add(&p1, &data[index2].point, &cached); |
| 307 | ge_p1p1_to_p3(&data[index2].point, &p1); |
| 308 | MULTIEXP_PERF(PERF_TIMER_PAUSE(add)); |
| 309 | |
| 310 | MULTIEXP_PERF(PERF_TIMER_RESUME(sub)); |
| 311 | sc_sub(data[index1].scalar.bytes, data[index1].scalar.bytes, data[index2].scalar.bytes); |
| 312 | MULTIEXP_PERF(PERF_TIMER_PAUSE(sub)); |
| 313 | |
| 314 | MULTIEXP_PERF(PERF_TIMER_RESUME(push)); |
| 315 | if (!(data[index1].scalar == rct::zero())) |
| 316 | { |
| 317 | heap.push_back(index1); |
| 318 | std::push_heap(heap.begin(), heap.end(), Comp); |
| 319 | } |
| 320 | |
| 321 | heap.push_back(index2); |
| 322 | std::push_heap(heap.begin(), heap.end(), Comp); |
| 323 | MULTIEXP_PERF(PERF_TIMER_PAUSE(push)); |
| 324 | } |
| 325 | MULTIEXP_PERF(PERF_TIMER_STOP(push)); |
| 326 | MULTIEXP_PERF(PERF_TIMER_STOP(sub)); |
| 327 | MULTIEXP_PERF(PERF_TIMER_STOP(add)); |
| 328 | MULTIEXP_PERF(PERF_TIMER_STOP(pop)); |
| 329 | MULTIEXP_PERF(PERF_TIMER_STOP(loop)); |
| 330 | |
| 331 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(end, 1000000)); |
| 332 | //return rct::scalarmultKey(data[index1].point, data[index1].scalar); |
| 333 | std::pop_heap(heap.begin(), heap.end(), Comp); |
| 334 | size_t index1 = heap.back(); |
| 335 | heap.pop_back(); |
| 336 | ge_p2 p2; |
| 337 | ge_scalarmult(&p2, data[index1].scalar.bytes, &data[index1].point); |
| 338 | rct::key res; |
| 339 | ge_tobytes(res.bytes, &p2); |
| 340 | return res; |
| 341 | } |
| 342 | |
| 343 | #define STRAUS_C 4 |
| 344 | |
| 345 | struct straus_cached_data |
| 346 | { |
| 347 | #ifdef RAW_MEMORY_BLOCK |
| 348 | size_t size; |
| 349 | ge_cached *multiples; |
| 350 | straus_cached_data(): size(0), multiples(NULL) {} |
| 351 | ~straus_cached_data() { aligned_free(multiples); } |
| 352 | #else |
| 353 | std::vector<std::vector<ge_cached>> multiples; |
| 354 | #endif |
| 355 | }; |
| 356 | #ifdef RAW_MEMORY_BLOCK |
| 357 | #ifdef ALTERNATE_LAYOUT |
| 358 | #define CACHE_OFFSET(cache,point,digit) cache->multiples[(point)*((1<<STRAUS_C)-1)+((digit)-1)] |
| 359 | #else |
| 360 | #define CACHE_OFFSET(cache,point,digit) cache->multiples[(point)+cache->size*((digit)-1)] |
| 361 | #endif |
| 362 | #else |
| 363 | #ifdef ALTERNATE_LAYOUT |
| 364 | #define CACHE_OFFSET(cache,point,digit) local_cache->multiples[j][digit-1] |
| 365 | #else |
| 366 | #define CACHE_OFFSET(cache,point,digit) local_cache->multiples[digit][j] |
| 367 | #endif |
| 368 | #endif |
| 369 | |
| 370 | std::shared_ptr<straus_cached_data> straus_init_cache(const std::vector<MultiexpData> &data, size_t N) |
| 371 | { |
| 372 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(multiples, 1000000)); |
| 373 | if (N == 0) |
| 374 | N = data.size(); |
| 375 | CHECK_AND_ASSERT_THROW_MES(N <= data.size(), "Bad cache base data"); |
| 376 | ge_p1p1 p1; |
| 377 | ge_p3 p3; |
| 378 | std::shared_ptr<straus_cached_data> cache(new straus_cached_data()); |
| 379 | |
| 380 | #ifdef RAW_MEMORY_BLOCK |
| 381 | const size_t offset = cache->size; |
| 382 | cache->multiples = (ge_cached*)aligned_realloc(cache->multiples, sizeof(ge_cached) * ((1<<STRAUS_C)-1) * std::max(offset, N), 4096); |
| 383 | CHECK_AND_ASSERT_THROW_MES(cache->multiples, "Out of memory"); |
| 384 | cache->size = N; |
| 385 | for (size_t j=offset;j<N;++j) |
| 386 | { |
| 387 | ge_p3_to_cached(&CACHE_OFFSET(cache, j, 1), &data[j].point); |
| 388 | for (size_t i=2;i<1<<STRAUS_C;++i) |
| 389 | { |
| 390 | ge_add(&p1, &data[j].point, &CACHE_OFFSET(cache, j, i-1)); |
| 391 | ge_p1p1_to_p3(&p3, &p1); |
| 392 | ge_p3_to_cached(&CACHE_OFFSET(cache, j, i), &p3); |
| 393 | } |
| 394 | } |
| 395 | #else |
| 396 | #ifdef ALTERNATE_LAYOUT |
| 397 | const size_t offset = cache->multiples.size(); |
| 398 | cache->multiples.resize(std::max(offset, N)); |
| 399 | for (size_t i = offset; i < N; ++i) |
| 400 | { |
| 401 | cache->multiples[i].resize((1<<STRAUS_C)-1); |
| 402 | ge_p3_to_cached(&cache->multiples[i][0], &data[i].point); |
| 403 | for (size_t j=2;j<1<<STRAUS_C;++j) |
| 404 | { |
| 405 | ge_add(&p1, &data[i].point, &cache->multiples[i][j-2]); |
| 406 | ge_p1p1_to_p3(&p3, &p1); |
| 407 | ge_p3_to_cached(&cache->multiples[i][j-1], &p3); |
| 408 | } |
| 409 | } |
| 410 | #else |
| 411 | cache->multiples.resize(1<<STRAUS_C); |
| 412 | size_t offset = cache->multiples[1].size(); |
| 413 | cache->multiples[1].resize(std::max(offset, N)); |
| 414 | for (size_t i = offset; i < N; ++i) |
| 415 | ge_p3_to_cached(&cache->multiples[1][i], &data[i].point); |
| 416 | for (size_t i=2;i<1<<STRAUS_C;++i) |
| 417 | cache->multiples[i].resize(std::max(offset, N)); |
| 418 | for (size_t j=offset;j<N;++j) |
| 419 | { |
| 420 | for (size_t i=2;i<1<<STRAUS_C;++i) |
| 421 | { |
| 422 | ge_add(&p1, &data[j].point, &cache->multiples[i-1][j]); |
| 423 | ge_p1p1_to_p3(&p3, &p1); |
| 424 | ge_p3_to_cached(&cache->multiples[i][j], &p3); |
| 425 | } |
| 426 | } |
| 427 | #endif |
| 428 | #endif |
| 429 | MULTIEXP_PERF(PERF_TIMER_STOP(multiples)); |
| 430 | |
| 431 | return cache; |
| 432 | } |
| 433 | |
| 434 | size_t straus_get_cache_size(const std::shared_ptr<straus_cached_data> &cache) |
| 435 | { |
| 436 | size_t sz = 0; |
| 437 | #ifdef RAW_MEMORY_BLOCK |
| 438 | sz += cache->size * sizeof(ge_cached) * ((1<<STRAUS_C)-1); |
| 439 | #else |
| 440 | for (const auto &e0: cache->multiples) |
| 441 | sz += e0.size() * sizeof(ge_cached); |
| 442 | #endif |
| 443 | return sz; |
| 444 | } |
| 445 | |
| 446 | rct::key straus(const std::vector<MultiexpData> &data, const std::shared_ptr<straus_cached_data> &cache, size_t STEP) |
| 447 | { |
| 448 | CHECK_AND_ASSERT_THROW_MES(cache == NULL || cache->size >= data.size(), "Cache is too small"); |
| 449 | MULTIEXP_PERF(PERF_TIMER_UNIT(straus, 1000000)); |
| 450 | STEP = STEP ? STEP : 192; |
| 451 | |
| 452 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(setup, 1000000)); |
| 453 | std::shared_ptr<straus_cached_data> local_cache = cache == NULL ? straus_init_cache(data) : cache; |
| 454 | ge_cached cached; |
| 455 | ge_p1p1 p1; |
| 456 | |
| 457 | #ifdef TRACK_STRAUS_ZERO_IDENTITY |
| 458 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(skip, 1000000)); |
| 459 | std::vector<uint8_t> skip(data.size()); |
| 460 | for (size_t i = 0; i < data.size(); ++i) |
| 461 | skip[i] = data[i].scalar == rct::zero() || ge_p3_is_point_at_infinity_vartime(&data[i].point); |
| 462 | MULTIEXP_PERF(PERF_TIMER_STOP(skip)); |
| 463 | #endif |
| 464 | |
| 465 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(digits, 1000000)); |
| 466 | #if STRAUS_C==4 |
| 467 | std::unique_ptr<uint8_t[]> digits{new uint8_t[64 * data.size()]}; |
| 468 | #else |
| 469 | std::unique_ptr<uint8_t[]> digits{new uint8_t[256 * data.size()]}; |
| 470 | #endif |
| 471 | for (size_t j = 0; j < data.size(); ++j) |
| 472 | { |
| 473 | const unsigned char *bytes = data[j].scalar.bytes; |
| 474 | #if STRAUS_C==4 |
| 475 | unsigned int i; |
| 476 | for (i = 0; i < 64; i += 2, bytes++) |
| 477 | { |
| 478 | digits[j*64+i] = bytes[0] & 0xf; |
| 479 | digits[j*64+i+1] = bytes[0] >> 4; |
| 480 | } |
| 481 | #elif 1 |
| 482 | unsigned char bytes33[33]; |
| 483 | memcpy(bytes33, data[j].scalar.bytes, 32); |
| 484 | bytes33[32] = 0; |
| 485 | bytes = bytes33; |
| 486 | static constexpr unsigned int mask = (1<<STRAUS_C)-1; |
| 487 | for (size_t i = 0; i < 256; ++i) |
| 488 | digits[j*256+i] = ((bytes[i>>3] | (bytes[(i>>3)+1]<<8)) >> (i&7)) & mask; |
| 489 | #else |
| 490 | rct::key shifted = data[j].scalar; |
| 491 | for (size_t i = 0; i < 256; ++i) |
| 492 | { |
| 493 | digits[j*256+i] = shifted.bytes[0] & 0xf; |
| 494 | shifted = div2(shifted, (256-i)>>3); |
| 495 | } |
| 496 | #endif |
| 497 | } |
| 498 | MULTIEXP_PERF(PERF_TIMER_STOP(digits)); |
| 499 | |
| 500 | rct::key maxscalar = rct::zero(); |
| 501 | for (size_t i = 0; i < data.size(); ++i) |
| 502 | if (maxscalar < data[i].scalar) |
| 503 | maxscalar = data[i].scalar; |
| 504 | size_t start_i = 0; |
| 505 | while (start_i < 256 && !(maxscalar < pow2(start_i))) |
| 506 | start_i += STRAUS_C; |
| 507 | MULTIEXP_PERF(PERF_TIMER_STOP(setup)); |
| 508 | |
| 509 | ge_p3 res_p3 = ge_p3_identity; |
| 510 | |
| 511 | for (size_t start_offset = 0; start_offset < data.size(); start_offset += STEP) |
| 512 | { |
| 513 | const size_t num_points = std::min(data.size() - start_offset, STEP); |
| 514 | |
| 515 | ge_p3 band_p3 = ge_p3_identity; |
| 516 | size_t i = start_i; |
| 517 | if (!(i < STRAUS_C)) |
| 518 | goto skipfirst; |
| 519 | while (!(i < STRAUS_C)) |
| 520 | { |
| 521 | ge_p2 p2; |
| 522 | ge_p3_to_p2(&p2, &band_p3); |
| 523 | for (size_t j = 0; j < STRAUS_C; ++j) |
| 524 | { |
| 525 | ge_p2_dbl(&p1, &p2); |
| 526 | if (j == STRAUS_C - 1) |
| 527 | ge_p1p1_to_p3(&band_p3, &p1); |
| 528 | else |
| 529 | ge_p1p1_to_p2(&p2, &p1); |
| 530 | } |
| 531 | skipfirst: |
| 532 | i -= STRAUS_C; |
| 533 | for (size_t j = start_offset; j < start_offset + num_points; ++j) |
| 534 | { |
| 535 | #ifdef TRACK_STRAUS_ZERO_IDENTITY |
| 536 | if (skip[j]) |
| 537 | continue; |
| 538 | #endif |
| 539 | #if STRAUS_C==4 |
| 540 | const uint8_t digit = digits[j*64+i/4]; |
| 541 | #else |
| 542 | const uint8_t digit = digits[j*256+i]; |
| 543 | #endif |
| 544 | if (digit) |
| 545 | { |
| 546 | ge_add(&p1, &band_p3, &CACHE_OFFSET(local_cache, j, digit)); |
| 547 | ge_p1p1_to_p3(&band_p3, &p1); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | ge_p3_to_cached(&cached, &band_p3); |
| 553 | ge_add(&p1, &res_p3, &cached); |
| 554 | ge_p1p1_to_p3(&res_p3, &p1); |
| 555 | } |
| 556 | |
| 557 | rct::key res; |
| 558 | ge_p3_tobytes(res.bytes, &res_p3); |
| 559 | return res; |
| 560 | } |
| 561 | |
| 562 | size_t get_pippenger_c(size_t N) |
| 563 | { |
| 564 | if (N <= 13) return 2; |
| 565 | if (N <= 29) return 3; |
| 566 | if (N <= 83) return 4; |
| 567 | if (N <= 185) return 5; |
| 568 | if (N <= 465) return 6; |
| 569 | if (N <= 1180) return 7; |
| 570 | if (N <= 2295) return 8; |
| 571 | return 9; |
| 572 | } |
| 573 | |
| 574 | struct pippenger_cached_data |
| 575 | { |
| 576 | size_t size; |
| 577 | ge_cached *cached; |
| 578 | pippenger_cached_data(): size(0), cached(NULL) {} |
| 579 | ~pippenger_cached_data() { aligned_free(cached); } |
| 580 | }; |
| 581 | |
| 582 | std::shared_ptr<pippenger_cached_data> pippenger_init_cache(const std::vector<MultiexpData> &data, size_t start_offset, size_t N) |
| 583 | { |
| 584 | MULTIEXP_PERF(PERF_TIMER_START_UNIT(pippenger_init_cache, 1000000)); |
| 585 | CHECK_AND_ASSERT_THROW_MES(start_offset <= data.size(), "Bad cache base data"); |
| 586 | if (N == 0) |
| 587 | N = data.size() - start_offset; |
| 588 | CHECK_AND_ASSERT_THROW_MES(N <= data.size() - start_offset, "Bad cache base data"); |
| 589 | std::shared_ptr<pippenger_cached_data> cache(new pippenger_cached_data()); |
| 590 | |
| 591 | cache->size = N; |
| 592 | cache->cached = (ge_cached*)aligned_realloc(cache->cached, N * sizeof(ge_cached), 4096); |
| 593 | CHECK_AND_ASSERT_THROW_MES(cache->cached, "Out of memory"); |
| 594 | for (size_t i = 0; i < N; ++i) |
| 595 | ge_p3_to_cached(&cache->cached[i], &data[i+start_offset].point); |
| 596 | |
| 597 | MULTIEXP_PERF(PERF_TIMER_STOP(pippenger_init_cache)); |
| 598 | return cache; |
| 599 | } |
| 600 | |
| 601 | size_t pippenger_get_cache_size(const std::shared_ptr<pippenger_cached_data> &cache) |
| 602 | { |
| 603 | return cache->size * sizeof(*cache->cached); |
| 604 | } |
| 605 | |
| 606 | rct::key pippenger(const std::vector<MultiexpData> &data, const std::shared_ptr<pippenger_cached_data> &cache, size_t cache_size, size_t c) |
| 607 | { |
| 608 | if (cache != NULL && cache_size == 0) |
| 609 | cache_size = cache->size; |
| 610 | CHECK_AND_ASSERT_THROW_MES(cache == NULL || cache_size <= cache->size, "Cache is too small"); |
| 611 | if (c == 0) |
| 612 | c = get_pippenger_c(data.size()); |
| 613 | CHECK_AND_ASSERT_THROW_MES(c <= 9, "c is too large"); |
| 614 | |
| 615 | ge_p3 result = ge_p3_identity; |
| 616 | bool result_init = false; |
| 617 | std::unique_ptr<ge_p3[]> buckets{new ge_p3[1<<c]}; |
| 618 | bool buckets_init[1<<9]; |
| 619 | std::shared_ptr<pippenger_cached_data> local_cache = cache == NULL ? pippenger_init_cache(data) : cache; |
| 620 | std::shared_ptr<pippenger_cached_data> local_cache_2 = data.size() > cache_size ? pippenger_init_cache(data, cache_size) : NULL; |
| 621 | |
| 622 | rct::key maxscalar = rct::zero(); |
| 623 | for (size_t i = 0; i < data.size(); ++i) |
| 624 | { |
| 625 | if (maxscalar < data[i].scalar) |
| 626 | maxscalar = data[i].scalar; |
| 627 | } |
| 628 | size_t groups = 0; |
| 629 | while (groups < 256 && !(maxscalar < pow2(groups))) |
| 630 | ++groups; |
| 631 | groups = (groups + c - 1) / c; |
| 632 | |
| 633 | for (size_t k = groups; k-- > 0; ) |
| 634 | { |
| 635 | if (result_init) |
| 636 | { |
| 637 | ge_p2 p2; |
| 638 | ge_p3_to_p2(&p2, &result); |
| 639 | for (size_t i = 0; i < c; ++i) |
| 640 | { |
| 641 | ge_p1p1 p1; |
| 642 | ge_p2_dbl(&p1, &p2); |
| 643 | if (i == c - 1) |
| 644 | ge_p1p1_to_p3(&result, &p1); |
| 645 | else |
| 646 | ge_p1p1_to_p2(&p2, &p1); |
| 647 | } |
| 648 | } |
| 649 | memset(buckets_init, 0, 1u<<c); |
| 650 | |
| 651 | // partition scalars into buckets |
| 652 | for (size_t i = 0; i < data.size(); ++i) |
| 653 | { |
| 654 | unsigned int bucket = 0; |
| 655 | for (size_t j = 0; j < c; ++j) |
| 656 | if (test(data[i].scalar, k*c+j)) |
| 657 | bucket |= 1<<j; |
| 658 | if (bucket == 0) |
| 659 | continue; |
| 660 | CHECK_AND_ASSERT_THROW_MES(bucket < (1u<<c), "bucket overflow"); |
| 661 | if (buckets_init[bucket]) |
| 662 | { |
| 663 | if (i < cache_size) |
| 664 | add(buckets[bucket], local_cache->cached[i]); |
| 665 | else |
| 666 | add(buckets[bucket], local_cache_2->cached[i - cache_size]); |
| 667 | } |
| 668 | else |
| 669 | { |
| 670 | buckets[bucket] = data[i].point; |
| 671 | buckets_init[bucket] = true; |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | // sum the buckets |
| 676 | ge_p3 pail; |
| 677 | bool pail_init = false; |
| 678 | for (size_t i = (1<<c)-1; i > 0; --i) |
| 679 | { |
| 680 | if (buckets_init[i]) |
| 681 | { |
| 682 | if (pail_init) |
| 683 | add(pail, buckets[i]); |
| 684 | else |
| 685 | { |
| 686 | pail = buckets[i]; |
| 687 | pail_init = true; |
| 688 | } |
| 689 | } |
| 690 | if (pail_init) |
| 691 | { |
| 692 | if (result_init) |
| 693 | add(result, pail); |
| 694 | else |
| 695 | { |
| 696 | result = pail; |
| 697 | result_init = true; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | rct::key res; |
| 704 | ge_p3_tobytes(res.bytes, &result); |
| 705 | return res; |
| 706 | } |
| 707 | |
| 708 | } |