@noble/curves 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -10
- package/lib/abstract/hash-to-curve.d.ts +10 -1
- package/lib/abstract/hash-to-curve.js +32 -10
- package/lib/abstract/modular.d.ts +8 -17
- package/lib/abstract/modular.js +131 -152
- package/lib/abstract/utils.d.ts +3 -1
- package/lib/bls12-381.d.ts +1 -1
- package/lib/bls12-381.js +1 -1
- package/lib/ed25519.js +70 -7
- package/lib/ed448.js +84 -1
- package/lib/esm/abstract/hash-to-curve.js +30 -9
- package/lib/esm/abstract/modular.js +125 -148
- package/lib/esm/bls12-381.js +1 -1
- package/lib/esm/ed25519.js +71 -8
- package/lib/esm/ed448.js +85 -2
- package/lib/esm/p256.js +1 -1
- package/lib/esm/p384.js +1 -1
- package/lib/esm/p521.js +1 -1
- package/lib/esm/secp256k1.js +5 -2
- package/lib/esm/stark.js +2 -0
- package/lib/p256.js +1 -1
- package/lib/p384.js +1 -1
- package/lib/p521.js +1 -1
- package/lib/secp256k1.js +5 -2
- package/lib/stark.d.ts +1 -1
- package/lib/stark.js +2 -0
- package/package.json +1 -1
package/lib/esm/ed25519.js
CHANGED
|
@@ -3,7 +3,7 @@ import { sha512 } from '@noble/hashes/sha512';
|
|
|
3
3
|
import { concatBytes, randomBytes, utf8ToBytes } from '@noble/hashes/utils';
|
|
4
4
|
import { twistedEdwards } from './abstract/edwards.js';
|
|
5
5
|
import { montgomery } from './abstract/montgomery.js';
|
|
6
|
-
import { mod, pow2, isNegativeLE, Fp as Field } from './abstract/modular.js';
|
|
6
|
+
import { mod, pow2, isNegativeLE, Fp as Field, FpSqrtEven } from './abstract/modular.js';
|
|
7
7
|
import { ensureBytes, equalBytes, bytesToHex, bytesToNumberLE, numberToBytesLE, } from './abstract/utils.js';
|
|
8
8
|
/**
|
|
9
9
|
* ed25519 Twisted Edwards curve with following addons:
|
|
@@ -78,7 +78,74 @@ export const ED25519_TORSION_SUBGROUP = [
|
|
|
78
78
|
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
79
79
|
'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa',
|
|
80
80
|
];
|
|
81
|
-
const Fp = Field(ED25519_P);
|
|
81
|
+
const Fp = Field(ED25519_P, undefined, true);
|
|
82
|
+
// Hash To Curve Elligator2 Map (NOTE: different from ristretto255 elligator)
|
|
83
|
+
// NOTE: very important part is usage of FpSqrtEven for ELL2_C1_EDWARDS, since
|
|
84
|
+
// SageMath returns different root first and everything falls apart
|
|
85
|
+
const ELL2_C1 = (Fp.ORDER + BigInt(3)) / BigInt(8); // 1. c1 = (q + 3) / 8 # Integer arithmetic
|
|
86
|
+
const ELL2_C2 = Fp.pow(_2n, ELL2_C1); // 2. c2 = 2^c1
|
|
87
|
+
const ELL2_C3 = Fp.sqrt(Fp.negate(Fp.ONE)); // 3. c3 = sqrt(-1)
|
|
88
|
+
const ELL2_C4 = (Fp.ORDER - BigInt(5)) / BigInt(8); // 4. c4 = (q - 5) / 8 # Integer arithmetic
|
|
89
|
+
const ELL2_J = BigInt(486662);
|
|
90
|
+
// prettier-ignore
|
|
91
|
+
function map_to_curve_elligator2_curve25519(u) {
|
|
92
|
+
let tv1 = Fp.square(u); // 1. tv1 = u^2
|
|
93
|
+
tv1 = Fp.mul(tv1, _2n); // 2. tv1 = 2 * tv1
|
|
94
|
+
let xd = Fp.add(tv1, Fp.ONE); // 3. xd = tv1 + 1 # Nonzero: -1 is square (mod p), tv1 is not
|
|
95
|
+
let x1n = Fp.negate(ELL2_J); // 4. x1n = -J # x1 = x1n / xd = -J / (1 + 2 * u^2)
|
|
96
|
+
let tv2 = Fp.square(xd); // 5. tv2 = xd^2
|
|
97
|
+
let gxd = Fp.mul(tv2, xd); // 6. gxd = tv2 * xd # gxd = xd^3
|
|
98
|
+
let gx1 = Fp.mul(tv1, ELL2_J); // 7. gx1 = J * tv1 # x1n + J * xd
|
|
99
|
+
gx1 = Fp.mul(gx1, x1n); // 8. gx1 = gx1 * x1n # x1n^2 + J * x1n * xd
|
|
100
|
+
gx1 = Fp.add(gx1, tv2); // 9. gx1 = gx1 + tv2 # x1n^2 + J * x1n * xd + xd^2
|
|
101
|
+
gx1 = Fp.mul(gx1, x1n); // 10. gx1 = gx1 * x1n # x1n^3 + J * x1n^2 * xd + x1n * xd^2
|
|
102
|
+
let tv3 = Fp.square(gxd); // 11. tv3 = gxd^2
|
|
103
|
+
tv2 = Fp.square(tv3); // 12. tv2 = tv3^2 # gxd^4
|
|
104
|
+
tv3 = Fp.mul(tv3, gxd); // 13. tv3 = tv3 * gxd # gxd^3
|
|
105
|
+
tv3 = Fp.mul(tv3, gx1); // 14. tv3 = tv3 * gx1 # gx1 * gxd^3
|
|
106
|
+
tv2 = Fp.mul(tv2, tv3); // 15. tv2 = tv2 * tv3 # gx1 * gxd^7
|
|
107
|
+
let y11 = Fp.pow(tv2, ELL2_C4); // 16. y11 = tv2^c4 # (gx1 * gxd^7)^((p - 5) / 8)
|
|
108
|
+
y11 = Fp.mul(y11, tv3); // 17. y11 = y11 * tv3 # gx1*gxd^3*(gx1*gxd^7)^((p-5)/8)
|
|
109
|
+
let y12 = Fp.mul(y11, ELL2_C3); // 18. y12 = y11 * c3
|
|
110
|
+
tv2 = Fp.square(y11); // 19. tv2 = y11^2
|
|
111
|
+
tv2 = Fp.mul(tv2, gxd); // 20. tv2 = tv2 * gxd
|
|
112
|
+
let e1 = Fp.equals(tv2, gx1); // 21. e1 = tv2 == gx1
|
|
113
|
+
let y1 = Fp.cmov(y12, y11, e1); // 22. y1 = CMOV(y12, y11, e1) # If g(x1) is square, this is its sqrt
|
|
114
|
+
let x2n = Fp.mul(x1n, tv1); // 23. x2n = x1n * tv1 # x2 = x2n / xd = 2 * u^2 * x1n / xd
|
|
115
|
+
let y21 = Fp.mul(y11, u); // 24. y21 = y11 * u
|
|
116
|
+
y21 = Fp.mul(y21, ELL2_C2); // 25. y21 = y21 * c2
|
|
117
|
+
let y22 = Fp.mul(y21, ELL2_C3); // 26. y22 = y21 * c3
|
|
118
|
+
let gx2 = Fp.mul(gx1, tv1); // 27. gx2 = gx1 * tv1 # g(x2) = gx2 / gxd = 2 * u^2 * g(x1)
|
|
119
|
+
tv2 = Fp.square(y21); // 28. tv2 = y21^2
|
|
120
|
+
tv2 = Fp.mul(tv2, gxd); // 29. tv2 = tv2 * gxd
|
|
121
|
+
let e2 = Fp.equals(tv2, gx2); // 30. e2 = tv2 == gx2
|
|
122
|
+
let y2 = Fp.cmov(y22, y21, e2); // 31. y2 = CMOV(y22, y21, e2) # If g(x2) is square, this is its sqrt
|
|
123
|
+
tv2 = Fp.square(y1); // 32. tv2 = y1^2
|
|
124
|
+
tv2 = Fp.mul(tv2, gxd); // 33. tv2 = tv2 * gxd
|
|
125
|
+
let e3 = Fp.equals(tv2, gx1); // 34. e3 = tv2 == gx1
|
|
126
|
+
let xn = Fp.cmov(x2n, x1n, e3); // 35. xn = CMOV(x2n, x1n, e3) # If e3, x = x1, else x = x2
|
|
127
|
+
let y = Fp.cmov(y2, y1, e3); // 36. y = CMOV(y2, y1, e3) # If e3, y = y1, else y = y2
|
|
128
|
+
let e4 = Fp.isOdd(y); // 37. e4 = sgn0(y) == 1 # Fix sign of y
|
|
129
|
+
y = Fp.cmov(y, Fp.negate(y), e3 !== e4); // 38. y = CMOV(y, -y, e3 XOR e4)
|
|
130
|
+
return { xMn: xn, xMd: xd, yMn: y, yMd: 1n }; // 39. return (xn, xd, y, 1)
|
|
131
|
+
}
|
|
132
|
+
const ELL2_C1_EDWARDS = FpSqrtEven(Fp, Fp.negate(BigInt(486664))); // sgn0(c1) MUST equal 0
|
|
133
|
+
function map_to_curve_elligator2_edwards25519(u) {
|
|
134
|
+
const { xMn, xMd, yMn, yMd } = map_to_curve_elligator2_curve25519(u); // 1. (xMn, xMd, yMn, yMd) = map_to_curve_elligator2_curve25519(u)
|
|
135
|
+
let xn = Fp.mul(xMn, yMd); // 2. xn = xMn * yMd
|
|
136
|
+
xn = Fp.mul(xn, ELL2_C1_EDWARDS); // 3. xn = xn * c1
|
|
137
|
+
let xd = Fp.mul(xMd, yMn); // 4. xd = xMd * yMn # xn / xd = c1 * xM / yM
|
|
138
|
+
let yn = Fp.sub(xMn, xMd); // 5. yn = xMn - xMd
|
|
139
|
+
let yd = Fp.add(xMn, xMd); // 6. yd = xMn + xMd # (n / d - 1) / (n / d + 1) = (n - d) / (n + d)
|
|
140
|
+
let tv1 = Fp.mul(xd, yd); // 7. tv1 = xd * yd
|
|
141
|
+
let e = Fp.equals(tv1, Fp.ZERO); // 8. e = tv1 == 0
|
|
142
|
+
xn = Fp.cmov(xn, Fp.ZERO, e); // 9. xn = CMOV(xn, 0, e)
|
|
143
|
+
xd = Fp.cmov(xd, Fp.ONE, e); // 10. xd = CMOV(xd, 1, e)
|
|
144
|
+
yn = Fp.cmov(yn, Fp.ONE, e); // 11. yn = CMOV(yn, 1, e)
|
|
145
|
+
yd = Fp.cmov(yd, Fp.ONE, e); // 12. yd = CMOV(yd, 1, e)
|
|
146
|
+
const inv = Fp.invertBatch([xd, yd]); // batch division
|
|
147
|
+
return { x: Fp.mul(xn, inv[0]), y: Fp.mul(yn, inv[1]) }; // 13. return (xn, xd, yn, yd)
|
|
148
|
+
}
|
|
82
149
|
const ED25519_DEF = {
|
|
83
150
|
// Param: a
|
|
84
151
|
a: BigInt(-1),
|
|
@@ -107,14 +174,10 @@ const ED25519_DEF = {
|
|
|
107
174
|
p: Fp.ORDER,
|
|
108
175
|
m: 1,
|
|
109
176
|
k: 128,
|
|
110
|
-
expand:
|
|
177
|
+
expand: 'xmd',
|
|
111
178
|
hash: sha512,
|
|
112
179
|
},
|
|
113
|
-
mapToCurve: (scalars) =>
|
|
114
|
-
throw new Error('Not supported yet');
|
|
115
|
-
// const { x, y } = calcElligatorRistrettoMap(scalars[0]).toAffine();
|
|
116
|
-
// return { x, y };
|
|
117
|
-
},
|
|
180
|
+
mapToCurve: (scalars) => map_to_curve_elligator2_edwards25519(scalars[0]),
|
|
118
181
|
};
|
|
119
182
|
export const ed25519 = twistedEdwards(ED25519_DEF);
|
|
120
183
|
function ed25519_domain(data, ctx, phflag) {
|
package/lib/esm/ed448.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { shake256 } from '@noble/hashes/sha3';
|
|
3
3
|
import { concatBytes, randomBytes, utf8ToBytes, wrapConstructor } from '@noble/hashes/utils';
|
|
4
4
|
import { twistedEdwards } from './abstract/edwards.js';
|
|
5
|
-
import { mod, pow2, Fp } from './abstract/modular.js';
|
|
5
|
+
import { mod, pow2, Fp as Field } from './abstract/modular.js';
|
|
6
6
|
import { montgomery } from './abstract/montgomery.js';
|
|
7
7
|
/**
|
|
8
8
|
* Edwards448 (not Ed448-Goldilocks) curve with following addons:
|
|
@@ -45,13 +45,87 @@ function adjustScalarBytes(bytes) {
|
|
|
45
45
|
bytes[56] = 0; // Byte outside of group (456 buts vs 448 bits)
|
|
46
46
|
return bytes;
|
|
47
47
|
}
|
|
48
|
+
const Fp = Field(ed448P, 456, true);
|
|
49
|
+
// Hash To Curve Elligator2 Map
|
|
50
|
+
const ELL2_C1 = (Fp.ORDER - BigInt(3)) / BigInt(4); // 1. c1 = (q - 3) / 4 # Integer arithmetic
|
|
51
|
+
const ELL2_J = BigInt(156326);
|
|
52
|
+
function map_to_curve_elligator2_curve448(u) {
|
|
53
|
+
let tv1 = Fp.square(u); // 1. tv1 = u^2
|
|
54
|
+
let e1 = Fp.equals(tv1, Fp.ONE); // 2. e1 = tv1 == 1
|
|
55
|
+
tv1 = Fp.cmov(tv1, Fp.ZERO, e1); // 3. tv1 = CMOV(tv1, 0, e1) # If Z * u^2 == -1, set tv1 = 0
|
|
56
|
+
let xd = Fp.sub(Fp.ONE, tv1); // 4. xd = 1 - tv1
|
|
57
|
+
let x1n = Fp.negate(ELL2_J); // 5. x1n = -J
|
|
58
|
+
let tv2 = Fp.square(xd); // 6. tv2 = xd^2
|
|
59
|
+
let gxd = Fp.mul(tv2, xd); // 7. gxd = tv2 * xd # gxd = xd^3
|
|
60
|
+
let gx1 = Fp.mul(tv1, Fp.negate(ELL2_J)); // 8. gx1 = -J * tv1 # x1n + J * xd
|
|
61
|
+
gx1 = Fp.mul(gx1, x1n); // 9. gx1 = gx1 * x1n # x1n^2 + J * x1n * xd
|
|
62
|
+
gx1 = Fp.add(gx1, tv2); // 10. gx1 = gx1 + tv2 # x1n^2 + J * x1n * xd + xd^2
|
|
63
|
+
gx1 = Fp.mul(gx1, x1n); // 11. gx1 = gx1 * x1n # x1n^3 + J * x1n^2 * xd + x1n * xd^2
|
|
64
|
+
let tv3 = Fp.square(gxd); // 12. tv3 = gxd^2
|
|
65
|
+
tv2 = Fp.mul(gx1, gxd); // 13. tv2 = gx1 * gxd # gx1 * gxd
|
|
66
|
+
tv3 = Fp.mul(tv3, tv2); // 14. tv3 = tv3 * tv2 # gx1 * gxd^3
|
|
67
|
+
let y1 = Fp.pow(tv3, ELL2_C1); // 15. y1 = tv3^c1 # (gx1 * gxd^3)^((p - 3) / 4)
|
|
68
|
+
y1 = Fp.mul(y1, tv2); // 16. y1 = y1 * tv2 # gx1 * gxd * (gx1 * gxd^3)^((p - 3) / 4)
|
|
69
|
+
let x2n = Fp.mul(x1n, Fp.negate(tv1)); // 17. x2n = -tv1 * x1n # x2 = x2n / xd = -1 * u^2 * x1n / xd
|
|
70
|
+
let y2 = Fp.mul(y1, u); // 18. y2 = y1 * u
|
|
71
|
+
y2 = Fp.cmov(y2, Fp.ZERO, e1); // 19. y2 = CMOV(y2, 0, e1)
|
|
72
|
+
tv2 = Fp.square(y1); // 20. tv2 = y1^2
|
|
73
|
+
tv2 = Fp.mul(tv2, gxd); // 21. tv2 = tv2 * gxd
|
|
74
|
+
let e2 = Fp.equals(tv2, gx1); // 22. e2 = tv2 == gx1
|
|
75
|
+
let xn = Fp.cmov(x2n, x1n, e2); // 23. xn = CMOV(x2n, x1n, e2) # If e2, x = x1, else x = x2
|
|
76
|
+
let y = Fp.cmov(y2, y1, e2); // 24. y = CMOV(y2, y1, e2) # If e2, y = y1, else y = y2
|
|
77
|
+
let e3 = Fp.isOdd(y); // 25. e3 = sgn0(y) == 1 # Fix sign of y
|
|
78
|
+
y = Fp.cmov(y, Fp.negate(y), e2 !== e3); // 26. y = CMOV(y, -y, e2 XOR e3)
|
|
79
|
+
return { xn, xd, yn: y, yd: Fp.ONE }; // 27. return (xn, xd, y, 1)
|
|
80
|
+
}
|
|
81
|
+
function map_to_curve_elligator2_edwards448(u) {
|
|
82
|
+
let { xn, xd, yn, yd } = map_to_curve_elligator2_curve448(u); // 1. (xn, xd, yn, yd) = map_to_curve_elligator2_curve448(u)
|
|
83
|
+
let xn2 = Fp.square(xn); // 2. xn2 = xn^2
|
|
84
|
+
let xd2 = Fp.square(xd); // 3. xd2 = xd^2
|
|
85
|
+
let xd4 = Fp.square(xd2); // 4. xd4 = xd2^2
|
|
86
|
+
let yn2 = Fp.square(yn); // 5. yn2 = yn^2
|
|
87
|
+
let yd2 = Fp.square(yd); // 6. yd2 = yd^2
|
|
88
|
+
let xEn = Fp.sub(xn2, xd2); // 7. xEn = xn2 - xd2
|
|
89
|
+
let tv2 = Fp.sub(xEn, xd2); // 8. tv2 = xEn - xd2
|
|
90
|
+
xEn = Fp.mul(xEn, xd2); // 9. xEn = xEn * xd2
|
|
91
|
+
xEn = Fp.mul(xEn, yd); // 10. xEn = xEn * yd
|
|
92
|
+
xEn = Fp.mul(xEn, yn); // 11. xEn = xEn * yn
|
|
93
|
+
xEn = Fp.mul(xEn, 4n); // 12. xEn = xEn * 4
|
|
94
|
+
tv2 = Fp.mul(tv2, xn2); // 13. tv2 = tv2 * xn2
|
|
95
|
+
tv2 = Fp.mul(tv2, yd2); // 14. tv2 = tv2 * yd2
|
|
96
|
+
let tv3 = Fp.mul(yn2, 4n); // 15. tv3 = 4 * yn2
|
|
97
|
+
let tv1 = Fp.add(tv3, yd2); // 16. tv1 = tv3 + yd2
|
|
98
|
+
tv1 = Fp.mul(tv1, xd4); // 17. tv1 = tv1 * xd4
|
|
99
|
+
let xEd = Fp.add(tv1, tv2); // 18. xEd = tv1 + tv2
|
|
100
|
+
tv2 = Fp.mul(tv2, xn); // 19. tv2 = tv2 * xn
|
|
101
|
+
let tv4 = Fp.mul(xn, xd4); // 20. tv4 = xn * xd4
|
|
102
|
+
let yEn = Fp.sub(tv3, yd2); // 21. yEn = tv3 - yd2
|
|
103
|
+
yEn = Fp.mul(yEn, tv4); // 22. yEn = yEn * tv4
|
|
104
|
+
yEn = Fp.sub(yEn, tv2); // 23. yEn = yEn - tv2
|
|
105
|
+
tv1 = Fp.add(xn2, xd2); // 24. tv1 = xn2 + xd2
|
|
106
|
+
tv1 = Fp.mul(tv1, xd2); // 25. tv1 = tv1 * xd2
|
|
107
|
+
tv1 = Fp.mul(tv1, xd); // 26. tv1 = tv1 * xd
|
|
108
|
+
tv1 = Fp.mul(tv1, yn2); // 27. tv1 = tv1 * yn2
|
|
109
|
+
tv1 = Fp.mul(tv1, BigInt(-2)); // 28. tv1 = -2 * tv1
|
|
110
|
+
let yEd = Fp.add(tv2, tv1); // 29. yEd = tv2 + tv1
|
|
111
|
+
tv4 = Fp.mul(tv4, yd2); // 30. tv4 = tv4 * yd2
|
|
112
|
+
yEd = Fp.add(yEd, tv4); // 31. yEd = yEd + tv4
|
|
113
|
+
tv1 = Fp.mul(xEd, yEd); // 32. tv1 = xEd * yEd
|
|
114
|
+
let e = Fp.equals(tv1, Fp.ZERO); // 33. e = tv1 == 0
|
|
115
|
+
xEn = Fp.cmov(xEn, Fp.ZERO, e); // 34. xEn = CMOV(xEn, 0, e)
|
|
116
|
+
xEd = Fp.cmov(xEd, Fp.ONE, e); // 35. xEd = CMOV(xEd, 1, e)
|
|
117
|
+
yEn = Fp.cmov(yEn, Fp.ONE, e); // 36. yEn = CMOV(yEn, 1, e)
|
|
118
|
+
yEd = Fp.cmov(yEd, Fp.ONE, e); // 37. yEd = CMOV(yEd, 1, e)
|
|
119
|
+
const inv = Fp.invertBatch([xEd, yEd]); // batch division
|
|
120
|
+
return { x: Fp.mul(xEn, inv[0]), y: Fp.mul(yEn, inv[1]) }; // 38. return (xEn, xEd, yEn, yEd)
|
|
121
|
+
}
|
|
48
122
|
const ED448_DEF = {
|
|
49
123
|
// Param: a
|
|
50
124
|
a: BigInt(1),
|
|
51
125
|
// -39081. Negative number is P - number
|
|
52
126
|
d: BigInt('726838724295606890549323807888004534353641360687318060281490199180612328166730772686396383698676545930088884461843637361053498018326358'),
|
|
53
127
|
// Finite field 𝔽p over which we'll do calculations; 2n ** 448n - 2n ** 224n - 1n
|
|
54
|
-
Fp
|
|
128
|
+
Fp,
|
|
55
129
|
// Subgroup order: how many points ed448 has; 2n**446n - 13818066809895115352007386748515426880336692474882178609894547503885n
|
|
56
130
|
n: BigInt('181709681073901722637330951972001133588410340171829515070372549795146003961539585716195755291692375963310293709091662304773755859649779'),
|
|
57
131
|
nBitLength: 456,
|
|
@@ -91,6 +165,15 @@ const ED448_DEF = {
|
|
|
91
165
|
// square root exists, and the decoding fails.
|
|
92
166
|
return { isValid: mod(x2 * v, P) === u, value: x };
|
|
93
167
|
},
|
|
168
|
+
htfDefaults: {
|
|
169
|
+
DST: 'edwards448_XOF:SHAKE256_ELL2_RO_',
|
|
170
|
+
p: Fp.ORDER,
|
|
171
|
+
m: 1,
|
|
172
|
+
k: 224,
|
|
173
|
+
expand: 'xof',
|
|
174
|
+
hash: shake256,
|
|
175
|
+
},
|
|
176
|
+
mapToCurve: (scalars) => map_to_curve_elligator2_edwards448(scalars[0]),
|
|
94
177
|
};
|
|
95
178
|
export const ed448 = twistedEdwards(ED448_DEF);
|
|
96
179
|
// NOTE: there is no ed448ctx, since ed448 supports ctx by default
|
package/lib/esm/p256.js
CHANGED
package/lib/esm/p384.js
CHANGED
package/lib/esm/p521.js
CHANGED
package/lib/esm/secp256k1.js
CHANGED
|
@@ -46,7 +46,10 @@ function sqrtMod(y) {
|
|
|
46
46
|
const b223 = (pow2(b220, _3n, P) * b3) % P;
|
|
47
47
|
const t1 = (pow2(b223, _23n, P) * b22) % P;
|
|
48
48
|
const t2 = (pow2(t1, _6n, P) * b2) % P;
|
|
49
|
-
|
|
49
|
+
const root = pow2(t2, _2n, P);
|
|
50
|
+
if (!Fp.equals(Fp.square(root), y))
|
|
51
|
+
throw new Error('Cannot find square root');
|
|
52
|
+
return root;
|
|
50
53
|
}
|
|
51
54
|
const Fp = Field(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
|
|
52
55
|
const isoMap = isogenyMap(Fp, [
|
|
@@ -134,7 +137,7 @@ export const secp256k1 = createCurve({
|
|
|
134
137
|
p: Fp.ORDER,
|
|
135
138
|
m: 1,
|
|
136
139
|
k: 128,
|
|
137
|
-
expand:
|
|
140
|
+
expand: 'xmd',
|
|
138
141
|
hash: sha256,
|
|
139
142
|
},
|
|
140
143
|
}, sha256);
|
package/lib/esm/stark.js
CHANGED
|
@@ -163,6 +163,8 @@ function pedersenPrecompute(p1, p2) {
|
|
|
163
163
|
out.push(p);
|
|
164
164
|
p = p.double();
|
|
165
165
|
}
|
|
166
|
+
// NOTE: we cannot use wNAF here, because last 4 bits will require full 248 bits multiplication
|
|
167
|
+
// We can add support for this to wNAF, but it will complicate wNAF.
|
|
166
168
|
p = p2;
|
|
167
169
|
for (let i = 0; i < 4; i++) {
|
|
168
170
|
out.push(p);
|
package/lib/p256.js
CHANGED
package/lib/p384.js
CHANGED
package/lib/p521.js
CHANGED
package/lib/secp256k1.js
CHANGED
|
@@ -49,7 +49,10 @@ function sqrtMod(y) {
|
|
|
49
49
|
const b223 = ((0, modular_js_1.pow2)(b220, _3n, P) * b3) % P;
|
|
50
50
|
const t1 = ((0, modular_js_1.pow2)(b223, _23n, P) * b22) % P;
|
|
51
51
|
const t2 = ((0, modular_js_1.pow2)(t1, _6n, P) * b2) % P;
|
|
52
|
-
|
|
52
|
+
const root = (0, modular_js_1.pow2)(t2, _2n, P);
|
|
53
|
+
if (!Fp.equals(Fp.square(root), y))
|
|
54
|
+
throw new Error('Cannot find square root');
|
|
55
|
+
return root;
|
|
53
56
|
}
|
|
54
57
|
const Fp = (0, modular_js_1.Fp)(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
|
|
55
58
|
const isoMap = (0, hash_to_curve_js_1.isogenyMap)(Fp, [
|
|
@@ -137,7 +140,7 @@ exports.secp256k1 = (0, _shortw_utils_js_1.createCurve)({
|
|
|
137
140
|
p: Fp.ORDER,
|
|
138
141
|
m: 1,
|
|
139
142
|
k: 128,
|
|
140
|
-
expand:
|
|
143
|
+
expand: 'xmd',
|
|
141
144
|
hash: sha256_1.sha256,
|
|
142
145
|
},
|
|
143
146
|
}, sha256_1.sha256);
|
package/lib/stark.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ declare type ProjectivePoint = ProjectivePointType<bigint>;
|
|
|
4
4
|
export declare const starkCurve: import("./abstract/weierstrass.js").CurveFn;
|
|
5
5
|
declare function getPublicKey0x(privKey: Hex, isCompressed?: boolean): Uint8Array;
|
|
6
6
|
declare function getSharedSecret0x(privKeyA: Hex, pubKeyB: Hex): Uint8Array;
|
|
7
|
-
declare function sign0x(msgHash: Hex, privKey: Hex, opts
|
|
7
|
+
declare function sign0x(msgHash: Hex, privKey: Hex, opts?: any): import("./abstract/weierstrass.js").SignatureType;
|
|
8
8
|
declare function verify0x(signature: Hex, msgHash: Hex, pubKey: Hex): boolean;
|
|
9
9
|
declare const CURVE: Readonly<{
|
|
10
10
|
readonly nBitLength: number;
|
package/lib/stark.js
CHANGED
|
@@ -180,6 +180,8 @@ function pedersenPrecompute(p1, p2) {
|
|
|
180
180
|
out.push(p);
|
|
181
181
|
p = p.double();
|
|
182
182
|
}
|
|
183
|
+
// NOTE: we cannot use wNAF here, because last 4 bits will require full 248 bits multiplication
|
|
184
|
+
// We can add support for this to wNAF, but it will complicate wNAF.
|
|
183
185
|
p = p2;
|
|
184
186
|
for (let i = 0; i < 4; i++) {
|
|
185
187
|
out.push(p);
|