quarkdash 1.0.2 → 1.0.7

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 (49) hide show
  1. package/README.md +2 -0
  2. package/dist/cjs/crypto.js +2 -2
  3. package/dist/cjs/crypto.js.map +1 -1
  4. package/dist/cjs/index.js +2 -0
  5. package/dist/cjs/index.js.map +1 -1
  6. package/dist/cjs/ringlwe.js +23 -6
  7. package/dist/cjs/ringlwe.js.map +1 -1
  8. package/dist/cjs/sha.js +242 -0
  9. package/dist/cjs/sha.js.map +1 -0
  10. package/dist/esm/crypto.js +2 -2
  11. package/dist/esm/crypto.js.map +1 -1
  12. package/dist/esm/index.js +2 -0
  13. package/dist/esm/index.js.map +1 -1
  14. package/dist/esm/ringlwe.js +23 -6
  15. package/dist/esm/ringlwe.js.map +1 -1
  16. package/dist/esm/sha.js +237 -0
  17. package/dist/esm/sha.js.map +1 -0
  18. package/dist/types/index.d.ts +1 -0
  19. package/dist/types/ringlwe.d.ts +13 -2
  20. package/dist/types/sha.d.ts +49 -0
  21. package/dist/types/types.d.ts +2 -2
  22. package/package.json +1 -1
  23. package/src/crypto.ts +2 -2
  24. package/src/index.ts +3 -0
  25. package/src/ringlwe.ts +30 -12
  26. package/src/sha.ts +265 -0
  27. package/src/types.ts +2 -2
  28. package/.idea/modules.xml +0 -8
  29. package/.idea/quarkdash.iml +0 -12
  30. package/.idea/vcs.xml +0 -6
  31. package/coverage/clover.xml +0 -506
  32. package/coverage/coverage-final.json +0 -9
  33. package/coverage/lcov-report/base.css +0 -224
  34. package/coverage/lcov-report/block-navigation.js +0 -87
  35. package/coverage/lcov-report/cipher.ts.html +0 -862
  36. package/coverage/lcov-report/crypto.ts.html +0 -1000
  37. package/coverage/lcov-report/favicon.png +0 -0
  38. package/coverage/lcov-report/index.html +0 -221
  39. package/coverage/lcov-report/index.ts.html +0 -154
  40. package/coverage/lcov-report/kdf.ts.html +0 -274
  41. package/coverage/lcov-report/mac.ts.html +0 -277
  42. package/coverage/lcov-report/prettify.css +0 -1
  43. package/coverage/lcov-report/prettify.js +0 -2
  44. package/coverage/lcov-report/ringlwe.ts.html +0 -895
  45. package/coverage/lcov-report/shake.ts.html +0 -571
  46. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  47. package/coverage/lcov-report/sorter.js +0 -210
  48. package/coverage/lcov-report/utils.ts.html +0 -952
  49. package/coverage/lcov.info +0 -796
