Skip to content

Commit 7de7fe9

Browse files
committed
Improve comments
1 parent e6b7c96 commit 7de7fe9

File tree

1 file changed

+39
-18
lines changed
  • crypto/src/main/java/org/tron/common/crypto/zksnark

1 file changed

+39
-18
lines changed

crypto/src/main/java/org/tron/common/crypto/zksnark/Fp.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
/**
2323
* Arithmetic in F_p, p = 21888242871839275222246405745257275088696311157297823662689037894645226208583
24-
*
24+
* This class stores elements of F_p in the Montgomery form: a*r mod p.
25+
*
2526
* @author Mikhail Kalinin
2627
* @since 01.09.2017
2728
*/
@@ -32,40 +33,42 @@ public class Fp implements Field<Fp> {
3233
static final BigInteger P = new BigInteger(
3334
"21888242871839275222246405745257275088696311157297823662689037894645226208583");
3435

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.
3741
*/
3842
static final BigInteger REDUCER = new BigInteger(
3943
"115792089237316195423570985008687907853269984665640564039457584007913129639936");
4044

45+
/**
46+
* The number of bits in the {@link #REDUCER} value.
47+
*/
4148
static final int REDUCER_BITS = 256;
4249

43-
/*
44-
* REDUCER^2 mod P
50+
/**
51+
* A precomputed value of {@link #REDUCER}^2 mod {@link #P}.
4552
*/
4653
static final BigInteger REDUCER_SQUARED = new BigInteger(
4754
"3096616502983703923843567936837374451735540968419076528771170197431451843209");
4855

49-
/*
50-
* REDUCER^3 mod P
56+
/**
57+
* A precomputed value of {@link #REDUCER}^3 mod {@link #P}.
5158
*/
5259
static final BigInteger REDUCER_CUBED = new BigInteger(
5360
"14921786541159648185948152738563080959093619838510245177710943249661917737183");
5461

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}.
6364
*/
6465
static final BigInteger FACTOR = new BigInteger(
6566
"111032442853175714102588374283752698368366046808579839647964533820976443843465");
6667

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.
6972
*/
7073
static final BigInteger MASK = new BigInteger(
7174
"115792089237316195423570985008687907853269984665640564039457584007913129639935");
@@ -174,17 +177,35 @@ public String toString() {
174177
return v.toString();
175178
}
176179

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+
*/
177186
private static BigInteger toMontgomery(BigInteger n) {
178187
return redc(n.multiply(REDUCER_SQUARED));
179188
}
180189

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+
*/
181196
private static BigInteger fromMontgomery(BigInteger n) {
182197
return redc(n);
183198
}
184199

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+
*/
185206
private static BigInteger redc(BigInteger x) {
186207
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);
188209
return reduced.compareTo(P) < 0 ? reduced : reduced.subtract(P);
189210
}
190211
}

0 commit comments

Comments
 (0)