@noble/curves 1.9.7 → 2.0.0-beta.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 +520 -505
- package/abstract/bls.d.ts +58 -120
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +108 -152
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +18 -54
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +30 -49
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +18 -77
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +68 -144
- package/abstract/edwards.js.map +1 -1
- package/abstract/fft.js +14 -27
- package/abstract/fft.js.map +1 -1
- package/abstract/hash-to-curve.d.ts +35 -47
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +42 -46
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +5 -17
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +170 -169
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +7 -12
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +22 -29
- package/abstract/montgomery.js.map +1 -1
- package/abstract/oprf.d.ts +282 -0
- package/abstract/oprf.d.ts.map +1 -0
- package/abstract/oprf.js +297 -0
- package/abstract/oprf.js.map +1 -0
- package/abstract/poseidon.d.ts.map +1 -1
- package/abstract/poseidon.js +26 -31
- package/abstract/poseidon.js.map +1 -1
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +43 -19
- package/abstract/tower.js.map +1 -1
- package/abstract/weierstrass.d.ts +77 -168
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +184 -389
- package/abstract/weierstrass.js.map +1 -1
- package/bls12-381.d.ts +5 -11
- package/bls12-381.d.ts.map +1 -1
- package/bls12-381.js +161 -181
- package/bls12-381.js.map +1 -1
- package/bn254.d.ts +59 -11
- package/bn254.d.ts.map +1 -1
- package/bn254.js +69 -97
- package/bn254.js.map +1 -1
- package/ed25519.d.ts +33 -48
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +147 -161
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +27 -36
- package/ed448.d.ts.map +1 -1
- package/ed448.js +143 -164
- package/ed448.js.map +1 -1
- package/index.d.ts +1 -0
- package/index.js +20 -4
- package/index.js.map +1 -1
- package/misc.d.ts +10 -14
- package/misc.d.ts.map +1 -1
- package/misc.js +53 -62
- package/misc.js.map +1 -1
- package/nist.d.ts +31 -16
- package/nist.d.ts.map +1 -1
- package/nist.js +75 -64
- package/nist.js.map +1 -1
- package/package.json +20 -234
- package/secp256k1.d.ts +17 -30
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +59 -73
- package/secp256k1.js.map +1 -1
- package/src/abstract/bls.ts +207 -354
- package/src/abstract/curve.ts +25 -84
- package/src/abstract/edwards.ts +68 -193
- package/src/abstract/hash-to-curve.ts +71 -85
- package/src/abstract/modular.ts +150 -134
- package/src/abstract/montgomery.ts +28 -35
- package/src/abstract/oprf.ts +600 -0
- package/src/abstract/poseidon.ts +6 -8
- package/src/abstract/tower.ts +0 -3
- package/src/abstract/weierstrass.ts +203 -525
- package/src/bls12-381.ts +133 -139
- package/src/bn254.ts +69 -93
- package/src/ed25519.ts +106 -133
- package/src/ed448.ts +111 -138
- package/src/index.ts +19 -3
- package/src/misc.ts +68 -51
- package/src/nist.ts +77 -70
- package/src/secp256k1.ts +46 -81
- package/src/utils.ts +67 -137
- package/src/webcrypto.ts +403 -0
- package/utils.d.ts +31 -38
- package/utils.d.ts.map +1 -1
- package/utils.js +66 -185
- package/utils.js.map +1 -1
- package/webcrypto.d.ts +99 -0
- package/webcrypto.d.ts.map +1 -0
- package/webcrypto.js +256 -0
- package/webcrypto.js.map +1 -0
- package/_shortw_utils.d.ts +0 -19
- package/_shortw_utils.d.ts.map +0 -1
- package/_shortw_utils.js +0 -20
- package/_shortw_utils.js.map +0 -1
- package/abstract/utils.d.ts +0 -78
- package/abstract/utils.d.ts.map +0 -1
- package/abstract/utils.js +0 -73
- package/abstract/utils.js.map +0 -1
- package/esm/_shortw_utils.d.ts +0 -19
- package/esm/_shortw_utils.d.ts.map +0 -1
- package/esm/_shortw_utils.js +0 -16
- package/esm/_shortw_utils.js.map +0 -1
- package/esm/abstract/bls.d.ts +0 -190
- package/esm/abstract/bls.d.ts.map +0 -1
- package/esm/abstract/bls.js +0 -408
- package/esm/abstract/bls.js.map +0 -1
- package/esm/abstract/curve.d.ts +0 -231
- package/esm/abstract/curve.d.ts.map +0 -1
- package/esm/abstract/curve.js +0 -465
- package/esm/abstract/curve.js.map +0 -1
- package/esm/abstract/edwards.d.ts +0 -243
- package/esm/abstract/edwards.d.ts.map +0 -1
- package/esm/abstract/edwards.js +0 -627
- package/esm/abstract/edwards.js.map +0 -1
- package/esm/abstract/fft.d.ts +0 -122
- package/esm/abstract/fft.d.ts.map +0 -1
- package/esm/abstract/fft.js +0 -425
- package/esm/abstract/fft.js.map +0 -1
- package/esm/abstract/hash-to-curve.d.ts +0 -102
- package/esm/abstract/hash-to-curve.d.ts.map +0 -1
- package/esm/abstract/hash-to-curve.js +0 -203
- package/esm/abstract/hash-to-curve.js.map +0 -1
- package/esm/abstract/modular.d.ts +0 -171
- package/esm/abstract/modular.d.ts.map +0 -1
- package/esm/abstract/modular.js +0 -530
- package/esm/abstract/modular.js.map +0 -1
- package/esm/abstract/montgomery.d.ts +0 -30
- package/esm/abstract/montgomery.d.ts.map +0 -1
- package/esm/abstract/montgomery.js +0 -157
- package/esm/abstract/montgomery.js.map +0 -1
- package/esm/abstract/poseidon.d.ts +0 -68
- package/esm/abstract/poseidon.d.ts.map +0 -1
- package/esm/abstract/poseidon.js +0 -296
- package/esm/abstract/poseidon.js.map +0 -1
- package/esm/abstract/tower.d.ts +0 -95
- package/esm/abstract/tower.d.ts.map +0 -1
- package/esm/abstract/tower.js +0 -714
- package/esm/abstract/tower.js.map +0 -1
- package/esm/abstract/utils.d.ts +0 -78
- package/esm/abstract/utils.d.ts.map +0 -1
- package/esm/abstract/utils.js +0 -70
- package/esm/abstract/utils.js.map +0 -1
- package/esm/abstract/weierstrass.d.ts +0 -416
- package/esm/abstract/weierstrass.d.ts.map +0 -1
- package/esm/abstract/weierstrass.js +0 -1413
- package/esm/abstract/weierstrass.js.map +0 -1
- package/esm/bls12-381.d.ts +0 -16
- package/esm/bls12-381.d.ts.map +0 -1
- package/esm/bls12-381.js +0 -705
- package/esm/bls12-381.js.map +0 -1
- package/esm/bn254.d.ts +0 -18
- package/esm/bn254.d.ts.map +0 -1
- package/esm/bn254.js +0 -214
- package/esm/bn254.js.map +0 -1
- package/esm/ed25519.d.ts +0 -106
- package/esm/ed25519.d.ts.map +0 -1
- package/esm/ed25519.js +0 -467
- package/esm/ed25519.js.map +0 -1
- package/esm/ed448.d.ts +0 -100
- package/esm/ed448.d.ts.map +0 -1
- package/esm/ed448.js +0 -459
- package/esm/ed448.js.map +0 -1
- package/esm/index.d.ts +0 -2
- package/esm/index.d.ts.map +0 -1
- package/esm/index.js +0 -17
- package/esm/index.js.map +0 -1
- package/esm/jubjub.d.ts +0 -12
- package/esm/jubjub.d.ts.map +0 -1
- package/esm/jubjub.js +0 -12
- package/esm/jubjub.js.map +0 -1
- package/esm/misc.d.ts +0 -19
- package/esm/misc.d.ts.map +0 -1
- package/esm/misc.js +0 -109
- package/esm/misc.js.map +0 -1
- package/esm/nist.d.ts +0 -21
- package/esm/nist.d.ts.map +0 -1
- package/esm/nist.js +0 -132
- package/esm/nist.js.map +0 -1
- package/esm/p256.d.ts +0 -16
- package/esm/p256.d.ts.map +0 -1
- package/esm/p256.js +0 -16
- package/esm/p256.js.map +0 -1
- package/esm/p384.d.ts +0 -16
- package/esm/p384.d.ts.map +0 -1
- package/esm/p384.js +0 -16
- package/esm/p384.js.map +0 -1
- package/esm/p521.d.ts +0 -16
- package/esm/p521.d.ts.map +0 -1
- package/esm/p521.js +0 -16
- package/esm/p521.js.map +0 -1
- package/esm/package.json +0 -4
- package/esm/pasta.d.ts +0 -10
- package/esm/pasta.d.ts.map +0 -1
- package/esm/pasta.js +0 -10
- package/esm/pasta.js.map +0 -1
- package/esm/secp256k1.d.ts +0 -89
- package/esm/secp256k1.d.ts.map +0 -1
- package/esm/secp256k1.js +0 -294
- package/esm/secp256k1.js.map +0 -1
- package/esm/utils.d.ts +0 -110
- package/esm/utils.d.ts.map +0 -1
- package/esm/utils.js +0 -322
- package/esm/utils.js.map +0 -1
- package/jubjub.d.ts +0 -12
- package/jubjub.d.ts.map +0 -1
- package/jubjub.js +0 -15
- package/jubjub.js.map +0 -1
- package/p256.d.ts +0 -16
- package/p256.d.ts.map +0 -1
- package/p256.js +0 -13
- package/p256.js.map +0 -1
- package/p384.d.ts +0 -16
- package/p384.d.ts.map +0 -1
- package/p384.js +0 -13
- package/p384.js.map +0 -1
- package/p521.d.ts +0 -16
- package/p521.d.ts.map +0 -1
- package/p521.js +0 -13
- package/p521.js.map +0 -1
- package/pasta.d.ts +0 -10
- package/pasta.d.ts.map +0 -1
- package/pasta.js +0 -13
- package/pasta.js.map +0 -1
- package/src/_shortw_utils.ts +0 -21
- package/src/abstract/utils.ts +0 -80
- package/src/jubjub.ts +0 -12
- package/src/p256.ts +0 -15
- package/src/p384.ts +0 -15
- package/src/p521.ts +0 -15
- package/src/package.json +0 -3
- package/src/pasta.ts +0 -9
package/src/ed25519.ts
CHANGED
|
@@ -7,26 +7,27 @@
|
|
|
7
7
|
*/
|
|
8
8
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
9
9
|
import { sha512 } from '@noble/hashes/sha2.js';
|
|
10
|
-
import { abytes, concatBytes,
|
|
11
|
-
import {
|
|
10
|
+
import { abytes, concatBytes, hexToBytes } from '@noble/hashes/utils.js';
|
|
11
|
+
import { type AffinePoint } from './abstract/curve.ts';
|
|
12
12
|
import {
|
|
13
|
+
eddsa,
|
|
14
|
+
edwards,
|
|
13
15
|
PrimeEdwardsPoint,
|
|
14
|
-
|
|
15
|
-
type
|
|
16
|
+
type EdDSA,
|
|
17
|
+
type EdDSAOpts,
|
|
16
18
|
type EdwardsOpts,
|
|
17
19
|
type EdwardsPoint,
|
|
20
|
+
type EdwardsPointCons,
|
|
18
21
|
} from './abstract/edwards.ts';
|
|
19
22
|
import {
|
|
20
23
|
_DST_scalar,
|
|
21
24
|
createHasher,
|
|
22
25
|
expand_message_xmd,
|
|
26
|
+
type H2CDSTOpts,
|
|
23
27
|
type H2CHasher,
|
|
24
28
|
type H2CHasherBase,
|
|
25
|
-
type H2CMethod,
|
|
26
|
-
type htfBasicOpts,
|
|
27
29
|
} from './abstract/hash-to-curve.ts';
|
|
28
30
|
import {
|
|
29
|
-
Field,
|
|
30
31
|
FpInvertBatch,
|
|
31
32
|
FpSqrtEven,
|
|
32
33
|
isNegativeLE,
|
|
@@ -34,19 +35,19 @@ import {
|
|
|
34
35
|
pow2,
|
|
35
36
|
type IField,
|
|
36
37
|
} from './abstract/modular.ts';
|
|
37
|
-
import { montgomery, type MontgomeryECDH
|
|
38
|
-
import {
|
|
38
|
+
import { montgomery, type MontgomeryECDH } from './abstract/montgomery.ts';
|
|
39
|
+
import { createORPF, type OPRF } from './abstract/oprf.ts';
|
|
40
|
+
import { asciiToBytes, bytesToNumberLE, equalBytes } from './utils.ts';
|
|
39
41
|
|
|
40
42
|
// prettier-ignore
|
|
41
|
-
const _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = BigInt(3);
|
|
43
|
+
const _0n = /* @__PURE__ */ BigInt(0), _1n = BigInt(1), _2n = BigInt(2), _3n = /* @__PURE__ */ BigInt(3);
|
|
42
44
|
// prettier-ignore
|
|
43
45
|
const _5n = BigInt(5), _8n = BigInt(8);
|
|
44
46
|
|
|
45
|
-
// P = 2n**255n-19n
|
|
47
|
+
// P = 2n**255n - 19n
|
|
46
48
|
const ed25519_CURVE_p = BigInt(
|
|
47
49
|
'0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed'
|
|
48
50
|
);
|
|
49
|
-
|
|
50
51
|
// N = 2n**252n + 27742317777372353535851937790883648493n
|
|
51
52
|
// a = Fp.create(BigInt(-1))
|
|
52
53
|
// d = -121665/121666 a.k.a. Fp.neg(121665 * Fp.inv(121666))
|
|
@@ -116,70 +117,56 @@ function uvRatio(u: bigint, v: bigint): { isValid: boolean; value: bigint } {
|
|
|
116
117
|
return { isValid: useRoot1 || useRoot2, value: x };
|
|
117
118
|
}
|
|
118
119
|
|
|
119
|
-
const
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
const ed25519Defaults = /* @__PURE__ */ (() => ({
|
|
123
|
-
...ed25519_CURVE,
|
|
124
|
-
Fp,
|
|
125
|
-
hash: sha512,
|
|
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
|
-
}))();
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* ed25519 curve with EdDSA signatures.
|
|
135
|
-
* @example
|
|
136
|
-
* import { ed25519 } from '@noble/curves/ed25519';
|
|
137
|
-
* const { secretKey, publicKey } = ed25519.keygen();
|
|
138
|
-
* const msg = new TextEncoder().encode('hello');
|
|
139
|
-
* const sig = ed25519.sign(msg, priv);
|
|
140
|
-
* ed25519.verify(sig, msg, pub); // Default mode: follows ZIP215
|
|
141
|
-
* ed25519.verify(sig, msg, pub, { zip215: false }); // RFC8032 / FIPS 186-5
|
|
142
|
-
*/
|
|
143
|
-
export const ed25519: CurveFn = /* @__PURE__ */ (() => twistedEdwards(ed25519Defaults))();
|
|
120
|
+
const ed25519_Point = /* @__PURE__ */ edwards(ed25519_CURVE, { uvRatio });
|
|
121
|
+
const Fp = /* @__PURE__ */ (() => ed25519_Point.Fp)();
|
|
122
|
+
const Fn = /* @__PURE__ */ (() => ed25519_Point.Fn)();
|
|
144
123
|
|
|
145
124
|
function ed25519_domain(data: Uint8Array, ctx: Uint8Array, phflag: boolean) {
|
|
146
125
|
if (ctx.length > 255) throw new Error('Context is too big');
|
|
147
126
|
return concatBytes(
|
|
148
|
-
|
|
127
|
+
asciiToBytes('SigEd25519 no Ed25519 collisions'),
|
|
149
128
|
new Uint8Array([phflag ? 1 : 0, ctx.length]),
|
|
150
129
|
ctx,
|
|
151
130
|
data
|
|
152
131
|
);
|
|
153
132
|
}
|
|
154
133
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
...ed25519Defaults,
|
|
159
|
-
domain: ed25519_domain,
|
|
160
|
-
}))();
|
|
134
|
+
function ed(opts: EdDSAOpts) {
|
|
135
|
+
return eddsa(ed25519_Point, sha512, Object.assign({ adjustScalarBytes }, opts));
|
|
136
|
+
}
|
|
161
137
|
|
|
162
|
-
/**
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
138
|
+
/**
|
|
139
|
+
* ed25519 curve with EdDSA signatures.
|
|
140
|
+
* @example
|
|
141
|
+
* ```js
|
|
142
|
+
* import { ed25519 } from '@noble/curves/ed25519.js';
|
|
143
|
+
* const { secretKey, publicKey } = ed25519.keygen();
|
|
144
|
+
* // const publicKey = ed25519.getPublicKey(secretKey);
|
|
145
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
146
|
+
* const sig = ed25519.sign(msg, secretKey);
|
|
147
|
+
* const isValid = ed25519.verify(sig, msg, pub); // ZIP215
|
|
148
|
+
* // RFC8032 / FIPS 186-5
|
|
149
|
+
* const isValid2 = ed25519.verify(sig, msg, pub, { zip215: false });
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export const ed25519: EdDSA = /* @__PURE__ */ ed({});
|
|
153
|
+
/** Context version of ed25519 (ctx for domain separation). See {@link ed25519} */
|
|
154
|
+
export const ed25519ctx: EdDSA = /* @__PURE__ */ ed({ domain: ed25519_domain });
|
|
155
|
+
/** Prehashed version of ed25519. See {@link ed25519} */
|
|
156
|
+
export const ed25519ph: EdDSA = /* @__PURE__ */ ed({ domain: ed25519_domain, prehash: sha512 });
|
|
170
157
|
|
|
171
158
|
/**
|
|
172
159
|
* ECDH using curve25519 aka x25519.
|
|
173
160
|
* @example
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
* const
|
|
177
|
-
*
|
|
178
|
-
* x25519.
|
|
179
|
-
*
|
|
161
|
+
* ```js
|
|
162
|
+
* import { x25519 } from '@noble/curves/ed25519.js';
|
|
163
|
+
* const alice = x25519.keygen();
|
|
164
|
+
* const bob = x25519.keygen();
|
|
165
|
+
* const shared = x25519.getSharedSecret(alice.secretKey, bob.publicKey);
|
|
166
|
+
* ```
|
|
180
167
|
*/
|
|
181
|
-
export const x25519:
|
|
182
|
-
const P =
|
|
168
|
+
export const x25519: MontgomeryECDH = /* @__PURE__ */ (() => {
|
|
169
|
+
const P = ed25519_CURVE_p;
|
|
183
170
|
return montgomery({
|
|
184
171
|
P,
|
|
185
172
|
type: 'x25519',
|
|
@@ -265,9 +252,9 @@ function map_to_curve_elligator2_edwards25519(u: bigint) {
|
|
|
265
252
|
}
|
|
266
253
|
|
|
267
254
|
/** Hashing to ed25519 points / field. RFC 9380 methods. */
|
|
268
|
-
export const ed25519_hasher: H2CHasher<
|
|
255
|
+
export const ed25519_hasher: H2CHasher<EdwardsPointCons> = /* @__PURE__ */ (() =>
|
|
269
256
|
createHasher(
|
|
270
|
-
|
|
257
|
+
ed25519_Point,
|
|
271
258
|
(scalars: bigint[]) => map_to_curve_elligator2_edwards25519(scalars[0]),
|
|
272
259
|
{
|
|
273
260
|
DST: 'edwards25519_XMD:SHA-512_ELL2_RO_',
|
|
@@ -304,17 +291,14 @@ const invertSqrt = (number: bigint) => uvRatio(_1n, number);
|
|
|
304
291
|
const MAX_255B = /* @__PURE__ */ BigInt(
|
|
305
292
|
'0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
|
|
306
293
|
);
|
|
307
|
-
const bytes255ToNumberLE = (bytes: Uint8Array) =>
|
|
308
|
-
ed25519.Point.Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
309
|
-
|
|
310
|
-
type ExtendedPoint = EdwardsPoint;
|
|
294
|
+
const bytes255ToNumberLE = (bytes: Uint8Array) => Fp.create(bytesToNumberLE(bytes) & MAX_255B);
|
|
311
295
|
|
|
312
296
|
/**
|
|
313
297
|
* Computes Elligator map for Ristretto255.
|
|
314
298
|
* Described in [RFC9380](https://www.rfc-editor.org/rfc/rfc9380#appendix-B) and on
|
|
315
299
|
* the [website](https://ristretto.group/formulas/elligator.html).
|
|
316
300
|
*/
|
|
317
|
-
function calcElligatorRistrettoMap(r0: bigint):
|
|
301
|
+
function calcElligatorRistrettoMap(r0: bigint): EdwardsPoint {
|
|
318
302
|
const { d } = ed25519_CURVE;
|
|
319
303
|
const P = ed25519_CURVE_p;
|
|
320
304
|
const mod = (n: bigint) => Fp.create(n);
|
|
@@ -333,24 +317,15 @@ function calcElligatorRistrettoMap(r0: bigint): ExtendedPoint {
|
|
|
333
317
|
const W1 = mod(Nt * SQRT_AD_MINUS_ONE); // 11
|
|
334
318
|
const W2 = mod(_1n - s2); // 12
|
|
335
319
|
const W3 = mod(_1n + s2); // 13
|
|
336
|
-
return new
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
function ristretto255_map(bytes: Uint8Array): _RistrettoPoint {
|
|
340
|
-
abytes(bytes, 64);
|
|
341
|
-
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
342
|
-
const R1 = calcElligatorRistrettoMap(r1);
|
|
343
|
-
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
344
|
-
const R2 = calcElligatorRistrettoMap(r2);
|
|
345
|
-
return new _RistrettoPoint(R1.add(R2));
|
|
320
|
+
return new ed25519_Point(mod(W0 * W3), mod(W2 * W1), mod(W1 * W3), mod(W0 * W2));
|
|
346
321
|
}
|
|
347
322
|
|
|
348
323
|
/**
|
|
349
324
|
* Wrapper over Edwards Point for ristretto255.
|
|
350
325
|
*
|
|
351
|
-
* Each ed25519/
|
|
326
|
+
* Each ed25519/EdwardsPoint has 8 different equivalent points. This can be
|
|
352
327
|
* a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
|
|
353
|
-
* Ristretto point operates in X:Y:Z:T extended coordinates like
|
|
328
|
+
* Ristretto point operates in X:Y:Z:T extended coordinates like EdwardsPoint,
|
|
354
329
|
* but it should work in its own namespace: do not combine those two.
|
|
355
330
|
* See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
|
|
356
331
|
*/
|
|
@@ -359,10 +334,10 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
|
|
|
359
334
|
// because typescript strips comments, which makes bundlers disable tree-shaking.
|
|
360
335
|
// prettier-ignore
|
|
361
336
|
static BASE: _RistrettoPoint =
|
|
362
|
-
/* @__PURE__ */ (() => new _RistrettoPoint(
|
|
337
|
+
/* @__PURE__ */ (() => new _RistrettoPoint(ed25519_Point.BASE))();
|
|
363
338
|
// prettier-ignore
|
|
364
339
|
static ZERO: _RistrettoPoint =
|
|
365
|
-
/* @__PURE__ */ (() => new _RistrettoPoint(
|
|
340
|
+
/* @__PURE__ */ (() => new _RistrettoPoint(ed25519_Point.ZERO))();
|
|
366
341
|
// prettier-ignore
|
|
367
342
|
static Fp: IField<bigint> =
|
|
368
343
|
/* @__PURE__ */ (() => Fp)();
|
|
@@ -370,12 +345,12 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
|
|
|
370
345
|
static Fn: IField<bigint> =
|
|
371
346
|
/* @__PURE__ */ (() => Fn)();
|
|
372
347
|
|
|
373
|
-
constructor(ep:
|
|
348
|
+
constructor(ep: EdwardsPoint) {
|
|
374
349
|
super(ep);
|
|
375
350
|
}
|
|
376
351
|
|
|
377
352
|
static fromAffine(ap: AffinePoint<bigint>): _RistrettoPoint {
|
|
378
|
-
return new _RistrettoPoint(
|
|
353
|
+
return new _RistrettoPoint(ed25519_Point.fromAffine(ap));
|
|
379
354
|
}
|
|
380
355
|
|
|
381
356
|
protected assertSame(other: _RistrettoPoint): void {
|
|
@@ -386,11 +361,6 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
|
|
|
386
361
|
return new _RistrettoPoint(ep);
|
|
387
362
|
}
|
|
388
363
|
|
|
389
|
-
/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
|
|
390
|
-
static hashToCurve(hex: Hex): _RistrettoPoint {
|
|
391
|
-
return ristretto255_map(ensureBytes('ristrettoHash', hex, 64));
|
|
392
|
-
}
|
|
393
|
-
|
|
394
364
|
static fromBytes(bytes: Uint8Array): _RistrettoPoint {
|
|
395
365
|
abytes(bytes, 32);
|
|
396
366
|
const { a, d } = ed25519_CURVE;
|
|
@@ -416,7 +386,7 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
|
|
|
416
386
|
const t = mod(x * y); // 12
|
|
417
387
|
if (!isValid || isNegativeLE(t, P) || y === _0n)
|
|
418
388
|
throw new Error('invalid ristretto255 encoding 2');
|
|
419
|
-
return new _RistrettoPoint(new
|
|
389
|
+
return new _RistrettoPoint(new ed25519_Point(x, y, _1n, t));
|
|
420
390
|
}
|
|
421
391
|
|
|
422
392
|
/**
|
|
@@ -424,12 +394,8 @@ class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
|
|
|
424
394
|
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
425
395
|
* @param hex Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
426
396
|
*/
|
|
427
|
-
static fromHex(hex:
|
|
428
|
-
return _RistrettoPoint.fromBytes(
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
static msm(points: _RistrettoPoint[], scalars: bigint[]): _RistrettoPoint {
|
|
432
|
-
return pippenger(_RistrettoPoint, ed25519.Point.Fn, points, scalars);
|
|
397
|
+
static fromHex(hex: string): _RistrettoPoint {
|
|
398
|
+
return _RistrettoPoint.fromBytes(hexToBytes(hex));
|
|
433
399
|
}
|
|
434
400
|
|
|
435
401
|
/**
|
|
@@ -489,25 +455,59 @@ export const ristretto255: {
|
|
|
489
455
|
} = { Point: _RistrettoPoint };
|
|
490
456
|
|
|
491
457
|
/** Hashing to ristretto255 points / field. RFC 9380 methods. */
|
|
492
|
-
export const ristretto255_hasher: H2CHasherBase<
|
|
493
|
-
|
|
458
|
+
export const ristretto255_hasher: H2CHasherBase<typeof _RistrettoPoint> = {
|
|
459
|
+
Point: _RistrettoPoint,
|
|
460
|
+
/**
|
|
461
|
+
* Spec: https://www.rfc-editor.org/rfc/rfc9380.html#name-hashing-to-ristretto255. Caveats:
|
|
462
|
+
* * There are no test vectors
|
|
463
|
+
* * encodeToCurve / mapToCurve is undefined
|
|
464
|
+
* * mapToCurve would be `calcElligatorRistrettoMap(scalars[0])`, not ristretto255_map!
|
|
465
|
+
* * hashToScalar is undefined too, so we just use OPRF implementation
|
|
466
|
+
* * We cannot re-use 'createHasher', because ristretto255_map is different algorithm/RFC
|
|
467
|
+
(os2ip -> bytes255ToNumberLE)
|
|
468
|
+
* * mapToCurve == calcElligatorRistrettoMap, hashToCurve == ristretto255_map
|
|
469
|
+
* * hashToScalar is undefined in RFC9380 for ristretto, we are using version from OPRF here, using bytes255ToNumblerLE will create different result if we use bytes255ToNumberLE as os2ip
|
|
470
|
+
* * current version is closest to spec.
|
|
471
|
+
*/
|
|
472
|
+
hashToCurve(msg: Uint8Array, options?: H2CDSTOpts): _RistrettoPoint {
|
|
473
|
+
// == 'hash_to_ristretto255'
|
|
494
474
|
const DST = options?.DST || 'ristretto255_XMD:SHA-512_R255MAP_RO_';
|
|
495
475
|
const xmd = expand_message_xmd(msg, DST, 64, sha512);
|
|
496
|
-
|
|
476
|
+
// NOTE: RFC 9380 incorrectly calls this function 'ristretto255_map', in RFC 9496 map was function inside (per point)
|
|
477
|
+
// That also lead to confustion that ristretto255_map is mapToCurve (it is not! it is old hashToCurve)
|
|
478
|
+
return ristretto255_hasher.deriveToCurve!(xmd);
|
|
497
479
|
},
|
|
498
|
-
hashToScalar(msg: Uint8Array, options:
|
|
480
|
+
hashToScalar(msg: Uint8Array, options: H2CDSTOpts = { DST: _DST_scalar }) {
|
|
499
481
|
const xmd = expand_message_xmd(msg, options.DST, 64, sha512);
|
|
500
482
|
return Fn.create(bytesToNumberLE(xmd));
|
|
501
483
|
},
|
|
484
|
+
/**
|
|
485
|
+
* HashToCurve-like construction based on RFC 9496 (Element Derivation).
|
|
486
|
+
* Converts 64 uniform random bytes into a curve point.
|
|
487
|
+
*
|
|
488
|
+
* WARNING: This represents an older hash-to-curve construction, preceding the finalization of RFC 9380.
|
|
489
|
+
* It was later reused as a component in the newer `hash_to_ristretto255` function defined in RFC 9380.
|
|
490
|
+
*/
|
|
491
|
+
deriveToCurve(bytes: Uint8Array): _RistrettoPoint {
|
|
492
|
+
// https://www.rfc-editor.org/rfc/rfc9496.html#name-element-derivation
|
|
493
|
+
abytes(bytes, 64);
|
|
494
|
+
const r1 = bytes255ToNumberLE(bytes.subarray(0, 32));
|
|
495
|
+
const R1 = calcElligatorRistrettoMap(r1);
|
|
496
|
+
const r2 = bytes255ToNumberLE(bytes.subarray(32, 64));
|
|
497
|
+
const R2 = calcElligatorRistrettoMap(r2);
|
|
498
|
+
return new _RistrettoPoint(R1.add(R2));
|
|
499
|
+
},
|
|
502
500
|
};
|
|
503
501
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
502
|
+
/** ristretto255 OPRF, defined in RFC 9497. */
|
|
503
|
+
export const ristretto255_oprf: OPRF = /* @__PURE__ */ (() =>
|
|
504
|
+
createORPF({
|
|
505
|
+
name: 'ristretto255-SHA512',
|
|
506
|
+
Point: _RistrettoPoint,
|
|
507
|
+
hash: sha512,
|
|
508
|
+
hashToGroup: ristretto255_hasher.hashToCurve,
|
|
509
|
+
hashToScalar: ristretto255_hasher.hashToScalar,
|
|
510
|
+
}))();
|
|
511
511
|
|
|
512
512
|
/**
|
|
513
513
|
* Weird / bogus points, useful for debugging.
|
|
@@ -525,30 +525,3 @@ export const ED25519_TORSION_SUBGROUP: string[] = [
|
|
|
525
525
|
'0000000000000000000000000000000000000000000000000000000000000000',
|
|
526
526
|
'c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa',
|
|
527
527
|
];
|
|
528
|
-
|
|
529
|
-
/** @deprecated use `ed25519.utils.toMontgomery` */
|
|
530
|
-
export function edwardsToMontgomeryPub(edwardsPub: Hex): Uint8Array {
|
|
531
|
-
return ed25519.utils.toMontgomery(ensureBytes('pub', edwardsPub));
|
|
532
|
-
}
|
|
533
|
-
/** @deprecated use `ed25519.utils.toMontgomery` */
|
|
534
|
-
export const edwardsToMontgomery: typeof edwardsToMontgomeryPub = edwardsToMontgomeryPub;
|
|
535
|
-
|
|
536
|
-
/** @deprecated use `ed25519.utils.toMontgomerySecret` */
|
|
537
|
-
export function edwardsToMontgomeryPriv(edwardsPriv: Uint8Array): Uint8Array {
|
|
538
|
-
return ed25519.utils.toMontgomerySecret(ensureBytes('pub', edwardsPriv));
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
/** @deprecated use `ristretto255.Point` */
|
|
542
|
-
export const RistrettoPoint: typeof _RistrettoPoint = _RistrettoPoint;
|
|
543
|
-
/** @deprecated use `import { ed25519_hasher } from '@noble/curves/ed25519.js';` */
|
|
544
|
-
export const hashToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() => ed25519_hasher.hashToCurve)();
|
|
545
|
-
/** @deprecated use `import { ed25519_hasher } from '@noble/curves/ed25519.js';` */
|
|
546
|
-
export const encodeToCurve: H2CMethod<bigint> = /* @__PURE__ */ (() =>
|
|
547
|
-
ed25519_hasher.encodeToCurve)();
|
|
548
|
-
type RistHasher = (msg: Uint8Array, options: htfBasicOpts) => _RistrettoPoint;
|
|
549
|
-
/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
|
|
550
|
-
export const hashToRistretto255: RistHasher = /* @__PURE__ */ (() =>
|
|
551
|
-
ristretto255_hasher.hashToCurve as RistHasher)();
|
|
552
|
-
/** @deprecated use `import { ristretto255_hasher } from '@noble/curves/ed25519.js';` */
|
|
553
|
-
export const hash_to_ristretto255: RistHasher = /* @__PURE__ */ (() =>
|
|
554
|
-
ristretto255_hasher.hashToCurve as RistHasher)();
|