package/src/sha.ts ADDED
@@ -0,0 +1,265 @@
1
+ /**
2
+ * QuarkDash SHA Implementations
3
+ *
4
+ * @git https://github.com/devsdaddy/quarkdash
5
+ * @version 1.0.0
6
+ * @author Elijah Rastorguev
7
+ * @build 1000
8
+ * @website https://dev.to/devsdaddy
9
+ */
10
+ /**
11
+ * SHA-256 Implementation
12
+ */
13
+ export class SHA256 {
14
+ // Constants
15
+ private static readonly K: number[] = [
16
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
17
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
18
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
19
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
20
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
21
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
22
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
23
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
24
+ ];
25
+
26
+ /**
27
+ * Right Rotate
28
+ * @param value {number} Value to rotate
29
+ * @param amount {number} Rotate amount
30
+ * @private
31
+ */
32
+ private static rightRotate(value: number, amount: number): number {
33
+ return (value >>> amount) | (value << (32 - amount));
34
+ }
35
+
36
+ /**
37
+ * Get SHA256 Hash
38
+ * @param data {string|Uint8Array} Raw string or bytes array
39
+ * @param returnBytes {boolean} Returns HEX String or Uint8Array
40
+ * @returns {string|Uint8Array} HEX String or Uint8Array
41
+ */
42
+ static hash(data: string | Uint8Array, returnBytes : boolean = false): string | Uint8Array {
43
+ const msgBytes = typeof data === 'string' ? new TextEncoder().encode(data) : data;
44
+
45
+ // Initial hash values
46
+ let h0 = 0x6a09e667;
47
+ let h1 = 0xbb67ae85;
48
+ let h2 = 0x3c6ef372;
49
+ let h3 = 0xa54ff53a;
50
+ let h4 = 0x510e527f;
51
+ let h5 = 0x9b05688c;
52
+ let h6 = 0x1f83d9ab;
53
+ let h7 = 0x5be0cd19;
54
+
55
+ // Pre-processing: padding
56
+ const ml = msgBytes.length * 8;
57
+ const padded = new Uint8Array(((ml + 64 + 511) & ~511) / 8);
58
+ padded.set(msgBytes);
59
+ padded[msgBytes.length] = 0x80;
60
+
61
+ // Append length
62
+ const dv = new DataView(padded.buffer);
63
+ dv.setUint32(padded.length - 8, 0, false);
64
+ dv.setUint32(padded.length - 4, ml, false);
65
+
66
+ // Process chunks
67
+ for (let i = 0; i < padded.length; i += 64) {
68
+ const w = new Array(64).fill(0);
69
+
70
+ // Prepare message schedule
71
+ for (let j = 0; j < 16; j++) {
72
+ w[j] = dv.getUint32(i + j * 4, false);
73
+ }
74
+
75
+ for (let j = 16; j < 64; j++) {
76
+ const s0 = this.rightRotate(w[j - 15], 7) ^ this.rightRotate(w[j - 15], 18) ^ (w[j - 15] >>> 3);
77
+ const s1 = this.rightRotate(w[j - 2], 17) ^ this.rightRotate(w[j - 2], 19) ^ (w[j - 2] >>> 10);
78
+ w[j] = (w[j - 16] + s0 + w[j - 7] + s1) >>> 0;
79
+ }
80
+
81
+ // Initialize working variables
82
+ let a = h0;
83
+ let b = h1;
84
+ let c = h2;
85
+ let d = h3;
86
+ let e = h4;
87
+ let f = h5;
88
+ let g = h6;
89
+ let h = h7;
90
+
91
+ // Main loop
92
+ for (let j = 0; j < 64; j++) {
93
+ const S1 = this.rightRotate(e, 6) ^ this.rightRotate(e, 11) ^ this.rightRotate(e, 25);
94
+ const ch = (e & f) ^ ((~e) & g);
95
+ const temp1 = (h + S1 + ch + this.K[j] + w[j]) >>> 0;
96
+ const S0 = this.rightRotate(a, 2) ^ this.rightRotate(a, 13) ^ this.rightRotate(a, 22);
97
+ const maj = (a & b) ^ (a & c) ^ (b & c);
98
+ const temp2 = (S0 + maj) >>> 0;
99
+
100
+ h = g;
101
+ g = f;
102
+ f = e;
103
+ e = (d + temp1) >>> 0;
104
+ d = c;
105
+ c = b;
106
+ b = a;
107
+ a = (temp1 + temp2) >>> 0;
108
+ }
109
+
110
+ // Update hash values
111
+ h0 = (h0 + a) >>> 0;
112
+ h1 = (h1 + b) >>> 0;
113
+ h2 = (h2 + c) >>> 0;
114
+ h3 = (h3 + d) >>> 0;
115
+ h4 = (h4 + e) >>> 0;
116
+ h5 = (h5 + f) >>> 0;
117
+ h6 = (h6 + g) >>> 0;
118
+ h7 = (h7 + h) >>> 0;
119
+ }
120
+
121
+ // Produce final hash
122
+ const result = new Uint8Array(32);
123
+ const resultView = new DataView(result.buffer);
124
+ resultView.setUint32(0, h0, false);
125
+ resultView.setUint32(4, h1, false);
126
+ resultView.setUint32(8, h2, false);
127
+ resultView.setUint32(12, h3, false);
128
+ resultView.setUint32(16, h4, false);
129
+ resultView.setUint32(20, h5, false);
130
+ resultView.setUint32(24, h6, false);
131
+ resultView.setUint32(28, h7, false);
132
+
133
+ return (returnBytes) ? result as Uint8Array : Array.from(result).map(b => b.toString(16).padStart(2, '0')).join('') as string;
134
+ }
135
+ }
136
+
137
+ /**
138
+ * SHA-512 Implementation
139
+ */
140
+ export class SHA512 {
141
+ // Constants
142
+ private static readonly K: bigint[] = [
143
+ 0x428a2f98d728ae22n, 0x7137449123ef65cdn, 0xb5c0fbcfec4d3b2fn, 0xe9b5dba58189dbbcn,
144
+ 0x3956c25bf348b538n, 0x59f111f1b605d019n, 0x923f82a4af194f9bn, 0xab1c5ed5da6d8118n,
145
+ 0xd807aa98a3030242n, 0x12835b0145706fben, 0x243185be4ee4b28cn, 0x550c7dc3d5ffb4e2n,
146
+ 0x72be5d74f27b896fn, 0x80deb1fe3b1696b1n, 0x9bdc06a725c71235n, 0xc19bf174cf692694n,
147
+ 0xe49b69c19ef14ad2n, 0xefbe4786384f25e3n, 0x0fc19dc68b8cd5b5n, 0x240ca1cc77ac9c65n,
148
+ 0x2de92c6f592b0275n, 0x4a7484aa6ea6e483n, 0x5cb0a9dcbd41fbd4n, 0x76f988da831153b5n,
149
+ 0x983e5152ee66dfabn, 0xa831c66d2db43210n, 0xb00327c898fb213fn, 0xbf597fc7beef0ee4n,
150
+ 0xc6e00bf33da88fc2n, 0xd5a79147930aa725n, 0x06ca6351e003826fn, 0x142929670a0e6e70n,
151
+ 0x27b70a8546d22ffcn, 0x2e1b21385c26c926n, 0x4d2c6dfc5ac42aedn, 0x53380d139d95b3dfn,
152
+ 0x650a73548baf63den, 0x766a0abb3c77b2a8n, 0x81c2c92e47edaee6n, 0x92722c851482353bn,
153
+ 0xa2bfe8a14cf10364n, 0xa81a664bbc423001n, 0xc24b8b70d0f89791n, 0xc76c51a30654be30n,
154
+ 0xd192e819d6ef5218n, 0xd69906245565a910n, 0xf40e35855771202an, 0x106aa07032bbd1b8n,
155
+ 0x19a4c116b8d2d0c8n, 0x1e376c085141ab53n, 0x2748774cdf8eeb99n, 0x34b0bcb5e19b48a8n,
156
+ 0x391c0cb3c5c95a63n, 0x4ed8aa4ae3418acbn, 0x5b9cca4f7763e373n, 0x682e6ff3d6b2b8a3n,
157
+ 0x748f82ee5defb2fcn, 0x78a5636f43172f60n, 0x84c87814a1f0ab72n, 0x8cc702081a6439ecn,
158
+ 0x90befffa23631e28n, 0xa4506cebde82bde9n, 0xbef9a3f7b2c67915n, 0xc67178f2e372532bn,
159
+ 0xca273eceea26619cn, 0xd186b8c721c0c207n, 0xeada7dd6cde0eb1en, 0xf57d4f7fee6ed178n,
160
+ 0x06f067aa72176fban, 0x0a637dc5a2c898a6n, 0x113f9804bef90daen, 0x1b710b35131c471bn,
161
+ 0x28db77f523047d84n, 0x32caab7b40c72493n, 0x3c9ebe0a15c9bebcn, 0x431d67c49c100d4cn,
162
+ 0x4cc5d4becb3e42b6n, 0x597f299cfc657e2an, 0x5fcb6fab3ad6faecn, 0x6c44198c4a475817n
163
+ ];
164
+
165
+ /**
166
+ * Bitint right rotate
167
+ * @param value {number} Value
168
+ * @param amount {number} Rotate amount
169
+ * @private
170
+ */
171
+ private static rightRotate(value: bigint, amount: number): bigint {
172
+ return (value >> BigInt(amount)) | (value << (64n - BigInt(amount)));
173
+ }
174
+
175
+ /**
176
+ * Get SHA512 Hash
177
+ * @param data {string|Uint8Array} Raw string or bytes array
178
+ * @param returnBytes {boolean} Returns HEX String or Uint8Array
179
+ * @returns {string|Uint8Array} HEX String or Uint8Array
180
+ */
181
+ static hash(data: string | Uint8Array, returnBytes : boolean = false): string | Uint8Array {
182
+ const msgBytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
183
+
184
+ // Initial hash values
185
+ let h0 = 0x6a09e667f3bcc908n;
186
+ let h1 = 0xbb67ae8584caa73bn;
187
+ let h2 = 0x3c6ef372fe94f82bn;
188
+ let h3 = 0xa54ff53a5f1d36f1n;
189
+ let h4 = 0x510e527fade682d1n;
190
+ let h5 = 0x9b05688c2b3e6c1fn;
191
+ let h6 = 0x1f83d9abfb41bd6bn;
192
+ let h7 = 0x5be0cd19137e2179n;
193
+
194
+ const ml = BigInt(msgBytes.length * 8);
195
+ const padded = new Uint8Array((((Number(ml) + 128 + 1023) & ~1023) / 8));
196
+ padded.set(msgBytes);
197
+ padded[msgBytes.length] = 0x80;
198
+
199
+ const dv = new DataView(padded.buffer);
200
+ dv.setBigUint64(padded.length - 8, ml, false);
201
+
202
+ for (let i = 0; i < padded.length; i += 128) {
203
+ const w = new Array(80).fill(0n);
204
+
205
+ for (let j = 0; j < 16; j++) {
206
+ w[j] = dv.getBigUint64(i + j * 8, false);
207
+ }
208
+
209
+ for (let j = 16; j < 80; j++) {
210
+ const s0 = this.rightRotate(w[j - 15], 1) ^ this.rightRotate(w[j - 15], 8) ^ (w[j - 15] >> 7n);
211
+ const s1 = this.rightRotate(w[j - 2], 19) ^ this.rightRotate(w[j - 2], 61) ^ (w[j - 2] >> 6n);
212
+ w[j] = (w[j - 16] + s0 + w[j - 7] + s1) & 0xffffffffffffffffn;
213
+ }
214
+
215
+ let a = h0;
216
+ let b = h1;
217
+ let c = h2;
218
+ let d = h3;
219
+ let e = h4;
220
+ let f = h5;
221
+ let g = h6;
222
+ let h = h7;
223
+
224
+ for (let j = 0; j < 80; j++) {
225
+ const S1 = this.rightRotate(e, 14) ^ this.rightRotate(e, 18) ^ this.rightRotate(e, 41);
226
+ const ch = (e & f) ^ ((~e) & g);
227
+ const temp1 = (h + S1 + ch + this.K[j] + w[j]) & 0xffffffffffffffffn;
228
+ const S0 = this.rightRotate(a, 28) ^ this.rightRotate(a, 34) ^ this.rightRotate(a, 39);
229
+ const maj = (a & b) ^ (a & c) ^ (b & c);
230
+ const temp2 = (S0 + maj) & 0xffffffffffffffffn;
231
+
232
+ h = g;
233
+ g = f;
234
+ f = e;
235
+ e = (d + temp1) & 0xffffffffffffffffn;
236
+ d = c;
237
+ c = b;
238
+ b = a;
239
+ a = (temp1 + temp2) & 0xffffffffffffffffn;
240
+ }
241
+
242
+ h0 = (h0 + a) & 0xffffffffffffffffn;
243
+ h1 = (h1 + b) & 0xffffffffffffffffn;
244
+ h2 = (h2 + c) & 0xffffffffffffffffn;
245
+ h3 = (h3 + d) & 0xffffffffffffffffn;
246
+ h4 = (h4 + e) & 0xffffffffffffffffn;
247
+ h5 = (h5 + f) & 0xffffffffffffffffn;
248
+ h6 = (h6 + g) & 0xffffffffffffffffn;
249
+ h7 = (h7 + h) & 0xffffffffffffffffn;
250
+ }
251
+
252
+ const result = new Uint8Array(64);
253
+ const resultView = new DataView(result.buffer);
254
+ resultView.setBigUint64(0, h0, false);
255
+ resultView.setBigUint64(8, h1, false);
256
+ resultView.setBigUint64(16, h2, false);
257
+ resultView.setBigUint64(24, h3, false);
258
+ resultView.setBigUint64(32, h4, false);
259
+ resultView.setBigUint64(40, h5, false);
260
+ resultView.setBigUint64(48, h6, false);
261
+ resultView.setBigUint64(56, h7, false);
262
+
263
+ return (returnBytes) ? result as Uint8Array : Array.from(result).map(b => b.toString(16).padStart(2, '0')).join('') as string;
264
+ }
265
+ }
package/src/types.ts CHANGED
@@ -54,8 +54,8 @@ export interface IKeyExchange {
54
54
  generateKeyPairSync(): ICryptoKeyPair;
55
55
  encapsulate(publicKey: Uint8Array): Promise<ICryptoEncapsulated>;
56
56
  encapsulateSync(publicKey: Uint8Array): ICryptoEncapsulated;
57
- decapsulate(privateKey: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array>;
58
- decapsulateSync(privateKey: Uint8Array, ciphertext: Uint8Array): Uint8Array;
57
+ decapsulate(privateKey: Uint8Array, peerPublicKey: Uint8Array, ciphertext: Uint8Array): Promise<Uint8Array>;
58
+ decapsulateSync(privateKey: Uint8Array, peerPublicKey: Uint8Array, ciphertext: Uint8Array): Uint8Array;
59
59
  }
60
60
 
61
61
  /**
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/quarkdash.iml" filepath="$PROJECT_DIR$/.idea/quarkdash.iml" />
6
- </modules>
7
- </component>
8
- </project>
@@ -1,12 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
6
- <excludeFolder url="file://$MODULE_DIR$/temp" />
7
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
8
- </content>
9
- <orderEntry type="inheritedJdk" />
10
- <orderEntry type="sourceFolder" forTests="false" />
11
- </component>
12
- </module>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="" vcs="Git" />
5
- </component>
6
- </project>