@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.
Files changed (121) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +60 -2
  3. package/dist/bech32.d.ts +53 -0
  4. package/dist/bech32.js +187 -0
  5. package/dist/blake2b.d.ts +7 -0
  6. package/dist/blake2b.js +213 -0
  7. package/dist/bls12_318.d.ts +48 -0
  8. package/dist/bls12_318.js +268 -0
  9. package/dist/ed25519.d.ts +55 -0
  10. package/dist/ed25519.js +721 -0
  11. package/dist/hasGlobalWebCrypto.d.ts +1 -0
  12. package/dist/hasGlobalWebCrypto.js +21 -0
  13. package/dist/hmacSHA512.d.ts +6 -0
  14. package/dist/hmacSHA512.js +14 -0
  15. package/dist/index.d.ts +14 -0
  16. package/dist/index.js +30 -0
  17. package/dist/keccak.d.ts +4 -0
  18. package/dist/keccak.js +43 -0
  19. package/dist/kes/constants.d.ts +2 -0
  20. package/dist/kes/constants.js +5 -0
  21. package/dist/kes/index.d.ts +0 -0
  22. package/dist/kes/index.js +0 -0
  23. package/dist/kes/macro/sum_kes.d.ts +15 -0
  24. package/dist/kes/macro/sum_kes.js +9 -0
  25. package/dist/kes/singleKes.d.ts +0 -0
  26. package/dist/kes/singleKes.js +0 -0
  27. package/dist/noble/_assert.d.ts +23 -0
  28. package/dist/noble/_assert.js +55 -0
  29. package/dist/noble/_md.d.ts +29 -0
  30. package/dist/noble/_md.js +170 -0
  31. package/dist/noble/_shortw_utils.d.ts +61 -0
  32. package/dist/noble/_shortw_utils.js +63 -0
  33. package/dist/noble/_u64.d.ts +28 -0
  34. package/dist/noble/_u64.js +116 -0
  35. package/dist/noble/abstract/bls.d.ts +119 -0
  36. package/dist/noble/abstract/bls.js +334 -0
  37. package/dist/noble/abstract/curve.d.ts +83 -0
  38. package/dist/noble/abstract/curve.js +319 -0
  39. package/dist/noble/abstract/edwards.d.ts +85 -0
  40. package/dist/noble/abstract/edwards.js +508 -0
  41. package/dist/noble/abstract/hash-to-curve.d.ts +57 -0
  42. package/dist/noble/abstract/hash-to-curve.js +220 -0
  43. package/dist/noble/abstract/modular.d.ts +122 -0
  44. package/dist/noble/abstract/modular.js +455 -0
  45. package/dist/noble/abstract/montgomery.d.ts +28 -0
  46. package/dist/noble/abstract/montgomery.js +172 -0
  47. package/dist/noble/abstract/utils.d.ts +106 -0
  48. package/dist/noble/abstract/utils.js +442 -0
  49. package/dist/noble/bls12_381.d.ts +95 -0
  50. package/dist/noble/bls12_381.js +1434 -0
  51. package/dist/noble/crypto.d.ts +1 -0
  52. package/dist/noble/crypto.js +4 -0
  53. package/dist/noble/curves/curve.d.ts +69 -0
  54. package/dist/noble/curves/curve.js +167 -0
  55. package/dist/noble/curves/modular.d.ts +122 -0
  56. package/dist/noble/curves/modular.js +442 -0
  57. package/dist/noble/curves/utils.d.ts +93 -0
  58. package/dist/noble/curves/utils.js +385 -0
  59. package/dist/noble/curves/weierstrass.d.ts +240 -0
  60. package/dist/noble/curves/weierstrass.js +1178 -0
  61. package/dist/noble/ed25519.d.ts +100 -0
  62. package/dist/noble/ed25519.js +494 -0
  63. package/dist/noble/hmac.d.ts +25 -0
  64. package/dist/noble/hmac.js +101 -0
  65. package/dist/noble/index.d.ts +2 -0
  66. package/dist/noble/index.js +20 -0
  67. package/dist/noble/keccak.d.ts +58 -0
  68. package/dist/noble/keccak.js +289 -0
  69. package/dist/noble/ripemd_160.d.ts +20 -0
  70. package/dist/noble/ripemd_160.js +147 -0
  71. package/dist/noble/secp256k1.d.ts +98 -0
  72. package/dist/noble/secp256k1.js +309 -0
  73. package/dist/noble/sha256.d.ts +34 -0
  74. package/dist/noble/sha256.js +146 -0
  75. package/dist/noble/sha512.d.ts +66 -0
  76. package/dist/noble/sha512.js +301 -0
  77. package/dist/noble/utils.d.ts +90 -0
  78. package/dist/noble/utils.js +278 -0
  79. package/dist/ripemd_160.d.ts +1 -0
  80. package/dist/ripemd_160.js +23 -0
  81. package/dist/rust_vrf_reimpl/curves.d.ts +162 -0
  82. package/dist/rust_vrf_reimpl/curves.js +4225 -0
  83. package/dist/rust_vrf_reimpl/vrf03.d.ts +60 -0
  84. package/dist/rust_vrf_reimpl/vrf03.js +373 -0
  85. package/dist/rust_vrf_reimpl/vrf10.d.ts +73 -0
  86. package/dist/rust_vrf_reimpl/vrf10.js +468 -0
  87. package/dist/rust_vrf_reimpl/vrf10_batch.d.ts +53 -0
  88. package/dist/rust_vrf_reimpl/vrf10_batch.js +245 -0
  89. package/dist/secp256k1.d.ts +4 -0
  90. package/dist/secp256k1.js +33 -0
  91. package/dist/sha2_256.d.ts +16 -0
  92. package/dist/sha2_256.js +403 -0
  93. package/dist/sha2_512.d.ts +16 -0
  94. package/dist/sha2_512.js +256 -0
  95. package/dist/sha3.d.ts +17 -0
  96. package/dist/sha3.js +181 -0
  97. package/dist/types.d.ts +26 -0
  98. package/dist/types.js +120 -0
  99. package/dist/utils/assert.d.ts +3 -0
  100. package/dist/utils/assert.js +55 -0
  101. package/dist/utils/base32.d.ts +5 -0
  102. package/dist/utils/base32.js +52 -0
  103. package/dist/utils/bigintToBuffer.d.ts +1 -0
  104. package/dist/utils/bigintToBuffer.js +27 -0
  105. package/dist/utils/bytesToHex.d.ts +2 -0
  106. package/dist/utils/bytesToHex.js +15 -0
  107. package/dist/utils/getRandomValues.d.ts +4 -0
  108. package/dist/utils/getRandomValues.js +12 -0
  109. package/dist/utils/hexToBytes.d.ts +2 -0
  110. package/dist/utils/hexToBytes.js +14 -0
  111. package/dist/utils/index.d.ts +3 -0
  112. package/dist/utils/index.js +19 -0
  113. package/dist/utils/positiveMod.d.ts +1 -0
  114. package/dist/utils/positiveMod.js +11 -0
  115. package/dist/utils/promisifySync.d.ts +0 -0
  116. package/dist/utils/promisifySync.js +0 -0
  117. package/dist/utils/rotr32.d.ts +2 -0
  118. package/dist/utils/rotr32.js +7 -0
  119. package/dist/vrf.d.ts +74 -0
  120. package/dist/vrf.js +286 -0
  121. package/package.json +49 -4
