@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,220 @@
|
|
|
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/hash-to-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
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
27
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
28
|
+
if (!m) return o;
|
|
29
|
+
var i = m.call(o), r, ar = [], e;
|
|
30
|
+
try {
|
|
31
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
32
|
+
}
|
|
33
|
+
catch (error) { e = { error: error }; }
|
|
34
|
+
finally {
|
|
35
|
+
try {
|
|
36
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
37
|
+
}
|
|
38
|
+
finally { if (e) throw e.error; }
|
|
39
|
+
}
|
|
40
|
+
return ar;
|
|
41
|
+
};
|
|
42
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
43
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
44
|
+
if (ar || !(i in from)) {
|
|
45
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
46
|
+
ar[i] = from[i];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
+
exports.createHasher = exports.isogenyMap = exports.hash_to_field = exports.expand_message_xof = exports.expand_message_xmd = void 0;
|
|
53
|
+
var modular_1 = require("./modular.js");
|
|
54
|
+
var utils_1 = require("./utils.js");
|
|
55
|
+
// Octet Stream to Integer. "spec" implementation of os2ip is 2.5x slower vs bytesToNumberBE.
|
|
56
|
+
var os2ip = utils_1.bytesToNumberBE;
|
|
57
|
+
// Integer to Octet Stream (numberToBytesBE)
|
|
58
|
+
function i2osp(value, length) {
|
|
59
|
+
if (value < 0 || value >= 1 << (8 * length)) {
|
|
60
|
+
throw new Error("bad I2OSP call: value=".concat(value, " length=").concat(length));
|
|
61
|
+
}
|
|
62
|
+
var res = Array.from({ length: length }).fill(0);
|
|
63
|
+
for (var i = length - 1; i >= 0; i--) {
|
|
64
|
+
res[i] = value & 0xff;
|
|
65
|
+
value >>>= 8;
|
|
66
|
+
}
|
|
67
|
+
return new Uint8Array(res);
|
|
68
|
+
}
|
|
69
|
+
function strxor(a, b) {
|
|
70
|
+
var arr = new Uint8Array(a.length);
|
|
71
|
+
for (var i = 0; i < a.length; i++) {
|
|
72
|
+
arr[i] = a[i] ^ b[i];
|
|
73
|
+
}
|
|
74
|
+
return arr;
|
|
75
|
+
}
|
|
76
|
+
function anum(item) {
|
|
77
|
+
if (!Number.isSafeInteger(item))
|
|
78
|
+
throw new Error('number expected');
|
|
79
|
+
}
|
|
80
|
+
// Produces a uniformly random byte string using a cryptographic hash function H that outputs b bits
|
|
81
|
+
// https://www.rfc-editor.org/rfc/rfc9380#section-5.3.1
|
|
82
|
+
function expand_message_xmd(msg, DST, lenInBytes, H) {
|
|
83
|
+
(0, utils_1.abytes)(msg);
|
|
84
|
+
(0, utils_1.abytes)(DST);
|
|
85
|
+
anum(lenInBytes);
|
|
86
|
+
// https://www.rfc-editor.org/rfc/rfc9380#section-5.3.3
|
|
87
|
+
if (DST.length > 255)
|
|
88
|
+
DST = H((0, utils_1.concatBytes)((0, utils_1.utf8ToBytes)('H2C-OVERSIZE-DST-'), DST));
|
|
89
|
+
var b_in_bytes = H.outputLen, r_in_bytes = H.blockLen;
|
|
90
|
+
var ell = Math.ceil(lenInBytes / b_in_bytes);
|
|
91
|
+
if (ell > 255)
|
|
92
|
+
throw new Error('Invalid xmd length');
|
|
93
|
+
var DST_prime = (0, utils_1.concatBytes)(DST, i2osp(DST.length, 1));
|
|
94
|
+
var Z_pad = i2osp(0, r_in_bytes);
|
|
95
|
+
var l_i_b_str = i2osp(lenInBytes, 2); // len_in_bytes_str
|
|
96
|
+
var b = new Array(ell);
|
|
97
|
+
var b_0 = H((0, utils_1.concatBytes)(Z_pad, msg, l_i_b_str, i2osp(0, 1), DST_prime));
|
|
98
|
+
b[0] = H((0, utils_1.concatBytes)(b_0, i2osp(1, 1), DST_prime));
|
|
99
|
+
for (var i = 1; i <= ell; i++) {
|
|
100
|
+
var args = [strxor(b_0, b[i - 1]), i2osp(i + 1, 1), DST_prime];
|
|
101
|
+
b[i] = H(utils_1.concatBytes.apply(void 0, __spreadArray([], __read(args), false)));
|
|
102
|
+
}
|
|
103
|
+
var pseudo_random_bytes = utils_1.concatBytes.apply(void 0, __spreadArray([], __read(b), false));
|
|
104
|
+
return pseudo_random_bytes.slice(0, lenInBytes);
|
|
105
|
+
}
|
|
106
|
+
exports.expand_message_xmd = expand_message_xmd;
|
|
107
|
+
// Produces a uniformly random byte string using an extendable-output function (XOF) H.
|
|
108
|
+
// 1. The collision resistance of H MUST be at least k bits.
|
|
109
|
+
// 2. H MUST be an XOF that has been proved indifferentiable from
|
|
110
|
+
// a random oracle under a reasonable cryptographic assumption.
|
|
111
|
+
// https://www.rfc-editor.org/rfc/rfc9380#section-5.3.2
|
|
112
|
+
function expand_message_xof(msg, DST, lenInBytes, k, H) {
|
|
113
|
+
(0, utils_1.abytes)(msg);
|
|
114
|
+
(0, utils_1.abytes)(DST);
|
|
115
|
+
anum(lenInBytes);
|
|
116
|
+
// https://www.rfc-editor.org/rfc/rfc9380#section-5.3.3
|
|
117
|
+
// DST = H('H2C-OVERSIZE-DST-' || a_very_long_DST, Math.ceil((lenInBytes * k) / 8));
|
|
118
|
+
if (DST.length > 255) {
|
|
119
|
+
var dkLen = Math.ceil((2 * k) / 8);
|
|
120
|
+
DST = H.create({ dkLen: dkLen }).update((0, utils_1.utf8ToBytes)('H2C-OVERSIZE-DST-')).update(DST).digest();
|
|
121
|
+
}
|
|
122
|
+
if (lenInBytes > 65535 || DST.length > 255)
|
|
123
|
+
throw new Error('expand_message_xof: invalid lenInBytes');
|
|
124
|
+
return (H.create({ dkLen: lenInBytes })
|
|
125
|
+
.update(msg)
|
|
126
|
+
.update(i2osp(lenInBytes, 2))
|
|
127
|
+
// 2. DST_prime = DST || I2OSP(len(DST), 1)
|
|
128
|
+
.update(DST)
|
|
129
|
+
.update(i2osp(DST.length, 1))
|
|
130
|
+
.digest());
|
|
131
|
+
}
|
|
132
|
+
exports.expand_message_xof = expand_message_xof;
|
|
133
|
+
/**
|
|
134
|
+
* Hashes arbitrary-length byte strings to a list of one or more elements of a finite field F
|
|
135
|
+
* https://www.rfc-editor.org/rfc/rfc9380#section-5.2
|
|
136
|
+
* @param msg a byte string containing the message to hash
|
|
137
|
+
* @param count the number of elements of F to output
|
|
138
|
+
* @param options `{DST: string, p: bigint, m: number, k: number, expand: 'xmd' | 'xof', hash: H}`, see above
|
|
139
|
+
* @returns [u_0, ..., u_(count - 1)], a list of field elements.
|
|
140
|
+
*/
|
|
141
|
+
function hash_to_field(msg, count, options) {
|
|
142
|
+
(0, utils_1.validateObject)(options, {
|
|
143
|
+
DST: 'stringOrUint8Array',
|
|
144
|
+
p: 'bigint',
|
|
145
|
+
m: 'isSafeInteger',
|
|
146
|
+
k: 'isSafeInteger',
|
|
147
|
+
hash: 'hash',
|
|
148
|
+
});
|
|
149
|
+
var p = options.p, k = options.k, m = options.m, hash = options.hash, expand = options.expand, _DST = options.DST;
|
|
150
|
+
(0, utils_1.abytes)(msg);
|
|
151
|
+
anum(count);
|
|
152
|
+
var DST = typeof _DST === 'string' ? (0, utils_1.utf8ToBytes)(_DST) : _DST;
|
|
153
|
+
var log2p = p.toString(2).length;
|
|
154
|
+
var L = Math.ceil((log2p + k) / 8); // section 5.1 of ietf draft link above
|
|
155
|
+
var len_in_bytes = count * m * L;
|
|
156
|
+
var prb; // pseudo_random_bytes
|
|
157
|
+
if (expand === 'xmd') {
|
|
158
|
+
prb = expand_message_xmd(msg, DST, len_in_bytes, hash);
|
|
159
|
+
}
|
|
160
|
+
else if (expand === 'xof') {
|
|
161
|
+
prb = expand_message_xof(msg, DST, len_in_bytes, k, hash);
|
|
162
|
+
}
|
|
163
|
+
else if (expand === '_internal_pass') {
|
|
164
|
+
// for internal tests only
|
|
165
|
+
prb = msg;
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
throw new Error('expand must be "xmd" or "xof"');
|
|
169
|
+
}
|
|
170
|
+
var u = new Array(count);
|
|
171
|
+
for (var i = 0; i < count; i++) {
|
|
172
|
+
var e = new Array(m);
|
|
173
|
+
for (var j = 0; j < m; j++) {
|
|
174
|
+
var elm_offset = L * (j + i * m);
|
|
175
|
+
var tv = prb.subarray(elm_offset, elm_offset + L);
|
|
176
|
+
e[j] = (0, modular_1.mod)(os2ip(tv), p);
|
|
177
|
+
}
|
|
178
|
+
u[i] = e;
|
|
179
|
+
}
|
|
180
|
+
return u;
|
|
181
|
+
}
|
|
182
|
+
exports.hash_to_field = hash_to_field;
|
|
183
|
+
function isogenyMap(field, map) {
|
|
184
|
+
// Make same order as in spec
|
|
185
|
+
var COEFF = map.map(function (i) { return Array.from(i).reverse(); });
|
|
186
|
+
return function (x, y) {
|
|
187
|
+
var _a = __read(COEFF.map(function (val) {
|
|
188
|
+
return val.reduce(function (acc, i) { return field.add(field.mul(acc, x), i); });
|
|
189
|
+
}), 4), xNum = _a[0], xDen = _a[1], yNum = _a[2], yDen = _a[3];
|
|
190
|
+
x = field.div(xNum, xDen); // xNum / xDen
|
|
191
|
+
y = field.mul(y, field.div(yNum, yDen)); // y * (yNum / yDev)
|
|
192
|
+
return { x: x, y: y };
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
exports.isogenyMap = isogenyMap;
|
|
196
|
+
function createHasher(Point, mapToCurve, def) {
|
|
197
|
+
if (typeof mapToCurve !== 'function')
|
|
198
|
+
throw new Error('mapToCurve() must be defined');
|
|
199
|
+
return {
|
|
200
|
+
// Encodes byte string to elliptic curve.
|
|
201
|
+
// hash_to_curve from https://www.rfc-editor.org/rfc/rfc9380#section-3
|
|
202
|
+
hashToCurve: function (msg, options) {
|
|
203
|
+
var u = hash_to_field(msg, 2, __assign(__assign(__assign({}, def), { DST: def.DST }), options));
|
|
204
|
+
var u0 = Point.fromAffine(mapToCurve(u[0]));
|
|
205
|
+
var u1 = Point.fromAffine(mapToCurve(u[1]));
|
|
206
|
+
var P = u0.add(u1).clearCofactor();
|
|
207
|
+
P.assertValidity();
|
|
208
|
+
return P;
|
|
209
|
+
},
|
|
210
|
+
// Encodes byte string to elliptic curve.
|
|
211
|
+
// encode_to_curve from https://www.rfc-editor.org/rfc/rfc9380#section-3
|
|
212
|
+
encodeToCurve: function (msg, options) {
|
|
213
|
+
var u = hash_to_field(msg, 1, __assign(__assign(__assign({}, def), { DST: def.encodeDST }), options));
|
|
214
|
+
var P = Point.fromAffine(mapToCurve(u[0])).clearCofactor();
|
|
215
|
+
P.assertValidity();
|
|
216
|
+
return P;
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
exports.createHasher = createHasher;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
export declare function mod(a: bigint, b: bigint): bigint;
|
|
2
|
+
/**
|
|
3
|
+
* Efficiently raise num to power and do modular division.
|
|
4
|
+
* Unsafe in some contexts: uses ladder, so can expose bigint bits.
|
|
5
|
+
* @example
|
|
6
|
+
* pow(2n, 6n, 11n) // 64n % 11n == 9n
|
|
7
|
+
*/
|
|
8
|
+
export declare function pow(num: bigint, power: bigint, modulo: bigint): bigint;
|
|
9
|
+
export declare function pow2(x: bigint, power: bigint, modulo: bigint): bigint;
|
|
10
|
+
export declare function invert(number: bigint, modulo: bigint): bigint;
|
|
11
|
+
/**
|
|
12
|
+
* Tonelli-Shanks square root search algorithm.
|
|
13
|
+
* 1. https://eprint.iacr.org/2012/685.pdf (page 12)
|
|
14
|
+
* 2. Square Roots from 1; 24, 51, 10 to Dan Shanks
|
|
15
|
+
* Will start an infinite loop if field order P is not prime.
|
|
16
|
+
* @param P field order
|
|
17
|
+
* @returns function that takes field Fp (created from P) and number n
|
|
18
|
+
*/
|
|
19
|
+
export declare function tonelliShanks(P: bigint): <T>(Fp: IField<T>, n: T) => T;
|
|
20
|
+
export declare function FpSqrt(P: bigint): <T>(Fp: IField<T>, n: T) => T;
|
|
21
|
+
export declare const isNegativeLE: (num: bigint, modulo: bigint) => boolean;
|
|
22
|
+
export interface IField<T> {
|
|
23
|
+
ORDER: bigint;
|
|
24
|
+
BYTES: number;
|
|
25
|
+
BITS: number;
|
|
26
|
+
MASK: bigint;
|
|
27
|
+
ZERO: T;
|
|
28
|
+
ONE: T;
|
|
29
|
+
create: (num: T) => T;
|
|
30
|
+
isValid: (num: T) => boolean;
|
|
31
|
+
is0: (num: T) => boolean;
|
|
32
|
+
neg(num: T): T;
|
|
33
|
+
inv(num: T): T;
|
|
34
|
+
sqrt(num: T): T;
|
|
35
|
+
sqr(num: T): T;
|
|
36
|
+
eql(lhs: T, rhs: T): boolean;
|
|
37
|
+
add(lhs: T, rhs: T): T;
|
|
38
|
+
sub(lhs: T, rhs: T): T;
|
|
39
|
+
mul(lhs: T, rhs: T | bigint): T;
|
|
40
|
+
pow(lhs: T, power: bigint): T;
|
|
41
|
+
div(lhs: T, rhs: T | bigint): T;
|
|
42
|
+
addN(lhs: T, rhs: T): T;
|
|
43
|
+
subN(lhs: T, rhs: T): T;
|
|
44
|
+
mulN(lhs: T, rhs: T | bigint): T;
|
|
45
|
+
sqrN(num: T): T;
|
|
46
|
+
isOdd?(num: T): boolean;
|
|
47
|
+
pow(lhs: T, power: bigint): T;
|
|
48
|
+
invertBatch: (lst: T[]) => T[];
|
|
49
|
+
toBytes(num: T): Uint8Array;
|
|
50
|
+
fromBytes(bytes: Uint8Array): T;
|
|
51
|
+
cmov(a: T, b: T, c: boolean): T;
|
|
52
|
+
}
|
|
53
|
+
export declare function validateField<T>(field: IField<T>): IField<T>;
|
|
54
|
+
/**
|
|
55
|
+
* Same as `pow` but for Fp: non-constant-time.
|
|
56
|
+
* Unsafe in some contexts: uses ladder, so can expose bigint bits.
|
|
57
|
+
*/
|
|
58
|
+
export declare function FpPow<T>(f: IField<T>, num: T, power: bigint): T;
|
|
59
|
+
/**
|
|
60
|
+
* Efficiently invert an array of Field elements.
|
|
61
|
+
* `inv(0)` will return `undefined` here: make sure to throw an error.
|
|
62
|
+
*/
|
|
63
|
+
export declare function FpInvertBatch<T>(f: IField<T>, nums: T[]): T[];
|
|
64
|
+
export declare function FpDiv<T>(f: IField<T>, lhs: T, rhs: T | bigint): T;
|
|
65
|
+
export declare function FpIsSquare<T>(f: IField<T>): (x: T) => boolean;
|
|
66
|
+
export declare function nLength(n: bigint, nBitLength?: number): {
|
|
67
|
+
nBitLength: number;
|
|
68
|
+
nByteLength: number;
|
|
69
|
+
};
|
|
70
|
+
type FpField = IField<bigint> & Required<Pick<IField<bigint>, 'isOdd'>>;
|
|
71
|
+
/**
|
|
72
|
+
* Initializes a finite field over prime. **Non-primes are not supported.**
|
|
73
|
+
* Do not init in loop: slow. Very fragile: always run a benchmark on a change.
|
|
74
|
+
* Major performance optimizations:
|
|
75
|
+
* * a) denormalized operations like mulN instead of mul
|
|
76
|
+
* * b) same object shape: never add or remove keys
|
|
77
|
+
* * c) Object.freeze
|
|
78
|
+
* @param ORDER prime positive bigint
|
|
79
|
+
* @param bitLen how many bits the field consumes
|
|
80
|
+
* @param isLE (def: false) if encoding / decoding should be in little-endian
|
|
81
|
+
* @param redef optional faster redefinitions of sqrt and other methods
|
|
82
|
+
*/
|
|
83
|
+
export declare function Field(ORDER: bigint, bitLen?: number, isLE?: boolean, redef?: Partial<IField<bigint>>): Readonly<FpField>;
|
|
84
|
+
export declare function FpSqrtOdd<T>(Fp: IField<T>, elm: T): T;
|
|
85
|
+
export declare function FpSqrtEven<T>(Fp: IField<T>, elm: T): T;
|
|
86
|
+
/**
|
|
87
|
+
* "Constant-time" private key generation utility.
|
|
88
|
+
* Same as mapKeyToField, but accepts less bytes (40 instead of 48 for 32-byte field).
|
|
89
|
+
* Which makes it slightly more biased, less secure.
|
|
90
|
+
* @deprecated use mapKeyToField instead
|
|
91
|
+
*/
|
|
92
|
+
export declare function hashToPrivateScalar(hash: string | Uint8Array, groupOrder: bigint, isLE?: boolean): bigint;
|
|
93
|
+
/**
|
|
94
|
+
* Returns total number of bytes consumed by the field element.
|
|
95
|
+
* For example, 32 bytes for usual 256-bit weierstrass curve.
|
|
96
|
+
* @param fieldOrder number of field elements, usually CURVE.n
|
|
97
|
+
* @returns byte length of field
|
|
98
|
+
*/
|
|
99
|
+
export declare function getFieldBytesLength(fieldOrder: bigint): number;
|
|
100
|
+
/**
|
|
101
|
+
* Returns minimal amount of bytes that can be safely reduced
|
|
102
|
+
* by field order.
|
|
103
|
+
* Should be 2^-128 for 128-bit curve such as P256.
|
|
104
|
+
* @param fieldOrder number of field elements, usually CURVE.n
|
|
105
|
+
* @returns byte length of target hash
|
|
106
|
+
*/
|
|
107
|
+
export declare function getMinHashLength(fieldOrder: bigint): number;
|
|
108
|
+
/**
|
|
109
|
+
* "Constant-time" private key generation utility.
|
|
110
|
+
* Can take (n + n/2) or more bytes of uniform input e.g. from CSPRNG or KDF
|
|
111
|
+
* and convert them into private scalar, with the modulo bias being negligible.
|
|
112
|
+
* Needs at least 48 bytes of input for 32-byte private key.
|
|
113
|
+
* https://research.kudelskisecurity.com/2020/07/28/the-definitive-guide-to-modulo-bias-and-how-to-avoid-it/
|
|
114
|
+
* FIPS 186-5, A.2 https://csrc.nist.gov/publications/detail/fips/186/5/final
|
|
115
|
+
* RFC 9380, https://www.rfc-editor.org/rfc/rfc9380#section-5
|
|
116
|
+
* @param hash hash output from SHA3 or a similar function
|
|
117
|
+
* @param groupOrder size of subgroup - (e.g. secp256k1.CURVE.n)
|
|
118
|
+
* @param isLE interpret hash bytes as LE num
|
|
119
|
+
* @returns valid private scalar
|
|
120
|
+
*/
|
|
121
|
+
export declare function mapHashToField(key: Uint8Array, fieldOrder: bigint, isLE?: boolean): Uint8Array;
|
|
122
|
+
export {};
|