@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/abstract/utils.ts
CHANGED
|
@@ -1,382 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Hex, bytes and number utilities.
|
|
3
2
|
* @module
|
|
4
3
|
*/
|
|
5
|
-
|
|
4
|
+
export * from '../utils.ts';
|
|
6
5
|
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
// User may opt-in into using different hashing library. This way, noble-hashes
|
|
10
|
-
// won't be included into their bundle.
|
|
11
|
-
const _0n = /* @__PURE__ */ BigInt(0);
|
|
12
|
-
const _1n = /* @__PURE__ */ BigInt(1);
|
|
13
|
-
export type Hex = Uint8Array | string; // hex strings are accepted for simplicity
|
|
14
|
-
export type PrivKey = Hex | bigint; // bigints are accepted to ease learning curve
|
|
15
|
-
export type CHash = {
|
|
16
|
-
(message: Uint8Array | string): Uint8Array;
|
|
17
|
-
blockLen: number;
|
|
18
|
-
outputLen: number;
|
|
19
|
-
create(opts?: { dkLen?: number }): any; // For shake
|
|
20
|
-
};
|
|
21
|
-
export type FHash = (message: Uint8Array | string) => Uint8Array;
|
|
22
|
-
|
|
23
|
-
export function isBytes(a: unknown): a is Uint8Array {
|
|
24
|
-
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function abytes(item: unknown): void {
|
|
28
|
-
if (!isBytes(item)) throw new Error('Uint8Array expected');
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function abool(title: string, value: boolean): void {
|
|
32
|
-
if (typeof value !== 'boolean') throw new Error(title + ' boolean expected, got ' + value);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// Used in weierstrass, der
|
|
36
|
-
export function numberToHexUnpadded(num: number | bigint): string {
|
|
37
|
-
const hex = num.toString(16);
|
|
38
|
-
return hex.length & 1 ? '0' + hex : hex;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function hexToNumber(hex: string): bigint {
|
|
42
|
-
if (typeof hex !== 'string') throw new Error('hex string expected, got ' + typeof hex);
|
|
43
|
-
return hex === '' ? _0n : BigInt('0x' + hex); // Big Endian
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex
|
|
47
|
-
const hasHexBuiltin: boolean =
|
|
48
|
-
// @ts-ignore
|
|
49
|
-
typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function';
|
|
50
|
-
|
|
51
|
-
// Array where index 0xf0 (240) is mapped to string 'f0'
|
|
52
|
-
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) =>
|
|
53
|
-
i.toString(16).padStart(2, '0')
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Convert byte array to hex string. Uses built-in function, when available.
|
|
58
|
-
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
59
|
-
*/
|
|
60
|
-
export function bytesToHex(bytes: Uint8Array): string {
|
|
61
|
-
abytes(bytes);
|
|
62
|
-
// @ts-ignore
|
|
63
|
-
if (hasHexBuiltin) return bytes.toHex();
|
|
64
|
-
// pre-caching improves the speed 6x
|
|
65
|
-
let hex = '';
|
|
66
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
67
|
-
hex += hexes[bytes[i]];
|
|
68
|
-
}
|
|
69
|
-
return hex;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// We use optimized technique to convert hex string to byte array
|
|
73
|
-
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 } as const;
|
|
74
|
-
function asciiToBase16(ch: number): number | undefined {
|
|
75
|
-
if (ch >= asciis._0 && ch <= asciis._9) return ch - asciis._0; // '2' => 50-48
|
|
76
|
-
if (ch >= asciis.A && ch <= asciis.F) return ch - (asciis.A - 10); // 'B' => 66-(65-10)
|
|
77
|
-
if (ch >= asciis.a && ch <= asciis.f) return ch - (asciis.a - 10); // 'b' => 98-(97-10)
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Convert hex string to byte array. Uses built-in function, when available.
|
|
83
|
-
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
84
|
-
*/
|
|
85
|
-
export function hexToBytes(hex: string): Uint8Array {
|
|
86
|
-
if (typeof hex !== 'string') throw new Error('hex string expected, got ' + typeof hex);
|
|
87
|
-
// @ts-ignore
|
|
88
|
-
if (hasHexBuiltin) return Uint8Array.fromHex(hex);
|
|
89
|
-
const hl = hex.length;
|
|
90
|
-
const al = hl / 2;
|
|
91
|
-
if (hl % 2) throw new Error('hex string expected, got unpadded hex of length ' + hl);
|
|
92
|
-
const array = new Uint8Array(al);
|
|
93
|
-
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
94
|
-
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
95
|
-
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
96
|
-
if (n1 === undefined || n2 === undefined) {
|
|
97
|
-
const char = hex[hi] + hex[hi + 1];
|
|
98
|
-
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
99
|
-
}
|
|
100
|
-
array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163
|
|
101
|
-
}
|
|
102
|
-
return array;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// BE: Big Endian, LE: Little Endian
|
|
106
|
-
export function bytesToNumberBE(bytes: Uint8Array): bigint {
|
|
107
|
-
return hexToNumber(bytesToHex(bytes));
|
|
108
|
-
}
|
|
109
|
-
export function bytesToNumberLE(bytes: Uint8Array): bigint {
|
|
110
|
-
abytes(bytes);
|
|
111
|
-
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function numberToBytesBE(n: number | bigint, len: number): Uint8Array {
|
|
115
|
-
return hexToBytes(n.toString(16).padStart(len * 2, '0'));
|
|
116
|
-
}
|
|
117
|
-
export function numberToBytesLE(n: number | bigint, len: number): Uint8Array {
|
|
118
|
-
return numberToBytesBE(n, len).reverse();
|
|
119
|
-
}
|
|
120
|
-
// Unpadded, rarely used
|
|
121
|
-
export function numberToVarBytesBE(n: number | bigint): Uint8Array {
|
|
122
|
-
return hexToBytes(numberToHexUnpadded(n));
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Takes hex string or Uint8Array, converts to Uint8Array.
|
|
127
|
-
* Validates output length.
|
|
128
|
-
* Will throw error for other types.
|
|
129
|
-
* @param title descriptive title for an error e.g. 'private key'
|
|
130
|
-
* @param hex hex string or Uint8Array
|
|
131
|
-
* @param expectedLength optional, will compare to result array's length
|
|
132
|
-
* @returns
|
|
133
|
-
*/
|
|
134
|
-
export function ensureBytes(title: string, hex: Hex, expectedLength?: number): Uint8Array {
|
|
135
|
-
let res: Uint8Array;
|
|
136
|
-
if (typeof hex === 'string') {
|
|
137
|
-
try {
|
|
138
|
-
res = hexToBytes(hex);
|
|
139
|
-
} catch (e) {
|
|
140
|
-
throw new Error(title + ' must be hex string or Uint8Array, cause: ' + e);
|
|
141
|
-
}
|
|
142
|
-
} else if (isBytes(hex)) {
|
|
143
|
-
// Uint8Array.from() instead of hash.slice() because node.js Buffer
|
|
144
|
-
// is instance of Uint8Array, and its slice() creates **mutable** copy
|
|
145
|
-
res = Uint8Array.from(hex);
|
|
146
|
-
} else {
|
|
147
|
-
throw new Error(title + ' must be hex string or Uint8Array');
|
|
148
|
-
}
|
|
149
|
-
const len = res.length;
|
|
150
|
-
if (typeof expectedLength === 'number' && len !== expectedLength)
|
|
151
|
-
throw new Error(title + ' of length ' + expectedLength + ' expected, got ' + len);
|
|
152
|
-
return res;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* Copies several Uint8Arrays into one.
|
|
157
|
-
*/
|
|
158
|
-
export function concatBytes(...arrays: Uint8Array[]): Uint8Array {
|
|
159
|
-
let sum = 0;
|
|
160
|
-
for (let i = 0; i < arrays.length; i++) {
|
|
161
|
-
const a = arrays[i];
|
|
162
|
-
abytes(a);
|
|
163
|
-
sum += a.length;
|
|
164
|
-
}
|
|
165
|
-
const res = new Uint8Array(sum);
|
|
166
|
-
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
167
|
-
const a = arrays[i];
|
|
168
|
-
res.set(a, pad);
|
|
169
|
-
pad += a.length;
|
|
170
|
-
}
|
|
171
|
-
return res;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Compares 2 u8a-s in kinda constant time
|
|
175
|
-
export function equalBytes(a: Uint8Array, b: Uint8Array): boolean {
|
|
176
|
-
if (a.length !== b.length) return false;
|
|
177
|
-
let diff = 0;
|
|
178
|
-
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
179
|
-
return diff === 0;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// Global symbols in both browsers and Node.js since v11
|
|
183
|
-
// See https://github.com/microsoft/TypeScript/issues/31535
|
|
184
|
-
declare const TextEncoder: any;
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
188
|
-
*/
|
|
189
|
-
export function utf8ToBytes(str: string): Uint8Array {
|
|
190
|
-
if (typeof str !== 'string') throw new Error('string expected');
|
|
191
|
-
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Is positive bigint
|
|
195
|
-
const isPosBig = (n: bigint) => typeof n === 'bigint' && _0n <= n;
|
|
196
|
-
|
|
197
|
-
export function inRange(n: bigint, min: bigint, max: bigint): boolean {
|
|
198
|
-
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Asserts min <= n < max. NOTE: It's < max and not <= max.
|
|
203
|
-
* @example
|
|
204
|
-
* aInRange('x', x, 1n, 256n); // would assume x is in (1n..255n)
|
|
205
|
-
*/
|
|
206
|
-
export function aInRange(title: string, n: bigint, min: bigint, max: bigint): void {
|
|
207
|
-
// Why min <= n < max and not a (min < n < max) OR b (min <= n <= max)?
|
|
208
|
-
// consider P=256n, min=0n, max=P
|
|
209
|
-
// - a for min=0 would require -1: `inRange('x', x, -1n, P)`
|
|
210
|
-
// - b would commonly require subtraction: `inRange('x', x, 0n, P - 1n)`
|
|
211
|
-
// - our way is the cleanest: `inRange('x', x, 0n, P)
|
|
212
|
-
if (!inRange(n, min, max))
|
|
213
|
-
throw new Error('expected valid ' + title + ': ' + min + ' <= n < ' + max + ', got ' + n);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// Bit operations
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Calculates amount of bits in a bigint.
|
|
220
|
-
* Same as `n.toString(2).length`
|
|
221
|
-
* TODO: merge with nLength in modular
|
|
222
|
-
*/
|
|
223
|
-
export function bitLen(n: bigint): number {
|
|
224
|
-
let len;
|
|
225
|
-
for (len = 0; n > _0n; n >>= _1n, len += 1);
|
|
226
|
-
return len;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Gets single bit at position.
|
|
231
|
-
* NOTE: first bit position is 0 (same as arrays)
|
|
232
|
-
* Same as `!!+Array.from(n.toString(2)).reverse()[pos]`
|
|
233
|
-
*/
|
|
234
|
-
export function bitGet(n: bigint, pos: number): bigint {
|
|
235
|
-
return (n >> BigInt(pos)) & _1n;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Sets single bit at position.
|
|
240
|
-
*/
|
|
241
|
-
export function bitSet(n: bigint, pos: number, value: boolean): bigint {
|
|
242
|
-
return n | ((value ? _1n : _0n) << BigInt(pos));
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Calculate mask for N bits. Not using ** operator with bigints because of old engines.
|
|
247
|
-
* Same as BigInt(`0b${Array(i).fill('1').join('')}`)
|
|
248
|
-
*/
|
|
249
|
-
export const bitMask = (n: number): bigint => (_1n << BigInt(n)) - _1n;
|
|
250
|
-
|
|
251
|
-
// DRBG
|
|
252
|
-
|
|
253
|
-
const u8n = (len: number) => new Uint8Array(len); // creates Uint8Array
|
|
254
|
-
const u8fr = (arr: ArrayLike<number>) => Uint8Array.from(arr); // another shortcut
|
|
255
|
-
type Pred<T> = (v: Uint8Array) => T | undefined;
|
|
256
|
-
/**
|
|
257
|
-
* Minimal HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
|
|
258
|
-
* @returns function that will call DRBG until 2nd arg returns something meaningful
|
|
259
|
-
* @example
|
|
260
|
-
* const drbg = createHmacDRBG<Key>(32, 32, hmac);
|
|
261
|
-
* drbg(seed, bytesToKey); // bytesToKey must return Key or undefined
|
|
262
|
-
*/
|
|
263
|
-
export function createHmacDrbg<T>(
|
|
264
|
-
hashLen: number,
|
|
265
|
-
qByteLen: number,
|
|
266
|
-
hmacFn: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array
|
|
267
|
-
): (seed: Uint8Array, predicate: Pred<T>) => T {
|
|
268
|
-
if (typeof hashLen !== 'number' || hashLen < 2) throw new Error('hashLen must be a number');
|
|
269
|
-
if (typeof qByteLen !== 'number' || qByteLen < 2) throw new Error('qByteLen must be a number');
|
|
270
|
-
if (typeof hmacFn !== 'function') throw new Error('hmacFn must be a function');
|
|
271
|
-
// Step B, Step C: set hashLen to 8*ceil(hlen/8)
|
|
272
|
-
let v = u8n(hashLen); // Minimal non-full-spec HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
|
|
273
|
-
let k = u8n(hashLen); // Steps B and C of RFC6979 3.2: set hashLen, in our case always same
|
|
274
|
-
let i = 0; // Iterations counter, will throw when over 1000
|
|
275
|
-
const reset = () => {
|
|
276
|
-
v.fill(1);
|
|
277
|
-
k.fill(0);
|
|
278
|
-
i = 0;
|
|
279
|
-
};
|
|
280
|
-
const h = (...b: Uint8Array[]) => hmacFn(k, v, ...b); // hmac(k)(v, ...values)
|
|
281
|
-
const reseed = (seed = u8n(0)) => {
|
|
282
|
-
// HMAC-DRBG reseed() function. Steps D-G
|
|
283
|
-
k = h(u8fr([0x00]), seed); // k = hmac(k || v || 0x00 || seed)
|
|
284
|
-
v = h(); // v = hmac(k || v)
|
|
285
|
-
if (seed.length === 0) return;
|
|
286
|
-
k = h(u8fr([0x01]), seed); // k = hmac(k || v || 0x01 || seed)
|
|
287
|
-
v = h(); // v = hmac(k || v)
|
|
288
|
-
};
|
|
289
|
-
const gen = () => {
|
|
290
|
-
// HMAC-DRBG generate() function
|
|
291
|
-
if (i++ >= 1000) throw new Error('drbg: tried 1000 values');
|
|
292
|
-
let len = 0;
|
|
293
|
-
const out: Uint8Array[] = [];
|
|
294
|
-
while (len < qByteLen) {
|
|
295
|
-
v = h();
|
|
296
|
-
const sl = v.slice();
|
|
297
|
-
out.push(sl);
|
|
298
|
-
len += v.length;
|
|
299
|
-
}
|
|
300
|
-
return concatBytes(...out);
|
|
301
|
-
};
|
|
302
|
-
const genUntil = (seed: Uint8Array, pred: Pred<T>): T => {
|
|
303
|
-
reset();
|
|
304
|
-
reseed(seed); // Steps D-G
|
|
305
|
-
let res: T | undefined = undefined; // Step H: grind until k is in [1..n-1]
|
|
306
|
-
while (!(res = pred(gen()))) reseed();
|
|
307
|
-
reset();
|
|
308
|
-
return res;
|
|
309
|
-
};
|
|
310
|
-
return genUntil;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// Validating curves and fields
|
|
314
|
-
|
|
315
|
-
const validatorFns = {
|
|
316
|
-
bigint: (val: any): boolean => typeof val === 'bigint',
|
|
317
|
-
function: (val: any): boolean => typeof val === 'function',
|
|
318
|
-
boolean: (val: any): boolean => typeof val === 'boolean',
|
|
319
|
-
string: (val: any): boolean => typeof val === 'string',
|
|
320
|
-
stringOrUint8Array: (val: any): boolean => typeof val === 'string' || isBytes(val),
|
|
321
|
-
isSafeInteger: (val: any): boolean => Number.isSafeInteger(val),
|
|
322
|
-
array: (val: any): boolean => Array.isArray(val),
|
|
323
|
-
field: (val: any, object: any): any => (object as any).Fp.isValid(val),
|
|
324
|
-
hash: (val: any): boolean => typeof val === 'function' && Number.isSafeInteger(val.outputLen),
|
|
325
|
-
} as const;
|
|
326
|
-
type Validator = keyof typeof validatorFns;
|
|
327
|
-
type ValMap<T extends Record<string, any>> = { [K in keyof T]?: Validator };
|
|
328
|
-
// type Record<K extends string | number | symbol, T> = { [P in K]: T; }
|
|
329
|
-
|
|
330
|
-
export function validateObject<T extends Record<string, any>>(
|
|
331
|
-
object: T,
|
|
332
|
-
validators: ValMap<T>,
|
|
333
|
-
optValidators: ValMap<T> = {}
|
|
334
|
-
): T {
|
|
335
|
-
const checkField = (fieldName: keyof T, type: Validator, isOptional: boolean) => {
|
|
336
|
-
const checkVal = validatorFns[type];
|
|
337
|
-
if (typeof checkVal !== 'function') throw new Error('invalid validator function');
|
|
338
|
-
|
|
339
|
-
const val = object[fieldName as keyof typeof object];
|
|
340
|
-
if (isOptional && val === undefined) return;
|
|
341
|
-
if (!checkVal(val, object)) {
|
|
342
|
-
throw new Error(
|
|
343
|
-
'param ' + String(fieldName) + ' is invalid. Expected ' + type + ', got ' + val
|
|
344
|
-
);
|
|
345
|
-
}
|
|
346
|
-
};
|
|
347
|
-
for (const [fieldName, type] of Object.entries(validators)) checkField(fieldName, type!, false);
|
|
348
|
-
for (const [fieldName, type] of Object.entries(optValidators)) checkField(fieldName, type!, true);
|
|
349
|
-
return object;
|
|
350
|
-
}
|
|
351
|
-
// validate type tests
|
|
352
|
-
// const o: { a: number; b: number; c: number } = { a: 1, b: 5, c: 6 };
|
|
353
|
-
// const z0 = validateObject(o, { a: 'isSafeInteger' }, { c: 'bigint' }); // Ok!
|
|
354
|
-
// // Should fail type-check
|
|
355
|
-
// const z1 = validateObject(o, { a: 'tmp' }, { c: 'zz' });
|
|
356
|
-
// const z2 = validateObject(o, { a: 'isSafeInteger' }, { c: 'zz' });
|
|
357
|
-
// const z3 = validateObject(o, { test: 'boolean', z: 'bug' });
|
|
358
|
-
// const z4 = validateObject(o, { a: 'boolean', z: 'bug' });
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* throws not implemented error
|
|
362
|
-
*/
|
|
363
|
-
export const notImplemented = (): never => {
|
|
364
|
-
throw new Error('not implemented');
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
/**
|
|
368
|
-
* Memoizes (caches) computation result.
|
|
369
|
-
* Uses WeakMap: the value is going auto-cleaned by GC after last reference is removed.
|
|
370
|
-
*/
|
|
371
|
-
export function memoized<T extends object, R, O extends any[]>(
|
|
372
|
-
fn: (arg: T, ...args: O) => R
|
|
373
|
-
): (arg: T, ...args: O) => R {
|
|
374
|
-
const map = new WeakMap<T, R>();
|
|
375
|
-
return (arg: T, ...args: O): R => {
|
|
376
|
-
const val = map.get(arg);
|
|
377
|
-
if (val !== undefined) return val;
|
|
378
|
-
const computed = fn(arg, ...args);
|
|
379
|
-
map.set(arg, computed);
|
|
380
|
-
return computed;
|
|
381
|
-
};
|
|
382
|
-
}
|
|
6
|
+
// TODO
|
|
7
|
+
// @deprecated use `@noble/curves/utils.js`
|