@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 @@
|
|
|
1
|
+
export declare const crypto: any;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2
|
+
import { IField } from './modular.js';
|
|
3
|
+
export type AffinePoint<T> = {
|
|
4
|
+
x: T;
|
|
5
|
+
y: T;
|
|
6
|
+
} & {
|
|
7
|
+
z?: never;
|
|
8
|
+
t?: never;
|
|
9
|
+
};
|
|
10
|
+
export interface Group<T extends Group<T>> {
|
|
11
|
+
double(): T;
|
|
12
|
+
negate(): T;
|
|
13
|
+
add(other: T): T;
|
|
14
|
+
subtract(other: T): T;
|
|
15
|
+
equals(other: T): boolean;
|
|
16
|
+
multiply(scalar: bigint): T;
|
|
17
|
+
}
|
|
18
|
+
export type GroupConstructor<T> = {
|
|
19
|
+
BASE: T;
|
|
20
|
+
ZERO: T;
|
|
21
|
+
};
|
|
22
|
+
export type Mapper<T> = (i: T[]) => T[];
|
|
23
|
+
export declare function wNAF<T extends Group<T>>(c: GroupConstructor<T>, bits: number): {
|
|
24
|
+
constTimeNegate: (condition: boolean, item: T) => T;
|
|
25
|
+
unsafeLadder(elm: T, n: bigint): T;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
28
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
29
|
+
* Number of precomputed points depends on the curve size:
|
|
30
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
31
|
+
* - 𝑊 is the window size
|
|
32
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
33
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
34
|
+
* @returns precomputed point tables flattened to a single array
|
|
35
|
+
*/
|
|
36
|
+
precomputeWindow(elm: T, W: number): Group<T>[];
|
|
37
|
+
/**
|
|
38
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
39
|
+
* @param W window size
|
|
40
|
+
* @param precomputes precomputed tables
|
|
41
|
+
* @param n scalar (we don't check here, but should be less than curve order)
|
|
42
|
+
* @returns real and fake (for const-time) points
|
|
43
|
+
*/
|
|
44
|
+
wNAF(W: number, precomputes: T[], n: bigint): {
|
|
45
|
+
p: T;
|
|
46
|
+
f: T;
|
|
47
|
+
};
|
|
48
|
+
wNAFCached(P: T, precomputesMap: Map<T, T[]>, n: bigint, transform: Mapper<T>): {
|
|
49
|
+
p: T;
|
|
50
|
+
f: T;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
export type BasicCurve<T> = {
|
|
54
|
+
Fp: IField<T>;
|
|
55
|
+
n: bigint;
|
|
56
|
+
nBitLength?: number;
|
|
57
|
+
nByteLength?: number;
|
|
58
|
+
h: bigint;
|
|
59
|
+
hEff?: bigint;
|
|
60
|
+
Gx: T;
|
|
61
|
+
Gy: T;
|
|
62
|
+
allowInfinityPoint?: boolean;
|
|
63
|
+
};
|
|
64
|
+
export declare function validateBasic<FP, T>(curve: BasicCurve<FP> & T): Readonly<{
|
|
65
|
+
readonly nBitLength: number;
|
|
66
|
+
readonly nByteLength: number;
|
|
67
|
+
} & BasicCurve<FP> & T & {
|
|
68
|
+
p: bigint;
|
|
69
|
+
}>;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.validateBasic = exports.wNAF = void 0;
|
|
15
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
16
|
+
// Abelian group utilities
|
|
17
|
+
var modular_1 = require("./modular.js");
|
|
18
|
+
var utils_1 = require("./utils.js");
|
|
19
|
+
var _0n = BigInt(0);
|
|
20
|
+
var _1n = BigInt(1);
|
|
21
|
+
// Elliptic curve multiplication of Point by scalar. Fragile.
|
|
22
|
+
// Scalars should always be less than curve order: this should be checked inside of a curve itself.
|
|
23
|
+
// Creates precomputation tables for fast multiplication:
|
|
24
|
+
// - private scalar is split by fixed size windows of W bits
|
|
25
|
+
// - every window point is collected from window's table & added to accumulator
|
|
26
|
+
// - since windows are different, same point inside tables won't be accessed more than once per calc
|
|
27
|
+
// - each multiplication is 'Math.ceil(CURVE_ORDER / 𝑊) + 1' point additions (fixed for any scalar)
|
|
28
|
+
// - +1 window is neccessary for wNAF
|
|
29
|
+
// - wNAF reduces table size: 2x less memory + 2x faster generation, but 10% slower multiplication
|
|
30
|
+
// TODO: Research returning 2d JS array of windows, instead of a single window. This would allow
|
|
31
|
+
// windows to be in different memory locations
|
|
32
|
+
function wNAF(c, bits) {
|
|
33
|
+
var constTimeNegate = function (condition, item) {
|
|
34
|
+
var neg = item.negate();
|
|
35
|
+
return condition ? neg : item;
|
|
36
|
+
};
|
|
37
|
+
var opts = function (W) {
|
|
38
|
+
var windows = Math.ceil(bits / W) + 1; // +1, because
|
|
39
|
+
var windowSize = Math.pow(2, (W - 1)); // -1 because we skip zero
|
|
40
|
+
return { windows: windows, windowSize: windowSize };
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
constTimeNegate: constTimeNegate,
|
|
44
|
+
// non-const time multiplication ladder
|
|
45
|
+
unsafeLadder: function (elm, n) {
|
|
46
|
+
var p = c.ZERO;
|
|
47
|
+
var d = elm;
|
|
48
|
+
while (n > _0n) {
|
|
49
|
+
if (n & _1n)
|
|
50
|
+
p = p.add(d);
|
|
51
|
+
d = d.double();
|
|
52
|
+
n >>= _1n;
|
|
53
|
+
}
|
|
54
|
+
return p;
|
|
55
|
+
},
|
|
56
|
+
/**
|
|
57
|
+
* Creates a wNAF precomputation window. Used for caching.
|
|
58
|
+
* Default window size is set by `utils.precompute()` and is equal to 8.
|
|
59
|
+
* Number of precomputed points depends on the curve size:
|
|
60
|
+
* 2^(𝑊−1) * (Math.ceil(𝑛 / 𝑊) + 1), where:
|
|
61
|
+
* - 𝑊 is the window size
|
|
62
|
+
* - 𝑛 is the bitlength of the curve order.
|
|
63
|
+
* For a 256-bit curve and window size 8, the number of precomputed points is 128 * 33 = 4224.
|
|
64
|
+
* @returns precomputed point tables flattened to a single array
|
|
65
|
+
*/
|
|
66
|
+
precomputeWindow: function (elm, W) {
|
|
67
|
+
var _a = opts(W), windows = _a.windows, windowSize = _a.windowSize;
|
|
68
|
+
var points = [];
|
|
69
|
+
var p = elm;
|
|
70
|
+
var base = p;
|
|
71
|
+
for (var window_1 = 0; window_1 < windows; window_1++) {
|
|
72
|
+
base = p;
|
|
73
|
+
points.push(base);
|
|
74
|
+
// =1, because we skip zero
|
|
75
|
+
for (var i = 1; i < windowSize; i++) {
|
|
76
|
+
base = base.add(p);
|
|
77
|
+
points.push(base);
|
|
78
|
+
}
|
|
79
|
+
p = base.double();
|
|
80
|
+
}
|
|
81
|
+
return points;
|
|
82
|
+
},
|
|
83
|
+
/**
|
|
84
|
+
* Implements ec multiplication using precomputed tables and w-ary non-adjacent form.
|
|
85
|
+
* @param W window size
|
|
86
|
+
* @param precomputes precomputed tables
|
|
87
|
+
* @param n scalar (we don't check here, but should be less than curve order)
|
|
88
|
+
* @returns real and fake (for const-time) points
|
|
89
|
+
*/
|
|
90
|
+
wNAF: function (W, precomputes, n) {
|
|
91
|
+
// TODO: maybe check that scalar is less than group order? wNAF behavious is undefined otherwise
|
|
92
|
+
// But need to carefully remove other checks before wNAF. ORDER == bits here
|
|
93
|
+
var _a = opts(W), windows = _a.windows, windowSize = _a.windowSize;
|
|
94
|
+
var p = c.ZERO;
|
|
95
|
+
var f = c.BASE;
|
|
96
|
+
var mask = BigInt(Math.pow(2, W) - 1); // Create mask with W ones: 0b1111 for W=4 etc.
|
|
97
|
+
var maxNumber = Math.pow(2, W);
|
|
98
|
+
var shiftBy = BigInt(W);
|
|
99
|
+
for (var window_2 = 0; window_2 < windows; window_2++) {
|
|
100
|
+
var offset = window_2 * windowSize;
|
|
101
|
+
// Extract W bits.
|
|
102
|
+
var wbits = Number(n & mask);
|
|
103
|
+
// Shift number by W bits.
|
|
104
|
+
n >>= shiftBy;
|
|
105
|
+
// If the bits are bigger than max size, we'll split those.
|
|
106
|
+
// +224 => 256 - 32
|
|
107
|
+
if (wbits > windowSize) {
|
|
108
|
+
wbits -= maxNumber;
|
|
109
|
+
n += _1n;
|
|
110
|
+
}
|
|
111
|
+
// This code was first written with assumption that 'f' and 'p' will never be infinity point:
|
|
112
|
+
// since each addition is multiplied by 2 ** W, it cannot cancel each other. However,
|
|
113
|
+
// there is negate now: it is possible that negated element from low value
|
|
114
|
+
// would be the same as high element, which will create carry into next window.
|
|
115
|
+
// It's not obvious how this can fail, but still worth investigating later.
|
|
116
|
+
// Check if we're onto Zero point.
|
|
117
|
+
// Add random point inside current window to f.
|
|
118
|
+
var offset1 = offset;
|
|
119
|
+
var offset2 = offset + Math.abs(wbits) - 1; // -1 because we skip zero
|
|
120
|
+
var cond1 = window_2 % 2 !== 0;
|
|
121
|
+
var cond2 = wbits < 0;
|
|
122
|
+
if (wbits === 0) {
|
|
123
|
+
// The most important part for const-time getPublicKey
|
|
124
|
+
f = f.add(constTimeNegate(cond1, precomputes[offset1]));
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
p = p.add(constTimeNegate(cond2, precomputes[offset2]));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// JIT-compiler should not eliminate f here, since it will later be used in normalizeZ()
|
|
131
|
+
// Even if the variable is still unused, there are some checks which will
|
|
132
|
+
// throw an exception, so compiler needs to prove they won't happen, which is hard.
|
|
133
|
+
// At this point there is a way to F be infinity-point even if p is not,
|
|
134
|
+
// which makes it less const-time: around 1 bigint multiply.
|
|
135
|
+
return { p: p, f: f };
|
|
136
|
+
},
|
|
137
|
+
wNAFCached: function (P, precomputesMap, n, transform) {
|
|
138
|
+
// @ts-ignore
|
|
139
|
+
var W = P._WINDOW_SIZE || 1;
|
|
140
|
+
// Calculate precomputes on a first run, reuse them after
|
|
141
|
+
var comp = precomputesMap.get(P);
|
|
142
|
+
if (!comp) {
|
|
143
|
+
comp = this.precomputeWindow(P, W);
|
|
144
|
+
if (W !== 1) {
|
|
145
|
+
precomputesMap.set(P, transform(comp));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return this.wNAF(W, comp, n);
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
exports.wNAF = wNAF;
|
|
153
|
+
function validateBasic(curve) {
|
|
154
|
+
(0, modular_1.validateField)(curve.Fp);
|
|
155
|
+
(0, utils_1.validateObject)(curve, {
|
|
156
|
+
n: 'bigint',
|
|
157
|
+
h: 'bigint',
|
|
158
|
+
Gx: 'field',
|
|
159
|
+
Gy: 'field',
|
|
160
|
+
}, {
|
|
161
|
+
nBitLength: 'isSafeInteger',
|
|
162
|
+
nByteLength: 'isSafeInteger',
|
|
163
|
+
});
|
|
164
|
+
// Set defaults
|
|
165
|
+
return Object.freeze(__assign(__assign(__assign({}, (0, modular_1.nLength)(curve.n, curve.nBitLength)), curve), { p: curve.Fp.ORDER }));
|
|
166
|
+
}
|
|
167
|
+
exports.validateBasic = validateBasic;
|
|
@@ -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 {};
|