@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,106 @@
|
|
|
1
|
+
export type Hex = Uint8Array | string;
|
|
2
|
+
export type PrivKey = Hex | bigint;
|
|
3
|
+
export type CHash = {
|
|
4
|
+
(message: Uint8Array | string): Uint8Array;
|
|
5
|
+
blockLen: number;
|
|
6
|
+
outputLen: number;
|
|
7
|
+
create(opts?: {
|
|
8
|
+
dkLen?: number;
|
|
9
|
+
}): any;
|
|
10
|
+
};
|
|
11
|
+
export type FHash = (message: Uint8Array | string) => Uint8Array;
|
|
12
|
+
export declare function abytes(item: unknown): void;
|
|
13
|
+
/**
|
|
14
|
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
15
|
+
*/
|
|
16
|
+
export declare function bytesToHex(bytes: Uint8Array): string;
|
|
17
|
+
export declare function numberToHexUnpadded(num: number | bigint): string;
|
|
18
|
+
export declare function hexToNumber(hex: string): bigint;
|
|
19
|
+
/**
|
|
20
|
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
21
|
+
*/
|
|
22
|
+
export declare function hexToBytes(hex: string): Uint8Array;
|
|
23
|
+
export declare function bytesToNumberBE(bytes: Uint8Array): bigint;
|
|
24
|
+
export declare function bytesToNumberLE(bytes: Uint8Array): bigint;
|
|
25
|
+
export declare function numberToBytesBE(n: number | bigint, len: number): Uint8Array;
|
|
26
|
+
export declare function numberToBytesLE(n: number | bigint, len: number): Uint8Array;
|
|
27
|
+
export declare function numberToVarBytesBE(n: number | bigint): Uint8Array;
|
|
28
|
+
/**
|
|
29
|
+
* Takes hex string or Uint8Array, converts to Uint8Array.
|
|
30
|
+
* Validates output length.
|
|
31
|
+
* Will throw error for other types.
|
|
32
|
+
* @param title descriptive title for an error e.g. 'private key'
|
|
33
|
+
* @param hex hex string or Uint8Array
|
|
34
|
+
* @param expectedLength optional, will compare to result array's length
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
export declare function ensureBytes(title: string, hex: Hex, expectedLength?: number): Uint8Array;
|
|
38
|
+
/**
|
|
39
|
+
* Copies several Uint8Arrays into one.
|
|
40
|
+
*/
|
|
41
|
+
export declare function concatBytes(...arrays: Uint8Array[]): Uint8Array;
|
|
42
|
+
export declare function equalBytes(a: Uint8Array, b: Uint8Array): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
45
|
+
*/
|
|
46
|
+
export declare function utf8ToBytes(str: string): Uint8Array;
|
|
47
|
+
/**
|
|
48
|
+
* Calculates amount of bits in a bigint.
|
|
49
|
+
* Same as `n.toString(2).length`
|
|
50
|
+
*/
|
|
51
|
+
export declare function bitLen(n: bigint): number;
|
|
52
|
+
/**
|
|
53
|
+
* Gets single bit at position.
|
|
54
|
+
* NOTE: first bit position is 0 (same as arrays)
|
|
55
|
+
* Same as `!!+Array.from(n.toString(2)).reverse()[pos]`
|
|
56
|
+
*/
|
|
57
|
+
export declare function bitGet(n: bigint, pos: number): bigint;
|
|
58
|
+
/**
|
|
59
|
+
* Sets single bit at position.
|
|
60
|
+
*/
|
|
61
|
+
export declare function bitSet(n: bigint, pos: number, value: boolean): bigint;
|
|
62
|
+
/**
|
|
63
|
+
* Calculate mask for N bits. Not using ** operator with bigints because of old engines.
|
|
64
|
+
* Same as BigInt(`0b${Array(i).fill('1').join('')}`)
|
|
65
|
+
*/
|
|
66
|
+
export declare const bitMask: (n: number) => bigint;
|
|
67
|
+
type Pred<T> = (v: Uint8Array) => T | undefined;
|
|
68
|
+
/**
|
|
69
|
+
* Minimal HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
|
|
70
|
+
* @returns function that will call DRBG until 2nd arg returns something meaningful
|
|
71
|
+
* @example
|
|
72
|
+
* const drbg = createHmacDRBG<Key>(32, 32, hmac);
|
|
73
|
+
* drbg(seed, bytesToKey); // bytesToKey must return Key or undefined
|
|
74
|
+
*/
|
|
75
|
+
export declare function createHmacDrbg<T>(hashLen: number, qByteLen: number, hmacFn: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array): (seed: Uint8Array, predicate: Pred<T>) => T;
|
|
76
|
+
declare const validatorFns: {
|
|
77
|
+
readonly bigint: (val: any) => boolean;
|
|
78
|
+
readonly function: (val: any) => boolean;
|
|
79
|
+
readonly boolean: (val: any) => boolean;
|
|
80
|
+
readonly string: (val: any) => boolean;
|
|
81
|
+
readonly stringOrUint8Array: (val: any) => boolean;
|
|
82
|
+
readonly isSafeInteger: (val: any) => boolean;
|
|
83
|
+
readonly array: (val: any) => boolean;
|
|
84
|
+
readonly field: (val: any, object: any) => any;
|
|
85
|
+
readonly hash: (val: any) => boolean;
|
|
86
|
+
};
|
|
87
|
+
type Validator = keyof typeof validatorFns;
|
|
88
|
+
type ValMap<T extends Record<string, any>> = {
|
|
89
|
+
[K in keyof T]?: Validator;
|
|
90
|
+
};
|
|
91
|
+
export declare function validateObject<T extends Record<string, any>>(object: T, validators: ValMap<T>, optValidators?: ValMap<T>): T;
|
|
92
|
+
/**
|
|
93
|
+
* Memoizes (caches) computation result.
|
|
94
|
+
* Uses WeakMap: the value is going auto-cleaned by GC after last reference is removed.
|
|
95
|
+
*/
|
|
96
|
+
export declare function memoized<T extends object, R, O extends any[]>(fn: (arg: T, ...args: O) => R): (arg: T, ...args: O) => R;
|
|
97
|
+
export declare function abool(title: string, value: boolean): void;
|
|
98
|
+
export declare function isBytes(a: unknown): a is Uint8Array;
|
|
99
|
+
export declare function inRange(n: bigint, min: bigint, max: bigint): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Asserts min <= n < max. NOTE: It's < max and not <= max.
|
|
102
|
+
* @example
|
|
103
|
+
* aInRange('x', x, 1n, 256n); // would assume x is in (1n..255n)
|
|
104
|
+
*/
|
|
105
|
+
export declare function aInRange(title: string, n: bigint, min: bigint, max: bigint): void;
|
|
106
|
+
export {};
|
|
@@ -0,0 +1,442 @@
|
|
|
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/utils.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 __read = (this && this.__read) || function (o, n) {
|
|
16
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
17
|
+
if (!m) return o;
|
|
18
|
+
var i = m.call(o), r, ar = [], e;
|
|
19
|
+
try {
|
|
20
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
21
|
+
}
|
|
22
|
+
catch (error) { e = { error: error }; }
|
|
23
|
+
finally {
|
|
24
|
+
try {
|
|
25
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
26
|
+
}
|
|
27
|
+
finally { if (e) throw e.error; }
|
|
28
|
+
}
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
32
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
33
|
+
if (ar || !(i in from)) {
|
|
34
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
35
|
+
ar[i] = from[i];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
39
|
+
};
|
|
40
|
+
var __values = (this && this.__values) || function(o) {
|
|
41
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
42
|
+
if (m) return m.call(o);
|
|
43
|
+
if (o && typeof o.length === "number") return {
|
|
44
|
+
next: function () {
|
|
45
|
+
if (o && i >= o.length) o = void 0;
|
|
46
|
+
return { value: o && o[i++], done: !o };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
52
|
+
exports.aInRange = exports.inRange = exports.isBytes = exports.abool = exports.memoized = exports.validateObject = exports.createHmacDrbg = exports.bitMask = exports.bitSet = exports.bitGet = exports.bitLen = exports.utf8ToBytes = exports.equalBytes = exports.concatBytes = exports.ensureBytes = exports.numberToVarBytesBE = exports.numberToBytesLE = exports.numberToBytesBE = exports.bytesToNumberLE = exports.bytesToNumberBE = exports.hexToBytes = exports.hexToNumber = exports.numberToHexUnpadded = exports.bytesToHex = exports.abytes = void 0;
|
|
53
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
54
|
+
// 100 lines of code in the file are duplicated from noble-hashes (utils).
|
|
55
|
+
// This is OK: `abstract` directory does not use noble-hashes.
|
|
56
|
+
// User may opt-in into using different hashing library. This way, noble-hashes
|
|
57
|
+
// won't be included into their bundle.
|
|
58
|
+
var _0n = BigInt(0);
|
|
59
|
+
var _1n = BigInt(1);
|
|
60
|
+
var _2n = BigInt(2);
|
|
61
|
+
function abytes(item) {
|
|
62
|
+
if (!isBytes(item))
|
|
63
|
+
throw new Error('Uint8Array expected');
|
|
64
|
+
}
|
|
65
|
+
exports.abytes = abytes;
|
|
66
|
+
// Array where index 0xf0 (240) is mapped to string 'f0'
|
|
67
|
+
var hexes = /* @__PURE__ */ Array.from({ length: 256 }, function (_, i) {
|
|
68
|
+
return i.toString(16).padStart(2, '0');
|
|
69
|
+
});
|
|
70
|
+
/**
|
|
71
|
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
|
72
|
+
*/
|
|
73
|
+
function bytesToHex(bytes) {
|
|
74
|
+
abytes(bytes);
|
|
75
|
+
// pre-caching improves the speed 6x
|
|
76
|
+
var hex = '';
|
|
77
|
+
for (var i = 0; i < bytes.length; i++) {
|
|
78
|
+
hex += hexes[bytes[i]];
|
|
79
|
+
}
|
|
80
|
+
return hex;
|
|
81
|
+
}
|
|
82
|
+
exports.bytesToHex = bytesToHex;
|
|
83
|
+
function numberToHexUnpadded(num) {
|
|
84
|
+
var hex = num.toString(16);
|
|
85
|
+
return hex.length & 1 ? "0".concat(hex) : hex;
|
|
86
|
+
}
|
|
87
|
+
exports.numberToHexUnpadded = numberToHexUnpadded;
|
|
88
|
+
function hexToNumber(hex) {
|
|
89
|
+
if (typeof hex !== 'string')
|
|
90
|
+
throw new Error('hex string expected, got ' + typeof hex);
|
|
91
|
+
// Big Endian
|
|
92
|
+
return BigInt(hex === '' ? '0' : "0x".concat(hex));
|
|
93
|
+
}
|
|
94
|
+
exports.hexToNumber = hexToNumber;
|
|
95
|
+
// We use optimized technique to convert hex string to byte array
|
|
96
|
+
var asciis = { _0: 48, _9: 57, _A: 65, _F: 70, _a: 97, _f: 102 };
|
|
97
|
+
function asciiToBase16(char) {
|
|
98
|
+
if (char >= asciis._0 && char <= asciis._9)
|
|
99
|
+
return char - asciis._0;
|
|
100
|
+
if (char >= asciis._A && char <= asciis._F)
|
|
101
|
+
return char - (asciis._A - 10);
|
|
102
|
+
if (char >= asciis._a && char <= asciis._f)
|
|
103
|
+
return char - (asciis._a - 10);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
|
108
|
+
*/
|
|
109
|
+
function hexToBytes(hex) {
|
|
110
|
+
if (typeof hex !== 'string')
|
|
111
|
+
throw new Error('hex string expected, got ' + typeof hex);
|
|
112
|
+
var hl = hex.length;
|
|
113
|
+
var al = hl / 2;
|
|
114
|
+
if (hl % 2)
|
|
115
|
+
throw new Error('padded hex string expected, got unpadded hex of length ' + hl);
|
|
116
|
+
var array = new Uint8Array(al);
|
|
117
|
+
for (var ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
118
|
+
var n1 = asciiToBase16(hex.charCodeAt(hi));
|
|
119
|
+
var n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
|
120
|
+
if (n1 === undefined || n2 === undefined) {
|
|
121
|
+
var char = hex[hi] + hex[hi + 1];
|
|
122
|
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
123
|
+
}
|
|
124
|
+
array[ai] = n1 * 16 + n2;
|
|
125
|
+
}
|
|
126
|
+
return array;
|
|
127
|
+
}
|
|
128
|
+
exports.hexToBytes = hexToBytes;
|
|
129
|
+
// BE: Big Endian, LE: Little Endian
|
|
130
|
+
function bytesToNumberBE(bytes) {
|
|
131
|
+
return hexToNumber(bytesToHex(bytes));
|
|
132
|
+
}
|
|
133
|
+
exports.bytesToNumberBE = bytesToNumberBE;
|
|
134
|
+
function bytesToNumberLE(bytes) {
|
|
135
|
+
abytes(bytes);
|
|
136
|
+
return hexToNumber(bytesToHex(Uint8Array.from(bytes).reverse()));
|
|
137
|
+
}
|
|
138
|
+
exports.bytesToNumberLE = bytesToNumberLE;
|
|
139
|
+
function numberToBytesBE(n, len) {
|
|
140
|
+
return hexToBytes(n.toString(16).padStart(len * 2, '0'));
|
|
141
|
+
}
|
|
142
|
+
exports.numberToBytesBE = numberToBytesBE;
|
|
143
|
+
function numberToBytesLE(n, len) {
|
|
144
|
+
return numberToBytesBE(n, len).reverse();
|
|
145
|
+
}
|
|
146
|
+
exports.numberToBytesLE = numberToBytesLE;
|
|
147
|
+
// Unpadded, rarely used
|
|
148
|
+
function numberToVarBytesBE(n) {
|
|
149
|
+
return hexToBytes(numberToHexUnpadded(n));
|
|
150
|
+
}
|
|
151
|
+
exports.numberToVarBytesBE = numberToVarBytesBE;
|
|
152
|
+
/**
|
|
153
|
+
* Takes hex string or Uint8Array, converts to Uint8Array.
|
|
154
|
+
* Validates output length.
|
|
155
|
+
* Will throw error for other types.
|
|
156
|
+
* @param title descriptive title for an error e.g. 'private key'
|
|
157
|
+
* @param hex hex string or Uint8Array
|
|
158
|
+
* @param expectedLength optional, will compare to result array's length
|
|
159
|
+
* @returns
|
|
160
|
+
*/
|
|
161
|
+
function ensureBytes(title, hex, expectedLength) {
|
|
162
|
+
var res;
|
|
163
|
+
if (typeof hex === 'string') {
|
|
164
|
+
try {
|
|
165
|
+
res = hexToBytes(hex);
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
throw new Error("".concat(title, " must be valid hex string, got \"").concat(hex, "\". Cause: ").concat(e));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else if (isBytes(hex)) {
|
|
172
|
+
// Uint8Array.from() instead of hash.slice() because node Buffer
|
|
173
|
+
// is instance of Uint8Array, and its slice() creates **mutable** copy
|
|
174
|
+
res = Uint8Array.from(hex);
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
throw new Error("".concat(title, " must be hex string or Uint8Array"));
|
|
178
|
+
}
|
|
179
|
+
var len = res.length;
|
|
180
|
+
if (typeof expectedLength === 'number' && len !== expectedLength)
|
|
181
|
+
throw new Error("".concat(title, " expected ").concat(expectedLength, " bytes, got ").concat(len));
|
|
182
|
+
return res;
|
|
183
|
+
}
|
|
184
|
+
exports.ensureBytes = ensureBytes;
|
|
185
|
+
/**
|
|
186
|
+
* Copies several Uint8Arrays into one.
|
|
187
|
+
*/
|
|
188
|
+
function concatBytes() {
|
|
189
|
+
var arrays = [];
|
|
190
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
191
|
+
arrays[_i] = arguments[_i];
|
|
192
|
+
}
|
|
193
|
+
var sum = 0;
|
|
194
|
+
for (var i = 0; i < arrays.length; i++) {
|
|
195
|
+
var a = arrays[i];
|
|
196
|
+
abytes(a);
|
|
197
|
+
sum += a.length;
|
|
198
|
+
}
|
|
199
|
+
var res = new Uint8Array(sum);
|
|
200
|
+
for (var i = 0, pad = 0; i < arrays.length; i++) {
|
|
201
|
+
var a = arrays[i];
|
|
202
|
+
res.set(a, pad);
|
|
203
|
+
pad += a.length;
|
|
204
|
+
}
|
|
205
|
+
return res;
|
|
206
|
+
}
|
|
207
|
+
exports.concatBytes = concatBytes;
|
|
208
|
+
// Compares 2 u8a-s in kinda constant time
|
|
209
|
+
function equalBytes(a, b) {
|
|
210
|
+
if (a.length !== b.length)
|
|
211
|
+
return false;
|
|
212
|
+
var diff = 0;
|
|
213
|
+
for (var i = 0; i < a.length; i++)
|
|
214
|
+
diff |= a[i] ^ b[i];
|
|
215
|
+
return diff === 0;
|
|
216
|
+
}
|
|
217
|
+
exports.equalBytes = equalBytes;
|
|
218
|
+
/**
|
|
219
|
+
* @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
|
|
220
|
+
*/
|
|
221
|
+
function utf8ToBytes(str) {
|
|
222
|
+
if (typeof str !== 'string')
|
|
223
|
+
throw new Error("utf8ToBytes expected string, got ".concat(typeof str));
|
|
224
|
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
|
225
|
+
}
|
|
226
|
+
exports.utf8ToBytes = utf8ToBytes;
|
|
227
|
+
// Bit operations
|
|
228
|
+
/**
|
|
229
|
+
* Calculates amount of bits in a bigint.
|
|
230
|
+
* Same as `n.toString(2).length`
|
|
231
|
+
*/
|
|
232
|
+
function bitLen(n) {
|
|
233
|
+
var len;
|
|
234
|
+
for (len = 0; n > _0n; n >>= _1n, len += 1)
|
|
235
|
+
;
|
|
236
|
+
return len;
|
|
237
|
+
}
|
|
238
|
+
exports.bitLen = bitLen;
|
|
239
|
+
/**
|
|
240
|
+
* Gets single bit at position.
|
|
241
|
+
* NOTE: first bit position is 0 (same as arrays)
|
|
242
|
+
* Same as `!!+Array.from(n.toString(2)).reverse()[pos]`
|
|
243
|
+
*/
|
|
244
|
+
function bitGet(n, pos) {
|
|
245
|
+
return (n >> BigInt(pos)) & _1n;
|
|
246
|
+
}
|
|
247
|
+
exports.bitGet = bitGet;
|
|
248
|
+
/**
|
|
249
|
+
* Sets single bit at position.
|
|
250
|
+
*/
|
|
251
|
+
function bitSet(n, pos, value) {
|
|
252
|
+
return n | ((value ? _1n : _0n) << BigInt(pos));
|
|
253
|
+
}
|
|
254
|
+
exports.bitSet = bitSet;
|
|
255
|
+
/**
|
|
256
|
+
* Calculate mask for N bits. Not using ** operator with bigints because of old engines.
|
|
257
|
+
* Same as BigInt(`0b${Array(i).fill('1').join('')}`)
|
|
258
|
+
*/
|
|
259
|
+
var bitMask = function (n) { return (_2n << BigInt(n - 1)) - _1n; };
|
|
260
|
+
exports.bitMask = bitMask;
|
|
261
|
+
// DRBG
|
|
262
|
+
var u8n = function (data) { return new Uint8Array(data); }; // creates Uint8Array
|
|
263
|
+
var u8fr = function (arr) { return Uint8Array.from(arr); }; // another shortcut
|
|
264
|
+
/**
|
|
265
|
+
* Minimal HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
|
|
266
|
+
* @returns function that will call DRBG until 2nd arg returns something meaningful
|
|
267
|
+
* @example
|
|
268
|
+
* const drbg = createHmacDRBG<Key>(32, 32, hmac);
|
|
269
|
+
* drbg(seed, bytesToKey); // bytesToKey must return Key or undefined
|
|
270
|
+
*/
|
|
271
|
+
function createHmacDrbg(hashLen, qByteLen, hmacFn) {
|
|
272
|
+
if (typeof hashLen !== 'number' || hashLen < 2)
|
|
273
|
+
throw new Error('hashLen must be a number');
|
|
274
|
+
if (typeof qByteLen !== 'number' || qByteLen < 2)
|
|
275
|
+
throw new Error('qByteLen must be a number');
|
|
276
|
+
if (typeof hmacFn !== 'function')
|
|
277
|
+
throw new Error('hmacFn must be a function');
|
|
278
|
+
// Step B, Step C: set hashLen to 8*ceil(hlen/8)
|
|
279
|
+
var v = u8n(hashLen); // Minimal non-full-spec HMAC-DRBG from NIST 800-90 for RFC6979 sigs.
|
|
280
|
+
var k = u8n(hashLen); // Steps B and C of RFC6979 3.2: set hashLen, in our case always same
|
|
281
|
+
var i = 0; // Iterations counter, will throw when over 1000
|
|
282
|
+
var reset = function () {
|
|
283
|
+
v.fill(1);
|
|
284
|
+
k.fill(0);
|
|
285
|
+
i = 0;
|
|
286
|
+
};
|
|
287
|
+
var h = function () {
|
|
288
|
+
var b = [];
|
|
289
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
290
|
+
b[_i] = arguments[_i];
|
|
291
|
+
}
|
|
292
|
+
return hmacFn.apply(void 0, __spreadArray([k, v], __read(b), false));
|
|
293
|
+
}; // hmac(k)(v, ...values)
|
|
294
|
+
var reseed = function (seed) {
|
|
295
|
+
if (seed === void 0) { seed = u8n(); }
|
|
296
|
+
// HMAC-DRBG reseed() function. Steps D-G
|
|
297
|
+
k = h(u8fr([0x00]), seed); // k = hmac(k || v || 0x00 || seed)
|
|
298
|
+
v = h(); // v = hmac(k || v)
|
|
299
|
+
if (seed.length === 0)
|
|
300
|
+
return;
|
|
301
|
+
k = h(u8fr([0x01]), seed); // k = hmac(k || v || 0x01 || seed)
|
|
302
|
+
v = h(); // v = hmac(k || v)
|
|
303
|
+
};
|
|
304
|
+
var gen = function () {
|
|
305
|
+
// HMAC-DRBG generate() function
|
|
306
|
+
if (i++ >= 1000)
|
|
307
|
+
throw new Error('drbg: tried 1000 values');
|
|
308
|
+
var len = 0;
|
|
309
|
+
var out = [];
|
|
310
|
+
while (len < qByteLen) {
|
|
311
|
+
v = h();
|
|
312
|
+
var sl = v.slice();
|
|
313
|
+
out.push(sl);
|
|
314
|
+
len += v.length;
|
|
315
|
+
}
|
|
316
|
+
return concatBytes.apply(void 0, __spreadArray([], __read(out), false));
|
|
317
|
+
};
|
|
318
|
+
var genUntil = function (seed, pred) {
|
|
319
|
+
reset();
|
|
320
|
+
reseed(seed); // Steps D-G
|
|
321
|
+
var res = undefined; // Step H: grind until k is in [1..n-1]
|
|
322
|
+
while (!(res = pred(gen())))
|
|
323
|
+
reseed();
|
|
324
|
+
reset();
|
|
325
|
+
return res;
|
|
326
|
+
};
|
|
327
|
+
return genUntil;
|
|
328
|
+
}
|
|
329
|
+
exports.createHmacDrbg = createHmacDrbg;
|
|
330
|
+
// Validating curves and fields
|
|
331
|
+
var validatorFns = {
|
|
332
|
+
bigint: function (val) { return typeof val === 'bigint'; },
|
|
333
|
+
function: function (val) { return typeof val === 'function'; },
|
|
334
|
+
boolean: function (val) { return typeof val === 'boolean'; },
|
|
335
|
+
string: function (val) { return typeof val === 'string'; },
|
|
336
|
+
stringOrUint8Array: function (val) { return typeof val === 'string' || isBytes(val); },
|
|
337
|
+
isSafeInteger: function (val) { return Number.isSafeInteger(val); },
|
|
338
|
+
array: function (val) { return Array.isArray(val); },
|
|
339
|
+
field: function (val, object) { return object.Fp.isValid(val); },
|
|
340
|
+
hash: function (val) { return typeof val === 'function' && Number.isSafeInteger(val.outputLen); },
|
|
341
|
+
};
|
|
342
|
+
// type Record<K extends string | number | symbol, T> = { [P in K]: T; }
|
|
343
|
+
function validateObject(object, validators, optValidators) {
|
|
344
|
+
var e_1, _b, e_2, _c;
|
|
345
|
+
if (optValidators === void 0) { optValidators = {}; }
|
|
346
|
+
var checkField = function (fieldName, type, isOptional) {
|
|
347
|
+
var checkVal = validatorFns[type];
|
|
348
|
+
if (typeof checkVal !== 'function')
|
|
349
|
+
throw new Error("Invalid validator \"".concat(type, "\", expected function"));
|
|
350
|
+
var val = object[fieldName];
|
|
351
|
+
if (isOptional && val === undefined)
|
|
352
|
+
return;
|
|
353
|
+
if (!checkVal(val, object)) {
|
|
354
|
+
throw new Error("Invalid param ".concat(String(fieldName), "=").concat(val, " (").concat(typeof val, "), expected ").concat(type));
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
try {
|
|
358
|
+
for (var _d = __values(Object.entries(validators)), _e = _d.next(); !_e.done; _e = _d.next()) {
|
|
359
|
+
var _g = __read(_e.value, 2), fieldName = _g[0], type = _g[1];
|
|
360
|
+
checkField(fieldName, type, false);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
364
|
+
finally {
|
|
365
|
+
try {
|
|
366
|
+
if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
|
|
367
|
+
}
|
|
368
|
+
finally { if (e_1) throw e_1.error; }
|
|
369
|
+
}
|
|
370
|
+
try {
|
|
371
|
+
for (var _h = __values(Object.entries(optValidators)), _j = _h.next(); !_j.done; _j = _h.next()) {
|
|
372
|
+
var _k = __read(_j.value, 2), fieldName = _k[0], type = _k[1];
|
|
373
|
+
checkField(fieldName, type, true);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
377
|
+
finally {
|
|
378
|
+
try {
|
|
379
|
+
if (_j && !_j.done && (_c = _h.return)) _c.call(_h);
|
|
380
|
+
}
|
|
381
|
+
finally { if (e_2) throw e_2.error; }
|
|
382
|
+
}
|
|
383
|
+
return object;
|
|
384
|
+
}
|
|
385
|
+
exports.validateObject = validateObject;
|
|
386
|
+
// validate type tests
|
|
387
|
+
// const o: { a: number; b: number; c: number } = { a: 1, b: 5, c: 6 };
|
|
388
|
+
// const z0 = validateObject(o, { a: 'isSafeInteger' }, { c: 'bigint' }); // Ok!
|
|
389
|
+
// // Should fail type-check
|
|
390
|
+
// const z1 = validateObject(o, { a: 'tmp' }, { c: 'zz' });
|
|
391
|
+
// const z2 = validateObject(o, { a: 'isSafeInteger' }, { c: 'zz' });
|
|
392
|
+
// const z3 = validateObject(o, { test: 'boolean', z: 'bug' });
|
|
393
|
+
// const z4 = validateObject(o, { a: 'boolean', z: 'bug' });
|
|
394
|
+
/**
|
|
395
|
+
* Memoizes (caches) computation result.
|
|
396
|
+
* Uses WeakMap: the value is going auto-cleaned by GC after last reference is removed.
|
|
397
|
+
*/
|
|
398
|
+
function memoized(fn) {
|
|
399
|
+
var map = new WeakMap();
|
|
400
|
+
return function (arg) {
|
|
401
|
+
var args = [];
|
|
402
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
403
|
+
args[_i - 1] = arguments[_i];
|
|
404
|
+
}
|
|
405
|
+
var val = map.get(arg);
|
|
406
|
+
if (val !== undefined)
|
|
407
|
+
return val;
|
|
408
|
+
var computed = fn.apply(void 0, __spreadArray([arg], __read(args), false));
|
|
409
|
+
map.set(arg, computed);
|
|
410
|
+
return computed;
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
exports.memoized = memoized;
|
|
414
|
+
function abool(title, value) {
|
|
415
|
+
if (typeof value !== 'boolean')
|
|
416
|
+
throw new Error(title + ' boolean expected, got ' + value);
|
|
417
|
+
}
|
|
418
|
+
exports.abool = abool;
|
|
419
|
+
function isBytes(a) {
|
|
420
|
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
|
421
|
+
}
|
|
422
|
+
exports.isBytes = isBytes;
|
|
423
|
+
var isPosBig = function (n) { return typeof n === 'bigint' && _0n <= n; };
|
|
424
|
+
function inRange(n, min, max) {
|
|
425
|
+
return isPosBig(n) && isPosBig(min) && isPosBig(max) && min <= n && n < max;
|
|
426
|
+
}
|
|
427
|
+
exports.inRange = inRange;
|
|
428
|
+
/**
|
|
429
|
+
* Asserts min <= n < max. NOTE: It's < max and not <= max.
|
|
430
|
+
* @example
|
|
431
|
+
* aInRange('x', x, 1n, 256n); // would assume x is in (1n..255n)
|
|
432
|
+
*/
|
|
433
|
+
function aInRange(title, n, min, max) {
|
|
434
|
+
// Why min <= n < max and not a (min < n < max) OR b (min <= n <= max)?
|
|
435
|
+
// consider P=256n, min=0n, max=P
|
|
436
|
+
// - a for min=0 would require -1: `inRange('x', x, -1n, P)`
|
|
437
|
+
// - b would commonly require subtraction: `inRange('x', x, 0n, P - 1n)`
|
|
438
|
+
// - our way is the cleanest: `inRange('x', x, 0n, P)
|
|
439
|
+
if (!inRange(n, min, max))
|
|
440
|
+
throw new Error('expected valid ' + title + ': ' + min + ' <= n < ' + max + ', got ' + n);
|
|
441
|
+
}
|
|
442
|
+
exports.aInRange = aInRange;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
2
|
+
import { CurveFn } from './abstract/bls.js';
|
|
3
|
+
import * as mod from './abstract/modular.js';
|
|
4
|
+
import { AffinePoint } from './curves/weierstrass.js';
|
|
5
|
+
export declare const Fp: Readonly<mod.IField<bigint> & Required<Pick<mod.IField<bigint>, "isOdd">>>;
|
|
6
|
+
type Fp = bigint;
|
|
7
|
+
type BigintTuple = [bigint, bigint];
|
|
8
|
+
type Fp2 = {
|
|
9
|
+
c0: bigint;
|
|
10
|
+
c1: bigint;
|
|
11
|
+
};
|
|
12
|
+
type Fp2Utils = {
|
|
13
|
+
fromBigTuple: (tuple: BigintTuple | bigint[]) => Fp2;
|
|
14
|
+
reim: (num: Fp2) => {
|
|
15
|
+
re: bigint;
|
|
16
|
+
im: bigint;
|
|
17
|
+
};
|
|
18
|
+
mulByNonresidue: (num: Fp2) => Fp2;
|
|
19
|
+
multiplyByB: (num: Fp2) => Fp2;
|
|
20
|
+
frobeniusMap(num: Fp2, power: number): Fp2;
|
|
21
|
+
};
|
|
22
|
+
export type Fp2_t = typeof Fp2;
|
|
23
|
+
export declare const Fp2: mod.IField<Fp2> & Fp2Utils;
|
|
24
|
+
type BigintSix = [bigint, bigint, bigint, bigint, bigint, bigint];
|
|
25
|
+
type Fp6 = {
|
|
26
|
+
c0: Fp2;
|
|
27
|
+
c1: Fp2;
|
|
28
|
+
c2: Fp2;
|
|
29
|
+
};
|
|
30
|
+
type Fp6Utils = {
|
|
31
|
+
fromBigSix: (tuple: BigintSix) => Fp6;
|
|
32
|
+
mulByNonresidue: (num: Fp6) => Fp6;
|
|
33
|
+
frobeniusMap(num: Fp6, power: number): Fp6;
|
|
34
|
+
multiplyBy1(num: Fp6, b1: Fp2): Fp6;
|
|
35
|
+
multiplyBy01(num: Fp6, b0: Fp2, b1: Fp2): Fp6;
|
|
36
|
+
multiplyByFp2(lhs: Fp6, rhs: Fp2): Fp6;
|
|
37
|
+
};
|
|
38
|
+
declare const Fp6: mod.IField<Fp6> & Fp6Utils;
|
|
39
|
+
type Fp12 = {
|
|
40
|
+
c0: Fp6;
|
|
41
|
+
c1: Fp6;
|
|
42
|
+
};
|
|
43
|
+
type BigintTwelve = [
|
|
44
|
+
bigint,
|
|
45
|
+
bigint,
|
|
46
|
+
bigint,
|
|
47
|
+
bigint,
|
|
48
|
+
bigint,
|
|
49
|
+
bigint,
|
|
50
|
+
bigint,
|
|
51
|
+
bigint,
|
|
52
|
+
bigint,
|
|
53
|
+
bigint,
|
|
54
|
+
bigint,
|
|
55
|
+
bigint
|
|
56
|
+
];
|
|
57
|
+
export declare const Fp12Multiply: ({ c0, c1 }: Fp12, rhs: Fp12 | bigint) => {
|
|
58
|
+
c0: Fp6;
|
|
59
|
+
c1: Fp6;
|
|
60
|
+
};
|
|
61
|
+
type Fp12Utils = {
|
|
62
|
+
fromBigTwelve: (t: BigintTwelve) => Fp12;
|
|
63
|
+
frobeniusMap(num: Fp12, power: number): Fp12;
|
|
64
|
+
multiplyBy014(num: Fp12, o0: Fp2, o1: Fp2, o4: Fp2): Fp12;
|
|
65
|
+
multiplyByFp2(lhs: Fp12, rhs: Fp2): Fp12;
|
|
66
|
+
conjugate(num: Fp12): Fp12;
|
|
67
|
+
finalExponentiate(num: Fp12): Fp12;
|
|
68
|
+
_cyclotomicSquare(num: Fp12): Fp12;
|
|
69
|
+
_cyclotomicExp(num: Fp12, n: bigint): Fp12;
|
|
70
|
+
};
|
|
71
|
+
export declare const Fp12_finalExponentiate: (num: Fp12) => Fp12;
|
|
72
|
+
export declare const Fp12_conjugate: ({ c0, c1 }: Fp12) => Fp12;
|
|
73
|
+
export declare const Fp12_eql: ({ c0, c1 }: Fp12, { c0: r0, c1: r1 }: Fp12) => boolean;
|
|
74
|
+
export declare const Fp12_ONE: {
|
|
75
|
+
c0: Fp6;
|
|
76
|
+
c1: Fp6;
|
|
77
|
+
};
|
|
78
|
+
export declare const Fp12: mod.IField<Fp12> & Fp12Utils;
|
|
79
|
+
export declare function parseMask(bytes: Uint8Array): {
|
|
80
|
+
compressed: boolean;
|
|
81
|
+
infinity: boolean;
|
|
82
|
+
sort: boolean;
|
|
83
|
+
value: Uint8Array;
|
|
84
|
+
};
|
|
85
|
+
export declare const G1_formBytes: (bytes: Uint8Array) => AffinePoint<Fp>;
|
|
86
|
+
export declare const G1_mapToCurve: (scalars: bigint[]) => {
|
|
87
|
+
x: bigint;
|
|
88
|
+
y: bigint;
|
|
89
|
+
};
|
|
90
|
+
export declare const G2_mapToCurve: (scalars: bigint[]) => {
|
|
91
|
+
x: Fp2;
|
|
92
|
+
y: Fp2;
|
|
93
|
+
};
|
|
94
|
+
export declare const bls12_381: CurveFn<Fp, Fp2, Fp6, Fp12>;
|
|
95
|
+
export {};
|