-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add specialized reduction for secp256k1
This takes advantage of the modulus being near a power of 2. Performance improvement varies by CPU, compiler, and specific algorithm in use, but generally ranges between 10% to 20%.
- Loading branch information
Showing
3 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* (C) 2024 Jack Lloyd | ||
* | ||
* Botan is released under the Simplified BSD License (see license.txt) | ||
*/ | ||
|
||
#include "mp_fuzzers.h" | ||
|
||
#include <botan/bigint.h> | ||
#include <botan/internal/loadstor.h> | ||
|
||
void fuzz(const uint8_t in[], size_t in_len) { | ||
if(in_len != 8 * sizeof(word)) { | ||
return; | ||
} | ||
|
||
#if BOTAN_MP_WORD_BITS == 64 | ||
// secp256k1 modulus | ||
const word C = 0x1000003d1; | ||
#else | ||
// 128 bit prime with largest possible C | ||
const word C = 0xffffffe1; | ||
#endif | ||
|
||
static const Botan::BigInt refp = Botan::BigInt::power_of_2(4 * BOTAN_MP_WORD_BITS) - C; | ||
static const Botan::BigInt refp2 = refp * refp; | ||
|
||
const auto refz = Botan::BigInt::from_bytes(std::span{in, in_len}); | ||
|
||
if(refz >= refp2) { | ||
return; | ||
} | ||
|
||
const auto refc = refz % refp; | ||
|
||
std::array<word, 8> z = {}; | ||
for(size_t i = 0; i != 8; ++i) { | ||
z[7 - i] = Botan::load_be<word>(in, i); | ||
} | ||
|
||
const auto rc = Botan::redc_crandall<word, 4, C>(z); | ||
|
||
compare_word_vec(rc.data(), 4, refc._data(), refc.sig_words(), "Crandall reduction"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters