@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/dist/sha3.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import { byte } from "./types.js";
2
+ /**
3
+ * getulates sha3-256 (32bytes) hash of a list of uint8 numbers.
4
+ * Result is also a list of uint8 number.
5
+ * Sha3 only bit-wise operations, so 64-bit operations can easily be replicated using 2 32-bit operations instead
6
+ * @example
7
+ * bytesToHex(sha3(textToBytes("abc"))) => "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"
8
+ * @example
9
+ * bytesToHex(sha3((new Array(136)).fill(1))) => "b36dc2167c4d9dda1a58b87046c8d76a6359afe3612c4de8a38857e09117b2db"
10
+ * @example
11
+ * bytesToHex(sha3((new Array(135)).fill(2))) => "5bdf5d815d29a9d7161c66520efc17c2edd7898f2b99a029e8d2e4ff153407f4"
12
+ * @example
13
+ * bytesToHex(sha3((new Array(134)).fill(3))) => "8e6575663dfb75a88f94a32c5b363c410278b65020734560d968aadd6896a621"
14
+ * @example
15
+ * bytesToHex(sha3((new Array(137)).fill(4))) => "f10b39c3e455006aa42120b9751faa0f35c821211c9d086beb28bf3c4134c6c6"
16
+ */
17
+ export declare function sha3(bytes: byte[] | Uint8Array): byte[];
package/dist/sha3.js ADDED
@@ -0,0 +1,181 @@
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.sha3 = void 0;
29
+ var types_1 = require("./types.js");
30
+ var types_2 = require("./types.js");
31
+ var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
32
+ var assert_1 = require("./utils/assert.js");
33
+ /**
34
+ * state width (1600 bits, )
35
+ */
36
+ var WIDTH = 200;
37
+ /**
38
+ * rate (1088 bits, 136 bytes)
39
+ */
40
+ var RATE = 136;
41
+ /**
42
+ * capacity
43
+ */
44
+ var CAP = WIDTH - RATE;
45
+ /**
46
+ * Apply 1000...1 padding until size is multiple of r.
47
+ * If already multiple of r then add a whole block of padding.
48
+ * @param {number[]} src - list of uint8 numbers
49
+ * @returns {number[]} - list of uint8 numbers
50
+ */
51
+ function pad(src) {
52
+ var dst = src.slice();
53
+ var nZeroes = RATE - 2 - (dst.length % RATE);
54
+ if (nZeroes < -1) {
55
+ nZeroes += RATE - 2;
56
+ }
57
+ if (nZeroes == -1) {
58
+ dst.push(0x86);
59
+ }
60
+ else {
61
+ dst.push(0x06);
62
+ for (var i = 0; i < nZeroes; i++) {
63
+ dst.push(0);
64
+ }
65
+ dst.push(0x80);
66
+ }
67
+ (0, assert_1.assert)((dst.length % RATE) === 0, "wrong destination length");
68
+ return dst;
69
+ }
70
+ /**
71
+ * 24 numbers used in the sha3 permute function
72
+ */
73
+ var OFFSETS = Object.freeze([6, 12, 18, 24, 3, 9, 10, 16, 22, 1, 7, 13, 19, 20, 4, 5, 11, 17, 23, 2, 8, 14, 15, 21]);
74
+ /**
75
+ * 24 numbers used in the sha3 permute function
76
+ */
77
+ var SHIFTS = Object.freeze([-12, -11, 21, 14, 28, 20, 3, -13, -29, 1, 6, 25, 8, 18, 27, -4, 10, 15, -24, -30, -23, -7, -9, 2]);
78
+ /**
79
+ * Round constants used in the sha3 permute function
80
+ */
81
+ var RC = Object.freeze([
82
+ (0, types_2.uint64)("0x0000000000000001"),
83
+ (0, types_2.uint64)("0x0000000000008082"),
84
+ (0, types_2.uint64)("0x800000000000808a"),
85
+ (0, types_2.uint64)("0x8000000080008000"),
86
+ (0, types_2.uint64)("0x000000000000808b"),
87
+ (0, types_2.uint64)("0x0000000080000001"),
88
+ (0, types_2.uint64)("0x8000000080008081"),
89
+ (0, types_2.uint64)("0x8000000000008009"),
90
+ (0, types_2.uint64)("0x000000000000008a"),
91
+ (0, types_2.uint64)("0x0000000000000088"),
92
+ (0, types_2.uint64)("0x0000000080008009"),
93
+ (0, types_2.uint64)("0x000000008000000a"),
94
+ (0, types_2.uint64)("0x000000008000808b"),
95
+ (0, types_2.uint64)("0x800000000000008b"),
96
+ (0, types_2.uint64)("0x8000000000008089"),
97
+ (0, types_2.uint64)("0x8000000000008003"),
98
+ (0, types_2.uint64)("0x8000000000008002"),
99
+ (0, types_2.uint64)("0x8000000000000080"),
100
+ (0, types_2.uint64)("0x000000000000800a"),
101
+ (0, types_2.uint64)("0x800000008000000a"),
102
+ (0, types_2.uint64)("0x8000000080008081"),
103
+ (0, types_2.uint64)("0x8000000000008080"),
104
+ (0, types_2.uint64)("0x0000000080000001"),
105
+ (0, types_2.uint64)("0x8000000080008008"),
106
+ ]);
107
+ function permute(s) {
108
+ var c = new Array(5);
109
+ var b = new Array(25);
110
+ for (var round = 0; round < 24; round++) {
111
+ for (var i = 0; i < 5; i++) {
112
+ c[i] = (0, types_2.uint64)((((s[i] ^ s[i + 5]) ^ s[i + 10]) ^ s[i + 15]) ^ s[i + 20]);
113
+ }
114
+ for (var i = 0; i < 5; i++) {
115
+ var tmp = (0, types_2.uint64)(c[(i + 4) % 5] ^ (0, types_2.uint64Rotr)(c[(i + 1) % 5], 63));
116
+ for (var j = 0; j < 5; j++) {
117
+ s[i + 5 * j] = (0, types_2.uint64)(s[i + 5 * j] ^ tmp);
118
+ }
119
+ }
120
+ b[0] = s[0];
121
+ for (var i = 1; i < 25; i++) {
122
+ var offset = OFFSETS[i - 1];
123
+ var left = Math.abs(SHIFTS[i - 1]);
124
+ var right = (32 - left);
125
+ if (SHIFTS[i - 1] < 0) {
126
+ b[i] = (0, types_2.uint64Rotr)(s[offset], right);
127
+ }
128
+ else {
129
+ b[i] = (0, types_2.uint64Rotr)(s[offset], (right + 32));
130
+ }
131
+ }
132
+ for (var i = 0; i < 5; i++) {
133
+ for (var j = 0; j < 5; j++) {
134
+ s[i * 5 + j] = (0, types_2.uint64)(b[i * 5 + j] ^ ((~b[i * 5 + (j + 1) % 5]) & b[i * 5 + (j + 2) % 5]));
135
+ }
136
+ }
137
+ s[0] = (s[0] ^ RC[round]);
138
+ }
139
+ }
140
+ /**
141
+ * getulates sha3-256 (32bytes) hash of a list of uint8 numbers.
142
+ * Result is also a list of uint8 number.
143
+ * Sha3 only bit-wise operations, so 64-bit operations can easily be replicated using 2 32-bit operations instead
144
+ * @example
145
+ * bytesToHex(sha3(textToBytes("abc"))) => "3a985da74fe225b2045c172d6bd390bd855f086e3e9d525b46bfe24511431532"
146
+ * @example
147
+ * bytesToHex(sha3((new Array(136)).fill(1))) => "b36dc2167c4d9dda1a58b87046c8d76a6359afe3612c4de8a38857e09117b2db"
148
+ * @example
149
+ * bytesToHex(sha3((new Array(135)).fill(2))) => "5bdf5d815d29a9d7161c66520efc17c2edd7898f2b99a029e8d2e4ff153407f4"
150
+ * @example
151
+ * bytesToHex(sha3((new Array(134)).fill(3))) => "8e6575663dfb75a88f94a32c5b363c410278b65020734560d968aadd6896a621"
152
+ * @example
153
+ * bytesToHex(sha3((new Array(137)).fill(4))) => "f10b39c3e455006aa42120b9751faa0f35c821211c9d086beb28bf3c4134c6c6"
154
+ */
155
+ function sha3(bytes) {
156
+ if ((0, uint8array_utils_1.isUint8Array)(bytes)) {
157
+ bytes = (0, types_1.buffToByteArr)(bytes);
158
+ }
159
+ bytes = pad(bytes);
160
+ // initialize the state
161
+ var state = (new Array(WIDTH / 8)).fill((0, types_2.uint64)(0));
162
+ for (var chunkStart = 0; chunkStart < bytes.length; chunkStart += RATE) {
163
+ // extend the chunk to become length WIDTH
164
+ var chunk = bytes.slice(chunkStart, chunkStart + RATE).concat((new Array(CAP)).fill(0));
165
+ // element-wise xor with 'state'
166
+ for (var i = 0; i < WIDTH; i += 8) {
167
+ state[i / 8] = (0, types_2.uint64)(state[i / 8] ^
168
+ (0, types_2.uint64)("0x" + (0, types_2.byteArrToHex)(chunk.slice(i, i + 8).reverse())));
169
+ // beware: a uint32 is stored as little endian, but a pair of uint32s that form a uin64 are stored in big endian format!
170
+ //state[i/4] ^= (chunk[i] << 0) | (chunk[i+1] << 8) | (chunk[i+2] << 16) | (chunk[i+3] << 24);
171
+ }
172
+ // apply block permutations
173
+ permute(state);
174
+ }
175
+ var hash = [];
176
+ for (var i = 0; i < 4; i++) {
177
+ hash.push.apply(hash, __spreadArray([], __read((0, types_2.uint64ToBytesLE)(state[i])), false));
178
+ }
179
+ return hash;
180
+ }
181
+ exports.sha3 = sha3;
@@ -0,0 +1,26 @@
1
+ export type uint5 = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31;
2
+ export declare function isUint5(n: number): n is uint6;
3
+ export type uint6 = uint5 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63;
4
+ export declare function isUint6(n: number): n is uint6;
5
+ export type byte = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255;
6
+ export declare function byte(bint: bigint | number): byte;
7
+ export declare function isByte(n: number): n is byte;
8
+ export declare function isByteArr(something: any): something is byte[];
9
+ export declare function buffToByteArr(buff: Uint8Array): byte[];
10
+ export declare function byteArrToHex(bytes: byte[] | Uint8Array): string;
11
+ export declare function byteArrToBin(bytes: byte[]): string;
12
+ /**
13
+ * Internal method
14
+ *
15
+ * `bytes` is **padded at the end** to be a multiple of 5
16
+ */
17
+ export declare function buffToUint5Arr(bytes: Uint8Array | byte[]): uint5[];
18
+ export type uint64 = bigint & {
19
+ __uint64__: never;
20
+ };
21
+ export declare function uint64(n: string | number | bigint | boolean): uint64;
22
+ export declare function forceUint64(n: string | number | bigint | boolean): uint64;
23
+ export declare function isUint64(n: bigint): n is uint64;
24
+ export declare function uint64ToBytesLE(uint: uint64): [byte, byte, byte, byte, byte, byte, byte, byte];
25
+ export declare function uint64ToBytesBE(uint: uint64): [byte, byte, byte, byte, byte, byte, byte, byte];
26
+ export declare function uint64Rotr(a: uint64, b: uint6): uint64;
package/dist/types.js ADDED
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uint64Rotr = exports.uint64ToBytesBE = exports.uint64ToBytesLE = exports.isUint64 = exports.forceUint64 = exports.uint64 = exports.buffToUint5Arr = exports.byteArrToBin = exports.byteArrToHex = exports.buffToByteArr = exports.isByteArr = exports.isByte = exports.byte = exports.isUint6 = exports.isUint5 = void 0;
4
+ var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
5
+ var assert_1 = require("./utils/assert.js");
6
+ function isUint5(n) {
7
+ return (typeof n === "number" &&
8
+ n >= 0 && n <= 31 &&
9
+ n === Math.round(n));
10
+ }
11
+ exports.isUint5 = isUint5;
12
+ function isUint6(n) {
13
+ return (typeof n === "number" &&
14
+ n >= 0 && n <= 63 &&
15
+ n === Math.round(n));
16
+ }
17
+ exports.isUint6 = isUint6;
18
+ function byte(bint) {
19
+ return (Number(bint) & 0xff);
20
+ }
21
+ exports.byte = byte;
22
+ function isByte(n) {
23
+ return (typeof n === "number" &&
24
+ n >= 0 && n <= 255 &&
25
+ n === Math.round(n));
26
+ }
27
+ exports.isByte = isByte;
28
+ function isByteArr(something) {
29
+ return (Array.isArray(something) &&
30
+ something.every(isByte));
31
+ }
32
+ exports.isByteArr = isByteArr;
33
+ function buffToByteArr(buff) {
34
+ if (!(0, uint8array_utils_1.isUint8Array)(buff)) {
35
+ if (isByteArr(buff))
36
+ return buff.slice();
37
+ throw new Error("can't convert non-buffer to byte array");
38
+ }
39
+ return Array.from(buff);
40
+ }
41
+ exports.buffToByteArr = buffToByteArr;
42
+ function byteArrToHex(bytes) {
43
+ if (bytes instanceof Uint8Array)
44
+ bytes = Array.from(bytes);
45
+ return bytes.reduce(function (acc, val) { return acc + val.toString(16).slice(0, 2).padStart(2, '0'); }, '');
46
+ }
47
+ exports.byteArrToHex = byteArrToHex;
48
+ function byteArrToBin(bytes) {
49
+ return bytes.reduce(function (acc, val) { return acc + val.toString(2).slice(0, 8).padStart(8, '0'); }, '');
50
+ }
51
+ exports.byteArrToBin = byteArrToBin;
52
+ /**
53
+ * Internal method
54
+ *
55
+ * `bytes` is **padded at the end** to be a multiple of 5
56
+ */
57
+ function buffToUint5Arr(bytes) {
58
+ var result = [];
59
+ var bits = (0, uint8array_utils_1.isUint8Array)(bytes) ?
60
+ Array.from(bytes).reduce(function (acc, n) { return acc + n.toString(2).padStart(8, '0'); }, "") :
61
+ (isByteArr(bytes) ?
62
+ byteArrToBin(bytes) :
63
+ undefined);
64
+ if (bits === undefined) {
65
+ throw new Error("invalid input to convert ot uint5 array");
66
+ }
67
+ var mod5Len = bits.length % 5;
68
+ if (mod5Len !== 0) {
69
+ bits = bits.padEnd(bits.length + (5 - mod5Len), '0');
70
+ }
71
+ bits = bits.split('');
72
+ for (var i = 0; i < bits.length;) {
73
+ result.push(Number("0b".concat(bits[i++]).concat(bits[i++]).concat(bits[i++]).concat(bits[i++]).concat(bits[i++])));
74
+ }
75
+ return result;
76
+ }
77
+ exports.buffToUint5Arr = buffToUint5Arr;
78
+ function uint64(n) {
79
+ var _n = BigInt(n);
80
+ if (!isUint64(_n))
81
+ throw new Error("can't convert " + n + " to uint64");
82
+ return _n;
83
+ }
84
+ exports.uint64 = uint64;
85
+ function forceUint64(n) {
86
+ return (BigInt(n) & BigInt("0x" + "ff".repeat(8)));
87
+ }
88
+ exports.forceUint64 = forceUint64;
89
+ function isUint64(n) {
90
+ return (typeof n === "bigint" &&
91
+ n >= BigInt(0) &&
92
+ n < BigInt("0x1" + "00".repeat(8)) // n < (1 << 64)
93
+ );
94
+ }
95
+ exports.isUint64 = isUint64;
96
+ function uint64ToBytesLE(uint) {
97
+ return [
98
+ byte(BigInt("0x00000000000000ff") & uint),
99
+ byte((BigInt("0x000000000000ff00") & uint) >> BigInt(8)),
100
+ byte((BigInt("0x0000000000ff0000") & uint) >> BigInt(16)),
101
+ byte((BigInt("0x00000000ff000000") & uint) >> BigInt(24)),
102
+ byte((BigInt("0x000000ff00000000") & uint) >> BigInt(32)),
103
+ byte((BigInt("0x0000ff0000000000") & uint) >> BigInt(40)),
104
+ byte((BigInt("0x00ff000000000000") & uint) >> BigInt(48)),
105
+ byte((BigInt("0xff00000000000000") & uint) >> BigInt(56)),
106
+ ];
107
+ }
108
+ exports.uint64ToBytesLE = uint64ToBytesLE;
109
+ function uint64ToBytesBE(uint) {
110
+ return uint64ToBytesLE(uint).reverse();
111
+ }
112
+ exports.uint64ToBytesBE = uint64ToBytesBE;
113
+ function uint64Rotr(a, b) {
114
+ (0, assert_1.assert)(isUint64(a) && isUint6(b), "invalid args for 'uint64And'");
115
+ if (b === 0)
116
+ return a;
117
+ var n = BigInt(b);
118
+ return forceUint64(forceUint64(a >> n) | forceUint64(a << (BigInt(64) - n)));
119
+ }
120
+ exports.uint64Rotr = uint64Rotr;
@@ -0,0 +1,3 @@
1
+ export declare function assert(condition: boolean, errorMessage: string | Error, addInfos?: any, ...args: any[]): void;
2
+ export declare function assertPostiveInteger(n: any): void;
3
+ export declare function assertBytes(stuff: any): void;
@@ -0,0 +1,55 @@
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.assertBytes = exports.assertPostiveInteger = exports.assert = void 0;
29
+ function assert(condition, errorMessage, addInfos) {
30
+ var args = [];
31
+ for (var _i = 3; _i < arguments.length; _i++) {
32
+ args[_i - 3] = arguments[_i];
33
+ }
34
+ if (condition)
35
+ return;
36
+ args.length > 0 && console.error.apply(console, __spreadArray([], __read(args), false));
37
+ addInfos && console.error(addInfos);
38
+ if (errorMessage instanceof Error) {
39
+ throw errorMessage;
40
+ }
41
+ ;
42
+ throw new Error(errorMessage);
43
+ }
44
+ exports.assert = assert;
45
+ // keccak asserts
46
+ function assertPostiveInteger(n) {
47
+ if (!Number.isSafeInteger(n) || n < 0)
48
+ throw new Error("positive integer expected, not ".concat(n));
49
+ }
50
+ exports.assertPostiveInteger = assertPostiveInteger;
51
+ function assertBytes(stuff) {
52
+ if (!(stuff instanceof Uint8Array))
53
+ throw new Error("Uint8Array expected");
54
+ }
55
+ exports.assertBytes = assertBytes;
@@ -0,0 +1,5 @@
1
+ export declare const rfc4648_ALPHABET: readonly ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "2", "3", "4", "5", "6", "7"];
2
+ export declare const BECH32_BASE32_ALPHABET: readonly ["q", "p", "z", "r", "y", "9", "x", "8", "g", "f", "2", "t", "v", "d", "w", "0", "s", "3", "j", "n", "5", "4", "k", "h", "c", "e", "6", "m", "u", "a", "7", "l"];
3
+ export declare function encodeBase32rfc4648(bytes: Uint8Array): string;
4
+ export declare function decodeBase32rfc4648(base32Str: string): Uint8Array;
5
+ export declare function decodeBase32Bech32(base32Str: string): Uint8Array;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeBase32Bech32 = exports.decodeBase32rfc4648 = exports.encodeBase32rfc4648 = exports.BECH32_BASE32_ALPHABET = exports.rfc4648_ALPHABET = void 0;
4
+ var types_1 = require("../types.js");
5
+ var bitstream_1 = require("@harmoniclabs/bitstream");
6
+ exports.rfc4648_ALPHABET = Object.freeze([
7
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g',
8
+ 'h', 'i', 'j', 'k', 'l', 'm', 'n',
9
+ 'o', 'p', 'q', 'r', 's', 't', 'u',
10
+ 'v', 'w', 'x', 'y', 'z', '2', '3',
11
+ '4', '5', '6', '7'
12
+ ]);
13
+ exports.BECH32_BASE32_ALPHABET = Object.freeze([
14
+ 'q', 'p', 'z', 'r', 'y', '9', 'x',
15
+ '8', 'g', 'f', '2', 't', 'v', 'd',
16
+ 'w', '0', 's', '3', 'j', 'n', '5',
17
+ '4', 'k', 'h', 'c', 'e', '6', 'm',
18
+ 'u', 'a', '7', 'l'
19
+ ]);
20
+ function encodeBase32rfc4648(bytes) {
21
+ var out = (0, types_1.buffToUint5Arr)(bytes).map(function (c) { return exports.rfc4648_ALPHABET[c]; }).join("");
22
+ var pad = [0, 6, 4, 3, 1][bytes.length % 5];
23
+ return out + '='.repeat(pad);
24
+ }
25
+ exports.encodeBase32rfc4648 = encodeBase32rfc4648;
26
+ function decodeBase32(base32Str, alphabet) {
27
+ if (!Array.from(base32Str).every(function (ch) { return alphabet.includes(ch); }))
28
+ throw new Error("can't decode base32 a string that is not in base32 (rfc 4648); string was: " + base32Str);
29
+ var len = base32Str.length;
30
+ var bits = '';
31
+ for (var i = 0; i < len - 1; i++) {
32
+ var num_1 = alphabet.indexOf(base32Str[i].toLowerCase());
33
+ bits += num_1.toString(2).padStart(5, '0');
34
+ }
35
+ // last, make sure we align to byte
36
+ var nCut = len * 5 - 8 * Math.floor(len * 5 / 8);
37
+ var num = alphabet.indexOf(base32Str[len - 1].toLowerCase());
38
+ var lastbits = num.toString(2).padStart(5, '0');
39
+ bits += lastbits.slice(0, 5 - nCut);
40
+ return bitstream_1.BitStream.fromBinStr(bits).toBuffer().buffer;
41
+ }
42
+ function decodeBase32rfc4648(base32Str) {
43
+ if (typeof base32Str === 'string') {
44
+ base32Str = base32Str.replace(/=+$/, '');
45
+ }
46
+ return decodeBase32(base32Str, exports.rfc4648_ALPHABET);
47
+ }
48
+ exports.decodeBase32rfc4648 = decodeBase32rfc4648;
49
+ function decodeBase32Bech32(base32Str) {
50
+ return decodeBase32(base32Str, exports.BECH32_BASE32_ALPHABET);
51
+ }
52
+ exports.decodeBase32Bech32 = decodeBase32Bech32;
@@ -0,0 +1 @@
1
+ export declare function bigintToBuffer(bigint: bigint, nBytes?: number | undefined): Uint8Array;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bigintToBuffer = void 0;
4
+ var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
5
+ function bigintToBuffer(bigint, nBytes) {
6
+ if (nBytes === void 0) { nBytes = undefined; }
7
+ if (bigint < BigInt(0))
8
+ throw new Error("cannot convert negative bigint to buffer");
9
+ if (typeof nBytes !== "number")
10
+ nBytes = undefined;
11
+ if (bigint === BigInt(0)) {
12
+ if (nBytes === undefined)
13
+ nBytes = 0;
14
+ return new Uint8Array(nBytes);
15
+ }
16
+ var buffHexString = bigint.toString(16);
17
+ buffHexString = buffHexString.length % 2 === 0 ? buffHexString : '0' + buffHexString;
18
+ if (typeof nBytes === "number") {
19
+ nBytes = Math.round(Math.abs(nBytes));
20
+ buffHexString = buffHexString.padStart(nBytes * 2, "00");
21
+ if (buffHexString.length > nBytes * 2)
22
+ // only keep the last nBytes
23
+ buffHexString = buffHexString.slice(buffHexString.length - (nBytes * 2));
24
+ }
25
+ return (0, uint8array_utils_1.fromHex)(buffHexString);
26
+ }
27
+ exports.bigintToBuffer = bigintToBuffer;
@@ -0,0 +1,2 @@
1
+ import { byte } from "../types.js";
2
+ export declare function bytesToHex(bytes: byte[] | number[]): string;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bytesToHex = void 0;
4
+ // Convert a byte array to a hex string
5
+ function bytesToHex(bytes) {
6
+ var len = bytes.length;
7
+ var hex = new Array(len);
8
+ var current = 0;
9
+ for (var i = 0; i < len;) {
10
+ current = (bytes[i] < 0 ? bytes[i] + 256 : bytes[i]) & 0xff;
11
+ hex[i++] = current.toString(16).padStart(2, '0');
12
+ }
13
+ return hex.join("");
14
+ }
15
+ exports.bytesToHex = bytesToHex;
@@ -0,0 +1,4 @@
1
+ declare function _getRandomValues(buff: Uint8Array): Uint8Array;
2
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/getRandomValues) */
3
+ export declare const getRandomValues: (<T extends ArrayBufferView | null>(array: T) => T) | typeof _getRandomValues;
4
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRandomValues = void 0;
4
+ var hasGlobalWebCrypto_1 = require("../hasGlobalWebCrypto.js");
5
+ function _getRandomValues(buff) {
6
+ for (var i = 0; i < buff.length; i++) {
7
+ buff[i] = (Math.random() * 256) >>> 0;
8
+ }
9
+ return buff;
10
+ }
11
+ /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Crypto/getRandomValues) */
12
+ exports.getRandomValues = hasGlobalWebCrypto_1.hasGlobalWebCrypto ? globalThis.crypto.getRandomValues.bind(globalThis.crypto) : _getRandomValues;
@@ -0,0 +1,2 @@
1
+ import { byte } from "../types.js";
2
+ export declare function hexToBytes(hex: string): byte[];
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hexToBytes = void 0;
4
+ // Convert a hex string to a byte array
5
+ function hexToBytes(hex) {
6
+ if ((hex.length % 2) !== 0)
7
+ hex = '0' + hex;
8
+ var len = hex.length;
9
+ var bytes = new Array(len / 2);
10
+ for (var c = 0; c < hex.length;)
11
+ bytes[c / 2] = parseInt(hex.substring(c, c += 2), 16);
12
+ return bytes;
13
+ }
14
+ exports.hexToBytes = hexToBytes;
@@ -0,0 +1,3 @@
1
+ export * from "./base32.js";
2
+ export * from "./positiveMod.js";
3
+ export * from "./rotr32.js";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./base32.js"), exports);
18
+ __exportStar(require("./positiveMod.js"), exports);
19
+ __exportStar(require("./rotr32.js"), exports);
@@ -0,0 +1 @@
1
+ export declare function positiveMod(x: bigint, n: bigint): bigint;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.positiveMod = void 0;
4
+ var _0n = BigInt(0);
5
+ function positiveMod(x, n) {
6
+ n = typeof n === "bigint" ? n : BigInt(n);
7
+ x = typeof x === "bigint" ? x : BigInt(x);
8
+ var res = x % n;
9
+ return res < _0n ? res + n : res;
10
+ }
11
+ exports.positiveMod = positiveMod;
File without changes
File without changes
@@ -0,0 +1,2 @@
1
+ import type { uint5 } from "../types.js";
2
+ export declare function rotr32(x: number, by: uint5): number;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rotr32 = void 0;
4
+ function rotr32(x, by) {
5
+ return (x >>> by) | (x << (32 - by)) | 0;
6
+ }
7
+ exports.rotr32 = rotr32;