/src/botan/src/fuzzer/mp_redc_crandall.cpp
Line | Count | Source |
1 | | /* |
2 | | * (C) 2024 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #include "mp_fuzzers.h" |
8 | | |
9 | | #include <botan/bigint.h> |
10 | | #include <botan/internal/loadstor.h> |
11 | | |
12 | 139 | void fuzz(std::span<const uint8_t> in) { |
13 | 139 | if(in.size() != 8 * sizeof(word)) { |
14 | 40 | return; |
15 | 40 | } |
16 | | |
17 | 99 | #if BOTAN_MP_WORD_BITS == 64 |
18 | | // secp256k1 modulus |
19 | 99 | const word C = 0x1000003d1; |
20 | | #else |
21 | | // 128 bit prime with largest possible C |
22 | | const word C = 0xffffffe1; |
23 | | #endif |
24 | | |
25 | 99 | static const Botan::BigInt refp = Botan::BigInt::power_of_2(4 * BOTAN_MP_WORD_BITS) - C; |
26 | 99 | static const Botan::BigInt refp2 = refp * refp; |
27 | | |
28 | 99 | const auto refz = Botan::BigInt::from_bytes(in); |
29 | | |
30 | 99 | if(refz >= refp2) { |
31 | 1 | return; |
32 | 1 | } |
33 | | |
34 | 98 | const auto refc = refz % refp; |
35 | | |
36 | 98 | std::array<word, 8> z = {}; |
37 | 882 | for(size_t i = 0; i != 8; ++i) { |
38 | 784 | z[7 - i] = Botan::load_be<word>(in.subspan(sizeof(word) * i, sizeof(word))); |
39 | 784 | } |
40 | | |
41 | 98 | const auto rc = Botan::redc_crandall<word, 4, C>(z); |
42 | | |
43 | 98 | compare_word_vec(rc.data(), 4, refc._data(), refc.sig_words(), "Crandall reduction"); |
44 | 98 | } |