@@ -0,0 +1,116 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.add5H = exports.add5L = exports.add4H = exports.add4L = exports.add3H = exports.add3L = exports.add = exports.rotlBL = exports.rotlBH = exports.rotlSL = exports.rotlSH = exports.rotr32L = exports.rotr32H = exports.rotrBL = exports.rotrBH = exports.rotrSL = exports.rotrSH = exports.shrSL = exports.shrSH = exports.toBig = exports.split = exports.fromBig = void 0;
20
+ /*
21
+ THIS IMPLEMENTATION WAS TAKEN FROM `@noble/hashes`
22
+
23
+ original source: https://github.com/paulmillr/noble-hashes/blob/6ae82e75d4c354d53a488e95b1bfe9cba90bd6ff/src/_u64.ts#L1
24
+
25
+ it was modified and adapted to this repository to re-use already defined stuff
26
+
27
+ the main reason for copying over and not using as dependency is because of different target in the `tsconfig.json`
28
+
29
+ `@noble/hashes` targets es2020, which tends to cause problems with some other (older) packages sometimes
30
+
31
+ this repo targets ES5+
32
+ */
33
+ var U32_MASK64 = /* @__PURE__ */ BigInt(Math.pow(2, 32) - 1);
34
+ var _32n = /* @__PURE__ */ BigInt(32);
35
+ // We are not using BigUint64Array, because they are extremely slow as per 2022
36
+ function fromBig(n, le) {
37
+ if (le === void 0) { le = false; }
38
+ if (le)
39
+ return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
40
+ return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
41
+ }
42
+ exports.fromBig = fromBig;
43
+ function split(lst, le) {
44
+ var _a;
45
+ if (le === void 0) { le = false; }
46
+ var Ah = new Uint32Array(lst.length);
47
+ var Al = new Uint32Array(lst.length);
48
+ for (var i = 0; i < lst.length; i++) {
49
+ var _b = fromBig(lst[i], le), h = _b.h, l = _b.l;
50
+ _a = __read([h, l], 2), Ah[i] = _a[0], Al[i] = _a[1];
51
+ }
52
+ return [Ah, Al];
53
+ }
54
+ exports.split = split;
55
+ var toBig = function (h, l) { return (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0); };
56
+ exports.toBig = toBig;
57
+ // for Shift in [0, 32)
58
+ var shrSH = function (h, _l, s) { return h >>> s; };
59
+ exports.shrSH = shrSH;
60
+ var shrSL = function (h, l, s) { return (h << (32 - s)) | (l >>> s); };
61
+ exports.shrSL = shrSL;
62
+ // Right rotate for Shift in [1, 32)
63
+ var rotrSH = function (h, l, s) { return (h >>> s) | (l << (32 - s)); };
64
+ exports.rotrSH = rotrSH;
65
+ var rotrSL = function (h, l, s) { return (h << (32 - s)) | (l >>> s); };
66
+ exports.rotrSL = rotrSL;
67
+ // Right rotate for Shift in (32, 64), NOTE: 32 is special case.
68
+ var rotrBH = function (h, l, s) { return (h << (64 - s)) | (l >>> (s - 32)); };
69
+ exports.rotrBH = rotrBH;
70
+ var rotrBL = function (h, l, s) { return (h >>> (s - 32)) | (l << (64 - s)); };
71
+ exports.rotrBL = rotrBL;
72
+ // Right rotate for shift===32 (just swaps l&h)
73
+ var rotr32H = function (_h, l) { return l; };
74
+ exports.rotr32H = rotr32H;
75
+ var rotr32L = function (h, _l) { return h; };
76
+ exports.rotr32L = rotr32L;
77
+ // Left rotate for Shift in [1, 32)
78
+ var rotlSH = function (h, l, s) { return (h << s) | (l >>> (32 - s)); };
79
+ exports.rotlSH = rotlSH;
80
+ var rotlSL = function (h, l, s) { return (l << s) | (h >>> (32 - s)); };
81
+ exports.rotlSL = rotlSL;
82
+ // Left rotate for Shift in (32, 64), NOTE: 32 is special case.
83
+ var rotlBH = function (h, l, s) { return (l << (s - 32)) | (h >>> (64 - s)); };
84
+ exports.rotlBH = rotlBH;
85
+ var rotlBL = function (h, l, s) { return (h << (s - 32)) | (l >>> (64 - s)); };
86
+ exports.rotlBL = rotlBL;
87
+ // JS uses 32-bit signed integers for bitwise operations which means we cannot
88
+ // simple take carry out of low bit sum by shift, we need to use division.
89
+ function add(Ah, Al, Bh, Bl) {
90
+ var l = (Al >>> 0) + (Bl >>> 0);
91
+ return { h: (Ah + Bh + ((l / Math.pow(2, 32)) | 0)) | 0, l: l | 0 };
92
+ }
93
+ exports.add = add;
94
+ // Addition with more than 2 elements
95
+ var add3L = function (Al, Bl, Cl) { return (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0); };
96
+ exports.add3L = add3L;
97
+ var add3H = function (low, Ah, Bh, Ch) {
98
+ return (Ah + Bh + Ch + ((low / Math.pow(2, 32)) | 0)) | 0;
99
+ };
100
+ exports.add3H = add3H;
101
+ var add4L = function (Al, Bl, Cl, Dl) {
102
+ return (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
103
+ };
104
+ exports.add4L = add4L;
105
+ var add4H = function (low, Ah, Bh, Ch, Dh) {
106
+ return (Ah + Bh + Ch + Dh + ((low / Math.pow(2, 32)) | 0)) | 0;
107
+ };
108
+ exports.add4H = add4H;
109
+ var add5L = function (Al, Bl, Cl, Dl, El) {
110
+ return (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
111
+ };
112
+ exports.add5L = add5L;
113
+ var add5H = function (low, Ah, Bh, Ch, Dh, Eh) {
114
+ return (Ah + Bh + Ch + Dh + Eh + ((low / Math.pow(2, 32)) | 0)) | 0;
115
+ };
116
+ exports.add5H = add5H;
@@ -0,0 +1,119 @@
1
+ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2
+ /**
3
+ * BLS (Barreto-Lynn-Scott) family of pairing-friendly curves.
4
+ * Implements BLS (Boneh-Lynn-Shacham) signatures.
5
+ * Consists of two curves: G1 and G2:
6
+ * - G1 is a subgroup of (x, y) E(Fq) over y² = x³ + 4.
7
+ * - G2 is a subgroup of ((x₁, x₂+i), (y₁, y₂+i)) E(Fq²) over y² = x³ + 4(1 + i) where i is √-1
8
+ * - Gt, created by bilinear (ate) pairing e(G1, G2), consists of p-th roots of unity in
9
+ * Fq^k where k is embedding degree. Only degree 12 is currently supported, 24 is not.
10
+ * Pairing is used to aggregate and verify signatures.
11
+ * We are using Fp for private keys (shorter) and Fp₂ for signatures (longer).
12
+ * Some projects may prefer to swap this relation, it is not supported for now.
13
+ */
14
+ import { AffinePoint } from './curve.js';
15
+ import { IField } from './modular.js';
16
+ import { Hex, PrivKey, CHash } from './utils.js';
17
+ import { MapToCurve, Opts as HTFOpts, htfBasicOpts, createHasher } from './hash-to-curve.js';
18
+ import { CurvePointsType, ProjPointType as ProjPointType, CurvePointsRes } from '../curves/weierstrass.js';
19
+ type Fp = bigint;
20
+ export type ShortSignatureCoder<Fp> = {
21
+ fromHex(hex: Hex): ProjPointType<Fp>;
22
+ toRawBytes(point: ProjPointType<Fp>): Uint8Array;
23
+ toHex(point: ProjPointType<Fp>): string;
24
+ };
25
+ export type SignatureCoder<Fp2> = {
26
+ fromHex(hex: Hex): ProjPointType<Fp2>;
27
+ toRawBytes(point: ProjPointType<Fp2>): Uint8Array;
28
+ toHex(point: ProjPointType<Fp2>): string;
29
+ };
30
+ export type CurveType<Fp, Fp2, Fp6, Fp12> = {
31
+ G1: Omit<CurvePointsType<Fp>, 'n'> & {
32
+ ShortSignature: SignatureCoder<Fp>;
33
+ mapToCurve: MapToCurve<Fp>;
34
+ htfDefaults: HTFOpts;
35
+ };
36
+ G2: Omit<CurvePointsType<Fp2>, 'n'> & {
37
+ Signature: SignatureCoder<Fp2>;
38
+ mapToCurve: MapToCurve<Fp2>;
39
+ htfDefaults: HTFOpts;
40
+ };
41
+ fields: {
42
+ Fp: IField<Fp>;
43
+ Fr: IField<bigint>;
44
+ Fp2: IField<Fp2> & {
45
+ reim: (num: Fp2) => {
46
+ re: bigint;
47
+ im: bigint;
48
+ };
49
+ multiplyByB: (num: Fp2) => Fp2;
50
+ frobeniusMap(num: Fp2, power: number): Fp2;
51
+ };
52
+ Fp6: IField<Fp6>;
53
+ Fp12: IField<Fp12> & {
54
+ frobeniusMap(num: Fp12, power: number): Fp12;
55
+ multiplyBy014(num: Fp12, o0: Fp2, o1: Fp2, o4: Fp2): Fp12;
56
+ conjugate(num: Fp12): Fp12;
57
+ finalExponentiate(num: Fp12): Fp12;
58
+ };
59
+ };
60
+ params: {
61
+ x: bigint;
62
+ r: bigint;
63
+ };
64
+ htfDefaults: HTFOpts;
65
+ hash: CHash;
66
+ randomBytes: (bytesLength?: number) => Uint8Array;
67
+ };
68
+ export type CurveFn<Fp, Fp2, Fp6, Fp12> = {
69
+ getPublicKey: (privateKey: PrivKey) => Uint8Array;
70
+ getPublicKeyForShortSignatures: (privateKey: PrivKey) => Uint8Array;
71
+ sign: {
72
+ (message: Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
73
+ (message: ProjPointType<Fp2>, privateKey: PrivKey, htfOpts?: htfBasicOpts): ProjPointType<Fp2>;
74
+ };
75
+ signShortSignature: {
76
+ (message: Hex, privateKey: PrivKey, htfOpts?: htfBasicOpts): Uint8Array;
77
+ (message: ProjPointType<Fp>, privateKey: PrivKey, htfOpts?: htfBasicOpts): ProjPointType<Fp>;
78
+ };
79
+ verify: (signature: Hex | ProjPointType<Fp2>, message: Hex | ProjPointType<Fp2>, publicKey: Hex | ProjPointType<Fp>, htfOpts?: htfBasicOpts) => boolean;
80
+ verifyShortSignature: (signature: Hex | ProjPointType<Fp>, message: Hex | ProjPointType<Fp>, publicKey: Hex | ProjPointType<Fp2>, htfOpts?: htfBasicOpts) => boolean;
81
+ verifyBatch: (signature: Hex | ProjPointType<Fp2>, messages: (Hex | ProjPointType<Fp2>)[], publicKeys: (Hex | ProjPointType<Fp>)[], htfOpts?: htfBasicOpts) => boolean;
82
+ aggregatePublicKeys: {
83
+ (publicKeys: Hex[]): Uint8Array;
84
+ (publicKeys: ProjPointType<Fp>[]): ProjPointType<Fp>;
85
+ };
86
+ aggregateSignatures: {
87
+ (signatures: Hex[]): Uint8Array;
88
+ (signatures: ProjPointType<Fp2>[]): ProjPointType<Fp2>;
89
+ };
90
+ aggregateShortSignatures: {
91
+ (signatures: Hex[]): Uint8Array;
92
+ (signatures: ProjPointType<Fp>[]): ProjPointType<Fp>;
93
+ };
94
+ millerLoop: (ell: [Fp2, Fp2, Fp2][], g1: [Fp, Fp]) => Fp12;
95
+ pairing: (P: ProjPointType<Fp>, Q: ProjPointType<Fp2>, withFinalExponent?: boolean) => Fp12;
96
+ G1: CurvePointsRes<Fp> & ReturnType<typeof createHasher<Fp>>;
97
+ G2: CurvePointsRes<Fp2> & ReturnType<typeof createHasher<Fp2>>;
98
+ Signature: SignatureCoder<Fp2>;
99
+ ShortSignature: ShortSignatureCoder<Fp>;
100
+ params: {
101
+ x: bigint;
102
+ r: bigint;
103
+ G1b: bigint;
104
+ G2b: Fp2;
105
+ };
106
+ fields: {
107
+ Fp: IField<Fp>;
108
+ Fp2: IField<Fp2>;
109
+ Fp6: IField<Fp6>;
110
+ Fp12: IField<Fp12>;
111
+ Fr: IField<bigint>;
112
+ };
113
+ utils: {
114
+ randomPrivateKey: () => Uint8Array;
115
+ calcPairingPrecomputes: (p: AffinePoint<Fp2>) => [Fp2, Fp2, Fp2][];
116
+ };
117
+ };
118
+ export declare function bls<Fp2, Fp6, Fp12>(CURVE: CurveType<Fp, Fp2, Fp6, Fp12>): CurveFn<Fp, Fp2, Fp6, Fp12>;
119
+ export {};
@@ -0,0 +1,334 @@
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/bls.ts#
6
+
7
+ it was modified and adapted to this repository
8
+
9
+ the main reason for copying over and not using as dependency is because of different target in the `tsconfig.json`
10
+
11
+ `@noble/hashes` targets es2020, which tends to cause problems with some other (older) packages sometimes
12
+
13
+ this repo targets ES5+
14
+ */
15
+ var __assign = (this && this.__assign) || function () {
16
+ __assign = Object.assign || function(t) {
17
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
18
+ s = arguments[i];
19
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
20
+ t[p] = s[p];
21
+ }
22
+ return t;
23
+ };
24
+ return __assign.apply(this, arguments);
25
+ };
26
+ var __values = (this && this.__values) || function(o) {
27
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
28
+ if (m) return m.call(o);
29
+ if (o && typeof o.length === "number") return {
30
+ next: function () {
31
+ if (o && i >= o.length) o = void 0;
32
+ return { value: o && o[i++], done: !o };
33
+ }
34
+ };
35
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.bls = void 0;
39
+ var modular_1 = require("./modular.js");
40
+ var utils_1 = require("./utils.js");
41
+ // prettier-ignore
42
+ var hash_to_curve_1 = require("./hash-to-curve.js");
43
+ var weierstrass_1 = require("../curves/weierstrass.js");
44
+ // prettier-ignore
45
+ var _2n = BigInt(2), _3n = BigInt(3);
46
+ function bls(CURVE) {
47
+ // Fields are specific for curve, so for now we'll need to pass them with opts
48
+ var _a = CURVE.fields, Fp = _a.Fp, Fr = _a.Fr, Fp2 = _a.Fp2, Fp6 = _a.Fp6, Fp12 = _a.Fp12;
49
+ var BLS_X_LEN = (0, utils_1.bitLen)(CURVE.params.x);
50
+ // Pre-compute coefficients for sparse multiplication
51
+ // Point addition and point double calculations is reused for coefficients
52
+ function calcPairingPrecomputes(p) {
53
+ var x = p.x, y = p.y;
54
+ // prettier-ignore
55
+ var Qx = x, Qy = y, Qz = Fp2.ONE;
56
+ // prettier-ignore
57
+ var Rx = Qx, Ry = Qy, Rz = Qz;
58
+ var ell_coeff = [];
59
+ for (var i = BLS_X_LEN - 2; i >= 0; i--) {
60
+ // Double
61
+ var t0 = Fp2.sqr(Ry); // Ry²
62
+ var t1 = Fp2.sqr(Rz); // Rz²
63
+ var t2 = Fp2.multiplyByB(Fp2.mul(t1, _3n)); // 3 * T1 * B
64
+ var t3 = Fp2.mul(t2, _3n); // 3 * T2
65
+ var t4 = Fp2.sub(Fp2.sub(Fp2.sqr(Fp2.add(Ry, Rz)), t1), t0); // (Ry + Rz)² - T1 - T0
66
+ ell_coeff.push([
67
+ Fp2.sub(t2, t0),
68
+ Fp2.mul(Fp2.sqr(Rx), _3n),
69
+ Fp2.neg(t4), // -T4
70
+ ]);
71
+ Rx = Fp2.div(Fp2.mul(Fp2.mul(Fp2.sub(t0, t3), Rx), Ry), _2n); // ((T0 - T3) * Rx * Ry) / 2
72
+ Ry = Fp2.sub(Fp2.sqr(Fp2.div(Fp2.add(t0, t3), _2n)), Fp2.mul(Fp2.sqr(t2), _3n)); // ((T0 + T3) / 2)² - 3 * T2²
73
+ Rz = Fp2.mul(t0, t4); // T0 * T4
74
+ if ((0, utils_1.bitGet)(CURVE.params.x, i)) {
75
+ // Addition
76
+ var t0_1 = Fp2.sub(Ry, Fp2.mul(Qy, Rz)); // Ry - Qy * Rz
77
+ var t1_1 = Fp2.sub(Rx, Fp2.mul(Qx, Rz)); // Rx - Qx * Rz
78
+ ell_coeff.push([
79
+ Fp2.sub(Fp2.mul(t0_1, Qx), Fp2.mul(t1_1, Qy)),
80
+ Fp2.neg(t0_1),
81
+ t1_1, // T1
82
+ ]);
83
+ var t2_1 = Fp2.sqr(t1_1); // T1²
84
+ var t3_1 = Fp2.mul(t2_1, t1_1); // T2 * T1
85
+ var t4_1 = Fp2.mul(t2_1, Rx); // T2 * Rx
86
+ var t5 = Fp2.add(Fp2.sub(t3_1, Fp2.mul(t4_1, _2n)), Fp2.mul(Fp2.sqr(t0_1), Rz)); // T3 - 2 * T4 + T0² * Rz
87
+ Rx = Fp2.mul(t1_1, t5); // T1 * T5
88
+ Ry = Fp2.sub(Fp2.mul(Fp2.sub(t4_1, t5), t0_1), Fp2.mul(t3_1, Ry)); // (T4 - T5) * T0 - T3 * Ry
89
+ Rz = Fp2.mul(Rz, t3_1); // Rz * T3
90
+ }
91
+ }
92
+ return ell_coeff;
93
+ }
94
+ function millerLoop(ell, g1) {
95
+ var x = CURVE.params.x;
96
+ var Px = g1[0];
97
+ var Py = g1[1];
98
+ var f12 = Fp12.ONE;
99
+ for (var j = 0, i = BLS_X_LEN - 2; i >= 0; i--, j++) {
100
+ var E = ell[j];
101
+ f12 = Fp12.multiplyBy014(f12, E[0], Fp2.mul(E[1], Px), Fp2.mul(E[2], Py));
102
+ if ((0, utils_1.bitGet)(x, i)) {
103
+ j += 1;
104
+ var F = ell[j];
105
+ f12 = Fp12.multiplyBy014(f12, F[0], Fp2.mul(F[1], Px), Fp2.mul(F[2], Py));
106
+ }
107
+ if (i !== 0)
108
+ f12 = Fp12.sqr(f12);
109
+ }
110
+ return Fp12.conjugate(f12);
111
+ }
112
+ var utils = {
113
+ randomPrivateKey: function () {
114
+ var length = (0, modular_1.getMinHashLength)(Fr.ORDER);
115
+ return (0, modular_1.mapHashToField)(CURVE.randomBytes(length), Fr.ORDER);
116
+ },
117
+ calcPairingPrecomputes: calcPairingPrecomputes,
118
+ };
119
+ // Point on G1 curve: (x, y)
120
+ var G1_ = (0, weierstrass_1.weierstrassPoints)(__assign({ n: Fr.ORDER }, CURVE.G1));
121
+ var G1 = Object.assign(G1_, (0, hash_to_curve_1.createHasher)(G1_.ProjectivePoint, CURVE.G1.mapToCurve, __assign(__assign({}, CURVE.htfDefaults), CURVE.G1.htfDefaults)));
122
+ function pairingPrecomputes(point) {
123
+ var p = point;
124
+ if (p._PPRECOMPUTES)
125
+ return p._PPRECOMPUTES;
126
+ p._PPRECOMPUTES = calcPairingPrecomputes(point.toAffine());
127
+ return p._PPRECOMPUTES;
128
+ }
129
+ // TODO: export
130
+ // function clearPairingPrecomputes(point: G2) {
131
+ // const p = point as G2 & withPairingPrecomputes;
132
+ // p._PPRECOMPUTES = undefined;
133
+ // }
134
+ // Point on G2 curve (complex numbers): (x₁, x₂+i), (y₁, y₂+i)
135
+ var G2_ = (0, weierstrass_1.weierstrassPoints)(__assign({ n: Fr.ORDER }, CURVE.G2));
136
+ var G2 = Object.assign(G2_, (0, hash_to_curve_1.createHasher)(G2_.ProjectivePoint, CURVE.G2.mapToCurve, __assign(__assign({}, CURVE.htfDefaults), CURVE.G2.htfDefaults)));
137
+ var ShortSignature = CURVE.G1.ShortSignature;
138
+ var Signature = CURVE.G2.Signature;
139
+ // Calculates bilinear pairing
140
+ function pairing(Q, P, withFinalExponent) {
141
+ if (withFinalExponent === void 0) { withFinalExponent = true; }
142
+ if (Q.equals(G1.ProjectivePoint.ZERO) || P.equals(G2.ProjectivePoint.ZERO))
143
+ throw new Error('pairing is not available for ZERO point');
144
+ Q.assertValidity();
145
+ P.assertValidity();
146
+ // Performance: 9ms for millerLoop and ~14ms for exp.
147
+ var Qa = Q.toAffine();
148
+ var looped = millerLoop(pairingPrecomputes(P), [Qa.x, Qa.y]);
149
+ return withFinalExponent ? Fp12.finalExponentiate(looped) : looped;
150
+ }
151
+ function normP1(point) {
152
+ return point instanceof G1.ProjectivePoint ? point : G1.ProjectivePoint.fromHex(point);
153
+ }
154
+ function normP1Hash(point, htfOpts) {
155
+ return point instanceof G1.ProjectivePoint
156
+ ? point
157
+ : G1.hashToCurve((0, utils_1.ensureBytes)('point', point), htfOpts);
158
+ }
159
+ function normP2(point) {
160
+ return point instanceof G2.ProjectivePoint ? point : Signature.fromHex(point);
161
+ }
162
+ function normP2Hash(point, htfOpts) {
163
+ return point instanceof G2.ProjectivePoint
164
+ ? point
165
+ : G2.hashToCurve((0, utils_1.ensureBytes)('point', point), htfOpts);
166
+ }
167
+ // Multiplies generator (G1) by private key.
168
+ // P = pk x G
169
+ function getPublicKey(privateKey) {
170
+ return G1.ProjectivePoint.fromPrivateKey(privateKey).toRawBytes(true);
171
+ }
172
+ // Multiplies generator (G2) by private key.
173
+ // P = pk x G
174
+ function getPublicKeyForShortSignatures(privateKey) {
175
+ return G2.ProjectivePoint.fromPrivateKey(privateKey).toRawBytes(true);
176
+ }
177
+ function sign(message, privateKey, htfOpts) {
178
+ var msgPoint = normP2Hash(message, htfOpts);
179
+ msgPoint.assertValidity();
180
+ var sigPoint = msgPoint.multiply(G1.normPrivateKeyToScalar(privateKey));
181
+ if (message instanceof G2.ProjectivePoint)
182
+ return sigPoint;
183
+ return Signature.toRawBytes(sigPoint);
184
+ }
185
+ function signShortSignature(message, privateKey, htfOpts) {
186
+ var msgPoint = normP1Hash(message, htfOpts);
187
+ msgPoint.assertValidity();
188
+ var sigPoint = msgPoint.multiply(G1.normPrivateKeyToScalar(privateKey));
189
+ if (message instanceof G1.ProjectivePoint)
190
+ return sigPoint;
191
+ return ShortSignature.toRawBytes(sigPoint);
192
+ }
193
+ // Checks if pairing of public key & hash is equal to pairing of generator & signature.
194
+ // e(P, H(m)) == e(G, S)
195
+ function verify(signature, message, publicKey, htfOpts) {
196
+ var P = normP1(publicKey);
197
+ var Hm = normP2Hash(message, htfOpts);
198
+ var G = G1.ProjectivePoint.BASE;
199
+ var S = normP2(signature);
200
+ // Instead of doing 2 exponentiations, we use property of billinear maps
201
+ // and do one exp after multiplying 2 points.
202
+ var ePHm = pairing(P.negate(), Hm, false);
203
+ var eGS = pairing(G, S, false);
204
+ var exp = Fp12.finalExponentiate(Fp12.mul(eGS, ePHm));
205
+ return Fp12.eql(exp, Fp12.ONE);
206
+ }
207
+ // Checks if pairing of public key & hash is equal to pairing of generator & signature.
208
+ // e(S, G) == e(H(m), P)
209
+ function verifyShortSignature(signature, message, publicKey, htfOpts) {
210
+ var P = normP2(publicKey);
211
+ var Hm = normP1Hash(message, htfOpts);
212
+ var G = G2.ProjectivePoint.BASE;
213
+ var S = normP1(signature);
214
+ // Instead of doing 2 exponentiations, we use property of billinear maps
215
+ // and do one exp after multiplying 2 points.
216
+ var eHmP = pairing(Hm, P, false);
217
+ var eSG = pairing(S, G.negate(), false);
218
+ var exp = Fp12.finalExponentiate(Fp12.mul(eSG, eHmP));
219
+ return Fp12.eql(exp, Fp12.ONE);
220
+ }
221
+ function aggregatePublicKeys(publicKeys) {
222
+ if (!publicKeys.length)
223
+ throw new Error('Expected non-empty array');
224
+ var agg = publicKeys.map(normP1).reduce(function (sum, p) { return sum.add(p); }, G1.ProjectivePoint.ZERO);
225
+ var aggAffine = agg; //.toAffine();
226
+ if (publicKeys[0] instanceof G1.ProjectivePoint) {
227
+ aggAffine.assertValidity();
228
+ return aggAffine;
229
+ }
230
+ // toRawBytes ensures point validity
231
+ return aggAffine.toRawBytes(true);
232
+ }
233
+ function aggregateSignatures(signatures) {
234
+ if (!signatures.length)
235
+ throw new Error('Expected non-empty array');
236
+ var agg = signatures.map(normP2).reduce(function (sum, s) { return sum.add(s); }, G2.ProjectivePoint.ZERO);
237
+ var aggAffine = agg; //.toAffine();
238
+ if (signatures[0] instanceof G2.ProjectivePoint) {
239
+ aggAffine.assertValidity();
240
+ return aggAffine;
241
+ }
242
+ return Signature.toRawBytes(aggAffine);
243
+ }
244
+ function aggregateShortSignatures(signatures) {
245
+ if (!signatures.length)
246
+ throw new Error('Expected non-empty array');
247
+ var agg = signatures.map(normP1).reduce(function (sum, s) { return sum.add(s); }, G1.ProjectivePoint.ZERO);
248
+ var aggAffine = agg; //.toAffine();
249
+ if (signatures[0] instanceof G1.ProjectivePoint) {
250
+ aggAffine.assertValidity();
251
+ return aggAffine;
252
+ }
253
+ return ShortSignature.toRawBytes(aggAffine);
254
+ }
255
+ // https://ethresear.ch/t/fast-verification-of-multiple-bls-signatures/5407
256
+ // e(G, S) = e(G, SUM(n)(Si)) = MUL(n)(e(G, Si))
257
+ function verifyBatch(signature, messages, publicKeys, htfOpts) {
258
+ // @ts-ignore
259
+ // console.log('verifyBatch', bytesToHex(signature as any), messages, publicKeys.map(bytesToHex));
260
+ var e_1, _a;
261
+ if (!messages.length)
262
+ throw new Error('Expected non-empty messages array');
263
+ if (publicKeys.length !== messages.length)
264
+ throw new Error('Pubkey count should equal msg count');
265
+ var sig = normP2(signature);
266
+ var nMessages = messages.map(function (i) { return normP2Hash(i, htfOpts); });
267
+ var nPublicKeys = publicKeys.map(normP1);
268
+ try {
269
+ var paired = [];
270
+ var _loop_1 = function (message) {
271
+ var groupPublicKey = nMessages.reduce(function (groupPublicKey, subMessage, i) {
272
+ return subMessage === message ? groupPublicKey.add(nPublicKeys[i]) : groupPublicKey;
273
+ }, G1.ProjectivePoint.ZERO);
274
+ // const msg = message instanceof PointG2 ? message : await PointG2.hashToCurve(message);
275
+ // Possible to batch pairing for same msg with different groupPublicKey here
276
+ paired.push(pairing(groupPublicKey, message, false));
277
+ };
278
+ try {
279
+ for (var _b = __values(new Set(nMessages)), _c = _b.next(); !_c.done; _c = _b.next()) {
280
+ var message = _c.value;
281
+ _loop_1(message);
282
+ }
283
+ }
284
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
285
+ finally {
286
+ try {
287
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
288
+ }
289
+ finally { if (e_1) throw e_1.error; }
290
+ }
291
+ paired.push(pairing(G1.ProjectivePoint.BASE.negate(), sig, false));
292
+ var product = paired.reduce(function (a, b) { return Fp12.mul(a, b); }, Fp12.ONE);
293
+ var exp = Fp12.finalExponentiate(product);
294
+ return Fp12.eql(exp, Fp12.ONE);
295
+ }
296
+ catch (_d) {
297
+ return false;
298
+ }
299
+ }
300
+ G1.ProjectivePoint.BASE._setWindowSize(4);
301
+ return {
302
+ getPublicKey: getPublicKey,
303
+ getPublicKeyForShortSignatures: getPublicKeyForShortSignatures,
304
+ sign: sign,
305
+ signShortSignature: signShortSignature,
306
+ verify: verify,
307
+ verifyBatch: verifyBatch,
308
+ verifyShortSignature: verifyShortSignature,
309
+ aggregatePublicKeys: aggregatePublicKeys,
310
+ aggregateSignatures: aggregateSignatures,
311
+ aggregateShortSignatures: aggregateShortSignatures,
312
+ millerLoop: millerLoop,
313
+ pairing: pairing,
314
+ G1: G1,
315
+ G2: G2,
316
+ Signature: Signature,
317
+ ShortSignature: ShortSignature,
318
+ fields: {
319
+ Fr: Fr,
320
+ Fp: Fp,
321
+ Fp2: Fp2,
322
+ Fp6: Fp6,
323
+ Fp12: Fp12,
324
+ },
325
+ params: {
326
+ x: CURVE.params.x,
327
+ r: CURVE.params.r,
328
+ G1b: CURVE.G1.b,
329
+ G2b: CURVE.G2.b,
330
+ },
331
+ utils: utils,
332
+ };
333
+ }
334
+ exports.bls = bls;
@@ -0,0 +1,83 @@
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 type IWNAF<T extends Group<T>> = {
24
+ constTimeNegate: <T extends Group<T>>(condition: boolean, item: T) => T;
25
+ hasPrecomputes(elm: T): boolean;
26
+ unsafeLadder(elm: T, n: bigint, p?: T): T;
27
+ precomputeWindow(elm: T, W: number): Group<T>[];
28
+ wNAF(W: number, precomputes: T[], n: bigint): {
29
+ p: T;
30
+ f: T;
31
+ };
32
+ wNAFUnsafe(W: number, precomputes: T[], n: bigint, acc?: T): T;
33
+ getPrecomputes(W: number, P: T, transform: Mapper<T>): T[];
34
+ wNAFCached(P: T, n: bigint, transform: Mapper<T>): {
35
+ p: T;
36
+ f: T;
37
+ };
38
+ wNAFCachedUnsafe(P: T, n: bigint, transform: Mapper<T>, prev?: T): T;
39
+ setWindowSize(P: T, W: number): void;
40
+ };
41
+ /**
42
+ * Elliptic curve multiplication of Point by scalar. Fragile.
43
+ * Scalars should always be less than curve order: this should be checked inside of a curve itself.
44
+ * Creates precomputation tables for fast multiplication:
45
+ * - private scalar is split by fixed size windows of W bits
46
+ * - every window point is collected from window's table & added to accumulator
47
+ * - since windows are different, same point inside tables won't be accessed more than once per calc
48
+ * - each multiplication is 'Math.ceil(CURVE_ORDER / 𝑊) + 1' point additions (fixed for any scalar)
49
+ * - +1 window is neccessary for wNAF
50
+ * - wNAF reduces table size: 2x less memory + 2x faster generation, but 10% slower multiplication
51
+ *
52
+ * @todo Research returning 2d JS array of windows, instead of a single window.
53
+ * This would allow windows to be in different memory locations
54
+ */
55
+ export declare function wNAF<T extends Group<T>>(c: GroupConstructor<T>, bits: number): IWNAF<T>;
56
+ export type BasicCurve<T> = {
57
+ Fp: IField<T>;
58
+ n: bigint;
59
+ nBitLength?: number;
60
+ nByteLength?: number;
61
+ h: bigint;
62
+ hEff?: bigint;
63
+ Gx: T;
64
+ Gy: T;
65
+ allowInfinityPoint?: boolean;
66
+ };
67
+ export declare function validateBasic<FP, T>(curve: BasicCurve<FP> & T): Readonly<{
68
+ readonly nBitLength: number;
69
+ readonly nByteLength: number;
70
+ } & BasicCurve<FP> & T & {
71
+ p: bigint;
72
+ }>;
73
+ /**
74
+ * Pippenger algorithm for multi-scalar multiplication (MSM, Pa + Qb + Rc + ...).
75
+ * 30x faster vs naive addition on L=4096, 10x faster with precomputes.
76
+ * For N=254bit, L=1, it does: 1024 ADD + 254 DBL. For L=5: 1536 ADD + 254 DBL.
77
+ * Algorithmically constant-time (for same L), even when 1 point + scalar, or when scalar = 0.
78
+ * @param c Curve Point constructor
79
+ * @param fieldN field over CURVE.N - important that it's not over CURVE.P
80
+ * @param points array of L curve points
81
+ * @param scalars array of L scalars (aka private keys / bigints)
82
+ */
83
+ export declare function pippenger<T extends Group<T>>(c: GroupConstructor<T>, fieldN: IField<bigint>, points: T[], scalars: bigint[]): T;