diff options
author | Jack Lloyd <[email protected]> | 2018-03-14 21:17:06 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-03-14 21:17:06 -0400 |
commit | 84f54b95b2698d71f4f1e6dc333812694bab3903 (patch) | |
tree | c46b80242e56d10e5cb4b7d0aecda477d3588e17 /src/lib/math | |
parent | 792a2bebf8fd1a4b5813680131267b77d06f6b98 (diff) |
Some additional operations on Montgomery_Int
Needed for #1432
Diffstat (limited to 'src/lib/math')
-rw-r--r-- | src/lib/math/numbertheory/monty.cpp | 32 | ||||
-rw-r--r-- | src/lib/math/numbertheory/monty.h | 13 |
2 files changed, 45 insertions, 0 deletions
diff --git a/src/lib/math/numbertheory/monty.cpp b/src/lib/math/numbertheory/monty.cpp index 503141ada..41c01abb1 100644 --- a/src/lib/math/numbertheory/monty.cpp +++ b/src/lib/math/numbertheory/monty.cpp @@ -27,6 +27,25 @@ Montgomery_Params::Montgomery_Params(const BigInt& p, m_r3 = mod_p.multiply(m_r1, m_r2); } +Montgomery_Params::Montgomery_Params(const BigInt& p) + { + + if(p.is_negative() || p.is_even()) + throw Invalid_Argument("Montgomery_Params invalid modulus"); + + m_p = p; + m_p_words = m_p.sig_words(); + m_p_dash = monty_inverse(m_p.word_at(0)); + + const BigInt r = BigInt::power_of_2(m_p_words * BOTAN_MP_WORD_BITS); + + Modular_Reducer mod_p(p); + + m_r1 = mod_p.reduce(r); + m_r2 = mod_p.square(m_r1); + m_r3 = mod_p.multiply(m_r1, m_r2); + } + BigInt Montgomery_Params::inv_mod_p(const BigInt& x) const { return ct_inverse_mod_odd_modulus(x, p()); @@ -182,6 +201,19 @@ Montgomery_Int::Montgomery_Int(const std::shared_ptr<const Montgomery_Params> pa } } +Montgomery_Int::Montgomery_Int(std::shared_ptr<const Montgomery_Params> params, + const uint8_t bits[], size_t len, + bool redc_needed) : + m_params(params), + m_v(bits, len) + { + if(redc_needed) + { + secure_vector<word> ws; + m_v = m_params->mul(m_v % m_params->p(), m_params->R2(), ws); + } + } + void Montgomery_Int::fix_size() { const size_t p_words = m_params->p_words(); diff --git a/src/lib/math/numbertheory/monty.h b/src/lib/math/numbertheory/monty.h index 137e0b967..499a4ac91 100644 --- a/src/lib/math/numbertheory/monty.h +++ b/src/lib/math/numbertheory/monty.h @@ -33,6 +33,13 @@ class Montgomery_Int final const BigInt& v, bool redc_needed = true); + /** + * Create a Montgomery_Int + */ + Montgomery_Int(std::shared_ptr<const Montgomery_Params> params, + const uint8_t bits[], size_t len, + bool redc_needed = true); + bool operator==(const Montgomery_Int& other) const; bool operator!=(const Montgomery_Int& other) const { return (m_v != other.m_v); } @@ -107,6 +114,12 @@ class Montgomery_Params final */ Montgomery_Params(const BigInt& p, const Modular_Reducer& mod_p); + /** + * Initialize a set of Montgomery reduction parameters. These values + * can be shared by all values in a specific Montgomery domain. + */ + Montgomery_Params(const BigInt& p); + const BigInt& p() const { return m_p; } const BigInt& R1() const { return m_r1; } const BigInt& R2() const { return m_r2; } |