8#include <botan/internal/curve_nistp.h>
10#include <botan/internal/ct_utils.h>
11#include <botan/internal/mp_core.h>
17 "0x1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
18 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
28 const size_t p_words = p_full_words + 1;
31 "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
32 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
34 if(ws.size() < p_words + 1) {
35 ws.resize(p_words + 1);
48 const word top_word = x.
word_at(p_full_words);
60 for(
size_t i = 0; i != p_full_words; ++i) {
65 const auto is_p521 = all_512_low_bits_set & has_p521_top_word;
67 const auto needs_reduction = is_p521 | bit_522_set;
78constexpr uint32_t
get_uint32(
const word xw[],
size_t i) {
79#if(BOTAN_MP_WORD_BITS == 32)
82 return static_cast<uint32_t
>(xw[i / 2] >> ((i % 2) * 32));
86inline void set_words(word x[],
size_t i, uint32_t R0, uint32_t R1) {
87#if(BOTAN_MP_WORD_BITS == 32)
91 x[i / 2] = (
static_cast<uint64_t
>(R1) << 32) | R0;
98 static const BigInt p192(
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF");
125 const uint64_t S0 = X00 + X06 + X10;
126 const uint64_t S1 = X01 + X07 + X11;
127 const uint64_t S2 = X02 + X06 + X08 + X10;
128 const uint64_t S3 = X03 + X07 + X09 + X11;
129 const uint64_t S4 = X04 + X08 + X10;
130 const uint64_t S5 = X05 + X09 + X11;
133 uint32_t R0 = 0, R1 = 0;
136 R0 =
static_cast<uint32_t
>(S);
140 R1 =
static_cast<uint32_t
>(S);
143 set_words(xw, 0, R0, R1);
146 R0 =
static_cast<uint32_t
>(S);
150 R1 =
static_cast<uint32_t
>(S);
153 set_words(xw, 2, R0, R1);
156 R0 =
static_cast<uint32_t
>(S);
160 R1 =
static_cast<uint32_t
>(S);
163 set_words(xw, 4, R0, R1);
170 static const constinit std::array<word, p192_limbs> p192_mults[3] = {
187 static const BigInt p224(
"0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001");
218 const int64_t S0 = 0x00000001 + X00 - X07 - X11;
219 const int64_t S1 = 0x00000000 + X01 - X08 - X12;
220 const int64_t S2 = 0x00000000 + X02 - X09 - X13;
221 const int64_t S3 = 0xFFFFFFFF + X03 + X07 + X11 - X10;
222 const int64_t S4 = 0xFFFFFFFF + X04 + X08 + X12 - X11;
223 const int64_t S5 = 0xFFFFFFFF + X05 + X09 + X13 - X12;
224 const int64_t S6 = 0xFFFFFFFF + X06 + X10 - X13;
227 uint32_t R0 = 0, R1 = 0;
230 R0 =
static_cast<uint32_t
>(S);
234 R1 =
static_cast<uint32_t
>(S);
237 set_words(xw, 0, R0, R1);
240 R0 =
static_cast<uint32_t
>(S);
244 R1 =
static_cast<uint32_t
>(S);
247 set_words(xw, 2, R0, R1);
250 R0 =
static_cast<uint32_t
>(S);
254 R1 =
static_cast<uint32_t
>(S);
257 set_words(xw, 4, R0, R1);
260 R0 =
static_cast<uint32_t
>(S);
263 set_words(xw, 6, R0, 0);
265 static const constinit std::array<word, p224_limbs> p224_mults[3] = {
282 static const BigInt p256(
"0xFFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF");
314 const int64_t S0 = 0xFFFFFFFA + X00 + X08 + X09 - (X11 + X12 + X13) - X14;
315 const int64_t S1 = 0xFFFFFFFF + X01 + X09 + X10 - X12 - (X13 + X14 + X15);
316 const int64_t S2 = 0xFFFFFFFF + X02 + X10 + X11 - (X13 + X14 + X15);
317 const int64_t S3 = 0x00000005 + X03 + (X11 + X12) * 2 + X13 - X15 - X08 - X09;
318 const int64_t S4 = 0x00000000 + X04 + (X12 + X13) * 2 + X14 - X09 - X10;
319 const int64_t S5 = 0x00000000 + X05 + (X13 + X14) * 2 + X15 - X10 - X11;
320 const int64_t S6 = 0x00000006 + X06 + X13 + X14 * 3 + X15 * 2 - X08 - X09;
321 const int64_t S7 = 0xFFFFFFFA + X07 + X15 * 3 + X08 - X10 - (X11 + X12 + X13);
325 uint32_t R0 = 0, R1 = 0;
328 R0 =
static_cast<uint32_t
>(S);
332 R1 =
static_cast<uint32_t
>(S);
335 set_words(xw, 0, R0, R1);
338 R0 =
static_cast<uint32_t
>(S);
342 R1 =
static_cast<uint32_t
>(S);
345 set_words(xw, 2, R0, R1);
348 R0 =
static_cast<uint32_t
>(S);
352 R1 =
static_cast<uint32_t
>(S);
355 set_words(xw, 4, R0, R1);
358 R0 =
static_cast<uint32_t
>(S);
362 R1 =
static_cast<uint32_t
>(S);
364 set_words(xw, 6, R0, R1);
371 static const constinit std::array<word, p256_limbs> p256_mults[11] = {
372 hex_to_words<word>(
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"),
373 hex_to_words<word>(
"FFFFFFFE00000002000000000000000000000001FFFFFFFFFFFFFFFFFFFFFFFE"),
374 hex_to_words<word>(
"FFFFFFFD00000003000000000000000000000002FFFFFFFFFFFFFFFFFFFFFFFD"),
375 hex_to_words<word>(
"FFFFFFFC00000004000000000000000000000003FFFFFFFFFFFFFFFFFFFFFFFC"),
376 hex_to_words<word>(
"FFFFFFFB00000005000000000000000000000004FFFFFFFFFFFFFFFFFFFFFFFB"),
377 hex_to_words<word>(
"FFFFFFFA00000006000000000000000000000005FFFFFFFFFFFFFFFFFFFFFFFA"),
378 hex_to_words<word>(
"FFFFFFF900000007000000000000000000000006FFFFFFFFFFFFFFFFFFFFFFF9"),
379 hex_to_words<word>(
"FFFFFFF800000008000000000000000000000007FFFFFFFFFFFFFFFFFFFFFFF8"),
380 hex_to_words<word>(
"FFFFFFF700000009000000000000000000000008FFFFFFFFFFFFFFFFFFFFFFF7"),
381 hex_to_words<word>(
"FFFFFFF60000000A000000000000000000000009FFFFFFFFFFFFFFFFFFFFFFF6"),
382 hex_to_words<word>(
"FFFFFFF50000000B00000000000000000000000AFFFFFFFFFFFFFFFFFFFFFFF5"),
397 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF");
437 const int64_t S0 = 0xFFFFFFFF + X00 + X12 + X20 + X21 - X23;
438 const int64_t S1 = 0x00000000 + X01 + X13 + X22 + X23 - X12 - X20;
439 const int64_t S2 = 0x00000000 + X02 + X14 + X23 - X13 - X21;
440 const int64_t S3 = 0xFFFFFFFF + X03 + X12 + X15 + X20 + X21 - X14 - X22 - X23;
441 const int64_t S4 = 0xFFFFFFFE + X04 + X12 + X13 + X16 + X20 + X21 * 2 + X22 - X15 - X23 * 2;
442 const int64_t S5 = 0xFFFFFFFF + X05 + X13 + X14 + X17 + X21 + X22 * 2 + X23 - X16;
443 const int64_t S6 = 0xFFFFFFFF + X06 + X14 + X15 + X18 + X22 + X23 * 2 - X17;
444 const int64_t S7 = 0xFFFFFFFF + X07 + X15 + X16 + X19 + X23 - X18;
445 const int64_t S8 = 0xFFFFFFFF + X08 + X16 + X17 + X20 - X19;
446 const int64_t S9 = 0xFFFFFFFF + X09 + X17 + X18 + X21 - X20;
447 const int64_t SA = 0xFFFFFFFF + X10 + X18 + X19 + X22 - X21;
448 const int64_t SB = 0xFFFFFFFF + X11 + X19 + X20 + X23 - X22;
452 uint32_t R0 = 0, R1 = 0;
455 R0 =
static_cast<uint32_t
>(S);
459 R1 =
static_cast<uint32_t
>(S);
462 set_words(xw, 0, R0, R1);
465 R0 =
static_cast<uint32_t
>(S);
469 R1 =
static_cast<uint32_t
>(S);
472 set_words(xw, 2, R0, R1);
475 R0 =
static_cast<uint32_t
>(S);
479 R1 =
static_cast<uint32_t
>(S);
482 set_words(xw, 4, R0, R1);
485 R0 =
static_cast<uint32_t
>(S);
489 R1 =
static_cast<uint32_t
>(S);
492 set_words(xw, 6, R0, R1);
495 R0 =
static_cast<uint32_t
>(S);
499 R1 =
static_cast<uint32_t
>(S);
502 set_words(xw, 8, R0, R1);
505 R0 =
static_cast<uint32_t
>(S);
509 R1 =
static_cast<uint32_t
>(S);
512 set_words(xw, 10, R0, R1);
517 static const constinit std::array<word, p384_limbs> p384_mults[5] = {
519 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"),
521 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFDFFFFFFFE0000000000000001FFFFFFFE"),
523 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFCFFFFFFFD0000000000000002FFFFFFFD"),
525 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBFFFFFFFC0000000000000003FFFFFFFC"),
527 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFAFFFFFFFB0000000000000004FFFFFFFB"),
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
#define BOTAN_ASSERT(expr, assertion_made)
void grow_to(size_t n) const
word word_at(size_t n) const
const word * _data() const
static constexpr Mask< T > expand(T v)
static constexpr Mask< T > is_equal(T x, T y)
#define BOTAN_MP_WORD_BITS
constexpr void unpoison(const T *p, size_t n)
BOTAN_TEST_API void redc_p521(BigInt &x, secure_vector< word > &ws)
constexpr void bigint_shr2(W y[], const W x[], size_t x_size, size_t shift)
BOTAN_TEST_API void redc_p192(BigInt &x, secure_vector< word > &ws)
BOTAN_TEST_API void redc_p256(BigInt &x, secure_vector< word > &ws)
BOTAN_TEST_API void redc_p224(BigInt &x, secure_vector< word > &ws)
BOTAN_TEST_API const BigInt & prime_p384()
BOTAN_TEST_API const BigInt & prime_p224()
constexpr uint32_t get_uint32(const W xw[], size_t i)
BOTAN_TEST_API void redc_p384(BigInt &x, secure_vector< word > &ws)
constexpr auto bigint_cnd_sub(W cnd, W x[], size_t x_size, const W y[], size_t y_size) -> W
constexpr W bigint_cnd_add(W cnd, W x[], size_t x_size, const W y[], size_t y_size)
void carry(int64_t &h0, int64_t &h1)
std::vector< T, secure_allocator< T > > secure_vector
constexpr auto bigint_sub2(W x[], size_t x_size, const W y[], size_t y_size) -> W
constexpr auto hex_to_words(const char(&s)[N])
BOTAN_TEST_API const BigInt & prime_p256()
BOTAN_TEST_API const BigInt & prime_p192()
BOTAN_TEST_API const BigInt & prime_p521()
constexpr void clear_mem(T *ptr, size_t n)
constexpr auto bigint_add3_nc(W z[], const W x[], size_t x_size, const W y[], size_t y_size) -> W