Modular arithmetic in ring
.
More...
#include "c4e_bigint.h"
Go to the source code of this file.
Defines | |
| #define | C4E_MODN_POW_SPACE(msize) |
| Size calculation macro for modular exponentiation parameters size (in units of C4eArchDigit). | |
Functions | |
| void | c4e_modn_pow (C4eElement *C4E_RESTRICT a, C4E_CONST C4eElement *C4E_RESTRICT n, C4eElement *C4E_RESTRICT m, C4eElement *C4E_RESTRICT tmp, C4eElement *c) |
Power in , means . | |
| void | c4e_modn_crt (C4eElemTriple *C4E_RESTRICT p, C4eElemTriple *C4E_RESTRICT q) |
| Chinese Remainder Theorem (CRT). | |
Modular arithmetic in ring
.
Definition in file c4e_modn.h.
| #define C4E_MODN_POW_SPACE | ( | msize | ) |
Size calculation macro for modular exponentiation parameters size (in units of C4eArchDigit).
| [in] | msize | Size of modulus in units of C4eArchDigit. |
Definition at line 47 of file c4e_modn.h.
| void c4e_modn_pow | ( | C4eElement *C4E_RESTRICT | a, | |
| C4E_CONST C4eElement *C4E_RESTRICT | n, | |||
| C4eElement *C4E_RESTRICT | m, | |||
| C4eElement *C4E_RESTRICT | tmp, | |||
| C4eElement * | c | |||
| ) |
Power
in
, means
.
The implemented left-to-right algorithm is based on the binary representation of exponent
, which gives:
m is odd, it uses Montgomery's algorithm.
, if
also designates the size of modulus m.m is temporary modified - so it must be writable. After function return it holds the same values as at function entry.a and n are zero. The caller has to ensure this, may be using macro C4E_ELEM_IS_ZERO(). a is (m->size + 1). The space for tmp->digits and c->digits is C4E_MODN_POW_SPACE(m->size), but only if m is odd (Montgomery algorithm). In case m is even, the required digits space for tmp->digits and c->digits is (2U * m->size) digits. m and a must be normalized, for example using function c4e_elem_norm(). a must be reduced to m on entry. Use for example function c4e_bigint_mod() to ensure this.| [in] | a | Big number a in . Notice that number a is destroyed. |
| [in] | n | Exponent n in . |
| [in] | m | Modulus (greater than zero), temporary modified. |
| tmp | Temporary space needed for intermediate results. | |
| [out] | c | Result c in , normalized. |
| void c4e_modn_crt | ( | C4eElemTriple *C4E_RESTRICT | p, | |
| C4eElemTriple *C4E_RESTRICT | q | |||
| ) |
Chinese Remainder Theorem (CRT).
The Chinese Remainder Theorem can be used to solve linear congruences of the form:
under the precondition that p and q are coprime (
). Typically the CRT is given in the literature by the following formula:
which is closely related to the extended GCD algorithm and Bezout's theorem:
. The implemented algorithm uses a variant introduced by H.L. Garner:
Notice that in this form the Bezout cofactor
is not needed - so q->y is used (internally) for intermediate calculations.
is negative the algorithm adds
once to the result, which so becomes positive. Therefore result
is always positive and less than
. In consequence the members p->attr and q->attr have no meaning as return parameters.p->x resp. q->x must be less than the associated modulus p->z resp. q->z. p and q, that are big numbers must be normalized, for example using function c4e_elem_norm(). p->z and q->z are relatively prime, else the algorithm does produce a wrong result. C4E_ARCH_DIGIT_SIZE):p->z.size + q->z.size + 1 for p->x and q->x;q->z.size for q->x, if that is greater than p->z.size + q->z.size + 1;p->x.size for q->y.| [in,out] | p | Input parameters of first (modular) congruence resp. first summand in Bezout's theorem and return value :
|
| [in,out] | q | Input parameters of second (modular) congruence resp. first summand in Bezout's theorem and return value :
|
1.6.1