|
Botan 3.11.0
Crypto and TLS for C&
|
#include <pcurves_impl.h>
Public Types | |
| using | Self = IntMod<Rep> |
Public Member Functions | |
| constexpr void | _const_time_poison () const |
| constexpr void | _const_time_unpoison () const |
| constexpr void | conditional_assign (CT::Choice cond, const Self &nx) |
| constexpr Self | correct_sign (CT::Choice even) const |
| Self | div2 () const |
| constexpr | IntMod () |
| IntMod (const Self &other)=default | |
| IntMod (Self &&other)=default | |
| constexpr Self | invert () const |
| constexpr Self | invert_vartime () const |
| constexpr CT::Choice | is_even () const |
| constexpr CT::Choice | is_nonzero () const |
| constexpr CT::Choice | is_one () const |
| constexpr CT::Choice | is_zero () const |
| constexpr BOTAN_FORCE_INLINE Self | mul2 () const |
| Return (*this) multiplied by 2. | |
| constexpr Self | mul3 () const |
| Return (*this) multiplied by 3. | |
| constexpr Self | mul4 () const |
| Return (*this) multiplied by 4. | |
| constexpr Self | mul8 () const |
| Return (*this) multiplied by 8. | |
| constexpr Self | negate () const |
| constexpr CT::Choice | operator!= (const Self &other) const |
| constexpr BOTAN_FORCE_INLINE Self & | operator*= (const Self &other) |
| IntMod & | operator= (const Self &other)=default |
| IntMod & | operator= (Self &&other)=default |
| constexpr CT::Choice | operator== (const Self &other) const |
| constexpr Self | pow_vartime (const std::array< W, N > &exp) const |
| constexpr void | serialize_to (std::span< uint8_t, Self::BYTES > bytes) const |
| constexpr CT::Option< Self > | sqrt () const |
| constexpr BOTAN_FORCE_INLINE Self | square () const |
| constexpr void | square_n (size_t n) |
| template<size_t L> | |
| std::array< W, L > | stash_value () const |
| constexpr std::array< W, Self::N > | to_words () const |
| ~IntMod ()=default | |
Static Public Member Functions | |
| static constexpr void | _invert_vartime_div2_helper (Self &a, Self &x) |
| static constexpr Self | choose (CT::Choice choice, const Self &x, const Self &y) |
| static constexpr void | conditional_assign (Self &x, Self &y, CT::Choice cond, const Self &nx, const Self &ny) |
| static constexpr void | conditional_assign (Self &x, Self &y, Self &z, CT::Choice cond, const Self &nx, const Self &ny, const Self &nz) |
| static constexpr void | conditional_swap (CT::Choice cond, Self &x, Self &y) |
| static consteval Self | constant (int8_t x) |
| static std::optional< Self > | deserialize (std::span< const uint8_t > bytes) |
| template<size_t L> | |
| static Self | from_stash (const std::array< W, L > &stash) |
| template<size_t L> | |
| static constexpr Self | from_wide_bytes (std::span< const uint8_t, L > bytes) |
| static constexpr std::optional< Self > | from_wide_bytes_varlen (std::span< const uint8_t > bytes) |
| template<size_t L> | |
| static constexpr Self | from_words (std::array< W, L > w) |
| static constexpr Self | one () |
| static Self | random (RandomNumberGenerator &rng) |
| static constexpr Self | zero () |
Static Public Attributes | |
| static constexpr size_t | BITS = count_bits(P) |
| static constexpr size_t | BYTES = (BITS + 7) / 8 |
| static constexpr auto | P_MOD_4 = P[0] % 4 |
Friends | |
| constexpr BOTAN_FORCE_INLINE Self | operator* (const Self &a, const Self &b) |
| constexpr BOTAN_FORCE_INLINE Self | operator+ (const Self &a, const Self &b) |
| constexpr BOTAN_FORCE_INLINE Self | operator- (const Self &a, const Self &b) |
Integers Modulo (a Prime)
This is used to store and manipulate integers modulo the field (for the affine x/y or Jacobian x/y/z coordinates) and group order (for scalar arithmetic).
This class is parameterized by Rep which handles the modular reduction step, as well (if required) any conversions into or out of the inner representation. This is primarily for Montgomery arithmetic; specialized reduction methods instead keep the integer in the "standard" form.
Most of the code in this class does work for arbitrary moduli. However at least div2 and invert make assumptions that the modulus is prime.
Any function that does not contain "vartime" or equivalent in the name is written such that it does not leak information about its arguments via control flow or memory access patterns.
Definition at line 163 of file pcurves_impl.h.
| using Botan::IntMod< Rep >::Self = IntMod<Rep> |
Definition at line 177 of file pcurves_impl.h.
|
inlineconstexpr |
Definition at line 180 of file pcurves_impl.h.
|
default |
|
default |
|
default |
|
inlineconstexpr |
Definition at line 889 of file pcurves_impl.h.
|
inlineconstexpr |
Definition at line 891 of file pcurves_impl.h.
|
inlinestaticconstexpr |
Helper for variable time BEEA
Note this function assumes that its arguments are in the standard domain, not the Montgomery domain. invert_vartime converts its argument out of Montgomery, and then back to Montgomery when returning the result.
Definition at line 547 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::invert_vartime().
|
inlinestaticconstexpr |
Return x or y depending on if choice is set or not
Definition at line 256 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::correct_sign().
|
inlineconstexpr |
Conditional assignment
If cond is true, sets *this to nx
Definition at line 367 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::choose().
|
inlinestaticconstexpr |
Conditional assignment
If cond is true, sets x to nx and y to ny
Definition at line 380 of file pcurves_impl.h.
|
inlinestaticconstexpr |
Conditional assignment
If cond is true, sets x to nx, y to ny, and z to nz
Definition at line 394 of file pcurves_impl.h.
|
inlinestaticconstexpr |
Conditional swap
If cond is true, swaps the values of x and y
Definition at line 410 of file pcurves_impl.h.
|
inlinestaticconsteval |
Create a small compile time constant
Notice this function is consteval, and so can only be called at compile time
Definition at line 882 of file pcurves_impl.h.
|
inlineconstexpr |
Return either this or -this depending on which is even
Definition at line 248 of file pcurves_impl.h.
|
inlinestatic |
Deserialize an integer from a bytestring
Returns nullopt if the input is an encoding greater than or equal P
This function also requires that the bytestring be exactly of the expected length; short bytestrings, or a long bytestring with leading zero bytes, are also rejected.
Definition at line 792 of file pcurves_impl.h.
|
inline |
Return the value of this divided by 2
Definition at line 302 of file pcurves_impl.h.
|
inlinestatic |
Restore the value previously stashed
See pcurves_wrap.h for why/where this is used
Definition at line 774 of file pcurves_impl.h.
|
inlinestaticconstexpr |
Modular reduce a larger input
This takes a bytestring that is at most twice the length of the modulus, and modular reduces it.
Definition at line 816 of file pcurves_impl.h.
|
inlinestaticconstexpr |
Modular reduce a larger input
This takes a bytestring that is at most twice the length of the modulus, and modular reduces it.
Definition at line 829 of file pcurves_impl.h.
|
inlinestaticconstexpr |
Consume an array of words and convert it to an IntMod
This handles the Montgomery conversion, if required.
Note that this function assumes that w represents an integer that is less than the modulus.
Definition at line 211 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::constant(), and Botan::IntMod< MontgomeryRep< ScalarParams > >::deserialize().
|
inlineconstexpr |
Returns the modular inverse, or 0 if no modular inverse exists.
If the modulus is prime the only value that has no modular inverse is 0.
This uses Fermat's little theorem, and so assumes that p is prime
Since P is public, P-2 is as well, thus using a variable time modular exponentiation routine is safe.
This function is only used if the curve does not provide an addition chain for specific inversions (see for example pcurves_secp256r1.cpp)
Definition at line 538 of file pcurves_impl.h.
|
inlineconstexpr |
Returns the modular inverse, or 0 if no modular inverse exists.
This function assumes that the modulus is prime
This function does something a bit nasty and converts from the normal representation (for scalars, Montgomery) into the "standard" representation. This relies on the fact that we aren't doing any multiplications within this function, just additions, subtractions, division by 2, and comparisons.
The reason is there is no good way to compare integers in the Montgomery domain; we could convert out for each comparison but this is slower than just doing a constant-time inversion.
This is loosely based on the algorithm BoringSSL uses in BN_mod_inverse_odd, which is a variant of the Binary Extended Euclidean algorithm. It is optimized somewhat by taking advantage of a couple of observations.
In the first two iterations, the control flow is known because a is less than the modulus and not zero, and we know that the modulus is odd. So we peel out those iterations. This also avoids having to initialize a with the modulus, because we instead set it directly to what the first loop iteration would have updated it to. This ensures that all values are always less than or equal to the modulus.
Then we take advantage of the fact that in each iteration of the loop, at the end we update either b/x or a/y, but never both. In the next iteration of the loop, we attempt to modify b/x or a/y depending on the low zero bits of b or a. But if a or b were not updated in the previous iteration than they will still be odd, and nothing will happen. Instead update just the pair we need to update, right after writing to b/x or a/y resp.
Definition at line 598 of file pcurves_impl.h.
|
inlineconstexpr |
Check in constant time if this is an even integer
Definition at line 240 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::correct_sign().
|
inlineconstexpr |
|
inlineconstexpr |
|
inlineconstexpr |
Check in constant time if this is equal to zero
Definition at line 225 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::invert_vartime(), and Botan::IntMod< MontgomeryRep< ScalarParams > >::is_nonzero().
|
inlineconstexpr |
Return (*this) multiplied by 2.
Definition at line 325 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::mul3(), Botan::IntMod< MontgomeryRep< ScalarParams > >::mul4(), and Botan::IntMod< MontgomeryRep< ScalarParams > >::mul8().
|
inlineconstexpr |
Return (*this) multiplied by 3.
Definition at line 335 of file pcurves_impl.h.
|
inlineconstexpr |
Return (*this) multiplied by 4.
Definition at line 338 of file pcurves_impl.h.
|
inlineconstexpr |
Return (*this) multiplied by 8.
Definition at line 341 of file pcurves_impl.h.
|
inlineconstexpr |
Modular negation
Returns the additive inverse of (*this)
Definition at line 452 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::correct_sign(), and Botan::IntMod< MontgomeryRep< ScalarParams > >::invert_vartime().
|
inlinestaticconstexpr |
Return integer one
Definition at line 200 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::pow_vartime().
|
inlineconstexpr |
|
inlineconstexpr |
Modular multiplication; set this to this * other
Definition at line 355 of file pcurves_impl.h.
|
default |
|
default |
|
inlineconstexpr |
Constant time integer equality test
Since both this and other are in Montgomery representation (if applicable), we can always compare the words directly, without having to convert out.
Definition at line 722 of file pcurves_impl.h.
|
inlineconstexpr |
Modular Exponentiation (Variable Time)
This function is variable time with respect to the exponent. It should only be used when exp is not secret. In the current code, exp is always a compile-time constant.
This function should not leak any information about this, since the value being operated on may be a secret.
TODO: this interface should be changed so that the exponent is always a compile-time constant; this should allow some interesting optimizations.
Definition at line 477 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::invert(), and Botan::IntMod< MontgomeryRep< ScalarParams > >::sqrt().
|
inlinestatic |
Return a random integer value in [1,p)
This uses rejection sampling. This could have alternatively been implemented by oversampling the random number generator and then performing a wide reduction. The main reason that approach is avoided here is because it makes testing ECDSA-style known answer tests more difficult.
This function avoids returning zero since in almost all contexts where a random integer is desired we want a random integer in Z_p*
Definition at line 851 of file pcurves_impl.h.
|
inlineconstexpr |
Serialize the integer to a bytestring
Definition at line 739 of file pcurves_impl.h.
|
inlineconstexpr |
Return the modular square root if it exists
The CT::Option will be unset if the square root does not exist
Definition at line 663 of file pcurves_impl.h.
|
inlineconstexpr |
Modular squaring
Returns the square of this after modular reduction
Definition at line 426 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::sqrt(), and Botan::EllipticCurve< Params, FieldRep >::x3_ax_b().
|
inlineconstexpr |
Repeated modular squaring
Returns the nth square of this
(Alternate view, returns this raised to the 2^nth power)
Definition at line 439 of file pcurves_impl.h.
|
inline |
Store the raw words to an array
See pcurves_wrap.h for why/where this is used
Definition at line 759 of file pcurves_impl.h.
|
inlineconstexpr |
Convert the integer to standard representation and return the sequence of words
Definition at line 734 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::invert_vartime().
|
inlinestaticconstexpr |
Return integer zero
Note this assumes that the representation of zero is an all zero sequence of words. This is true for both Montgomery and standard representations.
Definition at line 195 of file pcurves_impl.h.
Referenced by Botan::IntMod< MontgomeryRep< ScalarParams > >::invert_vartime().
|
friend |
|
friend |
Modular addition; return c = a + b
Definition at line 265 of file pcurves_impl.h.
|
friend |
Modular subtraction; return c = a - b
Definition at line 281 of file pcurves_impl.h.
|
staticconstexpr |
Definition at line 172 of file pcurves_impl.h.
|
staticconstexpr |
Definition at line 173 of file pcurves_impl.h.
|
staticconstexpr |
Definition at line 175 of file pcurves_impl.h.