21
21
22
22
/**
23
23
* Arithmetic in F_p, p = 21888242871839275222246405745257275088696311157297823662689037894645226208583
24
- *
24
+ * This class stores elements of F_p in the Montgomery form: a*r mod p.
25
+ *
25
26
* @author Mikhail Kalinin
26
27
* @since 01.09.2017
27
28
*/
@@ -32,40 +33,42 @@ public class Fp implements Field<Fp> {
32
33
static final BigInteger P = new BigInteger (
33
34
"21888242871839275222246405745257275088696311157297823662689037894645226208583" );
34
35
35
- /*
36
- * 2^256
36
+ /**
37
+ * This value is equal to 2^256. It should be greater than {@link #P} and coprime to it.
38
+ * Field elements are represented in Montgomery form as a*{@link #REDUCER} mod {@link #P}.
39
+ * This specific value of {@link #REDUCER} is selected to facilitate efficient division
40
+ * by {@link #REDUCER} through simple shifting.
37
41
*/
38
42
static final BigInteger REDUCER = new BigInteger (
39
43
"115792089237316195423570985008687907853269984665640564039457584007913129639936" );
40
44
45
+ /**
46
+ * The number of bits in the {@link #REDUCER} value.
47
+ */
41
48
static final int REDUCER_BITS = 256 ;
42
49
43
- /*
44
- * REDUCER^2 mod P
50
+ /**
51
+ * A precomputed value of {@link # REDUCER} ^2 mod {@link #P}.
45
52
*/
46
53
static final BigInteger REDUCER_SQUARED = new BigInteger (
47
54
"3096616502983703923843567936837374451735540968419076528771170197431451843209" );
48
55
49
- /*
50
- * REDUCER^3 mod P
56
+ /**
57
+ * A precomputed value of {@link # REDUCER} ^3 mod {@link #P}.
51
58
*/
52
59
static final BigInteger REDUCER_CUBED = new BigInteger (
53
60
"14921786541159648185948152738563080959093619838510245177710943249661917737183" );
54
61
55
- /*
56
- * REDUCER^(-1) mod P
57
- */
58
- static final BigInteger RECIPROCAL = new BigInteger (
59
- "20988524275117001072002809824448087578619730785600314334253784976379291040311" );
60
-
61
- /*
62
- * -P^(-1) mod REDUCER
62
+ /**
63
+ * A precomputed value of -{@link #P}^{-1} mod {@link #REDUCER}.
63
64
*/
64
65
static final BigInteger FACTOR = new BigInteger (
65
66
"111032442853175714102588374283752698368366046808579839647964533820976443843465" );
66
67
67
- /*
68
- * 2^256 - 1
68
+ /**
69
+ * The MASK value is set to 2^256 - 1 and is utilized to replace the operation % 2^256
70
+ * with a bitwise AND using this value. This choice ensures that only the lower 256 bits
71
+ * of a result are retained, effectively simulating the modulus operation.
69
72
*/
70
73
static final BigInteger MASK = new BigInteger (
71
74
"115792089237316195423570985008687907853269984665640564039457584007913129639935" );
@@ -174,17 +177,35 @@ public String toString() {
174
177
return v .toString ();
175
178
}
176
179
180
+ /**
181
+ * Converts a value in normal representation to Montgomery form.
182
+ *
183
+ * @param n value in normal form
184
+ * @return value in Montgomery form
185
+ */
177
186
private static BigInteger toMontgomery (BigInteger n ) {
178
187
return redc (n .multiply (REDUCER_SQUARED ));
179
188
}
180
189
190
+ /**
191
+ * Converts a value in Montgomery form to a normal representation.
192
+ *
193
+ * @param n value in Montgomery form
194
+ * @return value in normal form
195
+ */
181
196
private static BigInteger fromMontgomery (BigInteger n ) {
182
197
return redc (n );
183
198
}
184
199
200
+ /**
201
+ * Montgomery reduction; given a value x, computes x*{@link #REDUCER}^{-1} mod {@link #P}
202
+ *
203
+ * @param x value to reduce
204
+ * @return x*{@link #REDUCER}^{-1} mod {@link #P}
205
+ */
185
206
private static BigInteger redc (BigInteger x ) {
186
207
BigInteger temp = x .multiply (FACTOR ).and (MASK );
187
- BigInteger reduced = x . add ( temp .multiply (P )).shiftRight (REDUCER_BITS );
208
+ BigInteger reduced = temp .multiply (P ). add ( x ).shiftRight (REDUCER_BITS );
188
209
return reduced .compareTo (P ) < 0 ? reduced : reduced .subtract (P );
189
210
}
190
211
}
0 commit comments