@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,98 @@
|
|
|
1
|
+
import { mod } from './abstract/modular.js';
|
|
2
|
+
import { ProjPointType as PointType, VerOpts } from './curves/weierstrass.js';
|
|
3
|
+
import type { Hex, PrivKey } from './abstract/utils.js';
|
|
4
|
+
import { bytesToNumberBE, numberToBytesBE } from './abstract/utils.js';
|
|
5
|
+
export declare const secp256k1: Readonly<{
|
|
6
|
+
create: (hash: import("./abstract/utils.js").CHash) => import("./curves/weierstrass.js").CurveFn;
|
|
7
|
+
CURVE: Readonly<{
|
|
8
|
+
readonly nBitLength: number;
|
|
9
|
+
readonly nByteLength: number;
|
|
10
|
+
readonly Fp: import("./abstract/modular.js").IField<bigint>;
|
|
11
|
+
readonly n: bigint;
|
|
12
|
+
readonly h: bigint;
|
|
13
|
+
readonly hEff?: bigint | undefined;
|
|
14
|
+
readonly Gx: bigint;
|
|
15
|
+
readonly Gy: bigint;
|
|
16
|
+
readonly allowInfinityPoint?: boolean | undefined;
|
|
17
|
+
readonly a: bigint;
|
|
18
|
+
readonly b: bigint;
|
|
19
|
+
readonly allowedPrivateKeyLengths?: readonly number[] | undefined;
|
|
20
|
+
readonly wrapPrivateKey?: boolean | undefined;
|
|
21
|
+
readonly endo?: {
|
|
22
|
+
beta: bigint;
|
|
23
|
+
splitScalar: (k: bigint) => {
|
|
24
|
+
k1neg: boolean;
|
|
25
|
+
k1: bigint;
|
|
26
|
+
k2neg: boolean;
|
|
27
|
+
k2: bigint;
|
|
28
|
+
};
|
|
29
|
+
} | undefined;
|
|
30
|
+
readonly isTorsionFree?: ((c: import("./curves/weierstrass.js").ProjConstructor<bigint>, point: PointType<bigint>) => boolean) | undefined;
|
|
31
|
+
readonly clearCofactor?: ((c: import("./curves/weierstrass.js").ProjConstructor<bigint>, point: PointType<bigint>) => PointType<bigint>) | undefined;
|
|
32
|
+
readonly hash: import("./abstract/utils.js").CHash;
|
|
33
|
+
readonly hmac: (key: Uint8Array, ...messages: Uint8Array[]) => Uint8Array;
|
|
34
|
+
readonly randomBytes: (bytesLength?: number | undefined) => Uint8Array;
|
|
35
|
+
lowS: boolean;
|
|
36
|
+
readonly bits2int?: ((bytes: Uint8Array) => bigint) | undefined;
|
|
37
|
+
readonly bits2int_modN?: ((bytes: Uint8Array) => bigint) | undefined;
|
|
38
|
+
readonly p: bigint;
|
|
39
|
+
}>;
|
|
40
|
+
getPublicKey: (privateKey: PrivKey, isCompressed?: boolean | undefined) => Uint8Array;
|
|
41
|
+
getSharedSecret: (privateA: PrivKey, publicB: Hex, isCompressed?: boolean | undefined) => Uint8Array;
|
|
42
|
+
sign: (msgHash: Hex, privKey: PrivKey, opts?: import("./curves/weierstrass.js").SignOpts | undefined) => import("./curves/weierstrass.js").RecoveredSignatureType;
|
|
43
|
+
verify: (signature: Hex | {
|
|
44
|
+
r: bigint;
|
|
45
|
+
s: bigint;
|
|
46
|
+
}, msgHash: Hex, publicKey: Hex, opts?: VerOpts | undefined) => boolean;
|
|
47
|
+
ProjectivePoint: import("./curves/weierstrass.js").ProjConstructor<bigint>;
|
|
48
|
+
Signature: import("./curves/weierstrass.js").SignatureConstructor;
|
|
49
|
+
utils: {
|
|
50
|
+
normPrivateKeyToScalar: (key: PrivKey) => bigint;
|
|
51
|
+
isValidPrivateKey(privateKey: PrivKey): boolean;
|
|
52
|
+
randomPrivateKey: () => Uint8Array;
|
|
53
|
+
precompute: (windowSize?: number | undefined, point?: PointType<bigint> | undefined) => PointType<bigint>;
|
|
54
|
+
};
|
|
55
|
+
}>;
|
|
56
|
+
declare function taggedHash(tag: string, ...messages: Uint8Array[]): Uint8Array;
|
|
57
|
+
export declare const secp256k1PointToBytes: (point: PointType<bigint>) => Uint8Array;
|
|
58
|
+
export declare const Secp256k1Point: import("./curves/weierstrass.js").ProjConstructor<bigint>;
|
|
59
|
+
/**
|
|
60
|
+
* lift_x from BIP340. Convert 32-byte x coordinate to elliptic curve point.
|
|
61
|
+
* @returns valid point checked for being on-curve
|
|
62
|
+
*/
|
|
63
|
+
declare function lift_x(x: bigint): PointType<bigint>;
|
|
64
|
+
/**
|
|
65
|
+
* Schnorr public key is just `x` coordinate of Point as per BIP340.
|
|
66
|
+
*/
|
|
67
|
+
declare function schnorrGetPublicKey(privateKey: Hex): Uint8Array;
|
|
68
|
+
/**
|
|
69
|
+
* Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
|
|
70
|
+
* auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous.
|
|
71
|
+
*/
|
|
72
|
+
declare function schnorrSign(message: Hex, privateKey: PrivKey, auxRand?: Hex): Uint8Array;
|
|
73
|
+
/**
|
|
74
|
+
* Verifies Schnorr signature.
|
|
75
|
+
* Will swallow errors & return false except for initial type validation of arguments.
|
|
76
|
+
*/
|
|
77
|
+
declare function schnorrVerify(signature: Hex, message: Hex, publicKey: Hex): boolean;
|
|
78
|
+
export declare const signSchnorr: typeof schnorrSign;
|
|
79
|
+
export declare const verifySchnorr: typeof schnorrVerify;
|
|
80
|
+
export declare const schnorr: {
|
|
81
|
+
getPublicKey: typeof schnorrGetPublicKey;
|
|
82
|
+
sign: typeof schnorrSign;
|
|
83
|
+
verify: typeof schnorrVerify;
|
|
84
|
+
utils: {
|
|
85
|
+
randomPrivateKey: () => Uint8Array;
|
|
86
|
+
lift_x: typeof lift_x;
|
|
87
|
+
pointToBytes: (point: PointType<bigint>) => Uint8Array;
|
|
88
|
+
numberToBytesBE: typeof numberToBytesBE;
|
|
89
|
+
bytesToNumberBE: typeof bytesToNumberBE;
|
|
90
|
+
taggedHash: typeof taggedHash;
|
|
91
|
+
mod: typeof mod;
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
export declare const hashToCurve: (msg: Uint8Array, options?: import("./abstract/hash-to-curve.js").htfBasicOpts | undefined) => import("./abstract/hash-to-curve.js").H2CPoint<bigint>;
|
|
95
|
+
export declare const encodeToCurve: (msg: Uint8Array, options?: import("./abstract/hash-to-curve.js").htfBasicOpts | undefined) => import("./abstract/hash-to-curve.js").H2CPoint<bigint>;
|
|
96
|
+
export declare function signSecp256k1(data: Uint8Array, privateKey: Uint8Array, extraEntropy?: Uint8Array | undefined): Uint8Array;
|
|
97
|
+
export declare function verifySecp256k1(sig: Uint8Array, msgHash: Uint8Array, pubKey: Uint8Array): boolean;
|
|
98
|
+
export {};
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
3
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
4
|
+
if (!m) return o;
|
|
5
|
+
var i = m.call(o), r, ar = [], e;
|
|
6
|
+
try {
|
|
7
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
8
|
+
}
|
|
9
|
+
catch (error) { e = { error: error }; }
|
|
10
|
+
finally {
|
|
11
|
+
try {
|
|
12
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
13
|
+
}
|
|
14
|
+
finally { if (e) throw e.error; }
|
|
15
|
+
}
|
|
16
|
+
return ar;
|
|
17
|
+
};
|
|
18
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
19
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
20
|
+
if (ar || !(i in from)) {
|
|
21
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
22
|
+
ar[i] = from[i];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
26
|
+
};
|
|
27
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
+
exports.verifySecp256k1 = exports.signSecp256k1 = exports.encodeToCurve = exports.hashToCurve = exports.schnorr = exports.verifySchnorr = exports.signSchnorr = exports.Secp256k1Point = exports.secp256k1PointToBytes = exports.secp256k1 = void 0;
|
|
29
|
+
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
30
|
+
var sha256_1 = require("./sha256.js");
|
|
31
|
+
var utils_1 = require("./utils.js");
|
|
32
|
+
var modular_1 = require("./abstract/modular.js");
|
|
33
|
+
var weierstrass_1 = require("./curves/weierstrass.js");
|
|
34
|
+
var utils_2 = require("./abstract/utils.js");
|
|
35
|
+
var hash_to_curve_1 = require("./abstract/hash-to-curve.js");
|
|
36
|
+
var _shortw_utils_1 = require("./_shortw_utils.js");
|
|
37
|
+
var secp256k1P = BigInt('0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f');
|
|
38
|
+
var secp256k1N = BigInt('0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141');
|
|
39
|
+
var _1n = BigInt(1);
|
|
40
|
+
var _2n = BigInt(2);
|
|
41
|
+
var divNearest = function (a, b) { return (a + b / _2n) / b; };
|
|
42
|
+
/**
|
|
43
|
+
* √n = n^((p+1)/4) for fields p = 3 mod 4. We unwrap the loop and multiply bit-by-bit.
|
|
44
|
+
* (P+1n/4n).toString(2) would produce bits [223x 1, 0, 22x 1, 4x 0, 11, 00]
|
|
45
|
+
*/
|
|
46
|
+
function sqrtMod(y) {
|
|
47
|
+
var P = secp256k1P;
|
|
48
|
+
var _3n = BigInt(3), _6n = BigInt(6), _11n = BigInt(11), _22n = BigInt(22);
|
|
49
|
+
var _23n = BigInt(23), _44n = BigInt(44), _88n = BigInt(88);
|
|
50
|
+
var b2 = (y * y * y) % P; // x^3, 11
|
|
51
|
+
var b3 = (b2 * b2 * y) % P; // x^7
|
|
52
|
+
var b6 = ((0, modular_1.pow2)(b3, _3n, P) * b3) % P;
|
|
53
|
+
var b9 = ((0, modular_1.pow2)(b6, _3n, P) * b3) % P;
|
|
54
|
+
var b11 = ((0, modular_1.pow2)(b9, _2n, P) * b2) % P;
|
|
55
|
+
var b22 = ((0, modular_1.pow2)(b11, _11n, P) * b11) % P;
|
|
56
|
+
var b44 = ((0, modular_1.pow2)(b22, _22n, P) * b22) % P;
|
|
57
|
+
var b88 = ((0, modular_1.pow2)(b44, _44n, P) * b44) % P;
|
|
58
|
+
var b176 = ((0, modular_1.pow2)(b88, _88n, P) * b88) % P;
|
|
59
|
+
var b220 = ((0, modular_1.pow2)(b176, _44n, P) * b44) % P;
|
|
60
|
+
var b223 = ((0, modular_1.pow2)(b220, _3n, P) * b3) % P;
|
|
61
|
+
var t1 = ((0, modular_1.pow2)(b223, _23n, P) * b22) % P;
|
|
62
|
+
var t2 = ((0, modular_1.pow2)(t1, _6n, P) * b2) % P;
|
|
63
|
+
var root = (0, modular_1.pow2)(t2, _2n, P);
|
|
64
|
+
if (!Fp.eql(Fp.sqr(root), y))
|
|
65
|
+
throw new Error('Cannot find square root');
|
|
66
|
+
return root;
|
|
67
|
+
}
|
|
68
|
+
var Fp = (0, modular_1.Field)(secp256k1P, undefined, undefined, { sqrt: sqrtMod });
|
|
69
|
+
exports.secp256k1 = (0, _shortw_utils_1.createCurve)({
|
|
70
|
+
a: BigInt(0),
|
|
71
|
+
b: BigInt(7),
|
|
72
|
+
Fp: Fp,
|
|
73
|
+
n: secp256k1N,
|
|
74
|
+
// Base point (x, y) aka generator point
|
|
75
|
+
Gx: BigInt('55066263022277343669578718895168534326250603453777594175500187360389116729240'),
|
|
76
|
+
Gy: BigInt('32670510020758816978083085130507043184471273380659243275938904335757337482424'),
|
|
77
|
+
h: BigInt(1),
|
|
78
|
+
lowS: true,
|
|
79
|
+
/**
|
|
80
|
+
* secp256k1 belongs to Koblitz curves: it has efficiently computable endomorphism.
|
|
81
|
+
* Endomorphism uses 2x less RAM, speeds up precomputation by 2x and ECDH / key recovery by 20%.
|
|
82
|
+
* For precomputed wNAF it trades off 1/2 init time & 1/3 ram for 20% perf hit.
|
|
83
|
+
* Explanation: https://gist.github.com/paulmillr/eb670806793e84df628a7c434a873066
|
|
84
|
+
*/
|
|
85
|
+
endo: {
|
|
86
|
+
beta: BigInt('0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee'),
|
|
87
|
+
splitScalar: function (k) {
|
|
88
|
+
var n = secp256k1N;
|
|
89
|
+
var a1 = BigInt('0x3086d221a7d46bcde86c90e49284eb15');
|
|
90
|
+
var b1 = -_1n * BigInt('0xe4437ed6010e88286f547fa90abfe4c3');
|
|
91
|
+
var a2 = BigInt('0x114ca50f7a8e2f3f657c1108d9d44cfd8');
|
|
92
|
+
var b2 = a1;
|
|
93
|
+
var POW_2_128 = BigInt('0x100000000000000000000000000000000'); // (2n**128n).toString(16)
|
|
94
|
+
var c1 = divNearest(b2 * k, n);
|
|
95
|
+
var c2 = divNearest(-b1 * k, n);
|
|
96
|
+
var k1 = (0, modular_1.mod)(k - c1 * a1 - c2 * a2, n);
|
|
97
|
+
var k2 = (0, modular_1.mod)(-c1 * b1 - c2 * b2, n);
|
|
98
|
+
var k1neg = k1 > POW_2_128;
|
|
99
|
+
var k2neg = k2 > POW_2_128;
|
|
100
|
+
if (k1neg)
|
|
101
|
+
k1 = n - k1;
|
|
102
|
+
if (k2neg)
|
|
103
|
+
k2 = n - k2;
|
|
104
|
+
if (k1 > POW_2_128 || k2 > POW_2_128) {
|
|
105
|
+
throw new Error('splitScalar: Endomorphism failed, k=' + k);
|
|
106
|
+
}
|
|
107
|
+
return { k1neg: k1neg, k1: k1, k2neg: k2neg, k2: k2 };
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
}, sha256_1.sha256);
|
|
111
|
+
// Schnorr signatures are superior to ECDSA from above. Below is Schnorr-specific BIP0340 code.
|
|
112
|
+
// https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki
|
|
113
|
+
var _0n = BigInt(0);
|
|
114
|
+
var fe = function (x) { return typeof x === 'bigint' && _0n < x && x < secp256k1P; };
|
|
115
|
+
var ge = function (x) { return typeof x === 'bigint' && _0n < x && x < secp256k1N; };
|
|
116
|
+
/** An object mapping tags to their tagged hash prefix of [SHA256(tag) | SHA256(tag)] */
|
|
117
|
+
var TAGGED_HASH_PREFIXES = {};
|
|
118
|
+
function taggedHash(tag) {
|
|
119
|
+
var messages = [];
|
|
120
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
121
|
+
messages[_i - 1] = arguments[_i];
|
|
122
|
+
}
|
|
123
|
+
var tagP = TAGGED_HASH_PREFIXES[tag];
|
|
124
|
+
if (tagP === undefined) {
|
|
125
|
+
var tagH = (0, sha256_1.sha256)(Uint8Array.from(tag, function (c) { return c.charCodeAt(0); }));
|
|
126
|
+
tagP = (0, utils_2.concatBytes)(tagH, tagH);
|
|
127
|
+
TAGGED_HASH_PREFIXES[tag] = tagP;
|
|
128
|
+
}
|
|
129
|
+
return (0, sha256_1.sha256)(utils_2.concatBytes.apply(void 0, __spreadArray([tagP], __read(messages), false)));
|
|
130
|
+
}
|
|
131
|
+
// ECDSA compact points are 33-byte. Schnorr is 32: we strip first byte 0x02 or 0x03
|
|
132
|
+
var pointToBytes = function (point) { return point.toRawBytes(true).slice(1); };
|
|
133
|
+
exports.secp256k1PointToBytes = pointToBytes;
|
|
134
|
+
var numTo32b = function (n) { return (0, utils_2.numberToBytesBE)(n, 32); };
|
|
135
|
+
var modP = function (x) { return (0, modular_1.mod)(x, secp256k1P); };
|
|
136
|
+
var modN = function (x) { return (0, modular_1.mod)(x, secp256k1N); };
|
|
137
|
+
var Point = exports.secp256k1.ProjectivePoint;
|
|
138
|
+
exports.Secp256k1Point = exports.secp256k1.ProjectivePoint;
|
|
139
|
+
var GmulAdd = function (Q, a, b) {
|
|
140
|
+
return Point.BASE.multiplyAndAddUnsafe(Q, a, b);
|
|
141
|
+
};
|
|
142
|
+
// Calculate point, scalar and bytes
|
|
143
|
+
function schnorrGetExtPubKey(priv) {
|
|
144
|
+
var d_ = exports.secp256k1.utils.normPrivateKeyToScalar(priv); // same method executed in fromPrivateKey
|
|
145
|
+
var p = Point.fromPrivateKey(d_); // P = d'⋅G; 0 < d' < n check is done inside
|
|
146
|
+
var scalar = p.hasEvenY() ? d_ : modN(-d_);
|
|
147
|
+
return { scalar: scalar, bytes: pointToBytes(p) };
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* lift_x from BIP340. Convert 32-byte x coordinate to elliptic curve point.
|
|
151
|
+
* @returns valid point checked for being on-curve
|
|
152
|
+
*/
|
|
153
|
+
function lift_x(x) {
|
|
154
|
+
if (!fe(x))
|
|
155
|
+
throw new Error('bad x: need 0 < x < p'); // Fail if x ≥ p.
|
|
156
|
+
var xx = modP(x * x);
|
|
157
|
+
var c = modP(xx * x + BigInt(7)); // Let c = x³ + 7 mod p.
|
|
158
|
+
var y = sqrtMod(c); // Let y = c^(p+1)/4 mod p.
|
|
159
|
+
if (y % _2n !== _0n)
|
|
160
|
+
y = modP(-y); // Return the unique point P such that x(P) = x and
|
|
161
|
+
var p = new Point(x, y, _1n); // y(P) = y if y mod 2 = 0 or y(P) = p-y otherwise.
|
|
162
|
+
p.assertValidity();
|
|
163
|
+
return p;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Create tagged hash, convert it to bigint, reduce modulo-n.
|
|
167
|
+
*/
|
|
168
|
+
function challenge() {
|
|
169
|
+
var args = [];
|
|
170
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
171
|
+
args[_i] = arguments[_i];
|
|
172
|
+
}
|
|
173
|
+
return modN((0, utils_2.bytesToNumberBE)(taggedHash.apply(void 0, __spreadArray(['BIP0340/challenge'], __read(args), false))));
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Schnorr public key is just `x` coordinate of Point as per BIP340.
|
|
177
|
+
*/
|
|
178
|
+
function schnorrGetPublicKey(privateKey) {
|
|
179
|
+
return schnorrGetExtPubKey(privateKey).bytes; // d'=int(sk). Fail if d'=0 or d'≥n. Ret bytes(d'⋅G)
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Creates Schnorr signature as per BIP340. Verifies itself before returning anything.
|
|
183
|
+
* auxRand is optional and is not the sole source of k generation: bad CSPRNG won't be dangerous.
|
|
184
|
+
*/
|
|
185
|
+
function schnorrSign(message, privateKey, auxRand) {
|
|
186
|
+
if (auxRand === void 0) { auxRand = (0, utils_1.randomBytes)(32); }
|
|
187
|
+
var m = (0, utils_2.ensureBytes)('message', message);
|
|
188
|
+
var _a = schnorrGetExtPubKey(privateKey), px = _a.bytes, d = _a.scalar; // checks for isWithinCurveOrder
|
|
189
|
+
var a = (0, utils_2.ensureBytes)('auxRand', auxRand, 32); // Auxiliary random data a: a 32-byte array
|
|
190
|
+
var t = numTo32b(d ^ (0, utils_2.bytesToNumberBE)(taggedHash('BIP0340/aux', a))); // Let t be the byte-wise xor of bytes(d) and hash/aux(a)
|
|
191
|
+
var rand = taggedHash('BIP0340/nonce', t, px, m); // Let rand = hash/nonce(t || bytes(P) || m)
|
|
192
|
+
var k_ = modN((0, utils_2.bytesToNumberBE)(rand)); // Let k' = int(rand) mod n
|
|
193
|
+
if (k_ === _0n)
|
|
194
|
+
throw new Error('sign failed: k is zero'); // Fail if k' = 0.
|
|
195
|
+
var _b = schnorrGetExtPubKey(k_), rx = _b.bytes, k = _b.scalar; // Let R = k'⋅G.
|
|
196
|
+
var e = challenge(rx, px, m); // Let e = int(hash/challenge(bytes(R) || bytes(P) || m)) mod n.
|
|
197
|
+
var sig = new Uint8Array(64); // Let sig = bytes(R) || bytes((k + ed) mod n).
|
|
198
|
+
sig.set(rx, 0);
|
|
199
|
+
sig.set(numTo32b(modN(k + e * d)), 32);
|
|
200
|
+
// If Verify(bytes(P), m, sig) (see below) returns failure, abort
|
|
201
|
+
if (!schnorrVerify(sig, m, px))
|
|
202
|
+
throw new Error('sign: Invalid signature produced');
|
|
203
|
+
return sig;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Verifies Schnorr signature.
|
|
207
|
+
* Will swallow errors & return false except for initial type validation of arguments.
|
|
208
|
+
*/
|
|
209
|
+
function schnorrVerify(signature, message, publicKey) {
|
|
210
|
+
var sig = (0, utils_2.ensureBytes)('signature', signature, 64);
|
|
211
|
+
var m = (0, utils_2.ensureBytes)('message', message);
|
|
212
|
+
var pub = (0, utils_2.ensureBytes)('publicKey', publicKey, 32);
|
|
213
|
+
try {
|
|
214
|
+
var P = lift_x((0, utils_2.bytesToNumberBE)(pub)); // P = lift_x(int(pk)); fail if that fails
|
|
215
|
+
var r = (0, utils_2.bytesToNumberBE)(sig.subarray(0, 32)); // Let r = int(sig[0:32]); fail if r ≥ p.
|
|
216
|
+
if (!fe(r))
|
|
217
|
+
return false;
|
|
218
|
+
var s = (0, utils_2.bytesToNumberBE)(sig.subarray(32, 64)); // Let s = int(sig[32:64]); fail if s ≥ n.
|
|
219
|
+
if (!ge(s))
|
|
220
|
+
return false;
|
|
221
|
+
var e = challenge(numTo32b(r), pointToBytes(P), m); // int(challenge(bytes(r)||bytes(P)||m))%n
|
|
222
|
+
var R = GmulAdd(P, s, modN(-e)); // R = s⋅G - e⋅P
|
|
223
|
+
if (!R || !R.hasEvenY() || R.toAffine().x !== r)
|
|
224
|
+
return false; // -eP == (n-e)P
|
|
225
|
+
return true; // Fail if is_infinite(R) / not has_even_y(R) / x(R) ≠ r.
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exports.signSchnorr = schnorrSign;
|
|
232
|
+
exports.verifySchnorr = schnorrVerify;
|
|
233
|
+
exports.schnorr = (function () { return ({
|
|
234
|
+
getPublicKey: schnorrGetPublicKey,
|
|
235
|
+
sign: schnorrSign,
|
|
236
|
+
verify: schnorrVerify,
|
|
237
|
+
utils: {
|
|
238
|
+
randomPrivateKey: exports.secp256k1.utils.randomPrivateKey,
|
|
239
|
+
lift_x: lift_x,
|
|
240
|
+
pointToBytes: pointToBytes,
|
|
241
|
+
numberToBytesBE: utils_2.numberToBytesBE,
|
|
242
|
+
bytesToNumberBE: utils_2.bytesToNumberBE,
|
|
243
|
+
taggedHash: taggedHash,
|
|
244
|
+
mod: modular_1.mod,
|
|
245
|
+
},
|
|
246
|
+
}); })();
|
|
247
|
+
var isoMap = /* @__PURE__ */ (function () {
|
|
248
|
+
return (0, hash_to_curve_1.isogenyMap)(Fp, [
|
|
249
|
+
// xNum
|
|
250
|
+
[
|
|
251
|
+
'0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa8c7',
|
|
252
|
+
'0x7d3d4c80bc321d5b9f315cea7fd44c5d595d2fc0bf63b92dfff1044f17c6581',
|
|
253
|
+
'0x534c328d23f234e6e2a413deca25caece4506144037c40314ecbd0b53d9dd262',
|
|
254
|
+
'0x8e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38e38daaaaa88c',
|
|
255
|
+
],
|
|
256
|
+
// xDen
|
|
257
|
+
[
|
|
258
|
+
'0xd35771193d94918a9ca34ccbb7b640dd86cd409542f8487d9fe6b745781eb49b',
|
|
259
|
+
'0xedadc6f64383dc1df7c4b2d51b54225406d36b641f5e41bbc52a56612a8c6d14',
|
|
260
|
+
'0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
|
|
261
|
+
],
|
|
262
|
+
// yNum
|
|
263
|
+
[
|
|
264
|
+
'0x4bda12f684bda12f684bda12f684bda12f684bda12f684bda12f684b8e38e23c',
|
|
265
|
+
'0xc75e0c32d5cb7c0fa9d0a54b12a0a6d5647ab046d686da6fdffc90fc201d71a3',
|
|
266
|
+
'0x29a6194691f91a73715209ef6512e576722830a201be2018a765e85a9ecee931',
|
|
267
|
+
'0x2f684bda12f684bda12f684bda12f684bda12f684bda12f684bda12f38e38d84',
|
|
268
|
+
],
|
|
269
|
+
// yDen
|
|
270
|
+
[
|
|
271
|
+
'0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffff93b',
|
|
272
|
+
'0x7a06534bb8bdb49fd5e9e6632722c2989467c1bfc8e8d978dfb425d2685c2573',
|
|
273
|
+
'0x6484aa716545ca2cf3a70c3fa8fe337e0a3d21162f0d6299a7bf8192bfd2a76f',
|
|
274
|
+
'0x0000000000000000000000000000000000000000000000000000000000000001', // LAST 1
|
|
275
|
+
],
|
|
276
|
+
].map(function (i) { return i.map(function (j) { return BigInt(j); }); }));
|
|
277
|
+
})();
|
|
278
|
+
var mapSWU = /* @__PURE__ */ (function () {
|
|
279
|
+
return (0, weierstrass_1.mapToCurveSimpleSWU)(Fp, {
|
|
280
|
+
A: BigInt('0x3f8731abdd661adca08a5558f0f5d272e953d363cb6f0e5d405447c01a444533'),
|
|
281
|
+
B: BigInt('1771'),
|
|
282
|
+
Z: Fp.create(BigInt('-11')),
|
|
283
|
+
});
|
|
284
|
+
})();
|
|
285
|
+
var htf = /* @__PURE__ */ (function () {
|
|
286
|
+
return (0, hash_to_curve_1.createHasher)(exports.secp256k1.ProjectivePoint, function (scalars) {
|
|
287
|
+
var _a = mapSWU(Fp.create(scalars[0])), x = _a.x, y = _a.y;
|
|
288
|
+
return isoMap(x, y);
|
|
289
|
+
}, {
|
|
290
|
+
DST: 'secp256k1_XMD:SHA-256_SSWU_RO_',
|
|
291
|
+
encodeDST: 'secp256k1_XMD:SHA-256_SSWU_NU_',
|
|
292
|
+
p: Fp.ORDER,
|
|
293
|
+
m: 1,
|
|
294
|
+
k: 128,
|
|
295
|
+
expand: 'xmd',
|
|
296
|
+
hash: sha256_1.sha256,
|
|
297
|
+
});
|
|
298
|
+
})();
|
|
299
|
+
exports.hashToCurve = (function () { return htf.hashToCurve; })();
|
|
300
|
+
exports.encodeToCurve = (function () { return htf.encodeToCurve; })();
|
|
301
|
+
function signSecp256k1(data, privateKey, extraEntropy) {
|
|
302
|
+
return exports.secp256k1.sign(data, privateKey, { extraEntropy: extraEntropy }).toCompactRawBytes();
|
|
303
|
+
}
|
|
304
|
+
exports.signSecp256k1 = signSecp256k1;
|
|
305
|
+
var myDefaultVerOpts = { lowS: true, prehash: false };
|
|
306
|
+
function verifySecp256k1(sig, msgHash, pubKey) {
|
|
307
|
+
return exports.secp256k1.verify(sig, msgHash, pubKey, myDefaultVerOpts);
|
|
308
|
+
}
|
|
309
|
+
exports.verifySecp256k1 = verifySecp256k1;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { HashMD } from './_md.js';
|
|
2
|
+
declare class SHA256 extends HashMD<SHA256> {
|
|
3
|
+
A: number;
|
|
4
|
+
B: number;
|
|
5
|
+
C: number;
|
|
6
|
+
D: number;
|
|
7
|
+
E: number;
|
|
8
|
+
F: number;
|
|
9
|
+
G: number;
|
|
10
|
+
H: number;
|
|
11
|
+
constructor();
|
|
12
|
+
protected get(): [number, number, number, number, number, number, number, number];
|
|
13
|
+
protected set(A: number, B: number, C: number, D: number, E: number, F: number, G: number, H: number): void;
|
|
14
|
+
protected process(view: DataView, offset: number): void;
|
|
15
|
+
protected roundClean(): void;
|
|
16
|
+
destroy(): void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* SHA2-256 hash function
|
|
20
|
+
* @param message - data that would be hashed
|
|
21
|
+
*/
|
|
22
|
+
export declare const sha256: {
|
|
23
|
+
(msg: import("./utils.js").Input): Uint8Array;
|
|
24
|
+
outputLen: number;
|
|
25
|
+
blockLen: number;
|
|
26
|
+
create(): import("./utils.js").Hash<SHA256>;
|
|
27
|
+
};
|
|
28
|
+
export declare const sha224: {
|
|
29
|
+
(msg: import("./utils.js").Input): Uint8Array;
|
|
30
|
+
outputLen: number;
|
|
31
|
+
blockLen: number;
|
|
32
|
+
create(): import("./utils.js").Hash<SHA256>;
|
|
33
|
+
};
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.sha224 = exports.sha256 = void 0;
|
|
19
|
+
var _md_1 = require("./_md.js");
|
|
20
|
+
var utils_1 = require("./utils.js");
|
|
21
|
+
// SHA2-256 need to try 2^128 hashes to execute birthday attack.
|
|
22
|
+
// BTC network is doing 2^67 hashes/sec as per early 2023.
|
|
23
|
+
// Round constants:
|
|
24
|
+
// first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
|
|
25
|
+
// prettier-ignore
|
|
26
|
+
var SHA256_K = /* @__PURE__ */ new Uint32Array([
|
|
27
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
28
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
29
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
30
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
31
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
32
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
33
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
34
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
35
|
+
]);
|
|
36
|
+
// Initial state:
|
|
37
|
+
// first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
|
|
38
|
+
// prettier-ignore
|
|
39
|
+
var SHA256_IV = /* @__PURE__ */ new Uint32Array([
|
|
40
|
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
|
41
|
+
]);
|
|
42
|
+
// Temporary buffer, not used to store anything between runs
|
|
43
|
+
// Named this way because it matches specification.
|
|
44
|
+
var SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
|
45
|
+
var SHA256 = /** @class */ (function (_super) {
|
|
46
|
+
__extends(SHA256, _super);
|
|
47
|
+
function SHA256() {
|
|
48
|
+
var _this = _super.call(this, 64, 32, 8, false) || this;
|
|
49
|
+
// We cannot use array here since array allows indexing by variable
|
|
50
|
+
// which means optimizer/compiler cannot use registers.
|
|
51
|
+
_this.A = SHA256_IV[0] | 0;
|
|
52
|
+
_this.B = SHA256_IV[1] | 0;
|
|
53
|
+
_this.C = SHA256_IV[2] | 0;
|
|
54
|
+
_this.D = SHA256_IV[3] | 0;
|
|
55
|
+
_this.E = SHA256_IV[4] | 0;
|
|
56
|
+
_this.F = SHA256_IV[5] | 0;
|
|
57
|
+
_this.G = SHA256_IV[6] | 0;
|
|
58
|
+
_this.H = SHA256_IV[7] | 0;
|
|
59
|
+
return _this;
|
|
60
|
+
}
|
|
61
|
+
SHA256.prototype.get = function () {
|
|
62
|
+
var _a = this, A = _a.A, B = _a.B, C = _a.C, D = _a.D, E = _a.E, F = _a.F, G = _a.G, H = _a.H;
|
|
63
|
+
return [A, B, C, D, E, F, G, H];
|
|
64
|
+
};
|
|
65
|
+
// prettier-ignore
|
|
66
|
+
SHA256.prototype.set = function (A, B, C, D, E, F, G, H) {
|
|
67
|
+
this.A = A | 0;
|
|
68
|
+
this.B = B | 0;
|
|
69
|
+
this.C = C | 0;
|
|
70
|
+
this.D = D | 0;
|
|
71
|
+
this.E = E | 0;
|
|
72
|
+
this.F = F | 0;
|
|
73
|
+
this.G = G | 0;
|
|
74
|
+
this.H = H | 0;
|
|
75
|
+
};
|
|
76
|
+
SHA256.prototype.process = function (view, offset) {
|
|
77
|
+
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
|
78
|
+
for (var i = 0; i < 16; i++, offset += 4)
|
|
79
|
+
SHA256_W[i] = view.getUint32(offset, false);
|
|
80
|
+
for (var i = 16; i < 64; i++) {
|
|
81
|
+
var W15 = SHA256_W[i - 15];
|
|
82
|
+
var W2 = SHA256_W[i - 2];
|
|
83
|
+
var s0 = (0, utils_1.rotr)(W15, 7) ^ (0, utils_1.rotr)(W15, 18) ^ (W15 >>> 3);
|
|
84
|
+
var s1 = (0, utils_1.rotr)(W2, 17) ^ (0, utils_1.rotr)(W2, 19) ^ (W2 >>> 10);
|
|
85
|
+
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
|
86
|
+
}
|
|
87
|
+
// Compression function main loop, 64 rounds
|
|
88
|
+
var _a = this, A = _a.A, B = _a.B, C = _a.C, D = _a.D, E = _a.E, F = _a.F, G = _a.G, H = _a.H;
|
|
89
|
+
for (var i = 0; i < 64; i++) {
|
|
90
|
+
var sigma1 = (0, utils_1.rotr)(E, 6) ^ (0, utils_1.rotr)(E, 11) ^ (0, utils_1.rotr)(E, 25);
|
|
91
|
+
var T1 = (H + sigma1 + (0, _md_1.Chi)(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
|
92
|
+
var sigma0 = (0, utils_1.rotr)(A, 2) ^ (0, utils_1.rotr)(A, 13) ^ (0, utils_1.rotr)(A, 22);
|
|
93
|
+
var T2 = (sigma0 + (0, _md_1.Maj)(A, B, C)) | 0;
|
|
94
|
+
H = G;
|
|
95
|
+
G = F;
|
|
96
|
+
F = E;
|
|
97
|
+
E = (D + T1) | 0;
|
|
98
|
+
D = C;
|
|
99
|
+
C = B;
|
|
100
|
+
B = A;
|
|
101
|
+
A = (T1 + T2) | 0;
|
|
102
|
+
}
|
|
103
|
+
// Add the compressed chunk to the current hash value
|
|
104
|
+
A = (A + this.A) | 0;
|
|
105
|
+
B = (B + this.B) | 0;
|
|
106
|
+
C = (C + this.C) | 0;
|
|
107
|
+
D = (D + this.D) | 0;
|
|
108
|
+
E = (E + this.E) | 0;
|
|
109
|
+
F = (F + this.F) | 0;
|
|
110
|
+
G = (G + this.G) | 0;
|
|
111
|
+
H = (H + this.H) | 0;
|
|
112
|
+
this.set(A, B, C, D, E, F, G, H);
|
|
113
|
+
};
|
|
114
|
+
SHA256.prototype.roundClean = function () {
|
|
115
|
+
SHA256_W.fill(0);
|
|
116
|
+
};
|
|
117
|
+
SHA256.prototype.destroy = function () {
|
|
118
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
119
|
+
this.buffer.fill(0);
|
|
120
|
+
};
|
|
121
|
+
return SHA256;
|
|
122
|
+
}(_md_1.HashMD));
|
|
123
|
+
// Constants from https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
|
|
124
|
+
var SHA224 = /** @class */ (function (_super) {
|
|
125
|
+
__extends(SHA224, _super);
|
|
126
|
+
function SHA224() {
|
|
127
|
+
var _this = _super.call(this) || this;
|
|
128
|
+
_this.A = 0xc1059ed8 | 0;
|
|
129
|
+
_this.B = 0x367cd507 | 0;
|
|
130
|
+
_this.C = 0x3070dd17 | 0;
|
|
131
|
+
_this.D = 0xf70e5939 | 0;
|
|
132
|
+
_this.E = 0xffc00b31 | 0;
|
|
133
|
+
_this.F = 0x68581511 | 0;
|
|
134
|
+
_this.G = 0x64f98fa7 | 0;
|
|
135
|
+
_this.H = 0xbefa4fa4 | 0;
|
|
136
|
+
_this.outputLen = 28;
|
|
137
|
+
return _this;
|
|
138
|
+
}
|
|
139
|
+
return SHA224;
|
|
140
|
+
}(SHA256));
|
|
141
|
+
/**
|
|
142
|
+
* SHA2-256 hash function
|
|
143
|
+
* @param message - data that would be hashed
|
|
144
|
+
*/
|
|
145
|
+
exports.sha256 = (0, utils_1.wrapConstructor)(function () { return new SHA256(); });
|
|
146
|
+
exports.sha224 = (0, utils_1.wrapConstructor)(function () { return new SHA224(); });
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { HashMD } from './_md.js';
|
|
2
|
+
export declare class SHA512 extends HashMD<SHA512> {
|
|
3
|
+
Ah: number;
|
|
4
|
+
Al: number;
|
|
5
|
+
Bh: number;
|
|
6
|
+
Bl: number;
|
|
7
|
+
Ch: number;
|
|
8
|
+
Cl: number;
|
|
9
|
+
Dh: number;
|
|
10
|
+
Dl: number;
|
|
11
|
+
Eh: number;
|
|
12
|
+
El: number;
|
|
13
|
+
Fh: number;
|
|
14
|
+
Fl: number;
|
|
15
|
+
Gh: number;
|
|
16
|
+
Gl: number;
|
|
17
|
+
Hh: number;
|
|
18
|
+
Hl: number;
|
|
19
|
+
constructor();
|
|
20
|
+
protected get(): [
|
|
21
|
+
number,
|
|
22
|
+
number,
|
|
23
|
+
number,
|
|
24
|
+
number,
|
|
25
|
+
number,
|
|
26
|
+
number,
|
|
27
|
+
number,
|
|
28
|
+
number,
|
|
29
|
+
number,
|
|
30
|
+
number,
|
|
31
|
+
number,
|
|
32
|
+
number,
|
|
33
|
+
number,
|
|
34
|
+
number,
|
|
35
|
+
number,
|
|
36
|
+
number
|
|
37
|
+
];
|
|
38
|
+
protected set(Ah: number, Al: number, Bh: number, Bl: number, Ch: number, Cl: number, Dh: number, Dl: number, Eh: number, El: number, Fh: number, Fl: number, Gh: number, Gl: number, Hh: number, Hl: number): void;
|
|
39
|
+
protected process(view: DataView, offset: number): void;
|
|
40
|
+
protected roundClean(): void;
|
|
41
|
+
destroy(): void;
|
|
42
|
+
}
|
|
43
|
+
export declare const sha512: {
|
|
44
|
+
(msg: import("./utils.js").Input): Uint8Array;
|
|
45
|
+
outputLen: number;
|
|
46
|
+
blockLen: number;
|
|
47
|
+
create(): import("./utils.js").Hash<SHA512>;
|
|
48
|
+
};
|
|
49
|
+
export declare const sha512_224: {
|
|
50
|
+
(msg: import("./utils.js").Input): Uint8Array;
|
|
51
|
+
outputLen: number;
|
|
52
|
+
blockLen: number;
|
|
53
|
+
create(): import("./utils.js").Hash<SHA512>;
|
|
54
|
+
};
|
|
55
|
+
export declare const sha512_256: {
|
|
56
|
+
(msg: import("./utils.js").Input): Uint8Array;
|
|
57
|
+
outputLen: number;
|
|
58
|
+
blockLen: number;
|
|
59
|
+
create(): import("./utils.js").Hash<SHA512>;
|
|
60
|
+
};
|
|
61
|
+
export declare const sha384: {
|
|
62
|
+
(msg: import("./utils.js").Input): Uint8Array;
|
|
63
|
+
outputLen: number;
|
|
64
|
+
blockLen: number;
|
|
65
|
+
create(): import("./utils.js").Hash<SHA512>;
|
|
66
|
+
};
|