@ohos-ports/crypto 0.0.0-stage → 0.3.1-beta.1
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 +7 -0
- package/README.md +60 -2
- package/dist/bech32.d.ts +53 -0
- package/dist/bech32.js +187 -0
- package/dist/blake2b.d.ts +7 -0
- package/dist/blake2b.js +213 -0
- package/dist/bls12_318.d.ts +48 -0
- package/dist/bls12_318.js +268 -0
- package/dist/ed25519.d.ts +55 -0
- package/dist/ed25519.js +721 -0
- package/dist/hasGlobalWebCrypto.d.ts +1 -0
- package/dist/hasGlobalWebCrypto.js +21 -0
- package/dist/hmacSHA512.d.ts +6 -0
- package/dist/hmacSHA512.js +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +30 -0
- package/dist/keccak.d.ts +4 -0
- package/dist/keccak.js +43 -0
- package/dist/kes/constants.d.ts +2 -0
- package/dist/kes/constants.js +5 -0
- package/dist/kes/index.d.ts +0 -0
- package/dist/kes/index.js +0 -0
- package/dist/kes/macro/sum_kes.d.ts +15 -0
- package/dist/kes/macro/sum_kes.js +9 -0
- package/dist/kes/singleKes.d.ts +0 -0
- package/dist/kes/singleKes.js +0 -0
- package/dist/noble/_assert.d.ts +23 -0
- package/dist/noble/_assert.js +55 -0
- package/dist/noble/_md.d.ts +29 -0
- package/dist/noble/_md.js +170 -0
- package/dist/noble/_shortw_utils.d.ts +61 -0
- package/dist/noble/_shortw_utils.js +63 -0
- package/dist/noble/_u64.d.ts +28 -0
- package/dist/noble/_u64.js +116 -0
- package/dist/noble/abstract/bls.d.ts +119 -0
- package/dist/noble/abstract/bls.js +334 -0
- package/dist/noble/abstract/curve.d.ts +83 -0
- package/dist/noble/abstract/curve.js +319 -0
- package/dist/noble/abstract/edwards.d.ts +85 -0
- package/dist/noble/abstract/edwards.js +508 -0
- package/dist/noble/abstract/hash-to-curve.d.ts +57 -0
- package/dist/noble/abstract/hash-to-curve.js +220 -0
- package/dist/noble/abstract/modular.d.ts +122 -0
- package/dist/noble/abstract/modular.js +455 -0
- package/dist/noble/abstract/montgomery.d.ts +28 -0
- package/dist/noble/abstract/montgomery.js +172 -0
- package/dist/noble/abstract/utils.d.ts +106 -0
- package/dist/noble/abstract/utils.js +442 -0
- package/dist/noble/bls12_381.d.ts +95 -0
- package/dist/noble/bls12_381.js +1434 -0
- package/dist/noble/crypto.d.ts +1 -0
- package/dist/noble/crypto.js +4 -0
- package/dist/noble/curves/curve.d.ts +69 -0
- package/dist/noble/curves/curve.js +167 -0
- package/dist/noble/curves/modular.d.ts +122 -0
- package/dist/noble/curves/modular.js +442 -0
- package/dist/noble/curves/utils.d.ts +93 -0
- package/dist/noble/curves/utils.js +385 -0
- package/dist/noble/curves/weierstrass.d.ts +240 -0
- package/dist/noble/curves/weierstrass.js +1178 -0
- package/dist/noble/ed25519.d.ts +100 -0
- package/dist/noble/ed25519.js +494 -0
- package/dist/noble/hmac.d.ts +25 -0
- package/dist/noble/hmac.js +101 -0
- package/dist/noble/index.d.ts +2 -0
- package/dist/noble/index.js +20 -0
- package/dist/noble/keccak.d.ts +58 -0
- package/dist/noble/keccak.js +289 -0
- package/dist/noble/ripemd_160.d.ts +20 -0
- package/dist/noble/ripemd_160.js +147 -0
- package/dist/noble/secp256k1.d.ts +98 -0
- package/dist/noble/secp256k1.js +309 -0
- package/dist/noble/sha256.d.ts +34 -0
- package/dist/noble/sha256.js +146 -0
- package/dist/noble/sha512.d.ts +66 -0
- package/dist/noble/sha512.js +301 -0
- package/dist/noble/utils.d.ts +90 -0
- package/dist/noble/utils.js +278 -0
- package/dist/ripemd_160.d.ts +1 -0
- package/dist/ripemd_160.js +23 -0
- package/dist/rust_vrf_reimpl/curves.d.ts +162 -0
- package/dist/rust_vrf_reimpl/curves.js +4225 -0
- package/dist/rust_vrf_reimpl/vrf03.d.ts +60 -0
- package/dist/rust_vrf_reimpl/vrf03.js +373 -0
- package/dist/rust_vrf_reimpl/vrf10.d.ts +73 -0
- package/dist/rust_vrf_reimpl/vrf10.js +468 -0
- package/dist/rust_vrf_reimpl/vrf10_batch.d.ts +53 -0
- package/dist/rust_vrf_reimpl/vrf10_batch.js +245 -0
- package/dist/secp256k1.d.ts +4 -0
- package/dist/secp256k1.js +33 -0
- package/dist/sha2_256.d.ts +16 -0
- package/dist/sha2_256.js +403 -0
- package/dist/sha2_512.d.ts +16 -0
- package/dist/sha2_512.js +256 -0
- package/dist/sha3.d.ts +17 -0
- package/dist/sha3.js +181 -0
- package/dist/types.d.ts +26 -0
- package/dist/types.js +120 -0
- package/dist/utils/assert.d.ts +3 -0
- package/dist/utils/assert.js +55 -0
- package/dist/utils/base32.d.ts +5 -0
- package/dist/utils/base32.js +52 -0
- package/dist/utils/bigintToBuffer.d.ts +1 -0
- package/dist/utils/bigintToBuffer.js +27 -0
- package/dist/utils/bytesToHex.d.ts +2 -0
- package/dist/utils/bytesToHex.js +15 -0
- package/dist/utils/getRandomValues.d.ts +4 -0
- package/dist/utils/getRandomValues.js +12 -0
- package/dist/utils/hexToBytes.d.ts +2 -0
- package/dist/utils/hexToBytes.js +14 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +19 -0
- package/dist/utils/positiveMod.d.ts +1 -0
- package/dist/utils/positiveMod.js +11 -0
- package/dist/utils/promisifySync.d.ts +0 -0
- package/dist/utils/promisifySync.js +0 -0
- package/dist/utils/rotr32.d.ts +2 -0
- package/dist/utils/rotr32.js +7 -0
- package/dist/vrf.d.ts +74 -0
- package/dist/vrf.js +286 -0
- package/package.json +49 -4
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
THIS IMPLEMENTATION WAS TAKEN FROM `@noble/hashes`
|
|
4
|
+
|
|
5
|
+
original source: https://github.com/paulmillr/noble-curves/blob/38a4ca1e6b0a80c60b11bbb6b3d71e3bfabd5bbb/src/abstract/curve.ts
|
|
6
|
+
|
|
7
|
+
it was modified and adapted to this repository
|
|
8
|
+
|
|
9
|
+
the main reason for copying over and not using as dependency is because of different target in the `tsconfig.json`
|
|
10
|
+
|
|
11
|
+
`@noble/hashes` targets es2020, which tends to cause problems with some other (older) packages sometimes
|
|
12
|
+
|
|
13
|
+
this repo targets ES5+
|
|
14
|
+
*/
|
|
15
|
+
var __assign = (this && this.__assign) || function () {
|
|
16
|
+
__assign = Object.assign || function(t) {
|
|
17
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
18
|
+
s = arguments[i];
|
|
19
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
20
|
+
t[p] = s[p];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
24
|
+
return __assign.apply(this, arguments);
|
|
25
|
+
};
|
|
26
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
27
|
+
exports.pippenger = exports.validateBasic = exports.wNAF = void 0;
|
|
28
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
29
|
+
// Abelian group utilities
|
|
30
|
+
var modular_1 = require("./modular.js");
|
|
31
|
+
var utils_1 = require("./utils.js");
|
|
32
|
+
var _0n = BigInt(0);
|
|
33
|
+
var _1n = BigInt(1);
|
|
34
|
+
// Since points in different groups cannot be equal (different object constructor),
|
|
35
|
+
// we can have single place to store precomputes
|
|
36
|
+
var pointPrecomputes = new WeakMap();
|
|
37
|
+
var pointWindowSizes = new WeakMap(); // This allows use make points immutable (nothing changes inside)
|
|
38
|
+
function getW(P) {
|
|
39
|
+
return pointWindowSizes.get(P) || 1;
|
|
40
|
+
}
|
|
41
|
+
function constTimeNegate(condition, item) {
|
|
42
|
+
var neg = item.negate();
|
|
43
|
+
return condition ? neg : item;
|
|
44
|
+
}
|
|
45
|
+
function validateW(W, bits) {
|
|
46
|
+
if (!Number.isSafeInteger(W) || W <= 0 || W > bits)
|
|
47
|
+
throw new Error('invalid window size, expected [1..' + bits + '], got W=' + W);
|
|
48
|
+
}
|
|
49
|
+
function calcWOpts(W, bits) {
|
|
50
|
+
validateW(W, bits);
|
|
51
|
+
var windows = Math.ceil(bits / W) + 1; // +1, because
|
|
52
|
+
var windowSize = Math.pow(2, (W - 1)); // -1 because we skip zero
|
|
53
|
+
return { windows: windows, windowSize: windowSize };
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Elliptic curve multiplication of Point by scalar. Fragile.
|
|
57
|
+
* Scalars should always be less than curve order: this should be checked inside of a curve itself.
|
|
58
|
+
* Creates precomputation tables for fast multiplication:
|
|
59
|
+
* - private scalar is split by fixed size windows of W bits
|
|
60
|
+
* - every window point is collected from window's table & added to accumulator
|
|
61
|
+
* - since windows are different, same point inside tables won't be accessed more than once per calc
|
|
62
|
+
* - each multiplication is 'Math.ceil(CURVE_ORDER / 𝑊) + 1' point additions (fixed for any scalar)
|
|
63
|
+
* - +1 window is neccessary for wNAF
|
|
64
|
+
* - wNAF reduces table size: 2x less memory + 2x faster generation, but 10% slower multiplication
|
|
65
|
+
*
|
|
66
|
+
* @todo Research returning 2d JS array of windows, instead of a single window.
|
|
67
|
+
* This would allow windows to be in different memory locations
|
|
68
|
+
*/
|
|
69
|
+
function wNAF(c, bits) {
|
|
70
|
+
return {
|
|
71
|
+
constTimeNegate: constTimeNegate,
|
|
72
|
+
hasPrecomputes: function (elm) {
|
|
73
|
+
return getW(elm) !== 1;
|
|
74
|
+
},
|
|
75
|
+
// non-const time multiplication ladder
|
|
76
|
+
unsafeLadder: function (elm, n, p) {
|
|
77
|
+
if (p === void 0) { p = c.ZERO; }
|
|
78
|
+
var d = elm;
|
|
79
|
+
while (n > _0n) {
|
|
80
|
+
if (n & _1n)
|
|
81
|
+
p = p.add(d);
|
|
82
|
+
d = d.double();
|
|
83
|
+
n >>= _1n;
|
|
84
|
+
}
|
|
85
|
+
return p;
|
|
86
|
+
},
|
|
87
|
+
/**
|
|
88
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
89
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
90
|
+
* Number of precomputed points depends on the curve size:
|
|
91
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
92
|
+
* - 𝑊 is the window size
|
|
93
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
94
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
95
|
+
* @param elm Point instance
|
|
96
|
+
* @param W window size
|
|
97
|
+
* @returns precomputed point tables flattened to a single array
|
|
98
|
+
*/
|
|
99
|
+
precomputeWindow: function (elm, W) {
|
|
100
|
+
var _a = calcWOpts(W, bits), windows = _a.windows, windowSize = _a.windowSize;
|
|
101
|
+
var points = [];
|
|
102
|
+
var p = elm;
|
|
103
|
+
var base = p;
|
|
104
|
+
for (var window_1 = 0; window_1 < windows; window_1++) {
|
|
105
|
+
base = p;
|
|
106
|
+
points.push(base);
|
|
107
|
+
// =1, because we skip zero
|
|
108
|
+
for (var i = 1; i < windowSize; i++) {
|
|
109
|
+
base = base.add(p);
|
|
110
|
+
points.push(base);
|
|
111
|
+
}
|
|
112
|
+
p = base.double();
|
|
113
|
+
}
|
|
114
|
+
return points;
|
|
115
|
+
},
|
|
116
|
+
/**
|
|
117
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
118
|
+
* @param W window size
|
|
119
|
+
* @param precomputes precomputed tables
|
|
120
|
+
* @param n scalar (we don't check here, but should be less than curve order)
|
|
121
|
+
* @returns real and fake (for const-time) points
|
|
122
|
+
*/
|
|
123
|
+
wNAF: function (W, precomputes, n) {
|
|
124
|
+
n = BigInt(n);
|
|
125
|
+
// TODO: maybe check that scalar is less than group order? wNAF behavious is undefined otherwise
|
|
126
|
+
// But need to carefully remove other checks before wNAF. ORDER == bits here
|
|
127
|
+
var _a = calcWOpts(W, bits), windows = _a.windows, windowSize = _a.windowSize;
|
|
128
|
+
var p = c.ZERO;
|
|
129
|
+
var f = c.BASE;
|
|
130
|
+
var mask = BigInt(Math.pow(2, W) - 1); // Create mask with W ones: 0b1111 for W=4 etc.
|
|
131
|
+
var maxNumber = Math.pow(2, W);
|
|
132
|
+
var shiftBy = BigInt(W);
|
|
133
|
+
for (var window_2 = 0; window_2 < windows; window_2++) {
|
|
134
|
+
var offset = window_2 * windowSize;
|
|
135
|
+
// Extract W bits.
|
|
136
|
+
var wbits = Number(n & mask);
|
|
137
|
+
// Shift number by W bits.
|
|
138
|
+
n >>= shiftBy;
|
|
139
|
+
// If the bits are bigger than max size, we'll split those.
|
|
140
|
+
// +224 => 256 - 32
|
|
141
|
+
if (wbits > windowSize) {
|
|
142
|
+
wbits -= maxNumber;
|
|
143
|
+
n += _1n;
|
|
144
|
+
}
|
|
145
|
+
// This code was first written with assumption that 'f' and 'p' will never be infinity point:
|
|
146
|
+
// since each addition is multiplied by 2 ** W, it cannot cancel each other. However,
|
|
147
|
+
// there is negate now: it is possible that negated element from low value
|
|
148
|
+
// would be the same as high element, which will create carry into next window.
|
|
149
|
+
// It's not obvious how this can fail, but still worth investigating later.
|
|
150
|
+
// Check if we're onto Zero point.
|
|
151
|
+
// Add random point inside current window to f.
|
|
152
|
+
var offset1 = offset;
|
|
153
|
+
var offset2 = offset + Math.abs(wbits) - 1; // -1 because we skip zero
|
|
154
|
+
var cond1 = window_2 % 2 !== 0;
|
|
155
|
+
var cond2 = wbits < 0;
|
|
156
|
+
if (wbits === 0) {
|
|
157
|
+
// The most important part for const-time getPublicKey
|
|
158
|
+
f = f.add(constTimeNegate(cond1, precomputes[offset1]));
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
p = p.add(constTimeNegate(cond2, precomputes[offset2]));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// JIT-compiler should not eliminate f here, since it will later be used in normalizeZ()
|
|
165
|
+
// Even if the variable is still unused, there are some checks which will
|
|
166
|
+
// throw an exception, so compiler needs to prove they won't happen, which is hard.
|
|
167
|
+
// At this point there is a way to F be infinity-point even if p is not,
|
|
168
|
+
// which makes it less const-time: around 1 bigint multiply.
|
|
169
|
+
return { p: p, f: f };
|
|
170
|
+
},
|
|
171
|
+
/**
|
|
172
|
+
* Implements ec unsafe (non const-time) multiplication using precomputed tables and w-ary non-adjacent form.
|
|
173
|
+
* @param W window size
|
|
174
|
+
* @param precomputes precomputed tables
|
|
175
|
+
* @param n scalar (we don't check here, but should be less than curve order)
|
|
176
|
+
* @param acc accumulator point to add result of multiplication
|
|
177
|
+
* @returns point
|
|
178
|
+
*/
|
|
179
|
+
wNAFUnsafe: function (W, precomputes, n, acc) {
|
|
180
|
+
if (acc === void 0) { acc = c.ZERO; }
|
|
181
|
+
var _a = calcWOpts(W, bits), windows = _a.windows, windowSize = _a.windowSize;
|
|
182
|
+
var mask = BigInt(Math.pow(2, W) - 1); // Create mask with W ones: 0b1111 for W=4 etc.
|
|
183
|
+
var maxNumber = Math.pow(2, W);
|
|
184
|
+
var shiftBy = BigInt(W);
|
|
185
|
+
for (var window_3 = 0; window_3 < windows; window_3++) {
|
|
186
|
+
var offset = window_3 * windowSize;
|
|
187
|
+
if (n === _0n)
|
|
188
|
+
break; // No need to go over empty scalar
|
|
189
|
+
// Extract W bits.
|
|
190
|
+
var wbits = Number(n & mask);
|
|
191
|
+
// Shift number by W bits.
|
|
192
|
+
n >>= shiftBy;
|
|
193
|
+
// If the bits are bigger than max size, we'll split those.
|
|
194
|
+
// +224 => 256 - 32
|
|
195
|
+
if (wbits > windowSize) {
|
|
196
|
+
wbits -= maxNumber;
|
|
197
|
+
n += _1n;
|
|
198
|
+
}
|
|
199
|
+
if (wbits === 0)
|
|
200
|
+
continue;
|
|
201
|
+
var curr = precomputes[offset + Math.abs(wbits) - 1]; // -1 because we skip zero
|
|
202
|
+
if (wbits < 0)
|
|
203
|
+
curr = curr.negate();
|
|
204
|
+
// NOTE: by re-using acc, we can save a lot of additions in case of MSM
|
|
205
|
+
acc = acc.add(curr);
|
|
206
|
+
}
|
|
207
|
+
return acc;
|
|
208
|
+
},
|
|
209
|
+
getPrecomputes: function (W, P, transform) {
|
|
210
|
+
// Calculate precomputes on a first run, reuse them after
|
|
211
|
+
var comp = pointPrecomputes.get(P);
|
|
212
|
+
if (!comp) {
|
|
213
|
+
comp = this.precomputeWindow(P, W);
|
|
214
|
+
if (W !== 1)
|
|
215
|
+
pointPrecomputes.set(P, transform(comp));
|
|
216
|
+
}
|
|
217
|
+
return comp;
|
|
218
|
+
},
|
|
219
|
+
wNAFCached: function (P, n, transform) {
|
|
220
|
+
var W = getW(P);
|
|
221
|
+
return this.wNAF(W, this.getPrecomputes(W, P, transform), n);
|
|
222
|
+
},
|
|
223
|
+
wNAFCachedUnsafe: function (P, n, transform, prev) {
|
|
224
|
+
var W = getW(P);
|
|
225
|
+
if (W === 1)
|
|
226
|
+
return this.unsafeLadder(P, n, prev); // For W=1 ladder is ~x2 faster
|
|
227
|
+
return this.wNAFUnsafe(W, this.getPrecomputes(W, P, transform), n, prev);
|
|
228
|
+
},
|
|
229
|
+
// We calculate precomputes for elliptic curve point multiplication
|
|
230
|
+
// using windowed method. This specifies window size and
|
|
231
|
+
// stores precomputed values. Usually only base point would be precomputed.
|
|
232
|
+
setWindowSize: function (P, W) {
|
|
233
|
+
validateW(W, bits);
|
|
234
|
+
pointWindowSizes.set(P, W);
|
|
235
|
+
pointPrecomputes.delete(P);
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
exports.wNAF = wNAF;
|
|
240
|
+
function validateBasic(curve) {
|
|
241
|
+
(0, modular_1.validateField)(curve.Fp);
|
|
242
|
+
(0, utils_1.validateObject)(curve, {
|
|
243
|
+
n: 'bigint',
|
|
244
|
+
h: 'bigint',
|
|
245
|
+
Gx: 'field',
|
|
246
|
+
Gy: 'field',
|
|
247
|
+
}, {
|
|
248
|
+
nBitLength: 'isSafeInteger',
|
|
249
|
+
nByteLength: 'isSafeInteger',
|
|
250
|
+
});
|
|
251
|
+
// Set defaults
|
|
252
|
+
return Object.freeze(__assign(__assign(__assign({}, (0, modular_1.nLength)(curve.n, curve.nBitLength)), curve), { p: curve.Fp.ORDER }));
|
|
253
|
+
}
|
|
254
|
+
exports.validateBasic = validateBasic;
|
|
255
|
+
function validateMSMPoints(points, c) {
|
|
256
|
+
if (!Array.isArray(points))
|
|
257
|
+
throw new Error('array expected');
|
|
258
|
+
points.forEach(function (p, i) {
|
|
259
|
+
if (!(p instanceof c))
|
|
260
|
+
throw new Error('invalid point at index ' + i);
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
function validateMSMScalars(scalars, field) {
|
|
264
|
+
if (!Array.isArray(scalars))
|
|
265
|
+
throw new Error('array of scalars expected');
|
|
266
|
+
scalars.forEach(function (s, i) {
|
|
267
|
+
if (!field.isValid(s))
|
|
268
|
+
throw new Error('invalid scalar at index ' + i);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Pippenger algorithm for multi-scalar multiplication (MSM, Pa + Qb + Rc + ...).
|
|
273
|
+
* 30x faster vs naive addition on L=4096, 10x faster with precomputes.
|
|
274
|
+
* For N=254bit, L=1, it does: 1024 ADD + 254 DBL. For L=5: 1536 ADD + 254 DBL.
|
|
275
|
+
* Algorithmically constant-time (for same L), even when 1 point + scalar, or when scalar = 0.
|
|
276
|
+
* @param c Curve Point constructor
|
|
277
|
+
* @param fieldN field over CURVE.N - important that it's not over CURVE.P
|
|
278
|
+
* @param points array of L curve points
|
|
279
|
+
* @param scalars array of L scalars (aka private keys / bigints)
|
|
280
|
+
*/
|
|
281
|
+
function pippenger(c, fieldN, points, scalars) {
|
|
282
|
+
// If we split scalars by some window (let's say 8 bits), every chunk will only
|
|
283
|
+
// take 256 buckets even if there are 4096 scalars, also re-uses double.
|
|
284
|
+
// TODO:
|
|
285
|
+
// - https://eprint.iacr.org/2024/750.pdf
|
|
286
|
+
// - https://tches.iacr.org/index.php/TCHES/article/view/10287
|
|
287
|
+
// 0 is accepted in scalars
|
|
288
|
+
validateMSMPoints(points, c);
|
|
289
|
+
validateMSMScalars(scalars, fieldN);
|
|
290
|
+
if (points.length !== scalars.length)
|
|
291
|
+
throw new Error('arrays of points and scalars must have equal length');
|
|
292
|
+
var zero = c.ZERO;
|
|
293
|
+
var wbits = (0, utils_1.bitLen)(BigInt(points.length));
|
|
294
|
+
var windowSize = wbits > 12 ? wbits - 3 : wbits > 4 ? wbits - 2 : wbits ? 2 : 1; // in bits
|
|
295
|
+
var MASK = (1 << windowSize) - 1;
|
|
296
|
+
var buckets = new Array(MASK + 1).fill(zero); // +1 for zero array
|
|
297
|
+
var lastBits = Math.floor((fieldN.BITS - 1) / windowSize) * windowSize;
|
|
298
|
+
var sum = zero;
|
|
299
|
+
for (var i = lastBits; i >= 0; i -= windowSize) {
|
|
300
|
+
buckets.fill(zero);
|
|
301
|
+
for (var j = 0; j < scalars.length; j++) {
|
|
302
|
+
var scalar = scalars[j];
|
|
303
|
+
var wbits_1 = Number((scalar >> BigInt(i)) & BigInt(MASK));
|
|
304
|
+
buckets[wbits_1] = buckets[wbits_1].add(points[j]);
|
|
305
|
+
}
|
|
306
|
+
var resI = zero; // not using this will do small speed-up, but will lose ct
|
|
307
|
+
// Skip first bucket, because it is zero
|
|
308
|
+
for (var j = buckets.length - 1, sumI = zero; j > 0; j--) {
|
|
309
|
+
sumI = sumI.add(buckets[j]);
|
|
310
|
+
resI = resI.add(sumI);
|
|
311
|
+
}
|
|
312
|
+
sum = sum.add(resI);
|
|
313
|
+
if (i !== 0)
|
|
314
|
+
for (var j = 0; j < windowSize; j++)
|
|
315
|
+
sum = sum.double();
|
|
316
|
+
}
|
|
317
|
+
return sum;
|
|
318
|
+
}
|
|
319
|
+
exports.pippenger = pippenger;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type AffinePoint, type BasicCurve, type Group, type GroupConstructor } from "./curve.js";
|
|
2
|
+
import { type FHash, type Hex } from './utils.js';
|
|
3
|
+
/** Edwards curves must declare params a & d. */
|
|
4
|
+
export type CurveType = BasicCurve<bigint> & {
|
|
5
|
+
a: bigint;
|
|
6
|
+
d: bigint;
|
|
7
|
+
hash: FHash;
|
|
8
|
+
randomBytes: (bytesLength?: number) => Uint8Array;
|
|
9
|
+
adjustScalarBytes?: (bytes: Uint8Array) => Uint8Array;
|
|
10
|
+
domain?: (data: Uint8Array, ctx: Uint8Array, phflag: boolean) => Uint8Array;
|
|
11
|
+
uvRatio?: (u: bigint, v: bigint) => {
|
|
12
|
+
isValid: boolean;
|
|
13
|
+
value: bigint;
|
|
14
|
+
};
|
|
15
|
+
prehash?: FHash;
|
|
16
|
+
mapToCurve?: (scalar: bigint[]) => AffinePoint<bigint>;
|
|
17
|
+
};
|
|
18
|
+
export type CurveTypeWithLength = Readonly<CurveType & {
|
|
19
|
+
nByteLength: number;
|
|
20
|
+
nBitLength: number;
|
|
21
|
+
}>;
|
|
22
|
+
declare function validateOpts(curve: CurveType): CurveTypeWithLength;
|
|
23
|
+
/** Instance of Extended Point with coordinates in X, Y, Z, T. */
|
|
24
|
+
export interface ExtPointType extends Group<ExtPointType> {
|
|
25
|
+
readonly ex: bigint;
|
|
26
|
+
readonly ey: bigint;
|
|
27
|
+
readonly ez: bigint;
|
|
28
|
+
readonly et: bigint;
|
|
29
|
+
get x(): bigint;
|
|
30
|
+
get y(): bigint;
|
|
31
|
+
assertValidity(): void;
|
|
32
|
+
multiply(scalar: bigint): ExtPointType;
|
|
33
|
+
multiplyUnsafe(scalar: bigint): ExtPointType;
|
|
34
|
+
isSmallOrder(): boolean;
|
|
35
|
+
isTorsionFree(): boolean;
|
|
36
|
+
clearCofactor(): ExtPointType;
|
|
37
|
+
toAffine(iz?: bigint): AffinePoint<bigint>;
|
|
38
|
+
toRawBytes(isCompressed?: boolean): Uint8Array;
|
|
39
|
+
toHex(isCompressed?: boolean): string;
|
|
40
|
+
_setWindowSize(windowSize: number): void;
|
|
41
|
+
}
|
|
42
|
+
/** Static methods of Extended Point with coordinates in X, Y, Z, T. */
|
|
43
|
+
export interface ExtPointConstructor extends GroupConstructor<ExtPointType> {
|
|
44
|
+
new (x: bigint, y: bigint, z: bigint, t: bigint): ExtPointType;
|
|
45
|
+
fromAffine(p: AffinePoint<bigint>): ExtPointType;
|
|
46
|
+
fromHex(hex: Hex): ExtPointType;
|
|
47
|
+
fromPrivateKey(privateKey: Hex): ExtPointType;
|
|
48
|
+
msm(points: ExtPointType[], scalars: bigint[]): ExtPointType;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Edwards Curve interface.
|
|
52
|
+
* Main methods: `getPublicKey(priv)`, `sign(msg, priv)`, `verify(sig, msg, pub)`.
|
|
53
|
+
*/
|
|
54
|
+
export type CurveFn = {
|
|
55
|
+
CURVE: ReturnType<typeof validateOpts>;
|
|
56
|
+
getPublicKey: (privateKey: Hex) => Uint8Array;
|
|
57
|
+
sign: (message: Hex, privateKey: Hex, options?: {
|
|
58
|
+
context?: Hex;
|
|
59
|
+
}) => Uint8Array;
|
|
60
|
+
verify: (sig: Hex, message: Hex, publicKey: Hex, options?: {
|
|
61
|
+
context?: Hex;
|
|
62
|
+
zip215: boolean;
|
|
63
|
+
}) => boolean;
|
|
64
|
+
ExtendedPoint: ExtPointConstructor;
|
|
65
|
+
utils: {
|
|
66
|
+
randomPrivateKey: () => Uint8Array;
|
|
67
|
+
getExtendedPublicKey: (key: Hex) => {
|
|
68
|
+
head: Uint8Array;
|
|
69
|
+
prefix: Uint8Array;
|
|
70
|
+
scalar: bigint;
|
|
71
|
+
point: ExtPointType;
|
|
72
|
+
pointBytes: Uint8Array;
|
|
73
|
+
};
|
|
74
|
+
precompute: (windowSize?: number, point?: ExtPointType) => ExtPointType;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Creates Twisted Edwards curve with EdDSA signatures.
|
|
79
|
+
* @example
|
|
80
|
+
* import { Field } from '@noble/curves/abstract/modular';
|
|
81
|
+
* // Before that, define BigInt-s: a, d, p, n, Gx, Gy, h
|
|
82
|
+
* const curve = twistedEdwards({ a, d, Fp: Field(p), n, Gx, Gy, h })
|
|
83
|
+
*/
|
|
84
|
+
export declare function twistedEdwards(curveDef: CurveType): CurveFn;
|
|
85
|
+
export {};
|