@decryption/curves 0.2.0
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/LICENSE +21 -0
- package/README.md +75 -0
- package/abstract/bls.d.ts +410 -0
- package/abstract/bls.js +433 -0
- package/abstract/curve.d.ts +441 -0
- package/abstract/curve.js +622 -0
- package/abstract/edwards.d.ts +319 -0
- package/abstract/edwards.js +697 -0
- package/abstract/fft.d.ts +438 -0
- package/abstract/fft.js +580 -0
- package/abstract/frost.d.ts +292 -0
- package/abstract/frost.js +705 -0
- package/abstract/hash-to-curve.d.ts +238 -0
- package/abstract/hash-to-curve.js +353 -0
- package/abstract/modular.d.ts +550 -0
- package/abstract/modular.js +872 -0
- package/abstract/montgomery.d.ts +95 -0
- package/abstract/montgomery.js +180 -0
- package/abstract/oprf.d.ts +354 -0
- package/abstract/oprf.js +358 -0
- package/abstract/poseidon.d.ts +198 -0
- package/abstract/poseidon.js +471 -0
- package/abstract/tower.d.ts +213 -0
- package/abstract/tower.js +963 -0
- package/abstract/weierstrass.d.ts +587 -0
- package/abstract/weierstrass.js +1421 -0
- package/bls12-381.d.ts +23 -0
- package/bls12-381.js +680 -0
- package/bn254.d.ts +74 -0
- package/bn254.js +248 -0
- package/ed25519.d.ts +211 -0
- package/ed25519.js +626 -0
- package/ed448.d.ts +184 -0
- package/ed448.js +596 -0
- package/esm/abstract/bls.js +429 -0
- package/esm/abstract/curve.js +610 -0
- package/esm/abstract/edwards.js +691 -0
- package/esm/abstract/fft.js +567 -0
- package/esm/abstract/frost.js +702 -0
- package/esm/abstract/hash-to-curve.js +345 -0
- package/esm/abstract/modular.js +849 -0
- package/esm/abstract/montgomery.js +177 -0
- package/esm/abstract/oprf.js +355 -0
- package/esm/abstract/poseidon.js +462 -0
- package/esm/abstract/tower.js +925 -0
- package/esm/abstract/weierstrass.js +1411 -0
- package/esm/bls12-381.js +677 -0
- package/esm/bn254.js +244 -0
- package/esm/ed25519.js +622 -0
- package/esm/ed448.js +593 -0
- package/esm/index.js +38 -0
- package/esm/misc.js +211 -0
- package/esm/nist.js +266 -0
- package/esm/secp256k1.js +465 -0
- package/esm/utils.js +597 -0
- package/esm/webcrypto.js +362 -0
- package/index.d.ts +0 -0
- package/index.js +38 -0
- package/misc.d.ts +113 -0
- package/misc.js +216 -0
- package/nist.d.ts +136 -0
- package/nist.js +269 -0
- package/package.json +46 -0
- package/secp256k1.d.ts +152 -0
- package/secp256k1.js +468 -0
- package/utils.d.ts +535 -0
- package/utils.js +626 -0
- package/webcrypto.d.ts +240 -0
- package/webcrypto.js +365 -0
package/bn254.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* bn254, previously known as alt_bn_128, when it had 128-bit security.
|
|
3
|
+
|
|
4
|
+
Barbulescu-Duquesne 2017 shown it's weaker: just about 100 bits,
|
|
5
|
+
so the naming has been adjusted to its prime bit count:
|
|
6
|
+
https://hal.science/hal-01534101/file/main.pdf.
|
|
7
|
+
Compatible with EIP-196 and EIP-197.
|
|
8
|
+
|
|
9
|
+
There are huge compatibility issues in the ecosystem:
|
|
10
|
+
|
|
11
|
+
1. Different libraries call it in different ways: "bn254", "bn256", "alt_bn128", "bn128".
|
|
12
|
+
2. libff has bn128, but it's a different curve with different G2:
|
|
13
|
+
https://github.com/scipr-lab/libff/blob/a44f482e18b8ac04d034c193bd9d7df7817ad73f/libff/algebra/curves/bn128/bn128_init.cpp#L166-L169
|
|
14
|
+
3. halo2curves bn256 is also incompatible and returns different outputs
|
|
15
|
+
|
|
16
|
+
We don't implement Point methods toHex / toBytes.
|
|
17
|
+
To work around this limitation, has to initialize points on their own from BigInts.
|
|
18
|
+
Reason it's not implemented is because [there is no standard](https://github.com/privacy-scaling-explorations/halo2curves/issues/109).
|
|
19
|
+
Points of divergence:
|
|
20
|
+
|
|
21
|
+
- Endianness: LE vs BE (byte-swapped)
|
|
22
|
+
- Flags as first hex bits (similar to BLS) vs no-flags
|
|
23
|
+
- Imaginary part last in G2 vs first (c0, c1 vs c1, c0)
|
|
24
|
+
|
|
25
|
+
The goal of our implementation is to support "Ethereum" variant of the curve,
|
|
26
|
+
because it at least has specs:
|
|
27
|
+
|
|
28
|
+
- EIP196 (https://eips.ethereum.org/EIPS/eip-196) describes bn254 ECADD and ECMUL opcodes for EVM
|
|
29
|
+
- EIP197 (https://eips.ethereum.org/EIPS/eip-197) describes bn254 pairings
|
|
30
|
+
- It's hard: EIPs don't have proper tests. EIP-197 returns boolean output instead of Fp12
|
|
31
|
+
- The existing implementations are bad. Some are deprecated:
|
|
32
|
+
- https://github.com/paritytech/bn (old version)
|
|
33
|
+
- https://github.com/ewasm/ethereum-bn128.rs (uses paritytech/bn)
|
|
34
|
+
- https://github.com/zcash-hackworks/bn
|
|
35
|
+
- https://github.com/arkworks-rs/curves/blob/master/bn254/src/lib.rs
|
|
36
|
+
- Python implementations use different towers and produce different Fp12 outputs:
|
|
37
|
+
- https://github.com/ethereum/py_pairing
|
|
38
|
+
- https://github.com/ethereum/py_ecc/tree/main/py_ecc/bn128
|
|
39
|
+
- Points are encoded differently in different implementations
|
|
40
|
+
|
|
41
|
+
### Params
|
|
42
|
+
Seed (X): 4965661367192848881
|
|
43
|
+
Fr: (36x⁴+36x³+18x²+6x+1)
|
|
44
|
+
Fp: (36x⁴+36x³+24x²+6x+1)
|
|
45
|
+
(E / Fp ): Y² = X³+3
|
|
46
|
+
(Et / Fp²): Y² = X³+3/(u+9) (D-type twist)
|
|
47
|
+
Ate loop size: 6x+2
|
|
48
|
+
|
|
49
|
+
### Towers
|
|
50
|
+
- Fp²[u] = Fp/u²+1
|
|
51
|
+
- Fp⁶[v] = Fp²/v³-9-u
|
|
52
|
+
- Fp¹²[w] = Fp⁶/w²-v
|
|
53
|
+
|
|
54
|
+
* @module
|
|
55
|
+
*/
|
|
56
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
57
|
+
import { type BlsCurvePair, type BlsPostPrecomputeFn } from './abstract/bls';
|
|
58
|
+
import { type IField } from './abstract/modular';
|
|
59
|
+
import { type TRet } from './utils';
|
|
60
|
+
/** bn254 scalar field. */
|
|
61
|
+
export declare const bn254_Fr: TRet<IField<bigint>>;
|
|
62
|
+
export declare const _postPrecompute: BlsPostPrecomputeFn;
|
|
63
|
+
/**
|
|
64
|
+
* bn254 (a.k.a. alt_bn128) pairing-friendly curve.
|
|
65
|
+
* Contains G1 / G2 operations and pairings only; the commented-out
|
|
66
|
+
* hash-to-curve and signature surface is intentionally not exposed here.
|
|
67
|
+
* @example
|
|
68
|
+
* Compute a pairing from the two generator points.
|
|
69
|
+
*
|
|
70
|
+
* ```ts
|
|
71
|
+
* const gt = bn254.pairing(bn254.G1.Point.BASE, bn254.G2.Point.BASE);
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare const bn254: BlsCurvePair;
|
package/bn254.js
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.bn254 = exports._postPrecompute = exports.bn254_Fr = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* bn254, previously known as alt_bn_128, when it had 128-bit security.
|
|
6
|
+
|
|
7
|
+
Barbulescu-Duquesne 2017 shown it's weaker: just about 100 bits,
|
|
8
|
+
so the naming has been adjusted to its prime bit count:
|
|
9
|
+
https://hal.science/hal-01534101/file/main.pdf.
|
|
10
|
+
Compatible with EIP-196 and EIP-197.
|
|
11
|
+
|
|
12
|
+
There are huge compatibility issues in the ecosystem:
|
|
13
|
+
|
|
14
|
+
1. Different libraries call it in different ways: "bn254", "bn256", "alt_bn128", "bn128".
|
|
15
|
+
2. libff has bn128, but it's a different curve with different G2:
|
|
16
|
+
https://github.com/scipr-lab/libff/blob/a44f482e18b8ac04d034c193bd9d7df7817ad73f/libff/algebra/curves/bn128/bn128_init.cpp#L166-L169
|
|
17
|
+
3. halo2curves bn256 is also incompatible and returns different outputs
|
|
18
|
+
|
|
19
|
+
We don't implement Point methods toHex / toBytes.
|
|
20
|
+
To work around this limitation, has to initialize points on their own from BigInts.
|
|
21
|
+
Reason it's not implemented is because [there is no standard](https://github.com/privacy-scaling-explorations/halo2curves/issues/109).
|
|
22
|
+
Points of divergence:
|
|
23
|
+
|
|
24
|
+
- Endianness: LE vs BE (byte-swapped)
|
|
25
|
+
- Flags as first hex bits (similar to BLS) vs no-flags
|
|
26
|
+
- Imaginary part last in G2 vs first (c0, c1 vs c1, c0)
|
|
27
|
+
|
|
28
|
+
The goal of our implementation is to support "Ethereum" variant of the curve,
|
|
29
|
+
because it at least has specs:
|
|
30
|
+
|
|
31
|
+
- EIP196 (https://eips.ethereum.org/EIPS/eip-196) describes bn254 ECADD and ECMUL opcodes for EVM
|
|
32
|
+
- EIP197 (https://eips.ethereum.org/EIPS/eip-197) describes bn254 pairings
|
|
33
|
+
- It's hard: EIPs don't have proper tests. EIP-197 returns boolean output instead of Fp12
|
|
34
|
+
- The existing implementations are bad. Some are deprecated:
|
|
35
|
+
- https://github.com/paritytech/bn (old version)
|
|
36
|
+
- https://github.com/ewasm/ethereum-bn128.rs (uses paritytech/bn)
|
|
37
|
+
- https://github.com/zcash-hackworks/bn
|
|
38
|
+
- https://github.com/arkworks-rs/curves/blob/master/bn254/src/lib.rs
|
|
39
|
+
- Python implementations use different towers and produce different Fp12 outputs:
|
|
40
|
+
- https://github.com/ethereum/py_pairing
|
|
41
|
+
- https://github.com/ethereum/py_ecc/tree/main/py_ecc/bn128
|
|
42
|
+
- Points are encoded differently in different implementations
|
|
43
|
+
|
|
44
|
+
### Params
|
|
45
|
+
Seed (X): 4965661367192848881
|
|
46
|
+
Fr: (36x⁴+36x³+18x²+6x+1)
|
|
47
|
+
Fp: (36x⁴+36x³+24x²+6x+1)
|
|
48
|
+
(E / Fp ): Y² = X³+3
|
|
49
|
+
(Et / Fp²): Y² = X³+3/(u+9) (D-type twist)
|
|
50
|
+
Ate loop size: 6x+2
|
|
51
|
+
|
|
52
|
+
### Towers
|
|
53
|
+
- Fp²[u] = Fp/u²+1
|
|
54
|
+
- Fp⁶[v] = Fp²/v³-9-u
|
|
55
|
+
- Fp¹²[w] = Fp⁶/w²-v
|
|
56
|
+
|
|
57
|
+
* @module
|
|
58
|
+
*/
|
|
59
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
60
|
+
const bls_1 = require("./abstract/bls");
|
|
61
|
+
const modular_1 = require("./abstract/modular");
|
|
62
|
+
const tower_1 = require("./abstract/tower");
|
|
63
|
+
const weierstrass_1 = require("./abstract/weierstrass");
|
|
64
|
+
const utils_1 = require("./utils");
|
|
65
|
+
// prettier-ignore
|
|
66
|
+
const _0n = /* @__PURE__ */ BigInt(0), _1n = /* @__PURE__ */ BigInt(1), _2n = /* @__PURE__ */ BigInt(2), _3n = /* @__PURE__ */ BigInt(3);
|
|
67
|
+
const _6n = /* @__PURE__ */ BigInt(6);
|
|
68
|
+
// Locally documented BN pairing seed. EIP-197 does not name this scalar
|
|
69
|
+
// directly; noble stores the positive value and derives any `-x` uses later.
|
|
70
|
+
const BN_X = /* @__PURE__ */ BigInt('4965661367192848881');
|
|
71
|
+
// Bit width of the stored seed itself, not the derived Miller-loop scalar `6x+2`.
|
|
72
|
+
const BN_X_LEN = /* @__PURE__ */ (() => (0, utils_1.bitLen)(BN_X))();
|
|
73
|
+
// Derived scalar used by the optimized G2 subgroup test required by EIP-197.
|
|
74
|
+
const SIX_X_SQUARED = /* @__PURE__ */ (() => _6n * BN_X ** _2n)();
|
|
75
|
+
const bn254_G1_CURVE = {
|
|
76
|
+
p: BigInt('0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47'),
|
|
77
|
+
n: BigInt('0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001'),
|
|
78
|
+
// The Ethereum specs define G1 as prime-order but do not spell out the
|
|
79
|
+
// cofactor separately; `h = 1` is the implementation-derived value.
|
|
80
|
+
h: _1n,
|
|
81
|
+
a: _0n,
|
|
82
|
+
b: _3n,
|
|
83
|
+
Gx: _1n,
|
|
84
|
+
Gy: BigInt(2),
|
|
85
|
+
};
|
|
86
|
+
// r == n
|
|
87
|
+
// Finite field over r. It's for convenience and is not used in the code below,
|
|
88
|
+
// and its canonical `fromBytes()` decoder is stricter than the EIP-196 MUL
|
|
89
|
+
// scalar rule that accepts any 256-bit integer.
|
|
90
|
+
// These factories are side-effect free; mark them pure so single-export bundles can drop the rest.
|
|
91
|
+
/** bn254 scalar field. */
|
|
92
|
+
exports.bn254_Fr = (() => (0, modular_1.Field)(bn254_G1_CURVE.n))();
|
|
93
|
+
// `3 / (i + 9)` from EIP-197, stored in noble's internal `(c0, c1) = (b, a)`
|
|
94
|
+
// order rather than the spec's `a * i + b` notation.
|
|
95
|
+
const Fp2B = /* @__PURE__ */ (() => ({
|
|
96
|
+
c0: BigInt('19485874751759354771024239261021720505790618469301721065564631296452457478373'),
|
|
97
|
+
c1: BigInt('266929791119991161246907387137283842545076965332900288569378510910307636690'),
|
|
98
|
+
}))();
|
|
99
|
+
// Bootstrap binding: `Fp12finalExponentiate` needs to reference the finished
|
|
100
|
+
// field object while `tower12(...)` is still constructing it.
|
|
101
|
+
let Fp12;
|
|
102
|
+
const tower = /* @__PURE__ */ (() => {
|
|
103
|
+
const res = (0, tower_1.tower12)({
|
|
104
|
+
ORDER: bn254_G1_CURVE.p,
|
|
105
|
+
X_LEN: BN_X_LEN,
|
|
106
|
+
// Public `Fp2.NONRESIDUE` below is the sextic-tower seed `(9, 1)`, not the
|
|
107
|
+
// quadratic relation `i^2 + 1 = 0` from the EIP text.
|
|
108
|
+
FP2_NONRESIDUE: [BigInt(9), _1n],
|
|
109
|
+
Fp2mulByB: (num) => Fp2.mul(num, Fp2B),
|
|
110
|
+
Fp12finalExponentiate: (num) => {
|
|
111
|
+
const powMinusX = (num) => Fp12.conjugate(Fp12._cyclotomicExp(num, BN_X));
|
|
112
|
+
const r0 = Fp12.mul(Fp12.conjugate(num), Fp12.inv(num));
|
|
113
|
+
const r = Fp12.mul(Fp12.frobeniusMap(r0, 2), r0);
|
|
114
|
+
const y1 = Fp12._cyclotomicSquare(powMinusX(r));
|
|
115
|
+
const y2 = Fp12.mul(Fp12._cyclotomicSquare(y1), y1);
|
|
116
|
+
const y4 = powMinusX(y2);
|
|
117
|
+
const y6 = powMinusX(Fp12._cyclotomicSquare(y4));
|
|
118
|
+
const y8 = Fp12.mul(Fp12.mul(Fp12.conjugate(y6), y4), Fp12.conjugate(y2));
|
|
119
|
+
const y9 = Fp12.mul(y8, y1);
|
|
120
|
+
return Fp12.mul(Fp12.frobeniusMap(Fp12.mul(Fp12.conjugate(r), y9), 3), Fp12.mul(Fp12.frobeniusMap(y8, 2), Fp12.mul(Fp12.frobeniusMap(y9, 1), Fp12.mul(Fp12.mul(y8, y4), r))));
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
Fp12 = res.Fp12;
|
|
124
|
+
return res;
|
|
125
|
+
})();
|
|
126
|
+
const Fp = /* @__PURE__ */ (() => tower.Fp)();
|
|
127
|
+
const Fp2 = /* @__PURE__ */ (() => tower.Fp2)();
|
|
128
|
+
// END OF CURVE FIELDS
|
|
129
|
+
// BN254 uses the same tower seed `(9, 1)` for the Frobenius helper that powers
|
|
130
|
+
// the divisive-twist G2 endomorphism.
|
|
131
|
+
let frob;
|
|
132
|
+
const getFrob = () => frob || (frob = (0, tower_1.psiFrobenius)(Fp, Fp2, Fp2.NONRESIDUE));
|
|
133
|
+
// Eager psiFrobenius setup now dominates `bn254.js` import, so defer it to
|
|
134
|
+
// first use. After that these locals are rewritten to the direct helper refs.
|
|
135
|
+
let psi = (x, y) => {
|
|
136
|
+
const fn = getFrob().psi;
|
|
137
|
+
psi = fn;
|
|
138
|
+
return fn(x, y);
|
|
139
|
+
};
|
|
140
|
+
let G2psi = (c, P) => {
|
|
141
|
+
const fn = getFrob().G2psi;
|
|
142
|
+
G2psi = fn;
|
|
143
|
+
return fn(c, P);
|
|
144
|
+
};
|
|
145
|
+
const _postPrecompute = (Rx, Ry, Rz, Qx, Qy, pointAdd) => {
|
|
146
|
+
const q = psi(Qx, Qy);
|
|
147
|
+
({ Rx, Ry, Rz } = pointAdd(Rx, Ry, Rz, q[0], q[1]));
|
|
148
|
+
const q2 = psi(q[0], q[1]);
|
|
149
|
+
pointAdd(Rx, Ry, Rz, q2[0], Fp2.neg(q2[1]));
|
|
150
|
+
};
|
|
151
|
+
exports._postPrecompute = _postPrecompute;
|
|
152
|
+
// cofactor: (36 * X^4) + (36 * X^3) + (30 * X^2) + 6*X + 1
|
|
153
|
+
const bn254_G2_CURVE = /* @__PURE__ */ (() => ({
|
|
154
|
+
p: Fp2.ORDER,
|
|
155
|
+
n: bn254_G1_CURVE.n,
|
|
156
|
+
// As with G1, the Ethereum specs do not spell out the G2 cofactor
|
|
157
|
+
// separately; this literal is the implementation-derived value.
|
|
158
|
+
h: BigInt('0x30644e72e131a029b85045b68181585e06ceecda572a2489345f2299c0f9fa8d'),
|
|
159
|
+
a: Fp2.ZERO,
|
|
160
|
+
b: Fp2B,
|
|
161
|
+
Gx: Fp2.fromBigTuple([
|
|
162
|
+
BigInt('10857046999023057135944570762232829481370756359578518086990519993285655852781'),
|
|
163
|
+
BigInt('11559732032986387107991004021392285783925812861821192530917403151452391805634'),
|
|
164
|
+
]),
|
|
165
|
+
Gy: Fp2.fromBigTuple([
|
|
166
|
+
BigInt('8495653923123431417604973247489272438418190587263600148770280649306958101930'),
|
|
167
|
+
BigInt('4082367875863433681332203403145435568316851327593401208105741076214120093531'),
|
|
168
|
+
]),
|
|
169
|
+
}))();
|
|
170
|
+
const fields = /* @__PURE__ */ (() => ({ Fp, Fp2, Fp6: tower.Fp6, Fp12, Fr: exports.bn254_Fr }))();
|
|
171
|
+
const bn254_G1 = /* @__PURE__ */ (0, weierstrass_1.weierstrass)(bn254_G1_CURVE, {
|
|
172
|
+
Fp,
|
|
173
|
+
Fn: exports.bn254_Fr,
|
|
174
|
+
// Ethereum encodes infinity as `(0, 0)`, so the public point API accepts it
|
|
175
|
+
// even though it is not an affine curve point, and `fromAffine()` stays lazy:
|
|
176
|
+
// adversarial inputs still need `assertValidity()`.
|
|
177
|
+
allowInfinityPoint: true,
|
|
178
|
+
});
|
|
179
|
+
const bn254_G2 = /* @__PURE__ */ (0, weierstrass_1.weierstrass)(bn254_G2_CURVE, {
|
|
180
|
+
Fp: Fp2,
|
|
181
|
+
Fn: exports.bn254_Fr,
|
|
182
|
+
// Ethereum encodes infinity as `((0, 0), (0, 0))`, so the public point API
|
|
183
|
+
// accepts it even though it is not an affine curve point.
|
|
184
|
+
allowInfinityPoint: true,
|
|
185
|
+
// Optimized BN254 G2 subgroup test used to satisfy the EIP-197 order check.
|
|
186
|
+
isTorsionFree: (c, P) => P.multiplyUnsafe(SIX_X_SQUARED).equals(G2psi(c, P)), // [p]P = [6X^2]P
|
|
187
|
+
});
|
|
188
|
+
/*
|
|
189
|
+
No hashToCurve for now (and signatures):
|
|
190
|
+
|
|
191
|
+
- RFC 9380 doesn't mention bn254 and doesn't provide test vectors
|
|
192
|
+
- Overall seems like nobody is using BLS signatures on top of bn254
|
|
193
|
+
- Seems like it can utilize SVDW, which is not implemented yet
|
|
194
|
+
*/
|
|
195
|
+
// const htfDefaults = Object.freeze({
|
|
196
|
+
// // DST: a domain separation tag defined in section 2.2.5
|
|
197
|
+
// DST: 'BN254G2_XMD:SHA-256_SVDW_RO_',
|
|
198
|
+
// encodeDST: 'BN254G2_XMD:SHA-256_SVDW_RO_',
|
|
199
|
+
// p: Fp.ORDER,
|
|
200
|
+
// m: 2,
|
|
201
|
+
// k: 128,
|
|
202
|
+
// expand: 'xmd',
|
|
203
|
+
// hash: sha256,
|
|
204
|
+
// });
|
|
205
|
+
// const hasherOpts = {
|
|
206
|
+
// { ...htfDefaults, m: 1, DST: 'BN254G2_XMD:SHA-256_SVDW_RO_' }
|
|
207
|
+
// };
|
|
208
|
+
const bn254_params = /* @__PURE__ */ (() => ({
|
|
209
|
+
// Optimal-ate Miller loop parameter derived from the positive BN seed.
|
|
210
|
+
ateLoopSize: BN_X * _6n + _2n,
|
|
211
|
+
r: exports.bn254_Fr.ORDER,
|
|
212
|
+
xNegative: false,
|
|
213
|
+
// EIP-197 writes G2 as `y^2 = x^3 + 3 / (i + 9)`, so the pairing
|
|
214
|
+
// configuration uses the divisive twist convention.
|
|
215
|
+
twistType: 'divisive',
|
|
216
|
+
postPrecompute: exports._postPrecompute,
|
|
217
|
+
}))();
|
|
218
|
+
// const bn254_hasher = {
|
|
219
|
+
// hasherOpts: htfDefaults,
|
|
220
|
+
// hasherOptsG1: { m: 1, DST: 'BN254G2_XMD:SHA-256_SVDW_RO_' },
|
|
221
|
+
// hasherOptsG2: htfDefaults
|
|
222
|
+
// };
|
|
223
|
+
// G2_heff hEff: BigInt('21888242871839275222246405745257275088844257914179612981679871602714643921549'),
|
|
224
|
+
// fromBytes: notImplemented,
|
|
225
|
+
// toBytes: notImplemented,
|
|
226
|
+
// mapToCurve: notImplemented,
|
|
227
|
+
// fromBytes: notImplemented,
|
|
228
|
+
// toBytes: notImplemented,
|
|
229
|
+
// ShortSignature: {
|
|
230
|
+
// fromBytes: notImplemented,
|
|
231
|
+
// fromHex: notImplemented,
|
|
232
|
+
// toBytes: notImplemented,
|
|
233
|
+
// toRawBytes: notImplemented,
|
|
234
|
+
// toHex: notImplemented,
|
|
235
|
+
// },
|
|
236
|
+
/**
|
|
237
|
+
* bn254 (a.k.a. alt_bn128) pairing-friendly curve.
|
|
238
|
+
* Contains G1 / G2 operations and pairings only; the commented-out
|
|
239
|
+
* hash-to-curve and signature surface is intentionally not exposed here.
|
|
240
|
+
* @example
|
|
241
|
+
* Compute a pairing from the two generator points.
|
|
242
|
+
*
|
|
243
|
+
* ```ts
|
|
244
|
+
* const gt = bn254.pairing(bn254.G1.Point.BASE, bn254.G2.Point.BASE);
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
// bn254_hasher
|
|
248
|
+
exports.bn254 = (0, bls_1.blsBasic)(fields, bn254_G1, bn254_G2, bn254_params);
|
package/ed25519.d.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { type AffinePoint } from './abstract/curve';
|
|
2
|
+
import { PrimeEdwardsPoint, type EdDSA, type EdwardsPoint, type EdwardsPointCons } from './abstract/edwards';
|
|
3
|
+
import { type FROST } from './abstract/frost';
|
|
4
|
+
import { type H2CHasher, type H2CHasherBase } from './abstract/hash-to-curve';
|
|
5
|
+
import { type IField } from './abstract/modular';
|
|
6
|
+
import { type MontgomeryECDH } from './abstract/montgomery';
|
|
7
|
+
import { type OPRF } from './abstract/oprf';
|
|
8
|
+
import { type TArg, type TRet } from './utils';
|
|
9
|
+
/**
|
|
10
|
+
* ed25519 curve with EdDSA signatures.
|
|
11
|
+
* Seeded `keygen(seed)` / `utils.randomSecretKey(seed)` reuse the provided
|
|
12
|
+
* 32-byte seed buffer instead of copying it.
|
|
13
|
+
* @example
|
|
14
|
+
* Generate one Ed25519 keypair, sign a message, and verify it.
|
|
15
|
+
*
|
|
16
|
+
* ```js
|
|
17
|
+
* import { ed25519 } from './ed25519';
|
|
18
|
+
* const { secretKey, publicKey } = ed25519.keygen();
|
|
19
|
+
* // const publicKey = ed25519.getPublicKey(secretKey);
|
|
20
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
21
|
+
* const sig = ed25519.sign(msg, secretKey);
|
|
22
|
+
* const isValid = ed25519.verify(sig, msg, publicKey); // ZIP215
|
|
23
|
+
* // RFC8032 / FIPS 186-5
|
|
24
|
+
* const isValid2 = ed25519.verify(sig, msg, publicKey, { zip215: false });
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare const ed25519: EdDSA;
|
|
28
|
+
/**
|
|
29
|
+
* Context version of ed25519 (ctx for domain separation). See {@link ed25519}
|
|
30
|
+
* Seeded `keygen(seed)` / `utils.randomSecretKey(seed)` reuse the provided
|
|
31
|
+
* 32-byte seed buffer instead of copying it.
|
|
32
|
+
* @example
|
|
33
|
+
* Sign and verify with Ed25519ctx under one explicit context.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const context = new TextEncoder().encode('docs');
|
|
37
|
+
* const { secretKey, publicKey } = ed25519ctx.keygen();
|
|
38
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
39
|
+
* const sig = ed25519ctx.sign(msg, secretKey, { context });
|
|
40
|
+
* const isValid = ed25519ctx.verify(sig, msg, publicKey, { context });
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare const ed25519ctx: EdDSA;
|
|
44
|
+
/**
|
|
45
|
+
* Prehashed version of ed25519. See {@link ed25519}
|
|
46
|
+
* Seeded `keygen(seed)` / `utils.randomSecretKey(seed)` reuse the provided
|
|
47
|
+
* 32-byte seed buffer instead of copying it.
|
|
48
|
+
* @example
|
|
49
|
+
* Use the prehashed Ed25519 variant for one message.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* const { secretKey, publicKey } = ed25519ph.keygen();
|
|
53
|
+
* const msg = new TextEncoder().encode('hello noble');
|
|
54
|
+
* const sig = ed25519ph.sign(msg, secretKey);
|
|
55
|
+
* const isValid = ed25519ph.verify(sig, msg, publicKey);
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare const ed25519ph: EdDSA;
|
|
59
|
+
/**
|
|
60
|
+
* FROST threshold signatures over ed25519. RFC 9591.
|
|
61
|
+
* @example
|
|
62
|
+
* Create one trusted-dealer package for 2-of-3 ed25519 signing.
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* const alice = ed25519_FROST.Identifier.derive('alice@example.com');
|
|
66
|
+
* const bob = ed25519_FROST.Identifier.derive('bob@example.com');
|
|
67
|
+
* const carol = ed25519_FROST.Identifier.derive('carol@example.com');
|
|
68
|
+
* const deal = ed25519_FROST.trustedDealer({ min: 2, max: 3 }, [alice, bob, carol]);
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare const ed25519_FROST: TRet<FROST>;
|
|
72
|
+
/**
|
|
73
|
+
* ECDH using curve25519 aka x25519.
|
|
74
|
+
* `getSharedSecret()` rejects low-order peer inputs by default, and seeded
|
|
75
|
+
* `keygen(seed)` reuses the provided 32-byte seed buffer instead of copying it.
|
|
76
|
+
* @example
|
|
77
|
+
* Derive one shared secret between two X25519 peers.
|
|
78
|
+
*
|
|
79
|
+
* ```js
|
|
80
|
+
* import { x25519 } from './ed25519';
|
|
81
|
+
* const alice = x25519.keygen();
|
|
82
|
+
* const bob = x25519.keygen();
|
|
83
|
+
* const shared = x25519.getSharedSecret(alice.secretKey, bob.publicKey);
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare const x25519: TRet<MontgomeryECDH>;
|
|
87
|
+
/**
|
|
88
|
+
* RFC 9380 method `map_to_curve_elligator2_curve25519`. Experimental name: may be renamed later.
|
|
89
|
+
* @private
|
|
90
|
+
*/
|
|
91
|
+
export declare function _map_to_curve_elligator2_curve25519(u: bigint): {
|
|
92
|
+
xMn: bigint;
|
|
93
|
+
xMd: bigint;
|
|
94
|
+
yMn: bigint;
|
|
95
|
+
yMd: bigint;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Hashing to ed25519 points / field. RFC 9380 methods.
|
|
99
|
+
* Public `mapToCurve()` returns the cofactor-cleared subgroup point; the
|
|
100
|
+
* internal map callback below consumes one field element bigint, not `[bigint]`.
|
|
101
|
+
* @example
|
|
102
|
+
* Hash one message onto the ed25519 curve.
|
|
103
|
+
*
|
|
104
|
+
* ```ts
|
|
105
|
+
* const point = ed25519_hasher.hashToCurve(new TextEncoder().encode('hello noble'));
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare const ed25519_hasher: H2CHasher<EdwardsPointCons>;
|
|
109
|
+
/**
|
|
110
|
+
* Wrapper over Edwards Point for ristretto255.
|
|
111
|
+
*
|
|
112
|
+
* Each ed25519/EdwardsPoint has 8 different equivalent points. This can be
|
|
113
|
+
* a source of bugs for protocols like ring signatures. Ristretto was created to solve this.
|
|
114
|
+
* Ristretto point operates in X:Y:Z:T extended coordinates like EdwardsPoint,
|
|
115
|
+
* but it should work in its own namespace: do not combine those two.
|
|
116
|
+
* See [RFC9496](https://www.rfc-editor.org/rfc/rfc9496).
|
|
117
|
+
*/
|
|
118
|
+
declare class _RistrettoPoint extends PrimeEdwardsPoint<_RistrettoPoint> {
|
|
119
|
+
static BASE: _RistrettoPoint;
|
|
120
|
+
static ZERO: _RistrettoPoint;
|
|
121
|
+
static Fp: IField<bigint>;
|
|
122
|
+
static Fn: IField<bigint>;
|
|
123
|
+
constructor(ep: EdwardsPoint);
|
|
124
|
+
/**
|
|
125
|
+
* Create one Ristretto255 point from affine Edwards coordinates.
|
|
126
|
+
* This wraps the internal Edwards representative directly and is not a
|
|
127
|
+
* canonical ristretto255 decoding path.
|
|
128
|
+
* Use `toBytes()` / `fromBytes()` if canonical ristretto255 bytes matter.
|
|
129
|
+
*/
|
|
130
|
+
static fromAffine(ap: AffinePoint<bigint>): _RistrettoPoint;
|
|
131
|
+
protected assertSame(other: _RistrettoPoint): void;
|
|
132
|
+
protected init(ep: EdwardsPoint): _RistrettoPoint;
|
|
133
|
+
static fromBytes(bytes: TArg<Uint8Array>): _RistrettoPoint;
|
|
134
|
+
/**
|
|
135
|
+
* Converts ristretto-encoded string to ristretto point.
|
|
136
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-decode).
|
|
137
|
+
* @param hex - Ristretto-encoded 32 bytes. Not every 32-byte string is valid ristretto encoding
|
|
138
|
+
*/
|
|
139
|
+
static fromHex(hex: string): _RistrettoPoint;
|
|
140
|
+
/**
|
|
141
|
+
* Encodes ristretto point to Uint8Array.
|
|
142
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-encode).
|
|
143
|
+
*/
|
|
144
|
+
toBytes(): TRet<Uint8Array>;
|
|
145
|
+
/**
|
|
146
|
+
* Compares two Ristretto points.
|
|
147
|
+
* Described in [RFC9496](https://www.rfc-editor.org/rfc/rfc9496#name-equals).
|
|
148
|
+
*/
|
|
149
|
+
equals(other: _RistrettoPoint): boolean;
|
|
150
|
+
is0(): boolean;
|
|
151
|
+
}
|
|
152
|
+
/** Prime-order Ristretto255 group bundle. */
|
|
153
|
+
export declare const ristretto255: {
|
|
154
|
+
Point: typeof _RistrettoPoint;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Hashing to ristretto255 points / field. RFC 9380 methods.
|
|
158
|
+
* `hashToCurve()` is RFC 9380 Appendix B, `deriveToCurve()` is the RFC 9496
|
|
159
|
+
* §4.3.4 element-derivation building block, and `hashToScalar()` is a
|
|
160
|
+
* library-specific helper for OPRF-style use.
|
|
161
|
+
* @example
|
|
162
|
+
* Hash one message onto ristretto255.
|
|
163
|
+
*
|
|
164
|
+
* ```ts
|
|
165
|
+
* const point = ristretto255_hasher.hashToCurve(new TextEncoder().encode('hello noble'));
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
export declare const ristretto255_hasher: H2CHasherBase<typeof _RistrettoPoint>;
|
|
169
|
+
/**
|
|
170
|
+
* ristretto255 OPRF/VOPRF/POPRF bundle, defined in RFC 9497.
|
|
171
|
+
* @example
|
|
172
|
+
* Run one blind/evaluate/finalize OPRF round over ristretto255.
|
|
173
|
+
*
|
|
174
|
+
* ```ts
|
|
175
|
+
* const input = new TextEncoder().encode('hello noble');
|
|
176
|
+
* const keys = ristretto255_oprf.oprf.generateKeyPair();
|
|
177
|
+
* const blind = ristretto255_oprf.oprf.blind(input);
|
|
178
|
+
* const evaluated = ristretto255_oprf.oprf.blindEvaluate(keys.secretKey, blind.blinded);
|
|
179
|
+
* const output = ristretto255_oprf.oprf.finalize(input, blind.blind, evaluated);
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
export declare const ristretto255_oprf: TRet<OPRF>;
|
|
183
|
+
/**
|
|
184
|
+
* FROST threshold signatures over ristretto255. RFC 9591.
|
|
185
|
+
* @example
|
|
186
|
+
* Create one trusted-dealer package for 2-of-3 ristretto255 signing.
|
|
187
|
+
*
|
|
188
|
+
* ```ts
|
|
189
|
+
* const alice = ristretto255_FROST.Identifier.derive('alice@example.com');
|
|
190
|
+
* const bob = ristretto255_FROST.Identifier.derive('bob@example.com');
|
|
191
|
+
* const carol = ristretto255_FROST.Identifier.derive('carol@example.com');
|
|
192
|
+
* const deal = ristretto255_FROST.trustedDealer({ min: 2, max: 3 }, [alice, bob, carol]);
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
export declare const ristretto255_FROST: TRet<FROST>;
|
|
196
|
+
/**
|
|
197
|
+
* Weird / bogus points, useful for debugging.
|
|
198
|
+
* All 8 ed25519 points of 8-torsion subgroup can be generated from the point
|
|
199
|
+
* T = `26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05`.
|
|
200
|
+
* The subgroup generated by `T` is `{ O, T, 2T, 3T, 4T, 5T, 6T, 7T }`; the
|
|
201
|
+
* array below is that set, not the powers in that exact index order.
|
|
202
|
+
* @example
|
|
203
|
+
* Decode one known torsion point for debugging.
|
|
204
|
+
*
|
|
205
|
+
* ```ts
|
|
206
|
+
* import { ED25519_TORSION_SUBGROUP, ed25519 } from './ed25519';
|
|
207
|
+
* const point = ed25519.Point.fromHex(ED25519_TORSION_SUBGROUP[1]);
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
export declare const ED25519_TORSION_SUBGROUP: readonly string[];
|
|
211
|
+
export {};
|