@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
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright © 2023 Harmonic Laboratories
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,61 @@
1
- # Temporary Holding Version
1
+ # @harmoniclabs/crypto
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ collection of cryptographic functions that support every js runtime for ES5+
4
+
5
+ ## Disclaimer
6
+
7
+ The purpose of the library is to guarantee support for any javascript runtime with ES5+.
8
+
9
+ **THIS IMPLEMENTATION SHOULD NOT BE ASSUMED TO BE SAFE**
10
+
11
+ If you are in a node js environment you should use the [node crypto native module](https://nodejs.org/api/crypto.html); without doubt more preformant and secure.
12
+
13
+ ## vrf batch verification benchmarks
14
+
15
+ > NOTE: for batches with 38 elements or more the pippenger multiscalar multiplication should be used
16
+ >
17
+ > however it seems that there is something wrong wiht the implementation;
18
+ > since pippenger returns false where instead straus returns true (as it should be);
19
+ >
20
+ > for now then all batches sizes do use the straus algorithm, which is not optimal
21
+ >
22
+ > as you can see from the benchmarks, after size 32, the time/proof flattens
23
+ > (or get even worse in the case of bun)
24
+
25
+ ```
26
+ node --version
27
+ v22.13.1
28
+ ```
29
+
30
+ ```
31
+ size: 2; time: 36.216ms; time/proof: 18.108ms result: true
32
+ size: 4; time: 59.885ms; time/proof: 14.971ms result: true
33
+ size: 8; time: 105.061ms; time/proof: 13.133ms result: true
34
+ size: 16; time: 217.173ms; time/proof: 13.573ms result: true
35
+ size: 32; time: 397.940ms; time/proof: 12.436ms result: true
36
+ size: 64; time: 753.830ms; time/proof: 11.779ms result: true
37
+ size: 128; time: 1514.647ms; time/proof: 11.833ms result: true
38
+ size: 256; time: 3118.631ms; time/proof: 12.182ms result: true
39
+ size: 512; time: 6004.233ms; time/proof: 11.727ms result: true
40
+ size: 1024; time: 11948.110ms; time/proof: 11.668ms result: true
41
+ ```
42
+
43
+ > NOTE: bun performs better in basically all cryptography functions except for this specific batch vrf.
44
+
45
+ ```
46
+ bun --version
47
+ 1.1.13
48
+ ```
49
+
50
+ ```
51
+ size: 2; time: 68.439ms; time/proof: 34.219ms result: true
52
+ size: 4; time: 130.650ms; time/proof: 32.662ms result: true
53
+ size: 8; time: 254.191ms; time/proof: 31.774ms result: true
54
+ size: 16; time: 505.512ms; time/proof: 31.594ms result: true
55
+ size: 32; time: 1001.648ms; time/proof: 31.301ms result: true
56
+ size: 64; time: 1975.420ms; time/proof: 30.866ms result: true
57
+ size: 128; time: 4077.155ms; time/proof: 31.853ms result: true
58
+ size: 256; time: 9204.953ms; time/proof: 35.957ms result: true
59
+ size: 512; time: 18359.952ms; time/proof: 35.859ms result: true
60
+ size: 1024; time: 35875.629ms; time/proof: 35.035ms result: true
61
+ ```
@@ -0,0 +1,53 @@
1
+ import { byte, uint5 } from "./types.js";
2
+ /**
3
+ * Expand human readable prefix of the bech32 encoding so it can be used in the checkSum
4
+ */
5
+ export declare function expandBech32HumanReadablePart(hrp: string): byte[];
6
+ /**
7
+ * Used as part of the bech32 checksum.
8
+ */
9
+ export declare function getBech32Polymod(bytes: byte[]): number;
10
+ /**
11
+ * Generate the bech32 checksum
12
+ */
13
+ export declare function getBech32Checksum(humanReadablePart: string, data: uint5[]): [uint5, uint5, uint5, uint5, uint5, uint5];
14
+ /**
15
+ * Creates a bech32 checksummed string (used to represent Cardano addresses)
16
+ * @example
17
+ * encodeBech32("foo", textToBytes("foobar")) => "foo1vehk7cnpwgry9h96"
18
+ * @example
19
+ * encodeBech32("addr_test", hexToBytes("70a9508f015cfbcffc3d88ac4c1c934b5b82d2bb281d464672f6c49539")) => "addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld"
20
+ * @param {byte[]} data - uint8 0 - 256
21
+ */
22
+ export declare function encodeBech32(humanReadablePart: string, data: byte[] | Uint8Array): string;
23
+ /**
24
+ * Verify a bech32 checksum
25
+ * @example
26
+ * isBech32("foo1vehk7cnpwgry9h96") => true
27
+ * @example
28
+ * isBech32("foo1vehk7cnpwgry9h97") => false
29
+ * @example
30
+ * isBech32("a12uel5l") => true
31
+ * @example
32
+ * isBech32("mm1crxm3i") => false
33
+ * @example
34
+ * isBech32("A1G7SGD8") => false
35
+ * @example
36
+ * isBech32("abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw") => true
37
+ * @example
38
+ * isBech32("?1ezyfcl") => true
39
+ * @example
40
+ * isBech32("addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld") => true
41
+ * @param {string} addr
42
+ * @returns {boolean}
43
+ */
44
+ export declare function isBech32(addr: string): boolean;
45
+ /**
46
+ * Decomposes a bech32 checksummed string (i.e. Cardano address), and returns the human readable part and the original bytes
47
+ * Throws an error if checksum is invalid.
48
+ * @example
49
+ * bytesToHex(decodeBech32("addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld")[1]) => "70a9508f015cfbcffc3d88ac4c1c934b5b82d2bb281d464672f6c49539"
50
+ * @param {string} addr
51
+ * @returns {[humanReadablePart: string, bytes: byte[]]}
52
+ */
53
+ export declare function decodeBech32(addr: string): [humanReadablePart: string, bytes: byte[]];
package/dist/bech32.js ADDED
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ var __values = (this && this.__values) || function(o) {
3
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
4
+ if (m) return m.call(o);
5
+ if (o && typeof o.length === "number") return {
6
+ next: function () {
7
+ if (o && i >= o.length) o = void 0;
8
+ return { value: o && o[i++], done: !o };
9
+ }
10
+ };
11
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.decodeBech32 = exports.isBech32 = exports.encodeBech32 = exports.getBech32Checksum = exports.getBech32Polymod = exports.expandBech32HumanReadablePart = void 0;
15
+ var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
16
+ var types_1 = require("./types.js");
17
+ var base32_1 = require("./utils/base32.js");
18
+ var assert_1 = require("./utils/assert.js");
19
+ /**
20
+ * Expand human readable prefix of the bech32 encoding so it can be used in the checkSum
21
+ */
22
+ function expandBech32HumanReadablePart(hrp) {
23
+ var e_1, _a, e_2, _b;
24
+ var bytes = [];
25
+ try {
26
+ for (var hrp_1 = __values(hrp), hrp_1_1 = hrp_1.next(); !hrp_1_1.done; hrp_1_1 = hrp_1.next()) {
27
+ var c = hrp_1_1.value;
28
+ bytes.push(c.charCodeAt(0) >> 5);
29
+ }
30
+ }
31
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
32
+ finally {
33
+ try {
34
+ if (hrp_1_1 && !hrp_1_1.done && (_a = hrp_1.return)) _a.call(hrp_1);
35
+ }
36
+ finally { if (e_1) throw e_1.error; }
37
+ }
38
+ bytes.push(0);
39
+ try {
40
+ for (var hrp_2 = __values(hrp), hrp_2_1 = hrp_2.next(); !hrp_2_1.done; hrp_2_1 = hrp_2.next()) {
41
+ var c = hrp_2_1.value;
42
+ bytes.push(c.charCodeAt(0) & 31);
43
+ }
44
+ }
45
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
46
+ finally {
47
+ try {
48
+ if (hrp_2_1 && !hrp_2_1.done && (_b = hrp_2.return)) _b.call(hrp_2);
49
+ }
50
+ finally { if (e_2) throw e_2.error; }
51
+ }
52
+ return bytes;
53
+ }
54
+ exports.expandBech32HumanReadablePart = expandBech32HumanReadablePart;
55
+ var BECH32_POLYMOD_GEN = Object.freeze([0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]);
56
+ /**
57
+ * Used as part of the bech32 checksum.
58
+ */
59
+ function getBech32Polymod(bytes) {
60
+ var e_3, _a;
61
+ var checksum = 1;
62
+ try {
63
+ for (var bytes_1 = __values(bytes), bytes_1_1 = bytes_1.next(); !bytes_1_1.done; bytes_1_1 = bytes_1.next()) {
64
+ var byte_1 = bytes_1_1.value;
65
+ var c = (checksum >> 25);
66
+ checksum = (checksum & 0x1fffffff) << 5 ^ byte_1;
67
+ for (var i = 0; i < 5; i++) {
68
+ if (((c >> i) & 1) != 0) {
69
+ checksum ^= BECH32_POLYMOD_GEN[i];
70
+ }
71
+ }
72
+ }
73
+ }
74
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
75
+ finally {
76
+ try {
77
+ if (bytes_1_1 && !bytes_1_1.done && (_a = bytes_1.return)) _a.call(bytes_1);
78
+ }
79
+ finally { if (e_3) throw e_3.error; }
80
+ }
81
+ return checksum;
82
+ }
83
+ exports.getBech32Polymod = getBech32Polymod;
84
+ /**
85
+ * Generate the bech32 checksum
86
+ */
87
+ function getBech32Checksum(humanReadablePart, data) {
88
+ var checksum = getBech32Polymod(expandBech32HumanReadablePart(humanReadablePart)
89
+ .concat(data)
90
+ .concat([0, 0, 0, 0, 0, 0])) ^ 1;
91
+ var chkSum = [];
92
+ for (var i = 0; i < 6; i++) {
93
+ chkSum.push((checksum >> 5 * (5 - i)) & 31);
94
+ }
95
+ return chkSum;
96
+ }
97
+ exports.getBech32Checksum = getBech32Checksum;
98
+ /**
99
+ * Creates a bech32 checksummed string (used to represent Cardano addresses)
100
+ * @example
101
+ * encodeBech32("foo", textToBytes("foobar")) => "foo1vehk7cnpwgry9h96"
102
+ * @example
103
+ * encodeBech32("addr_test", hexToBytes("70a9508f015cfbcffc3d88ac4c1c934b5b82d2bb281d464672f6c49539")) => "addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld"
104
+ * @param {byte[]} data - uint8 0 - 256
105
+ */
106
+ function encodeBech32(humanReadablePart, data) {
107
+ (0, assert_1.assert)(humanReadablePart.length > 0, "human-readable-part must have non-zero length");
108
+ var _data = (0, types_1.buffToUint5Arr)((0, uint8array_utils_1.isUint8Array)(data) ? data : new Uint8Array(data));
109
+ return humanReadablePart + "1" +
110
+ _data.concat(getBech32Checksum(humanReadablePart, _data)).map(function (val) { return base32_1.BECH32_BASE32_ALPHABET[val]; }).join("");
111
+ }
112
+ exports.encodeBech32 = encodeBech32;
113
+ /**
114
+ * Verify a bech32 checksum
115
+ * @example
116
+ * isBech32("foo1vehk7cnpwgry9h96") => true
117
+ * @example
118
+ * isBech32("foo1vehk7cnpwgry9h97") => false
119
+ * @example
120
+ * isBech32("a12uel5l") => true
121
+ * @example
122
+ * isBech32("mm1crxm3i") => false
123
+ * @example
124
+ * isBech32("A1G7SGD8") => false
125
+ * @example
126
+ * isBech32("abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw") => true
127
+ * @example
128
+ * isBech32("?1ezyfcl") => true
129
+ * @example
130
+ * isBech32("addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld") => true
131
+ * @param {string} addr
132
+ * @returns {boolean}
133
+ */
134
+ function isBech32(addr) {
135
+ var e_4, _a;
136
+ var i = addr.lastIndexOf("1");
137
+ if (i == -1 || i == 0) {
138
+ return false;
139
+ }
140
+ var hrp = addr.slice(0, i);
141
+ addr = addr.slice(i + 1);
142
+ var data = [];
143
+ try {
144
+ for (var addr_1 = __values(addr), addr_1_1 = addr_1.next(); !addr_1_1.done; addr_1_1 = addr_1.next()) {
145
+ var ch = addr_1_1.value;
146
+ var j = base32_1.BECH32_BASE32_ALPHABET.indexOf(ch);
147
+ if (j == -1) {
148
+ return false;
149
+ }
150
+ data.push(j);
151
+ }
152
+ }
153
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
154
+ finally {
155
+ try {
156
+ if (addr_1_1 && !addr_1_1.done && (_a = addr_1.return)) _a.call(addr_1);
157
+ }
158
+ finally { if (e_4) throw e_4.error; }
159
+ }
160
+ var chkSumA = data.slice(data.length - 6);
161
+ var chkSumB = getBech32Checksum(hrp, data.slice(0, data.length - 6));
162
+ for (var i_1 = 0; i_1 < 6; i_1++) {
163
+ if (chkSumA[i_1] != chkSumB[i_1]) {
164
+ return false;
165
+ }
166
+ }
167
+ return true;
168
+ }
169
+ exports.isBech32 = isBech32;
170
+ /**
171
+ * Decomposes a bech32 checksummed string (i.e. Cardano address), and returns the human readable part and the original bytes
172
+ * Throws an error if checksum is invalid.
173
+ * @example
174
+ * bytesToHex(decodeBech32("addr_test1wz54prcptnaullpa3zkyc8ynfddc954m9qw5v3nj7mzf2wggs2uld")[1]) => "70a9508f015cfbcffc3d88ac4c1c934b5b82d2bb281d464672f6c49539"
175
+ * @param {string} addr
176
+ * @returns {[humanReadablePart: string, bytes: byte[]]}
177
+ */
178
+ function decodeBech32(addr) {
179
+ (0, assert_1.assert)(isBech32(addr), "invalid bech32 addr");
180
+ var i = addr.lastIndexOf("1");
181
+ (0, assert_1.assert)(i != -1, "bech32 address missing the '1' separator");
182
+ var hrp = addr.slice(0, i);
183
+ addr = addr.slice(i + 1);
184
+ var data = (0, base32_1.decodeBase32Bech32)(addr.slice(0, addr.length - 6));
185
+ return [hrp, Array.from(data)];
186
+ }
187
+ exports.decodeBech32 = decodeBech32;
@@ -0,0 +1,7 @@
1
+ import type { byte } from "./types.js";
2
+ export type SupportedDigestSize = 16 | 28 | 32 | 64;
3
+ export declare function blake2b(data: byte[] | Uint8Array, digestSize?: SupportedDigestSize): Uint8Array;
4
+ export declare function blake2b_128(data: byte[] | Uint8Array): Uint8Array;
5
+ export declare function blake2b_224(data: byte[] | Uint8Array): Uint8Array;
6
+ export declare function blake2b_256(data: byte[] | Uint8Array): Uint8Array;
7
+ export declare function blake2b_512(data: byte[] | Uint8Array): Uint8Array;
@@ -0,0 +1,213 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.blake2b_512 = exports.blake2b_256 = exports.blake2b_224 = exports.blake2b_128 = exports.blake2b = void 0;
4
+ /**
5
+ * 64-bit unsigned addition
6
+ * Sets v[a,a+1] += v[b,b+1]
7
+ * v should be a Uint32Array
8
+ */
9
+ function ADD64AA(v, a, b) {
10
+ var o0 = v[a] + v[b];
11
+ var o1 = v[a + 1] + v[b + 1];
12
+ if (o0 >= 0x100000000) {
13
+ o1++;
14
+ }
15
+ v[a] = o0;
16
+ v[a + 1] = o1;
17
+ }
18
+ // 64-bit unsigned addition
19
+ // Sets v[a,a+1] += b
20
+ // b0 is the low 32 bits of b, b1 represents the high 32 bits
21
+ function ADD64AC(v, a, b0, b1) {
22
+ var o0 = v[a] + b0;
23
+ if (b0 < 0) {
24
+ o0 += 0x100000000;
25
+ }
26
+ var o1 = v[a + 1] + b1;
27
+ if (o0 >= 0x100000000) {
28
+ o1++;
29
+ }
30
+ v[a] = o0;
31
+ v[a + 1] = o1;
32
+ }
33
+ // Little-endian byte access
34
+ function B2B_GET32(arr, i) {
35
+ return arr[i] ^ (arr[i + 1] << 8) ^ (arr[i + 2] << 16) ^ (arr[i + 3] << 24);
36
+ }
37
+ // G Mixing function
38
+ // The ROTRs are inlined for speed
39
+ function B2B_G(a, b, c, d, ix, iy) {
40
+ var x0 = m[ix];
41
+ var x1 = m[ix + 1];
42
+ var y0 = m[iy];
43
+ var y1 = m[iy + 1];
44
+ ADD64AA(v, a, b); // v[a,a+1] += v[b,b+1] ... in JS we must store a uint64 as two uint32s
45
+ ADD64AC(v, a, x0, x1); // v[a, a+1] += x ... x0 is the low 32 bits of x, x1 is the high 32 bits
46
+ // v[d,d+1] = (v[d,d+1] xor v[a,a+1]) rotated to the right by 32 bits
47
+ var xor0 = v[d] ^ v[a];
48
+ var xor1 = v[d + 1] ^ v[a + 1];
49
+ v[d] = xor1;
50
+ v[d + 1] = xor0;
51
+ ADD64AA(v, c, d);
52
+ // v[b,b+1] = (v[b,b+1] xor v[c,c+1]) rotated right by 24 bits
53
+ xor0 = v[b] ^ v[c];
54
+ xor1 = v[b + 1] ^ v[c + 1];
55
+ v[b] = (xor0 >>> 24) ^ (xor1 << 8);
56
+ v[b + 1] = (xor1 >>> 24) ^ (xor0 << 8);
57
+ ADD64AA(v, a, b);
58
+ ADD64AC(v, a, y0, y1);
59
+ // v[d,d+1] = (v[d,d+1] xor v[a,a+1]) rotated right by 16 bits
60
+ xor0 = v[d] ^ v[a];
61
+ xor1 = v[d + 1] ^ v[a + 1];
62
+ v[d] = (xor0 >>> 16) ^ (xor1 << 16);
63
+ v[d + 1] = (xor1 >>> 16) ^ (xor0 << 16);
64
+ ADD64AA(v, c, d);
65
+ // v[b,b+1] = (v[b,b+1] xor v[c,c+1]) rotated right by 63 bits
66
+ xor0 = v[b] ^ v[c];
67
+ xor1 = v[b + 1] ^ v[c + 1];
68
+ v[b] = (xor1 >>> 31) ^ (xor0 << 1);
69
+ v[b + 1] = (xor0 >>> 31) ^ (xor1 << 1);
70
+ }
71
+ // Initialization Vector
72
+ var BLAKE2B_IV32 = new Uint32Array([
73
+ 0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372,
74
+ 0x5f1d36f1, 0xa54ff53a, 0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c,
75
+ 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19
76
+ ]);
77
+ var SIGMA8 = [
78
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 10, 4, 8, 9, 15, 13,
79
+ 6, 1, 12, 0, 2, 11, 7, 5, 3, 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1,
80
+ 9, 4, 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8, 9, 0, 5, 7, 2, 4,
81
+ 10, 15, 14, 1, 11, 12, 6, 8, 3, 13, 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5,
82
+ 15, 14, 1, 9, 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11, 13, 11, 7,
83
+ 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10, 6, 15, 14, 9, 11, 3, 0, 8, 12, 2,
84
+ 13, 7, 1, 4, 10, 5, 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0, 0,
85
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 10, 4, 8, 9, 15, 13, 6,
86
+ 1, 12, 0, 2, 11, 7, 5, 3
87
+ ];
88
+ // These are offsets into a uint64 buffer.
89
+ // Multiply them all by 2 to make them offsets into a uint32 buffer,
90
+ // because this is Javascript and we don't have uint64s
91
+ var SIGMA82 = new Uint8Array(SIGMA8.map(function (x) {
92
+ return x * 2;
93
+ }));
94
+ // Compression function. 'last' flag indicates last block.
95
+ // Note we're representing 16 uint64s as 32 uint32s
96
+ var v = new Uint32Array(32);
97
+ var m = new Uint32Array(32);
98
+ function blake2bCompress(ctx, last) {
99
+ var i = 0;
100
+ // init work variables
101
+ for (i = 0; i < 16; i++) {
102
+ v[i] = ctx.h[i];
103
+ v[i + 16] = BLAKE2B_IV32[i];
104
+ }
105
+ // low 64 bits of offset
106
+ v[24] = v[24] ^ ctx.t;
107
+ v[25] = v[25] ^ (ctx.t / 0x100000000);
108
+ // high 64 bits not supported, offset may not be higher than 2**53-1
109
+ // last block flag set ?
110
+ if (last) {
111
+ v[28] = ~v[28];
112
+ v[29] = ~v[29];
113
+ }
114
+ // get little-endian words
115
+ for (i = 0; i < 32; i++) {
116
+ m[i] = B2B_GET32(ctx.b, 4 * i);
117
+ }
118
+ // twelve rounds of mixing
119
+ // uncomment the DebugPrint calls to log the computation
120
+ // and match the RFC sample documentation
121
+ // util.debugPrint(' m[16]', m, 64)
122
+ for (i = 0; i < 12; i++) {
123
+ // util.debugPrint(' (i=' + (i < 10 ? ' ' : '') + i + ') v[16]', v, 64)
124
+ B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]);
125
+ B2B_G(2, 10, 18, 26, SIGMA82[i * 16 + 2], SIGMA82[i * 16 + 3]);
126
+ B2B_G(4, 12, 20, 28, SIGMA82[i * 16 + 4], SIGMA82[i * 16 + 5]);
127
+ B2B_G(6, 14, 22, 30, SIGMA82[i * 16 + 6], SIGMA82[i * 16 + 7]);
128
+ B2B_G(0, 10, 20, 30, SIGMA82[i * 16 + 8], SIGMA82[i * 16 + 9]);
129
+ B2B_G(2, 12, 22, 24, SIGMA82[i * 16 + 10], SIGMA82[i * 16 + 11]);
130
+ B2B_G(4, 14, 16, 26, SIGMA82[i * 16 + 12], SIGMA82[i * 16 + 13]);
131
+ B2B_G(6, 8, 18, 28, SIGMA82[i * 16 + 14], SIGMA82[i * 16 + 15]);
132
+ }
133
+ // util.debugPrint(' (i=12) v[16]', v, 64)
134
+ for (i = 0; i < 16; i++) {
135
+ ctx.h[i] = ctx.h[i] ^ v[i] ^ v[i + 16];
136
+ }
137
+ // util.debugPrint('h[8]', ctx.h, 64)
138
+ }
139
+ /** reusable parameterBlock */
140
+ var parameterBlock = new Uint8Array(64).fill(0);
141
+ function blake2bInit(digestSize) {
142
+ // state, 'param block'
143
+ var ctx = {
144
+ b: new Uint8Array(128),
145
+ h: new Uint32Array(16),
146
+ t: 0,
147
+ c: 0,
148
+ digestSize: digestSize // output length in bytes
149
+ };
150
+ // initialize parameterBlock before usage
151
+ parameterBlock.fill(0);
152
+ parameterBlock[0] = digestSize;
153
+ parameterBlock[2] = 1; // fanout
154
+ parameterBlock[3] = 1; // depth
155
+ // initialize hash state
156
+ for (var i = 0; i < 16; i++) {
157
+ ctx.h[i] = BLAKE2B_IV32[i] ^ B2B_GET32(parameterBlock, i * 4);
158
+ }
159
+ return ctx;
160
+ }
161
+ // Updates a BLAKE2b streaming hash
162
+ // Requires hash context and Uint8Array (byte array)
163
+ function blake2bUpdate(ctx, input) {
164
+ for (var i = 0; i < input.length; i++) {
165
+ if (ctx.c === 128) {
166
+ // buffer full ?
167
+ ctx.t += ctx.c; // add counters
168
+ blake2bCompress(ctx, false); // compress (not last)
169
+ ctx.c = 0; // counter to zero
170
+ }
171
+ ctx.b[ctx.c++] = input[i];
172
+ }
173
+ }
174
+ // Completes a BLAKE2b streaming hash
175
+ // Returns a Uint8Array containing the message digest
176
+ function blake2bFinal(ctx) {
177
+ ctx.t += ctx.c; // mark last block offset
178
+ while (ctx.c < 128) {
179
+ // fill up with zeros
180
+ ctx.b[ctx.c++] = 0;
181
+ }
182
+ blake2bCompress(ctx, true); // final block flag = 1
183
+ // little endian convert and store
184
+ var out = new Uint8Array(ctx.digestSize);
185
+ for (var i = 0; i < ctx.digestSize; i++) {
186
+ out[i] = ctx.h[i >> 2] >> (8 * (i & 3));
187
+ }
188
+ return out;
189
+ }
190
+ function blake2b(data, digestSize) {
191
+ if (digestSize === void 0) { digestSize = 32; }
192
+ data = new Uint8Array(data);
193
+ var ctx = blake2bInit(digestSize);
194
+ blake2bUpdate(ctx, data);
195
+ return blake2bFinal(ctx);
196
+ }
197
+ exports.blake2b = blake2b;
198
+ function blake2b_128(data) {
199
+ return blake2b(data, 16);
200
+ }
201
+ exports.blake2b_128 = blake2b_128;
202
+ function blake2b_224(data) {
203
+ return blake2b(data, 28);
204
+ }
205
+ exports.blake2b_224 = blake2b_224;
206
+ function blake2b_256(data) {
207
+ return blake2b(data, 32);
208
+ }
209
+ exports.blake2b_256 = blake2b_256;
210
+ function blake2b_512(data) {
211
+ return blake2b(data, 64);
212
+ }
213
+ exports.blake2b_512 = blake2b_512;
@@ -0,0 +1,48 @@
1
+ import { ProjConstructor } from "./noble/curves/weierstrass.js";
2
+ export declare const BlsG1: ProjConstructor<bigint>;
3
+ export declare const BlsG2: ProjConstructor<{
4
+ c0: bigint;
5
+ c1: bigint;
6
+ }>;
7
+ export declare function isBlsG1(stuff: any): stuff is BlsG1;
8
+ export declare function isBlsG2(stuff: any): stuff is BlsG2;
9
+ type UnwrapProjConstructor<Stuff extends ProjConstructor<any>> = Stuff extends ProjConstructor<infer T> ? T : never;
10
+ export type Fp2 = UnwrapProjConstructor<typeof BlsG2>;
11
+ export declare function isFp2(stuff: any): stuff is Fp2;
12
+ type ConstructorReturnType<CtorLike extends {
13
+ new (...args: any): any;
14
+ }> = CtorLike extends {
15
+ new (...args: any): infer ReturnT;
16
+ } ? ReturnT : never;
17
+ export type BlsG1 = ConstructorReturnType<typeof BlsG1>;
18
+ export type BlsG2 = ConstructorReturnType<typeof BlsG2>;
19
+ export declare function bls12_381_G1_add(a: BlsG1, b: BlsG1): BlsG1;
20
+ export declare function bls12_381_G1_neg(elem: BlsG1): BlsG1;
21
+ export declare function bls12_381_G1_scalarMul(n: number | bigint, g1: BlsG1): BlsG1;
22
+ export declare function bls12_381_G1_equal(a: BlsG1, b: BlsG1): boolean;
23
+ export declare function bls12_381_G1_hashToGroup(a: Uint8Array, b: Uint8Array): BlsG1;
24
+ export declare function bls12_381_G1_compress(elem: BlsG1): Uint8Array;
25
+ export declare function bls12_381_G1_uncompress(compressed: Uint8Array): BlsG1;
26
+ export declare function bls12_381_G2_add(a: BlsG2, b: BlsG2): BlsG2;
27
+ export declare function bls12_381_G2_neg(elem: BlsG2): BlsG2;
28
+ export declare function bls12_381_G2_scalarMul(n: number | bigint, g2: BlsG2): BlsG2;
29
+ export declare function bls12_381_G2_equal(a: BlsG2, b: BlsG2): boolean;
30
+ export declare function bls12_381_G2_hashToGroup(a: Uint8Array, b: Uint8Array): BlsG2;
31
+ export declare function bls12_381_G2_compress(elem: BlsG2): Uint8Array;
32
+ export declare function bls12_381_G2_uncompress(compressed: Uint8Array): BlsG2;
33
+ export declare function bls12_381_millerLoop(g1: BlsG1, g2: BlsG2): BlsResult;
34
+ export declare function bls12_381_mulMlResult(a: BlsResult, b: BlsResult): BlsResult;
35
+ export declare const bls12_381_eqMlResult: (a: BlsResult, b: BlsResult) => boolean;
36
+ export declare function bls12_381_finalVerify(a: BlsResult, b: BlsResult): boolean;
37
+ export type BlsResult = {
38
+ c0: Fp6_t;
39
+ c1: Fp6_t;
40
+ };
41
+ export declare function isBlsResult(stuff: any): stuff is BlsResult;
42
+ type Fp6_t = {
43
+ c0: Fp2;
44
+ c1: Fp2;
45
+ c2: Fp2;
46
+ };
47
+ export declare function isFp6(stuff: any): stuff is Fp6_t;
48
+ export {};