@noble/curves 1.9.0 → 1.9.2
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 +78 -34
- package/_shortw_utils.d.ts +7 -5
- package/_shortw_utils.d.ts.map +1 -1
- package/_shortw_utils.js +2 -8
- package/_shortw_utils.js.map +1 -1
- package/abstract/bls.d.ts +60 -24
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +158 -109
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +44 -9
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +99 -11
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +112 -25
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +141 -92
- package/abstract/edwards.js.map +1 -1
- package/abstract/fft.d.ts +122 -0
- package/abstract/fft.d.ts.map +1 -0
- package/abstract/fft.js +438 -0
- package/abstract/fft.js.map +1 -0
- package/abstract/hash-to-curve.d.ts +25 -11
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +17 -14
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +28 -17
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +156 -139
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +3 -8
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +73 -93
- package/abstract/montgomery.js.map +1 -1
- package/abstract/poseidon.d.ts +5 -13
- package/abstract/poseidon.d.ts.map +1 -1
- package/abstract/poseidon.js +12 -7
- package/abstract/poseidon.js.map +1 -1
- package/abstract/tower.d.ts +20 -46
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +10 -4
- package/abstract/tower.js.map +1 -1
- package/abstract/utils.d.ts +1 -115
- package/abstract/utils.d.ts.map +1 -1
- package/abstract/utils.js +17 -371
- package/abstract/utils.js.map +1 -1
- package/abstract/weierstrass.d.ts +152 -73
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +487 -404
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts +2 -0
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +504 -480
- package/bls12-381.js.map +1 -1
- package/bn254.d.ts +2 -0
- package/bn254.d.ts.map +1 -1
- package/bn254.js +44 -32
- package/bn254.js.map +1 -1
- package/ed25519.d.ts +25 -9
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +89 -65
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +29 -10
- package/ed448.d.ts.map +1 -1
- package/ed448.js +116 -81
- package/ed448.js.map +1 -1
- package/esm/_shortw_utils.d.ts +7 -5
- package/esm/_shortw_utils.d.ts.map +1 -1
- package/esm/_shortw_utils.js +2 -8
- package/esm/_shortw_utils.js.map +1 -1
- package/esm/abstract/bls.d.ts +60 -24
- package/esm/abstract/bls.d.ts.map +1 -1
- package/esm/abstract/bls.js +158 -109
- package/esm/abstract/bls.js.map +1 -1
- package/esm/abstract/curve.d.ts +44 -9
- package/esm/abstract/curve.d.ts.map +1 -1
- package/esm/abstract/curve.js +96 -12
- package/esm/abstract/curve.js.map +1 -1
- package/esm/abstract/edwards.d.ts +112 -25
- package/esm/abstract/edwards.d.ts.map +1 -1
- package/esm/abstract/edwards.js +141 -94
- package/esm/abstract/edwards.js.map +1 -1
- package/esm/abstract/fft.d.ts +122 -0
- package/esm/abstract/fft.d.ts.map +1 -0
- package/esm/abstract/fft.js +425 -0
- package/esm/abstract/fft.js.map +1 -0
- package/esm/abstract/hash-to-curve.d.ts +25 -11
- package/esm/abstract/hash-to-curve.d.ts.map +1 -1
- package/esm/abstract/hash-to-curve.js +17 -14
- package/esm/abstract/hash-to-curve.js.map +1 -1
- package/esm/abstract/modular.d.ts +28 -17
- package/esm/abstract/modular.d.ts.map +1 -1
- package/esm/abstract/modular.js +155 -138
- package/esm/abstract/modular.js.map +1 -1
- package/esm/abstract/montgomery.d.ts +3 -8
- package/esm/abstract/montgomery.d.ts.map +1 -1
- package/esm/abstract/montgomery.js +74 -94
- package/esm/abstract/montgomery.js.map +1 -1
- package/esm/abstract/poseidon.d.ts +5 -13
- package/esm/abstract/poseidon.d.ts.map +1 -1
- package/esm/abstract/poseidon.js +12 -7
- package/esm/abstract/poseidon.js.map +1 -1
- package/esm/abstract/tower.d.ts +20 -46
- package/esm/abstract/tower.d.ts.map +1 -1
- package/esm/abstract/tower.js +10 -4
- package/esm/abstract/tower.js.map +1 -1
- package/esm/abstract/utils.d.ts +1 -115
- package/esm/abstract/utils.d.ts.map +1 -1
- package/esm/abstract/utils.js +3 -344
- package/esm/abstract/utils.js.map +1 -1
- package/esm/abstract/weierstrass.d.ts +152 -73
- package/esm/abstract/weierstrass.d.ts.map +1 -1
- package/esm/abstract/weierstrass.js +485 -406
- package/esm/abstract/weierstrass.js.map +1 -1
- package/esm/bls12-381.d.ts +2 -0
- package/esm/bls12-381.d.ts.map +1 -1
- package/esm/bls12-381.js +503 -479
- package/esm/bls12-381.js.map +1 -1
- package/esm/bn254.d.ts +2 -0
- package/esm/bn254.d.ts.map +1 -1
- package/esm/bn254.js +41 -29
- package/esm/bn254.js.map +1 -1
- package/esm/ed25519.d.ts +25 -9
- package/esm/ed25519.d.ts.map +1 -1
- package/esm/ed25519.js +84 -60
- package/esm/ed25519.js.map +1 -1
- package/esm/ed448.d.ts +29 -10
- package/esm/ed448.d.ts.map +1 -1
- package/esm/ed448.js +113 -78
- package/esm/ed448.js.map +1 -1
- package/esm/jubjub.d.ts +4 -0
- package/esm/jubjub.d.ts.map +1 -1
- package/esm/jubjub.js +4 -0
- package/esm/jubjub.js.map +1 -1
- package/esm/misc.d.ts.map +1 -1
- package/esm/misc.js +31 -26
- package/esm/misc.js.map +1 -1
- package/esm/nist.d.ts +8 -16
- package/esm/nist.d.ts.map +1 -1
- package/esm/nist.js +87 -97
- package/esm/nist.js.map +1 -1
- package/esm/p256.d.ts +3 -3
- package/esm/p384.d.ts +3 -3
- package/esm/p521.d.ts +3 -3
- package/esm/pasta.d.ts +4 -0
- package/esm/pasta.d.ts.map +1 -1
- package/esm/pasta.js +4 -0
- package/esm/pasta.js.map +1 -1
- package/esm/secp256k1.d.ts +6 -6
- package/esm/secp256k1.d.ts.map +1 -1
- package/esm/secp256k1.js +44 -41
- package/esm/secp256k1.js.map +1 -1
- package/esm/utils.d.ts +96 -0
- package/esm/utils.d.ts.map +1 -0
- package/esm/utils.js +279 -0
- package/esm/utils.js.map +1 -0
- package/jubjub.d.ts +4 -0
- package/jubjub.d.ts.map +1 -1
- package/jubjub.js +4 -0
- package/jubjub.js.map +1 -1
- package/misc.d.ts.map +1 -1
- package/misc.js +35 -30
- package/misc.js.map +1 -1
- package/nist.d.ts +8 -16
- package/nist.d.ts.map +1 -1
- package/nist.js +87 -97
- package/nist.js.map +1 -1
- package/p256.d.ts +3 -3
- package/p384.d.ts +3 -3
- package/p521.d.ts +3 -3
- package/package.json +26 -8
- package/pasta.d.ts +4 -0
- package/pasta.d.ts.map +1 -1
- package/pasta.js +4 -0
- package/pasta.js.map +1 -1
- package/secp256k1.d.ts +6 -6
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +47 -44
- package/secp256k1.js.map +1 -1
- package/src/_shortw_utils.ts +5 -15
- package/src/abstract/bls.ts +260 -145
- package/src/abstract/curve.ts +125 -18
- package/src/abstract/edwards.ts +282 -127
- package/src/abstract/fft.ts +519 -0
- package/src/abstract/hash-to-curve.ts +51 -27
- package/src/abstract/modular.ts +156 -143
- package/src/abstract/montgomery.ts +81 -111
- package/src/abstract/poseidon.ts +22 -18
- package/src/abstract/tower.ts +37 -68
- package/src/abstract/utils.ts +3 -378
- package/src/abstract/weierstrass.ts +752 -461
- package/src/bls12-381.ts +542 -507
- package/src/bn254.ts +47 -35
- package/src/ed25519.ts +104 -76
- package/src/ed448.ts +156 -105
- package/src/jubjub.ts +4 -0
- package/src/misc.ts +39 -34
- package/src/nist.ts +138 -126
- package/src/p256.ts +3 -3
- package/src/p384.ts +3 -3
- package/src/p521.ts +3 -3
- package/src/pasta.ts +5 -1
- package/src/secp256k1.ts +59 -47
- package/src/utils.ts +328 -0
- package/utils.d.ts +96 -0
- package/utils.d.ts.map +1 -0
- package/utils.js +313 -0
- package/utils.js.map +1 -0
package/src/bn254.ts
CHANGED
|
@@ -13,7 +13,7 @@ There are huge compatibility issues in the ecosystem:
|
|
|
13
13
|
https://github.com/scipr-lab/libff/blob/a44f482e18b8ac04d034c193bd9d7df7817ad73f/libff/algebra/curves/bn128/bn128_init.cpp#L166-L169
|
|
14
14
|
3. halo2curves bn256 is also incompatible and returns different outputs
|
|
15
15
|
|
|
16
|
-
We don't implement Point methods toHex /
|
|
16
|
+
We don't implement Point methods toHex / toBytes.
|
|
17
17
|
To work around this limitation, has to initialize points on their own from BigInts.
|
|
18
18
|
Reason it's not implemented is because [there is no standard](https://github.com/privacy-scaling-explorations/halo2curves/issues/109).
|
|
19
19
|
Points of divergence:
|
|
@@ -54,32 +54,40 @@ Ate loop size: 6x+2
|
|
|
54
54
|
* @module
|
|
55
55
|
*/
|
|
56
56
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
57
|
-
import { sha256 } from '@noble/hashes/sha2';
|
|
58
|
-
import { randomBytes } from '@noble/hashes/utils';
|
|
59
|
-
import { getHash } from './_shortw_utils.ts';
|
|
57
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
60
58
|
import {
|
|
61
59
|
bls,
|
|
62
60
|
type CurveFn as BLSCurveFn,
|
|
63
61
|
type PostPrecomputeFn,
|
|
64
62
|
type PostPrecomputePointAddFn,
|
|
65
63
|
} from './abstract/bls.ts';
|
|
66
|
-
import { Field } from './abstract/modular.ts';
|
|
64
|
+
import { Field, type IField } from './abstract/modular.ts';
|
|
67
65
|
import type { Fp, Fp12, Fp2, Fp6 } from './abstract/tower.ts';
|
|
68
66
|
import { psiFrobenius, tower12 } from './abstract/tower.ts';
|
|
69
|
-
import {
|
|
70
|
-
import {
|
|
67
|
+
import { type CurveFn, weierstrass, type WeierstrassOpts } from './abstract/weierstrass.ts';
|
|
68
|
+
import { bitGet, bitLen, notImplemented } from './utils.ts';
|
|
71
69
|
// prettier-ignore
|
|
72
|
-
const _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
|
|
70
|
+
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
|
|
73
71
|
const _6n = BigInt(6);
|
|
74
72
|
|
|
75
73
|
const BN_X = BigInt('4965661367192848881');
|
|
76
74
|
const BN_X_LEN = bitLen(BN_X);
|
|
77
75
|
const SIX_X_SQUARED = _6n * BN_X ** _2n;
|
|
78
76
|
|
|
77
|
+
const bn254_G1_CURVE: WeierstrassOpts<bigint> = {
|
|
78
|
+
p: BigInt('0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47'),
|
|
79
|
+
n: BigInt('0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001'),
|
|
80
|
+
h: _1n,
|
|
81
|
+
a: _0n,
|
|
82
|
+
b: _3n,
|
|
83
|
+
Gx: _1n,
|
|
84
|
+
Gy: BigInt(2),
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// r == n
|
|
79
88
|
// Finite field over r. It's for convenience and is not used in the code below.
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
);
|
|
89
|
+
export const bn254_Fr: IField<bigint> = Field(bn254_G1_CURVE.n);
|
|
90
|
+
|
|
83
91
|
// Fp2.div(Fp2.mul(Fp2.ONE, _3n), Fp2.NONRESIDUE)
|
|
84
92
|
const Fp2B = {
|
|
85
93
|
c0: BigInt('19485874751759354771024239261021720505790618469301721065564631296452457478373'),
|
|
@@ -87,7 +95,7 @@ const Fp2B = {
|
|
|
87
95
|
};
|
|
88
96
|
|
|
89
97
|
const { Fp, Fp2, Fp6, Fp4Square, Fp12 } = tower12({
|
|
90
|
-
ORDER:
|
|
98
|
+
ORDER: bn254_G1_CURVE.p,
|
|
91
99
|
FP2_NONRESIDUE: [BigInt(9), _1n],
|
|
92
100
|
Fp2mulByB: (num) => Fp2.mul(num, Fp2B),
|
|
93
101
|
// The result of any pairing is in a cyclotomic subgroup
|
|
@@ -161,7 +169,7 @@ const htfDefaults = Object.freeze({
|
|
|
161
169
|
k: 128,
|
|
162
170
|
expand: 'xmd',
|
|
163
171
|
hash: sha256,
|
|
164
|
-
}
|
|
172
|
+
});
|
|
165
173
|
|
|
166
174
|
export const _postPrecompute: PostPrecomputeFn = (
|
|
167
175
|
Rx: Fp2,
|
|
@@ -177,20 +185,33 @@ export const _postPrecompute: PostPrecomputeFn = (
|
|
|
177
185
|
pointAdd(Rx, Ry, Rz, q2[0], Fp2.neg(q2[1]));
|
|
178
186
|
};
|
|
179
187
|
|
|
188
|
+
// cofactor: (36 * X^4) + (36 * X^3) + (30 * X^2) + 6*X + 1
|
|
189
|
+
const bn254_G2_CURVE: WeierstrassOpts<Fp2> = {
|
|
190
|
+
p: Fp2.ORDER,
|
|
191
|
+
n: bn254_G1_CURVE.n,
|
|
192
|
+
h: BigInt('0x30644e72e131a029b85045b68181585e06ceecda572a2489345f2299c0f9fa8d'),
|
|
193
|
+
a: Fp2.ZERO,
|
|
194
|
+
b: Fp2B,
|
|
195
|
+
Gx: Fp2.fromBigTuple([
|
|
196
|
+
BigInt('10857046999023057135944570762232829481370756359578518086990519993285655852781'),
|
|
197
|
+
BigInt('11559732032986387107991004021392285783925812861821192530917403151452391805634'),
|
|
198
|
+
]),
|
|
199
|
+
Gy: Fp2.fromBigTuple([
|
|
200
|
+
BigInt('8495653923123431417604973247489272438418190587263600148770280649306958101930'),
|
|
201
|
+
BigInt('4082367875863433681332203403145435568316851327593401208105741076214120093531'),
|
|
202
|
+
]),
|
|
203
|
+
};
|
|
204
|
+
|
|
180
205
|
/**
|
|
181
206
|
* bn254 (a.k.a. alt_bn128) pairing-friendly curve.
|
|
182
207
|
* Contains G1 / G2 operations and pairings.
|
|
183
208
|
*/
|
|
184
209
|
export const bn254: BLSCurveFn = bls({
|
|
185
210
|
// Fields
|
|
186
|
-
fields: { Fp, Fp2, Fp6, Fp12, Fr },
|
|
211
|
+
fields: { Fp, Fp2, Fp6, Fp12, Fr: bn254_Fr },
|
|
187
212
|
G1: {
|
|
213
|
+
...bn254_G1_CURVE,
|
|
188
214
|
Fp,
|
|
189
|
-
h: BigInt(1),
|
|
190
|
-
Gx: BigInt(1),
|
|
191
|
-
Gy: BigInt(2),
|
|
192
|
-
a: Fp.ZERO,
|
|
193
|
-
b: _3n,
|
|
194
215
|
htfDefaults: { ...htfDefaults, m: 1, DST: 'BN254G2_XMD:SHA-256_SVDW_RO_' },
|
|
195
216
|
wrapPrivateKey: true,
|
|
196
217
|
allowInfinityPoint: true,
|
|
@@ -198,25 +219,16 @@ export const bn254: BLSCurveFn = bls({
|
|
|
198
219
|
fromBytes: notImplemented,
|
|
199
220
|
toBytes: notImplemented,
|
|
200
221
|
ShortSignature: {
|
|
222
|
+
fromBytes: notImplemented,
|
|
201
223
|
fromHex: notImplemented,
|
|
224
|
+
toBytes: notImplemented,
|
|
202
225
|
toRawBytes: notImplemented,
|
|
203
226
|
toHex: notImplemented,
|
|
204
227
|
},
|
|
205
228
|
},
|
|
206
229
|
G2: {
|
|
230
|
+
...bn254_G2_CURVE,
|
|
207
231
|
Fp: Fp2,
|
|
208
|
-
// cofactor: (36 * X^4) + (36 * X^3) + (30 * X^2) + 6*X + 1
|
|
209
|
-
h: BigInt('21888242871839275222246405745257275088844257914179612981679871602714643921549'),
|
|
210
|
-
Gx: Fp2.fromBigTuple([
|
|
211
|
-
BigInt('10857046999023057135944570762232829481370756359578518086990519993285655852781'),
|
|
212
|
-
BigInt('11559732032986387107991004021392285783925812861821192530917403151452391805634'),
|
|
213
|
-
]),
|
|
214
|
-
Gy: Fp2.fromBigTuple([
|
|
215
|
-
BigInt('8495653923123431417604973247489272438418190587263600148770280649306958101930'),
|
|
216
|
-
BigInt('4082367875863433681332203403145435568316851327593401208105741076214120093531'),
|
|
217
|
-
]),
|
|
218
|
-
a: Fp2.ZERO,
|
|
219
|
-
b: Fp2B,
|
|
220
232
|
hEff: BigInt('21888242871839275222246405745257275088844257914179612981679871602714643921549'),
|
|
221
233
|
htfDefaults: { ...htfDefaults },
|
|
222
234
|
wrapPrivateKey: true,
|
|
@@ -226,21 +238,21 @@ export const bn254: BLSCurveFn = bls({
|
|
|
226
238
|
fromBytes: notImplemented,
|
|
227
239
|
toBytes: notImplemented,
|
|
228
240
|
Signature: {
|
|
241
|
+
fromBytes: notImplemented,
|
|
229
242
|
fromHex: notImplemented,
|
|
243
|
+
toBytes: notImplemented,
|
|
230
244
|
toRawBytes: notImplemented,
|
|
231
245
|
toHex: notImplemented,
|
|
232
246
|
},
|
|
233
247
|
},
|
|
234
248
|
params: {
|
|
235
249
|
ateLoopSize: BN_X * _6n + _2n,
|
|
236
|
-
r:
|
|
250
|
+
r: bn254_Fr.ORDER,
|
|
237
251
|
xNegative: false,
|
|
238
252
|
twistType: 'divisive',
|
|
239
253
|
},
|
|
240
254
|
htfDefaults,
|
|
241
255
|
hash: sha256,
|
|
242
|
-
randomBytes,
|
|
243
|
-
|
|
244
256
|
postPrecompute: _postPrecompute,
|
|
245
257
|
});
|
|
246
258
|
|
|
@@ -258,5 +270,5 @@ export const bn254_weierstrass: CurveFn = weierstrass({
|
|
|
258
270
|
Gx: BigInt(1),
|
|
259
271
|
Gy: BigInt(2),
|
|
260
272
|
h: BigInt(1),
|
|
261
|
-
|
|
273
|
+
hash: sha256,
|
|
262
274
|
});
|
package/src/ed25519.ts
CHANGED
|
@@ -6,16 +6,21 @@
|
|
|
6
6
|
* @module
|
|
7
7
|
*/
|
|
8
8
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
9
|
-
import { sha512 } from '@noble/hashes/sha2';
|
|
10
|
-
import {
|
|
9
|
+
import { sha512 } from '@noble/hashes/sha2.js';
|
|
10
|
+
import { abytes, concatBytes, utf8ToBytes } from '@noble/hashes/utils.js';
|
|
11
11
|
import { type AffinePoint, type Group, pippenger } from './abstract/curve.ts';
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
type CurveFn,
|
|
14
|
+
type EdwardsOpts,
|
|
15
|
+
type ExtPointType,
|
|
16
|
+
twistedEdwards,
|
|
17
|
+
} from './abstract/edwards.ts';
|
|
13
18
|
import {
|
|
14
19
|
createHasher,
|
|
15
20
|
expand_message_xmd,
|
|
16
|
-
type
|
|
21
|
+
type H2CHasher,
|
|
22
|
+
type H2CMethod,
|
|
17
23
|
type htfBasicOpts,
|
|
18
|
-
type HTFMethod,
|
|
19
24
|
} from './abstract/hash-to-curve.ts';
|
|
20
25
|
import { Field, FpInvertBatch, FpSqrtEven, isNegativeLE, mod, pow2 } from './abstract/modular.ts';
|
|
21
26
|
import { montgomery, type CurveFn as XCurveFn } from './abstract/montgomery.ts';
|
|
@@ -26,27 +31,33 @@ import {
|
|
|
26
31
|
equalBytes,
|
|
27
32
|
type Hex,
|
|
28
33
|
numberToBytesLE,
|
|
29
|
-
} from './
|
|
30
|
-
|
|
31
|
-
// 2n**255n - 19n
|
|
32
|
-
const ED25519_P = BigInt(
|
|
33
|
-
'57896044618658097711785492504343953926634992332820282019728792003956564819949'
|
|
34
|
-
);
|
|
35
|
-
// √(-1) aka √(a) aka 2^((p-1)/4)
|
|
36
|
-
// Fp.sqrt(Fp.neg(1))
|
|
37
|
-
const ED25519_SQRT_M1 = /* @__PURE__ */ BigInt(
|
|
38
|
-
'19681161376707505956807079304988542015446066515923890162744021073123829784752'
|
|
39
|
-
);
|
|
34
|
+
} from './utils.ts';
|
|
40
35
|
|
|
41
36
|
// prettier-ignore
|
|
42
37
|
const _0n = BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
|
|
43
38
|
// prettier-ignore
|
|
44
39
|
const _5n = BigInt(5), _8n = BigInt(8);
|
|
45
40
|
|
|
41
|
+
// 2n**255n - 19n
|
|
42
|
+
// Removing Fp.create() will still work, and is 10% faster on sign
|
|
43
|
+
// a: Fp.create(BigInt(-1)),
|
|
44
|
+
// d is -121665/121666 a.k.a. Fp.neg(121665 * Fp.inv(121666))
|
|
45
|
+
// Finite field 2n**255n - 19n
|
|
46
|
+
// Subgroup order 2n**252n + 27742317777372353535851937790883648493n;
|
|
47
|
+
const ed25519_CURVE: EdwardsOpts = {
|
|
48
|
+
p: BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed'),
|
|
49
|
+
n: BigInt('0x1000000000000000000000000000000014def9dea2f79cd65812631a5cf5d3ed'),
|
|
50
|
+
h: _8n,
|
|
51
|
+
a: BigInt('0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec'),
|
|
52
|
+
d: BigInt('0x52036cee2b6ffe738cc740797779e89800700a4d4141d8ab75eb4dca135978a3'),
|
|
53
|
+
Gx: BigInt('0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a'),
|
|
54
|
+
Gy: BigInt('0x6666666666666666666666666666666666666666666666666666666666666658'),
|
|
55
|
+
};
|
|
56
|
+
|
|
46
57
|
function ed25519_pow_2_252_3(x: bigint) {
|
|
47
58
|
// prettier-ignore
|
|
48
59
|
const _10n = BigInt(10), _20n = BigInt(20), _40n = BigInt(40), _80n = BigInt(80);
|
|
49
|
-
const P =
|
|
60
|
+
const P = ed25519_CURVE.p;
|
|
50
61
|
const x2 = (x * x) % P;
|
|
51
62
|
const b2 = (x2 * x) % P; // x^3, 11
|
|
52
63
|
const b4 = (pow2(b2, _2n, P) * b2) % P; // x^15, 1111
|
|
@@ -74,9 +85,14 @@ function adjustScalarBytes(bytes: Uint8Array): Uint8Array {
|
|
|
74
85
|
return bytes;
|
|
75
86
|
}
|
|
76
87
|
|
|
88
|
+
// √(-1) aka √(a) aka 2^((p-1)/4)
|
|
89
|
+
// Fp.sqrt(Fp.neg(1))
|
|
90
|
+
const ED25519_SQRT_M1 = /* @__PURE__ */ BigInt(
|
|
91
|
+
'19681161376707505956807079304988542015446066515923890162744021073123829784752'
|
|
92
|
+
);
|
|
77
93
|
// sqrt(u/v)
|
|
78
94
|
function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
|
|
79
|
-
const P =
|
|
95
|
+
const P = ed25519_CURVE.p;
|
|
80
96
|
const v3 = mod(v * v * v, P); // v³
|
|
81
97
|
const v7 = mod(v3 * v3 * v, P); // v⁷
|
|
82
98
|
// (p+3)/8 and (p-5)/8
|
|
@@ -106,29 +122,18 @@ export const ED25519_TORSION_SUBGROUP: string[] = [
|
|
|
106
122
|
'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa',
|
|
107
123
|
];
|
|
108
124
|
|
|
109
|
-
const Fp = /* @__PURE__ */ (() => Field(
|
|
110
|
-
|
|
111
|
-
const ed25519Defaults = /* @__PURE__ */ (() =>
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
h: _8n,
|
|
122
|
-
Gx: BigInt('15112221349535400772501151409588531511454012693041857206046113283949847762202'),
|
|
123
|
-
Gy: BigInt('46316835694926478169428394003475163141307993866256225615783033603165251855960'),
|
|
124
|
-
hash: sha512,
|
|
125
|
-
randomBytes,
|
|
126
|
-
adjustScalarBytes,
|
|
127
|
-
// dom2
|
|
128
|
-
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
129
|
-
// Constant-time, u/√v
|
|
130
|
-
uvRatio,
|
|
131
|
-
}) as const)();
|
|
125
|
+
const Fp = /* @__PURE__ */ (() => Field(ed25519_CURVE.p, undefined, true))();
|
|
126
|
+
|
|
127
|
+
const ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
128
|
+
...ed25519_CURVE,
|
|
129
|
+
Fp,
|
|
130
|
+
hash: sha512,
|
|
131
|
+
adjustScalarBytes,
|
|
132
|
+
// dom2
|
|
133
|
+
// Ratio of u to v. Allows us to combine inversion and square root. Uses algo from RFC8032 5.1.3.
|
|
134
|
+
// Constant-time, u/√v
|
|
135
|
+
uvRatio,
|
|
136
|
+
}))();
|
|
132
137
|
|
|
133
138
|
/**
|
|
134
139
|
* ed25519 curve with EdDSA signatures.
|
|
@@ -176,22 +181,19 @@ export const ed25519ph: CurveFn = /* @__PURE__ */ (() =>
|
|
|
176
181
|
* x25519.getPublicKey(priv) === x25519.scalarMultBase(priv);
|
|
177
182
|
* x25519.getPublicKey(x25519.utils.randomPrivateKey());
|
|
178
183
|
*/
|
|
179
|
-
export const x25519: XCurveFn = /* @__PURE__ */ (() =>
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
nByteLength: 32,
|
|
185
|
-
Gu: BigInt(9),
|
|
184
|
+
export const x25519: XCurveFn = /* @__PURE__ */ (() => {
|
|
185
|
+
const P = ed25519_CURVE.p;
|
|
186
|
+
return montgomery({
|
|
187
|
+
P,
|
|
188
|
+
type: 'x25519',
|
|
186
189
|
powPminus2: (x: bigint): bigint => {
|
|
187
|
-
const P = ED25519_P;
|
|
188
190
|
// x^(p-2) aka x^(2^255-21)
|
|
189
191
|
const { pow_p_5_8, b2 } = ed25519_pow_2_252_3(x);
|
|
190
192
|
return mod(pow2(pow_p_5_8, _3n, P) * b2, P);
|
|
191
193
|
},
|
|
192
194
|
adjustScalarBytes,
|
|
193
|
-
|
|
194
|
-
|
|
195
|
+
});
|
|
196
|
+
})();
|
|
195
197
|
|
|
196
198
|
/**
|
|
197
199
|
* Converts ed25519 public key to x25519 public key. Uses formula:
|
|
@@ -203,7 +205,8 @@ export const x25519: XCurveFn = /* @__PURE__ */ (() =>
|
|
|
203
205
|
* x25519.getSharedSecret(aPriv, edwardsToMontgomeryPub(someonesPub))
|
|
204
206
|
*/
|
|
205
207
|
export function edwardsToMontgomeryPub(edwardsPub: Hex): Uint8Array {
|
|
206
|
-
const
|
|
208
|
+
const bpub = ensureBytes('pub', edwardsPub);
|
|
209
|
+
const { y } = ed25519.Point.fromHex(bpub);
|
|
207
210
|
const _1n = BigInt(1);
|
|
208
211
|
return Fp.toBytes(Fp.create((_1n + y) * Fp.inv(_1n - y)));
|
|
209
212
|
}
|
|
@@ -294,9 +297,9 @@ function map_to_curve_elligator2_edwards25519(u: bigint) {
|
|
|
294
297
|
return { x: Fp.mul(xn, xd_inv), y: Fp.mul(yn, yd_inv) }; // 13. return (xn, xd, yn, yd)
|
|
295
298
|
}
|
|
296
299
|
|
|
297
|
-
export const ed25519_hasher:
|
|
300
|
+
export const ed25519_hasher: H2CHasher<bigint> = /* @__PURE__ */ (() =>
|
|
298
301
|
createHasher(
|
|
299
|
-
ed25519.
|
|
302
|
+
ed25519.Point,
|
|
300
303
|
(scalars: bigint[]) => map_to_curve_elligator2_edwards25519(scalars[0]),
|
|
301
304
|
{
|
|
302
305
|
DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',
|
|
@@ -308,8 +311,8 @@ export const ed25519_hasher: Hasher<bigint> = /* @__PURE__ */ (() =>
|
|
|
308
311
|
hash: sha512,
|
|
309
312
|
}
|
|
310
313
|
))();
|
|
311
|
-
export const hashToCurve:
|
|
312
|
-
export const encodeToCurve:
|
|
314
|
+
export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();
|
|
315
|
+
export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
|
|
313
316
|
ed25519_hasher.encodeToCurve)();
|
|
314
317
|
|
|
315
318
|
function aristp(other: unknown) {
|
|
@@ -345,8 +348,11 @@ const bytes255ToNumberLE = (bytes: Uint8Array) =>
|
|
|
345
348
|
|
|
346
349
|
type ExtendedPoint = ExtPointType;
|
|
347
350
|
|
|
348
|
-
|
|
349
|
-
|
|
351
|
+
/**
|
|
352
|
+
* Computes Elligator map for Ristretto255.
|
|
353
|
+
* Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on
|
|
354
|
+
* the [website](https://ristretto.group/formulas/elligator.html).
|
|
355
|
+
*/
|
|
350
356
|
function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
|
|
351
357
|
const { d } = ed25519.CURVE;
|
|
352
358
|
const P = ed25519.CURVE.Fp.ORDER;
|
|
@@ -366,7 +372,7 @@ function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
|
|
|
366
372
|
const W1 = mod(Nt * SQRT_AD_MINUS_ONE); // 11
|
|
367
373
|
const W2 = mod(_1n - s2); // 12
|
|
368
374
|
const W3 = mod(_1n + s2); // 13
|
|
369
|
-
return new ed25519.
|
|
375
|
+
return new ed25519.Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
|
|
370
376
|
}
|
|
371
377
|
|
|
372
378
|
/**
|
|
@@ -374,7 +380,7 @@ function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
|
|
|
374
380
|
* a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
|
|
375
381
|
* Ristretto point operates in X:Y:Z:T extended coordinates like ExtendedPoint,
|
|
376
382
|
* but it should work in its own namespace: do not combine those two.
|
|
377
|
-
* https://
|
|
383
|
+
* See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
|
|
378
384
|
*/
|
|
379
385
|
class RistPoint implements Group<RistPoint> {
|
|
380
386
|
static BASE: RistPoint;
|
|
@@ -387,14 +393,15 @@ class RistPoint implements Group<RistPoint> {
|
|
|
387
393
|
}
|
|
388
394
|
|
|
389
395
|
static fromAffine(ap: AffinePoint<bigint>): RistPoint {
|
|
390
|
-
return new RistPoint(ed25519.
|
|
396
|
+
return new RistPoint(ed25519.Point.fromAffine(ap));
|
|
391
397
|
}
|
|
392
398
|
|
|
393
399
|
/**
|
|
394
400
|
* Takes uniform output of 64-byte hash function like sha512 and converts it to `RistrettoPoint`.
|
|
395
401
|
* The hash-to-group operation applies Elligator twice and adds the results.
|
|
396
402
|
* **Note:** this is one-way map, there is no conversion from point to hash.
|
|
397
|
-
* https://
|
|
403
|
+
* Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on
|
|
404
|
+
* the [website](https://ristretto.group/formulas/elligator.html).
|
|
398
405
|
* @param hex 64-byte output of a hash function
|
|
399
406
|
*/
|
|
400
407
|
static hashToCurve(hex: Hex): RistPoint {
|
|
@@ -406,16 +413,21 @@ class RistPoint implements Group<RistPoint> {
|
|
|
406
413
|
return new RistPoint(R1.add(R2));
|
|
407
414
|
}
|
|
408
415
|
|
|
416
|
+
static fromBytes(bytes: Uint8Array): RistPoint {
|
|
417
|
+
abytes(bytes);
|
|
418
|
+
return this.fromHex(bytes);
|
|
419
|
+
}
|
|
420
|
+
|
|
409
421
|
/**
|
|
410
422
|
* Converts ristretto-encoded string to ristretto point.
|
|
411
|
-
* https://
|
|
423
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
412
424
|
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
413
425
|
*/
|
|
414
426
|
static fromHex(hex: Hex): RistPoint {
|
|
415
427
|
hex = ensureBytes('ristrettoHex', hex, 32);
|
|
416
428
|
const { a, d } = ed25519.CURVE;
|
|
417
|
-
const P =
|
|
418
|
-
const mod =
|
|
429
|
+
const P = Fp.ORDER;
|
|
430
|
+
const mod = Fp.create;
|
|
419
431
|
const emsg = 'RistrettoPoint.fromHex: the hex is not valid encoding of RistrettoPoint';
|
|
420
432
|
const s = bytes255ToNumberLE(hex);
|
|
421
433
|
// 1. Check that s_bytes is the canonical encoding of a field element, or else abort.
|
|
@@ -435,7 +447,7 @@ class RistPoint implements Group<RistPoint> {
|
|
|
435
447
|
const y = mod(u1 * Dy); // 11
|
|
436
448
|
const t = mod(x * y); // 12
|
|
437
449
|
if (!isValid || isNegativeLE(t, P) || y === _0n) throw new Error(emsg);
|
|
438
|
-
return new RistPoint(new ed25519.
|
|
450
|
+
return new RistPoint(new ed25519.Point(x, y, _1n, t));
|
|
439
451
|
}
|
|
440
452
|
|
|
441
453
|
static msm(points: RistPoint[], scalars: bigint[]): RistPoint {
|
|
@@ -445,12 +457,12 @@ class RistPoint implements Group<RistPoint> {
|
|
|
445
457
|
|
|
446
458
|
/**
|
|
447
459
|
* Encodes ristretto point to Uint8Array.
|
|
448
|
-
* https://
|
|
460
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
449
461
|
*/
|
|
450
|
-
|
|
462
|
+
toBytes(): Uint8Array {
|
|
451
463
|
let { ex: x, ey: y, ez: z, et: t } = this.ep;
|
|
452
|
-
const P =
|
|
453
|
-
const mod =
|
|
464
|
+
const P = Fp.ORDER;
|
|
465
|
+
const mod = Fp.create;
|
|
454
466
|
const u1 = mod(mod(z + y) * mod(z - y)); // 1
|
|
455
467
|
const u2 = mod(x * y); // 2
|
|
456
468
|
// Square root always exists
|
|
@@ -475,20 +487,28 @@ class RistPoint implements Group<RistPoint> {
|
|
|
475
487
|
return numberToBytesLE(s, 32); // 11
|
|
476
488
|
}
|
|
477
489
|
|
|
490
|
+
/** @deprecated use `toBytes` */
|
|
491
|
+
toRawBytes(): Uint8Array {
|
|
492
|
+
return this.toBytes();
|
|
493
|
+
}
|
|
494
|
+
|
|
478
495
|
toHex(): string {
|
|
479
|
-
return bytesToHex(this.
|
|
496
|
+
return bytesToHex(this.toBytes());
|
|
480
497
|
}
|
|
481
498
|
|
|
482
499
|
toString(): string {
|
|
483
500
|
return this.toHex();
|
|
484
501
|
}
|
|
485
502
|
|
|
486
|
-
|
|
503
|
+
/**
|
|
504
|
+
* Compares two Ristretto points.
|
|
505
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
506
|
+
*/
|
|
487
507
|
equals(other: RistPoint): boolean {
|
|
488
508
|
aristp(other);
|
|
489
509
|
const { ex: X1, ey: Y1 } = this.ep;
|
|
490
510
|
const { ex: X2, ey: Y2 } = other.ep;
|
|
491
|
-
const mod =
|
|
511
|
+
const mod = Fp.create;
|
|
492
512
|
// (x1 * y2 == y1 * x2) | (y1 * y2 == x1 * x2)
|
|
493
513
|
const one = mod(X1 * Y2) === mod(Y1 * X2);
|
|
494
514
|
const two = mod(Y1 * Y2) === mod(X1 * X2);
|
|
@@ -521,13 +541,21 @@ class RistPoint implements Group<RistPoint> {
|
|
|
521
541
|
return new RistPoint(this.ep.negate());
|
|
522
542
|
}
|
|
523
543
|
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Wrapper over Edwards Point for ristretto255 from
|
|
547
|
+
* [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
|
|
548
|
+
*/
|
|
524
549
|
export const RistrettoPoint: typeof RistPoint = /* @__PURE__ */ (() => {
|
|
525
|
-
if (!RistPoint.BASE) RistPoint.BASE = new RistPoint(ed25519.
|
|
526
|
-
if (!RistPoint.ZERO) RistPoint.ZERO = new RistPoint(ed25519.
|
|
550
|
+
if (!RistPoint.BASE) RistPoint.BASE = new RistPoint(ed25519.Point.BASE);
|
|
551
|
+
if (!RistPoint.ZERO) RistPoint.ZERO = new RistPoint(ed25519.Point.ZERO);
|
|
527
552
|
return RistPoint;
|
|
528
553
|
})();
|
|
529
554
|
|
|
530
|
-
|
|
555
|
+
/**
|
|
556
|
+
* hash-to-curve for ristretto255.
|
|
557
|
+
* Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B).
|
|
558
|
+
*/
|
|
531
559
|
export const hashToRistretto255 = (msg: Uint8Array, options: htfBasicOpts): RistPoint => {
|
|
532
560
|
const d = options.DST;
|
|
533
561
|
const DST = typeof d === 'string' ? utf8ToBytes(d) : d;
|