Debian Codegraph @

src/Arithmetic/MontgomeryReduction/Definition.v

raw
1 (*** Montgomery Multiplication *)
2 (** This file implements Montgomery Form, Montgomery Reduction, and
3 Montgomery Multiplication on [Z]. We follow Wikipedia. *)
4 From Coq Require Import ZArith.
5 Require Import Crypto.Util.ZUtil.EquivModulo.
6 Require Import Crypto.Util.Notations.
7
8 Local Open Scope Z_scope.
9 Declare Scope montgomery_naive_scope.
10 Declare Scope montgomery_scope.
11 Delimit Scope montgomery_naive_scope with montgomery_naive.
12 Delimit Scope montgomery_scope with montgomery.
13 Definition montgomeryZ := Z.
14 Bind Scope montgomery_scope with montgomeryZ.
15
16 Section montgomery.
17 Context (N : Z)
18 (R : Z)
19 (R' : Z). (* R' is R⁻¹ mod N *)
20 Local Notation "x ≡ y" := (Z.equiv_modulo N x y) : type_scope.
21 Local Notation "x ≡ᵣ y" := (Z.equiv_modulo R x y) : type_scope.
22 (** Quoting Wikipedia <https://en.wikipedia.org/wiki/Montgomery_modular_multiplication>: *)
23 (** In modular arithmetic computation, Montgomery modular
24 multiplication, more commonly referred to as Montgomery
25 multiplication, is a method for performing fast modular
26 multiplication, introduced in 1985 by the American mathematician
27 Peter L. Montgomery. *)
28 (** Given two integers [a] and [b], the classical modular
29 multiplication algorithm computes [ab mod N]. Montgomery
30 multiplication works by transforming [a] and [b] into a special
31 representation known as Montgomery form. For a modulus [N], the
32 Montgomery form of [a] is defined to be [aR mod N] for some
33 constant [R] depending only on [N] and the underlying computer
34 architecture. If [aR mod N] and [bR mod N] are the Montgomery
35 forms of [a] and [b], then their Montgomery product is [abR mod
36 N]. Montgomery multiplication is a fast algorithm to compute the
37 Montgomery product. Transforming the result out of Montgomery
38 form yields the classical modular product [ab mod N]. *)
39
40 Definition to_montgomery_naive (x : Z) : montgomeryZ := x * R.
41 Definition from_montgomery_naive (x : montgomeryZ) : Z := x * R'.
42
43 (** * Modular arithmetic and Montgomery form *)
44 Section general.
45 (** Addition and subtraction in Montgomery form are the same as
46 ordinary modular addition and subtraction because of the
47 distributive law: *)
48 (** [aR + bR = (a+b)R], *)
49 (** [aR - bR = (a-b)R]. *)
50 (** This is a consequence of the fact that, because [gcd(R, N) =
51 1], multiplication by [R] is an isomorphism on the additive
52 group [ℤ/Nℤ]. *)
53
54 Definition add : montgomeryZ -> montgomeryZ -> montgomeryZ := fun aR bR => aR + bR.
55 Definition sub : montgomeryZ -> montgomeryZ -> montgomeryZ := fun aR bR => aR - bR.
56
57 (** Multiplication in Montgomery form, however, is seemingly more
58 complicated. The usual product of [aR] and [bR] does not
59 represent the product of [a] and [b] because it has an extra
60 factor of R: *)
61 (** [(aR mod N)(bR mod N) mod N = (abR)R mod N]. *)
62 (** Computing products in Montgomery form requires removing the
63 extra factor of [R]. While division by [R] is cheap, the
64 intermediate product [(aR mod N)(bR mod N)] is not divisible
65 by [R] because the modulo operation has destroyed that
66 property. *)
67 (** Removing the extra factor of R can be done by multiplying by
68 an integer [R′] such that [RR' ≡ 1 (mod N)], that is, by an
69 [R′] whose residue class is the modular inverse of [R] mod
70 [N]. Then, working modulo [N], *)
71 (** [(aR mod N)(bR mod N)R' ≡ (aR)(bR)R⁻¹ ≡ (ab)R (mod N)]. *)
72
73 Definition mul_naive : montgomeryZ -> montgomeryZ -> montgomeryZ
74 := fun aR bR => aR * bR * R'.
75 End general.
76
77 (** * The REDC algorithm *)
78 Section redc.
79 (** While the above algorithm is correct, it is slower than
80 multiplication in the standard representation because of the
81 need to multiply by [R′] and divide by [N]. Montgomery
82 reduction, also known as REDC, is an algorithm that
83 simultaneously computes the product by [R′] and reduces modulo
84 [N] more quickly than the naive method. The speed is because
85 all computations are done using only reduction and divisions
86 with respect to [R], not [N]: *)
87 (**
88 <<
89 function REDC is
90 input: Integers R and N with gcd(R, N) = 1,
91 Integer N′ in [0, R − 1] such that NN′ ≡ −1 mod R,
92 Integer T in the range [0, RN − 1]
93 output: Integer S in the range [0, N − 1] such that S ≡ TR⁻¹ mod N
94
95 m ← ((T mod R)N′) mod R
96 t ← (T + mN) / R
97 if t ≥ N then
98 return t − N
99 else
100 return t
101 end if
102 end function
103 >> *)
104 Context (N' : Z). (* N' is (-N⁻¹) mod R *)
105 Section redc.
106 Context (T : Z).
107
108 Let m := ((T mod R) * N') mod R.
109 Let t := (T + m * N) / R.
110 Definition prereduce : montgomeryZ := t.
111
112 Definition partial_reduce : montgomeryZ
113 := if R <=? t then
114 prereduce - N
115 else
116 prereduce.
117
118 Definition partial_reduce_alt : montgomeryZ
119 := let v0 := (T + m * N) in
120 let v := (v0 mod (R * R)) / R in
121 if R * R <=? v0 then
122 (v - N) mod R
123 else
124 v.
125
126 Definition reduce : montgomeryZ
127 := if N <=? t then
128 prereduce - N
129 else
130 prereduce.
131
132 Definition reduce_via_partial : montgomeryZ
133 := if N <=? partial_reduce then
134 partial_reduce - N
135 else
136 partial_reduce.
137
138 Definition reduce_via_partial_alt : montgomeryZ
139 := if N <=? partial_reduce_alt then
140 partial_reduce_alt - N
141 else
142 partial_reduce_alt.
143 End redc.
144
145 (** * Arithmetic in Montgomery form *)
146 Section arithmetic.
147 (** Many operations of interest modulo [N] can be expressed
148 equally well in Montgomery form. Addition, subtraction,
149 negation, comparison for equality, multiplication by an
150 integer not in Montgomery form, and greatest common divisors
151 with [N] may all be done with the standard algorithms. *)
152 (** When [R > N], most other arithmetic operations can be
153 expressed in terms of REDC. This assumption implies that the
154 product of two representatives [mod N] is less than [RN],
155 the exact hypothesis necessary for REDC to generate correct
156 output. In particular, the product of [aR mod N] and [bR mod
157 N] is [REDC((aR mod N)(bR mod N))]. The combined operation
158 of multiplication and REDC is often called Montgomery
159 multiplication. *)
160 Definition mul : montgomeryZ -> montgomeryZ -> montgomeryZ
161 := fun aR bR => reduce (aR * bR).
162
163 (** Conversion into Montgomery form is done by computing
164 [REDC((a mod N)(R² mod N))]. Conversion out of Montgomery
165 form is done by computing [REDC(aR mod N)]. The modular
166 inverse of [aR mod N] is [REDC((aR mod N)⁻¹(R³ mod
167 N))]. Modular exponentiation can be done using
168 exponentiation by squaring by initializing the initial
169 product to the Montgomery representation of 1, that is, to
170 [R mod N], and by replacing the multiply and square steps by
171 Montgomery multiplies. *)
172 Definition to_montgomery (a : Z) : montgomeryZ := reduce (a * (R*R mod N)).
173 Definition from_montgomery (aR : montgomeryZ) : Z := reduce aR.
174 End arithmetic.
175 End redc.
176 End montgomery.
177
178 Infix "+" := add : montgomery_scope.
179 Infix "-" := sub : montgomery_scope.
180 Infix "*" := mul_naive : montgomery_naive_scope.
181 Infix "*" := mul : montgomery_scope.