@noble/curves 1.9.1 → 1.9.3
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 +238 -227
- package/_shortw_utils.d.ts +8 -5
- package/_shortw_utils.d.ts.map +1 -1
- package/_shortw_utils.js +3 -8
- package/_shortw_utils.js.map +1 -1
- package/abstract/bls.d.ts +123 -62
- package/abstract/bls.d.ts.map +1 -1
- package/abstract/bls.js +219 -163
- package/abstract/bls.js.map +1 -1
- package/abstract/curve.d.ts +142 -21
- package/abstract/curve.d.ts.map +1 -1
- package/abstract/curve.js +224 -143
- package/abstract/curve.js.map +1 -1
- package/abstract/edwards.d.ts +190 -49
- package/abstract/edwards.d.ts.map +1 -1
- package/abstract/edwards.js +322 -136
- package/abstract/edwards.js.map +1 -1
- package/abstract/fft.d.ts +12 -10
- package/abstract/fft.d.ts.map +1 -1
- package/abstract/fft.js +12 -13
- package/abstract/fft.js.map +1 -1
- package/abstract/hash-to-curve.d.ts +31 -13
- package/abstract/hash-to-curve.d.ts.map +1 -1
- package/abstract/hash-to-curve.js +34 -19
- package/abstract/hash-to-curve.js.map +1 -1
- package/abstract/modular.d.ts +31 -13
- package/abstract/modular.d.ts.map +1 -1
- package/abstract/modular.js +125 -52
- package/abstract/modular.js.map +1 -1
- package/abstract/montgomery.d.ts +18 -5
- package/abstract/montgomery.d.ts.map +1 -1
- package/abstract/montgomery.js +23 -6
- 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 +23 -49
- package/abstract/tower.d.ts.map +1 -1
- package/abstract/tower.js +9 -3
- 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 +206 -124
- package/abstract/weierstrass.d.ts.map +1 -1
- package/abstract/weierstrass.js +747 -604
- 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 -466
- 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 +55 -66
- package/ed25519.d.ts.map +1 -1
- package/ed25519.js +172 -186
- package/ed25519.js.map +1 -1
- package/ed448.d.ts +60 -57
- package/ed448.d.ts.map +1 -1
- package/ed448.js +172 -166
- package/ed448.js.map +1 -1
- package/esm/_shortw_utils.d.ts +8 -5
- package/esm/_shortw_utils.d.ts.map +1 -1
- package/esm/_shortw_utils.js +3 -8
- package/esm/_shortw_utils.js.map +1 -1
- package/esm/abstract/bls.d.ts +123 -62
- package/esm/abstract/bls.d.ts.map +1 -1
- package/esm/abstract/bls.js +220 -164
- package/esm/abstract/bls.js.map +1 -1
- package/esm/abstract/curve.d.ts +142 -21
- package/esm/abstract/curve.d.ts.map +1 -1
- package/esm/abstract/curve.js +219 -143
- package/esm/abstract/curve.js.map +1 -1
- package/esm/abstract/edwards.d.ts +190 -49
- package/esm/abstract/edwards.d.ts.map +1 -1
- package/esm/abstract/edwards.js +320 -138
- package/esm/abstract/edwards.js.map +1 -1
- package/esm/abstract/fft.d.ts +12 -10
- package/esm/abstract/fft.d.ts.map +1 -1
- package/esm/abstract/fft.js +10 -11
- package/esm/abstract/fft.js.map +1 -1
- package/esm/abstract/hash-to-curve.d.ts +31 -13
- package/esm/abstract/hash-to-curve.d.ts.map +1 -1
- package/esm/abstract/hash-to-curve.js +33 -19
- package/esm/abstract/hash-to-curve.js.map +1 -1
- package/esm/abstract/modular.d.ts +31 -13
- package/esm/abstract/modular.d.ts.map +1 -1
- package/esm/abstract/modular.js +124 -51
- package/esm/abstract/modular.js.map +1 -1
- package/esm/abstract/montgomery.d.ts +18 -5
- package/esm/abstract/montgomery.d.ts.map +1 -1
- package/esm/abstract/montgomery.js +23 -6
- 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 +23 -49
- package/esm/abstract/tower.d.ts.map +1 -1
- package/esm/abstract/tower.js +9 -3
- 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 +206 -124
- package/esm/abstract/weierstrass.d.ts.map +1 -1
- package/esm/abstract/weierstrass.js +743 -605
- 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 -465
- 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 +55 -66
- package/esm/ed25519.d.ts.map +1 -1
- package/esm/ed25519.js +170 -183
- package/esm/ed25519.js.map +1 -1
- package/esm/ed448.d.ts +60 -57
- package/esm/ed448.d.ts.map +1 -1
- package/esm/ed448.js +169 -162
- package/esm/ed448.js.map +1 -1
- package/esm/index.js +7 -9
- package/esm/index.js.map +1 -1
- package/esm/jubjub.d.ts +3 -3
- package/esm/jubjub.d.ts.map +1 -1
- package/esm/jubjub.js +3 -3
- package/esm/jubjub.js.map +1 -1
- package/esm/misc.d.ts +3 -5
- package/esm/misc.d.ts.map +1 -1
- package/esm/misc.js +31 -29
- package/esm/misc.js.map +1 -1
- package/esm/nist.d.ts +7 -22
- package/esm/nist.d.ts.map +1 -1
- package/esm/nist.js +106 -101
- package/esm/nist.js.map +1 -1
- package/esm/p256.d.ts +7 -3
- package/esm/p256.d.ts.map +1 -1
- package/esm/p256.js +4 -0
- package/esm/p256.js.map +1 -1
- package/esm/p384.d.ts +7 -4
- package/esm/p384.d.ts.map +1 -1
- package/esm/p384.js +4 -1
- package/esm/p384.js.map +1 -1
- package/esm/p521.d.ts +7 -3
- package/esm/p521.d.ts.map +1 -1
- package/esm/p521.js +4 -0
- package/esm/p521.js.map +1 -1
- package/esm/secp256k1.d.ts +38 -21
- package/esm/secp256k1.d.ts.map +1 -1
- package/esm/secp256k1.js +112 -104
- 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/index.js +7 -9
- package/index.js.map +1 -1
- package/jubjub.d.ts +3 -3
- package/jubjub.d.ts.map +1 -1
- package/jubjub.js +3 -3
- package/jubjub.js.map +1 -1
- package/misc.d.ts +3 -5
- package/misc.d.ts.map +1 -1
- package/misc.js +35 -33
- package/misc.js.map +1 -1
- package/nist.d.ts +7 -22
- package/nist.d.ts.map +1 -1
- package/nist.js +106 -101
- package/nist.js.map +1 -1
- package/p256.d.ts +7 -3
- package/p256.d.ts.map +1 -1
- package/p256.js +4 -0
- package/p256.js.map +1 -1
- package/p384.d.ts +7 -4
- package/p384.d.ts.map +1 -1
- package/p384.js +4 -1
- package/p384.js.map +1 -1
- package/p521.d.ts +7 -3
- package/p521.d.ts.map +1 -1
- package/p521.js +4 -0
- package/p521.js.map +1 -1
- package/package.json +17 -6
- package/secp256k1.d.ts +38 -21
- package/secp256k1.d.ts.map +1 -1
- package/secp256k1.js +112 -104
- package/secp256k1.js.map +1 -1
- package/src/_shortw_utils.ts +6 -15
- package/src/abstract/bls.ts +428 -251
- package/src/abstract/curve.ts +307 -149
- package/src/abstract/edwards.ts +555 -203
- package/src/abstract/fft.ts +30 -19
- package/src/abstract/hash-to-curve.ts +75 -34
- package/src/abstract/modular.ts +131 -59
- package/src/abstract/montgomery.ts +44 -15
- package/src/abstract/poseidon.ts +22 -18
- package/src/abstract/tower.ts +40 -71
- package/src/abstract/utils.ts +3 -378
- package/src/abstract/weierstrass.ts +1086 -746
- package/src/bls12-381.ts +549 -490
- package/src/bn254.ts +47 -35
- package/src/ed25519.ts +214 -216
- package/src/ed448.ts +251 -220
- package/src/index.ts +7 -9
- package/src/jubjub.ts +3 -3
- package/src/misc.ts +41 -40
- package/src/nist.ts +161 -126
- package/src/p256.ts +7 -3
- package/src/p384.ts +7 -5
- package/src/p521.ts +7 -3
- package/src/secp256k1.ts +145 -115
- 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
|
@@ -5,14 +5,16 @@
|
|
|
5
5
|
* @module
|
|
6
6
|
*/
|
|
7
7
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
8
|
-
import { mod } from './modular.ts';
|
|
9
8
|
import {
|
|
9
|
+
_validateObject,
|
|
10
10
|
aInRange,
|
|
11
11
|
bytesToNumberLE,
|
|
12
12
|
ensureBytes,
|
|
13
13
|
numberToBytesLE,
|
|
14
|
-
|
|
15
|
-
} from '
|
|
14
|
+
randomBytes,
|
|
15
|
+
} from '../utils.ts';
|
|
16
|
+
import type { CurveInfo } from './curve.ts';
|
|
17
|
+
import { mod } from './modular.ts';
|
|
16
18
|
|
|
17
19
|
const _0n = BigInt(0);
|
|
18
20
|
const _1n = BigInt(1);
|
|
@@ -24,31 +26,43 @@ export type CurveType = {
|
|
|
24
26
|
type: 'x25519' | 'x448';
|
|
25
27
|
adjustScalarBytes: (bytes: Uint8Array) => Uint8Array;
|
|
26
28
|
powPminus2: (x: bigint) => bigint;
|
|
27
|
-
randomBytes
|
|
29
|
+
randomBytes?: (bytesLength?: number) => Uint8Array;
|
|
28
30
|
};
|
|
29
31
|
|
|
30
|
-
export type
|
|
32
|
+
export type MontgomeryECDH = {
|
|
31
33
|
scalarMult: (scalar: Hex, u: Hex) => Uint8Array;
|
|
32
34
|
scalarMultBase: (scalar: Hex) => Uint8Array;
|
|
33
|
-
getSharedSecret: (
|
|
34
|
-
getPublicKey: (
|
|
35
|
-
utils: {
|
|
35
|
+
getSharedSecret: (secretKeyA: Hex, publicKeyB: Hex) => Uint8Array;
|
|
36
|
+
getPublicKey: (secretKey: Hex) => Uint8Array;
|
|
37
|
+
utils: {
|
|
38
|
+
randomSecretKey: () => Uint8Array;
|
|
39
|
+
/** @deprecated use `randomSecretKey` */
|
|
40
|
+
randomPrivateKey: () => Uint8Array;
|
|
41
|
+
};
|
|
36
42
|
GuBytes: Uint8Array;
|
|
43
|
+
info: {
|
|
44
|
+
type: 'montgomery';
|
|
45
|
+
lengths: Omit<CurveInfo['lengths'], 'signature'>;
|
|
46
|
+
publicKeyHasPrefix?: boolean;
|
|
47
|
+
};
|
|
48
|
+
keygen: (seed?: Uint8Array) => { secretKey: Uint8Array; publicKey: Uint8Array };
|
|
37
49
|
};
|
|
50
|
+
export type CurveFn = MontgomeryECDH;
|
|
38
51
|
|
|
39
52
|
function validateOpts(curve: CurveType) {
|
|
40
|
-
|
|
53
|
+
_validateObject(curve, {
|
|
41
54
|
adjustScalarBytes: 'function',
|
|
42
55
|
powPminus2: 'function',
|
|
43
56
|
});
|
|
44
57
|
return Object.freeze({ ...curve } as const);
|
|
45
58
|
}
|
|
46
59
|
|
|
47
|
-
export function montgomery(curveDef: CurveType):
|
|
60
|
+
export function montgomery(curveDef: CurveType): MontgomeryECDH {
|
|
48
61
|
const CURVE = validateOpts(curveDef);
|
|
49
|
-
const { P, type, adjustScalarBytes, powPminus2 } = CURVE;
|
|
62
|
+
const { P, type, adjustScalarBytes, powPminus2, randomBytes: rand } = CURVE;
|
|
50
63
|
const is25519 = type === 'x25519';
|
|
51
64
|
if (!is25519 && type !== 'x448') throw new Error('invalid type');
|
|
65
|
+
const randomBytes_ = rand || randomBytes;
|
|
52
66
|
|
|
53
67
|
const montgomeryBits = is25519 ? 255 : 448;
|
|
54
68
|
const fieldLen = is25519 ? 32 : 56;
|
|
@@ -153,13 +167,28 @@ export function montgomery(curveDef: CurveType): CurveFn {
|
|
|
153
167
|
const z2 = powPminus2(z_2); // `Fp.pow(x, P - _2n)` is much slower equivalent
|
|
154
168
|
return modP(x_2 * z2); // Return x_2 * (z_2^(p - 2))
|
|
155
169
|
}
|
|
156
|
-
|
|
170
|
+
const randomSecretKey = (seed = randomBytes_(fieldLen)) => seed;
|
|
171
|
+
const utils = {
|
|
172
|
+
randomSecretKey,
|
|
173
|
+
randomPrivateKey: randomSecretKey,
|
|
174
|
+
};
|
|
175
|
+
function keygen(seed?: Uint8Array) {
|
|
176
|
+
const secretKey = utils.randomSecretKey(seed);
|
|
177
|
+
return { secretKey, publicKey: scalarMultBase(secretKey) };
|
|
178
|
+
}
|
|
179
|
+
const lengths = {
|
|
180
|
+
secret: fieldLen,
|
|
181
|
+
public: fieldLen,
|
|
182
|
+
seed: fieldLen,
|
|
183
|
+
};
|
|
157
184
|
return {
|
|
185
|
+
keygen,
|
|
186
|
+
getSharedSecret: (secretKey: Hex, publicKey: Hex) => scalarMult(secretKey, publicKey),
|
|
187
|
+
getPublicKey: (secretKey: Hex): Uint8Array => scalarMultBase(secretKey),
|
|
158
188
|
scalarMult,
|
|
159
189
|
scalarMultBase,
|
|
160
|
-
|
|
161
|
-
getPublicKey: (privateKey: Hex): Uint8Array => scalarMultBase(privateKey),
|
|
162
|
-
utils: { randomPrivateKey: () => CURVE.randomBytes!(fieldLen) },
|
|
190
|
+
utils,
|
|
163
191
|
GuBytes: GuBytes.slice(),
|
|
192
|
+
info: { type: 'montgomery' as const, lengths },
|
|
164
193
|
};
|
|
165
194
|
}
|
package/src/abstract/poseidon.ts
CHANGED
|
@@ -7,8 +7,8 @@
|
|
|
7
7
|
* @module
|
|
8
8
|
*/
|
|
9
9
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
10
|
+
import { _validateObject, bitGet } from '../utils.ts';
|
|
10
11
|
import { FpInvertBatch, FpPow, type IField, validateField } from './modular.ts';
|
|
11
|
-
import { bitGet } from './utils.ts';
|
|
12
12
|
|
|
13
13
|
// Grain LFSR (Linear-Feedback Shift Register): https://eprint.iacr.org/2009/109.pdf
|
|
14
14
|
function grainLFSR(state: number[]): () => boolean {
|
|
@@ -41,20 +41,28 @@ export type PoseidonBasicOpts = {
|
|
|
41
41
|
isSboxInverse?: boolean;
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
function
|
|
44
|
+
function assertValidPosOpts(opts: PoseidonBasicOpts) {
|
|
45
45
|
const { Fp, roundsFull } = opts;
|
|
46
46
|
validateField(Fp);
|
|
47
|
+
_validateObject(
|
|
48
|
+
opts,
|
|
49
|
+
{
|
|
50
|
+
t: 'number',
|
|
51
|
+
roundsFull: 'number',
|
|
52
|
+
roundsPartial: 'number',
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
isSboxInverse: 'boolean',
|
|
56
|
+
}
|
|
57
|
+
);
|
|
47
58
|
for (const i of ['t', 'roundsFull', 'roundsPartial'] as const) {
|
|
48
|
-
if (
|
|
49
|
-
throw new Error('invalid number ' + i);
|
|
59
|
+
if (!Number.isSafeInteger(opts[i]) || opts[i] < 1) throw new Error('invalid number ' + i);
|
|
50
60
|
}
|
|
51
|
-
if (opts.isSboxInverse !== undefined && typeof opts.isSboxInverse !== 'boolean')
|
|
52
|
-
throw new Error(`Poseidon: invalid param isSboxInverse=${opts.isSboxInverse}`);
|
|
53
61
|
if (roundsFull & 1) throw new Error('roundsFull is not even' + roundsFull);
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
function poseidonGrain(opts: PoseidonBasicOpts) {
|
|
57
|
-
|
|
65
|
+
assertValidPosOpts(opts);
|
|
58
66
|
const { Fp } = opts;
|
|
59
67
|
const state = Array(80).fill(1);
|
|
60
68
|
let pos = 0;
|
|
@@ -140,7 +148,7 @@ export function validateOpts(opts: PoseidonOpts): Readonly<{
|
|
|
140
148
|
sboxPower?: number;
|
|
141
149
|
reversePartialPowIdx?: boolean; // Hack for stark
|
|
142
150
|
}> {
|
|
143
|
-
|
|
151
|
+
assertValidPosOpts(opts);
|
|
144
152
|
const { Fp, mds, reversePartialPowIdx: rev, roundConstants: rc } = opts;
|
|
145
153
|
const { roundsFull, roundsPartial, sboxPower, t } = opts;
|
|
146
154
|
|
|
@@ -196,12 +204,13 @@ export function splitConstants(rc: bigint[], t: number): bigint[][] {
|
|
|
196
204
|
return res;
|
|
197
205
|
}
|
|
198
206
|
|
|
199
|
-
|
|
200
|
-
export function poseidon(opts: PoseidonOpts): {
|
|
207
|
+
export type PoseidonFn = {
|
|
201
208
|
(values: bigint[]): bigint[];
|
|
202
209
|
// For verification in tests
|
|
203
210
|
roundConstants: bigint[][];
|
|
204
|
-
}
|
|
211
|
+
};
|
|
212
|
+
/** Poseidon NTT-friendly hash. */
|
|
213
|
+
export function poseidon(opts: PoseidonOpts): PoseidonFn {
|
|
205
214
|
const _opts = validateOpts(opts);
|
|
206
215
|
const { Fp, mds, roundConstants, rounds: totalRounds, roundsPartial, sboxFn, t } = _opts;
|
|
207
216
|
const halfRoundsFull = _opts.roundsFull / 2;
|
|
@@ -242,17 +251,12 @@ export class PoseidonSponge {
|
|
|
242
251
|
private Fp: IField<bigint>;
|
|
243
252
|
readonly rate: number;
|
|
244
253
|
readonly capacity: number;
|
|
245
|
-
readonly hash:
|
|
254
|
+
readonly hash: PoseidonFn;
|
|
246
255
|
private state: bigint[]; // [...capacity, ...rate]
|
|
247
256
|
private pos = 0;
|
|
248
257
|
private isAbsorbing = true;
|
|
249
258
|
|
|
250
|
-
constructor(
|
|
251
|
-
Fp: IField<bigint>,
|
|
252
|
-
rate: number,
|
|
253
|
-
capacity: number,
|
|
254
|
-
hash: ReturnType<typeof poseidon>
|
|
255
|
-
) {
|
|
259
|
+
constructor(Fp: IField<bigint>, rate: number, capacity: number, hash: PoseidonFn) {
|
|
256
260
|
this.Fp = Fp;
|
|
257
261
|
this.hash = hash;
|
|
258
262
|
this.rate = rate;
|
package/src/abstract/tower.ts
CHANGED
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
* @module
|
|
11
11
|
*/
|
|
12
12
|
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
13
|
+
import { bitLen, bitMask, concatBytes, notImplemented } from '../utils.ts';
|
|
13
14
|
import * as mod from './modular.ts';
|
|
14
|
-
import {
|
|
15
|
-
import type { ProjConstructor, ProjPointType } from './weierstrass.ts';
|
|
15
|
+
import type { WeierstrassPoint, WeierstrassPointCons } from './weierstrass.ts';
|
|
16
16
|
|
|
17
17
|
// Be friendly to bad ECMAScript parsers by not using bigint literals
|
|
18
18
|
// prettier-ignore
|
|
@@ -34,19 +34,33 @@ export type BigintTwelve = [
|
|
|
34
34
|
];
|
|
35
35
|
|
|
36
36
|
export type Fp2Bls = mod.IField<Fp2> & {
|
|
37
|
-
reim: (num: Fp2) => { re: Fp; im: Fp };
|
|
38
|
-
mulByB: (num: Fp2) => Fp2;
|
|
39
37
|
frobeniusMap(num: Fp2, power: number): Fp2;
|
|
40
38
|
fromBigTuple(num: [bigint, bigint]): Fp2;
|
|
39
|
+
mulByB: (num: Fp2) => Fp2;
|
|
40
|
+
mulByNonresidue: (num: Fp2) => Fp2;
|
|
41
|
+
reim: (num: Fp2) => { re: Fp; im: Fp };
|
|
42
|
+
NONRESIDUE: Fp2;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type Fp6Bls = mod.IField<Fp6> & {
|
|
46
|
+
frobeniusMap(num: Fp6, power: number): Fp6;
|
|
47
|
+
fromBigSix: (tuple: BigintSix) => Fp6;
|
|
48
|
+
mul1(num: Fp6, b1: Fp2): Fp6;
|
|
49
|
+
mul01(num: Fp6, b0: Fp2, b1: Fp2): Fp6;
|
|
50
|
+
mulByFp2(lhs: Fp6, rhs: Fp2): Fp6;
|
|
51
|
+
mulByNonresidue: (num: Fp6) => Fp6;
|
|
41
52
|
};
|
|
42
53
|
|
|
43
54
|
export type Fp12Bls = mod.IField<Fp12> & {
|
|
44
55
|
frobeniusMap(num: Fp12, power: number): Fp12;
|
|
56
|
+
fromBigTwelve: (t: BigintTwelve) => Fp12;
|
|
45
57
|
mul014(num: Fp12, o0: Fp2, o1: Fp2, o4: Fp2): Fp12;
|
|
46
58
|
mul034(num: Fp12, o0: Fp2, o3: Fp2, o4: Fp2): Fp12;
|
|
59
|
+
mulByFp2(lhs: Fp12, rhs: Fp2): Fp12;
|
|
47
60
|
conjugate(num: Fp12): Fp12;
|
|
48
61
|
finalExponentiate(num: Fp12): Fp12;
|
|
49
|
-
|
|
62
|
+
_cyclotomicSquare(num: Fp12): Fp12;
|
|
63
|
+
_cyclotomicExp(num: Fp12, n: bigint): Fp12;
|
|
50
64
|
};
|
|
51
65
|
|
|
52
66
|
function calcFrobeniusCoefficients<T>(
|
|
@@ -81,8 +95,8 @@ export function psiFrobenius(
|
|
|
81
95
|
): {
|
|
82
96
|
psi: (x: Fp2, y: Fp2) => [Fp2, Fp2];
|
|
83
97
|
psi2: (x: Fp2, y: Fp2) => [Fp2, Fp2];
|
|
84
|
-
G2psi: (c:
|
|
85
|
-
G2psi2: (c:
|
|
98
|
+
G2psi: (c: WeierstrassPointCons<Fp2>, P: WeierstrassPoint<Fp2>) => WeierstrassPoint<Fp2>;
|
|
99
|
+
G2psi2: (c: WeierstrassPointCons<Fp2>, P: WeierstrassPoint<Fp2>) => WeierstrassPoint<Fp2>;
|
|
86
100
|
PSI_X: Fp2;
|
|
87
101
|
PSI_Y: Fp2;
|
|
88
102
|
PSI2_X: Fp2;
|
|
@@ -109,7 +123,7 @@ export function psiFrobenius(
|
|
|
109
123
|
// Map points
|
|
110
124
|
const mapAffine =
|
|
111
125
|
<T>(fn: (x: T, y: T) => [T, T]) =>
|
|
112
|
-
(c:
|
|
126
|
+
(c: WeierstrassPointCons<T>, P: WeierstrassPoint<T>) => {
|
|
113
127
|
const affine = P.toAffine();
|
|
114
128
|
const p = fn(affine.x, affine.y);
|
|
115
129
|
return c.fromAffine({ x: p[0], y: p[1] });
|
|
@@ -134,34 +148,10 @@ export type Tower12Opts = {
|
|
|
134
148
|
|
|
135
149
|
export function tower12(opts: Tower12Opts): {
|
|
136
150
|
Fp: Readonly<mod.IField<bigint> & Required<Pick<mod.IField<bigint>, 'isOdd'>>>;
|
|
137
|
-
Fp2:
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
reim: (num: Fp2) => { re: bigint; im: bigint };
|
|
141
|
-
mulByNonresidue: (num: Fp2) => Fp2;
|
|
142
|
-
mulByB: (num: Fp2) => Fp2;
|
|
143
|
-
frobeniusMap(num: Fp2, power: number): Fp2;
|
|
144
|
-
};
|
|
145
|
-
Fp6: mod.IField<Fp6> & {
|
|
146
|
-
fromBigSix: (tuple: BigintSix) => Fp6;
|
|
147
|
-
mulByNonresidue: (num: Fp6) => Fp6;
|
|
148
|
-
frobeniusMap(num: Fp6, power: number): Fp6;
|
|
149
|
-
mul1(num: Fp6, b1: Fp2): Fp6;
|
|
150
|
-
mul01(num: Fp6, b0: Fp2, b1: Fp2): Fp6;
|
|
151
|
-
mulByFp2(lhs: Fp6, rhs: Fp2): Fp6;
|
|
152
|
-
};
|
|
151
|
+
Fp2: Fp2Bls;
|
|
152
|
+
Fp6: Fp6Bls;
|
|
153
|
+
Fp12: Fp12Bls;
|
|
153
154
|
Fp4Square: (a: Fp2, b: Fp2) => { first: Fp2; second: Fp2 };
|
|
154
|
-
Fp12: mod.IField<Fp12> & {
|
|
155
|
-
fromBigTwelve: (t: BigintTwelve) => Fp12;
|
|
156
|
-
frobeniusMap(num: Fp12, power: number): Fp12;
|
|
157
|
-
mul014(num: Fp12, o0: Fp2, o1: Fp2, o4: Fp2): Fp12;
|
|
158
|
-
mul034(num: Fp12, o0: Fp2, o3: Fp2, o4: Fp2): Fp12;
|
|
159
|
-
mulByFp2(lhs: Fp12, rhs: Fp2): Fp12;
|
|
160
|
-
conjugate(num: Fp12): Fp12;
|
|
161
|
-
finalExponentiate(num: Fp12): Fp12;
|
|
162
|
-
_cyclotomicSquare(num: Fp12): Fp12;
|
|
163
|
-
_cyclotomicExp(num: Fp12, n: bigint): Fp12;
|
|
164
|
-
};
|
|
165
155
|
} {
|
|
166
156
|
const { ORDER } = opts;
|
|
167
157
|
// Fp
|
|
@@ -196,23 +186,19 @@ export function tower12(opts: Tower12Opts): {
|
|
|
196
186
|
const c = Fp.add(c0, c0);
|
|
197
187
|
return { c0: Fp.mul(a, b), c1: Fp.mul(c, c1) };
|
|
198
188
|
};
|
|
199
|
-
type Fp2Utils = {
|
|
200
|
-
NONRESIDUE: Fp2;
|
|
201
|
-
fromBigTuple: (tuple: BigintTuple | bigint[]) => Fp2;
|
|
202
|
-
reim: (num: Fp2) => { re: bigint; im: bigint };
|
|
203
|
-
mulByNonresidue: (num: Fp2) => Fp2;
|
|
204
|
-
mulByB: (num: Fp2) => Fp2;
|
|
205
|
-
frobeniusMap(num: Fp2, power: number): Fp2;
|
|
206
|
-
};
|
|
207
189
|
const Fp2fromBigTuple = (tuple: BigintTuple | bigint[]) => {
|
|
208
190
|
if (tuple.length !== 2) throw new Error('invalid tuple');
|
|
209
191
|
const fps = tuple.map((n) => Fp.create(n)) as [Fp, Fp];
|
|
210
192
|
return { c0: fps[0], c1: fps[1] };
|
|
211
193
|
};
|
|
212
194
|
|
|
195
|
+
function isValidC(num: bigint, ORDER: bigint) {
|
|
196
|
+
return typeof num === 'bigint' && _0n <= num && num < ORDER;
|
|
197
|
+
}
|
|
198
|
+
|
|
213
199
|
const FP2_ORDER = ORDER * ORDER;
|
|
214
200
|
const Fp2Nonresidue = Fp2fromBigTuple(opts.FP2_NONRESIDUE);
|
|
215
|
-
const Fp2:
|
|
201
|
+
const Fp2: Fp2Bls = {
|
|
216
202
|
ORDER: FP2_ORDER,
|
|
217
203
|
isLE: Fp.isLE,
|
|
218
204
|
NONRESIDUE: Fp2Nonresidue,
|
|
@@ -222,8 +208,9 @@ export function tower12(opts: Tower12Opts): {
|
|
|
222
208
|
ZERO: { c0: Fp.ZERO, c1: Fp.ZERO },
|
|
223
209
|
ONE: { c0: Fp.ONE, c1: Fp.ZERO },
|
|
224
210
|
create: (num) => num,
|
|
225
|
-
isValid: ({ c0, c1 }) =>
|
|
211
|
+
isValid: ({ c0, c1 }) => isValidC(c0, FP2_ORDER) && isValidC(c1, FP2_ORDER),
|
|
226
212
|
is0: ({ c0, c1 }) => Fp.is0(c0) && Fp.is0(c1),
|
|
213
|
+
isValidNot0: (num) => !Fp2.is0(num) && Fp2.isValid(num),
|
|
227
214
|
eql: ({ c0, c1 }: Fp2, { c0: r0, c1: r1 }: Fp2) => Fp.eql(c0, r0) && Fp.eql(c1, r1),
|
|
228
215
|
neg: ({ c0, c1 }) => ({ c0: Fp.neg(c0), c1: Fp.neg(c1) }),
|
|
229
216
|
pow: (num, power) => mod.FpPow(Fp2, num, power),
|
|
@@ -361,15 +348,6 @@ export function tower12(opts: Tower12Opts): {
|
|
|
361
348
|
c2: Fp2.sub(Fp2.sub(Fp2.add(Fp2.add(t1, Fp2.sqr(Fp2.add(Fp2.sub(c0, c1), c2))), t3), t0), t4),
|
|
362
349
|
};
|
|
363
350
|
};
|
|
364
|
-
type Fp6Utils = {
|
|
365
|
-
fromBigSix: (tuple: BigintSix) => Fp6;
|
|
366
|
-
mulByNonresidue: (num: Fp6) => Fp6;
|
|
367
|
-
frobeniusMap(num: Fp6, power: number): Fp6;
|
|
368
|
-
mul1(num: Fp6, b1: Fp2): Fp6;
|
|
369
|
-
mul01(num: Fp6, b0: Fp2, b1: Fp2): Fp6;
|
|
370
|
-
mulByFp2(lhs: Fp6, rhs: Fp2): Fp6;
|
|
371
|
-
};
|
|
372
|
-
|
|
373
351
|
const [FP6_FROBENIUS_COEFFICIENTS_1, FP6_FROBENIUS_COEFFICIENTS_2] = calcFrobeniusCoefficients(
|
|
374
352
|
Fp2,
|
|
375
353
|
Fp2Nonresidue,
|
|
@@ -379,7 +357,7 @@ export function tower12(opts: Tower12Opts): {
|
|
|
379
357
|
3
|
|
380
358
|
);
|
|
381
359
|
|
|
382
|
-
const Fp6:
|
|
360
|
+
const Fp6: Fp6Bls = {
|
|
383
361
|
ORDER: Fp2.ORDER, // TODO: unused, but need to verify
|
|
384
362
|
isLE: Fp2.isLE,
|
|
385
363
|
BITS: 3 * Fp2.BITS,
|
|
@@ -390,6 +368,7 @@ export function tower12(opts: Tower12Opts): {
|
|
|
390
368
|
create: (num) => num,
|
|
391
369
|
isValid: ({ c0, c1, c2 }) => Fp2.isValid(c0) && Fp2.isValid(c1) && Fp2.isValid(c2),
|
|
392
370
|
is0: ({ c0, c1, c2 }) => Fp2.is0(c0) && Fp2.is0(c1) && Fp2.is0(c2),
|
|
371
|
+
isValidNot0: (num) => !Fp6.is0(num) && Fp6.isValid(num),
|
|
393
372
|
neg: ({ c0, c1, c2 }) => ({ c0: Fp2.neg(c0), c1: Fp2.neg(c1), c2: Fp2.neg(c2) }),
|
|
394
373
|
eql: ({ c0, c1, c2 }, { c0: r0, c1: r1, c2: r2 }) =>
|
|
395
374
|
Fp2.eql(c0, r0) && Fp2.eql(c1, r1) && Fp2.eql(c2, r2),
|
|
@@ -439,9 +418,9 @@ export function tower12(opts: Tower12Opts): {
|
|
|
439
418
|
fromBigSix: (t: BigintSix): Fp6 => {
|
|
440
419
|
if (!Array.isArray(t) || t.length !== 6) throw new Error('invalid Fp6 usage');
|
|
441
420
|
return {
|
|
442
|
-
c0: Fp2.fromBigTuple(t.slice(0, 2)),
|
|
443
|
-
c1: Fp2.fromBigTuple(t.slice(2, 4)),
|
|
444
|
-
c2: Fp2.fromBigTuple(t.slice(4, 6)),
|
|
421
|
+
c0: Fp2.fromBigTuple(t.slice(0, 2) as BigintTuple),
|
|
422
|
+
c1: Fp2.fromBigTuple(t.slice(2, 4) as BigintTuple),
|
|
423
|
+
c2: Fp2.fromBigTuple(t.slice(4, 6) as BigintTuple),
|
|
445
424
|
};
|
|
446
425
|
},
|
|
447
426
|
frobeniusMap: ({ c0, c1, c2 }, power: number) => ({
|
|
@@ -524,19 +503,8 @@ export function tower12(opts: Tower12Opts): {
|
|
|
524
503
|
second: Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(a, b)), a2), b2), // (a + b)² - a² - b²
|
|
525
504
|
};
|
|
526
505
|
}
|
|
527
|
-
type Fp12Utils = {
|
|
528
|
-
fromBigTwelve: (t: BigintTwelve) => Fp12;
|
|
529
|
-
frobeniusMap(num: Fp12, power: number): Fp12;
|
|
530
|
-
mul014(num: Fp12, o0: Fp2, o1: Fp2, o4: Fp2): Fp12;
|
|
531
|
-
mul034(num: Fp12, o0: Fp2, o3: Fp2, o4: Fp2): Fp12;
|
|
532
|
-
mulByFp2(lhs: Fp12, rhs: Fp2): Fp12;
|
|
533
|
-
conjugate(num: Fp12): Fp12;
|
|
534
|
-
finalExponentiate(num: Fp12): Fp12;
|
|
535
|
-
_cyclotomicSquare(num: Fp12): Fp12;
|
|
536
|
-
_cyclotomicExp(num: Fp12, n: bigint): Fp12;
|
|
537
|
-
};
|
|
538
506
|
|
|
539
|
-
const Fp12:
|
|
507
|
+
const Fp12: Fp12Bls = {
|
|
540
508
|
ORDER: Fp2.ORDER, // TODO: unused, but need to verify
|
|
541
509
|
isLE: Fp6.isLE,
|
|
542
510
|
BITS: 2 * Fp6.BITS,
|
|
@@ -547,6 +515,7 @@ export function tower12(opts: Tower12Opts): {
|
|
|
547
515
|
create: (num) => num,
|
|
548
516
|
isValid: ({ c0, c1 }) => Fp6.isValid(c0) && Fp6.isValid(c1),
|
|
549
517
|
is0: ({ c0, c1 }) => Fp6.is0(c0) && Fp6.is0(c1),
|
|
518
|
+
isValidNot0: (num) => !Fp12.is0(num) && Fp12.isValid(num),
|
|
550
519
|
neg: ({ c0, c1 }) => ({ c0: Fp6.neg(c0), c1: Fp6.neg(c1) }),
|
|
551
520
|
eql: ({ c0, c1 }, { c0: r0, c1: r1 }) => Fp6.eql(c0, r0) && Fp6.eql(c1, r1),
|
|
552
521
|
sqrt: notImplemented,
|
|
@@ -646,5 +615,5 @@ export function tower12(opts: Tower12Opts): {
|
|
|
646
615
|
finalExponentiate: opts.Fp12finalExponentiate,
|
|
647
616
|
};
|
|
648
617
|
|
|
649
|
-
return { Fp, Fp2, Fp6,
|
|
618
|
+
return { Fp, Fp2, Fp6, Fp12, Fp4Square };
|
|
650
619
|
}
|