openttt 0.1.3 → 0.2.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 (44) hide show
  1. package/README.md +48 -26
  2. package/dist/adaptive_switch.js +1 -1
  3. package/dist/auto_mint.js +2 -2
  4. package/dist/http_client.d.ts +98 -0
  5. package/dist/http_client.js +252 -0
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.js +2 -0
  8. package/dist/integrity_client.d.ts +39 -0
  9. package/dist/integrity_client.js +114 -0
  10. package/dist/time_synthesis.js +0 -4
  11. package/dist/ttt_client.d.ts +18 -0
  12. package/dist/ttt_client.js +20 -0
  13. package/package.json +6 -2
  14. package/dist/golay.d.ts +0 -6
  15. package/dist/golay.js +0 -166
  16. package/dist/grg_forward.d.ts +0 -11
  17. package/dist/grg_forward.js +0 -74
  18. package/dist/grg_inverse.d.ts +0 -7
  19. package/dist/grg_inverse.js +0 -100
  20. package/dist/grg_pipeline.d.ts +0 -13
  21. package/dist/grg_pipeline.js +0 -64
  22. package/dist/reed_solomon.d.ts +0 -12
  23. package/dist/reed_solomon.js +0 -179
  24. package/vendor/helm-crypto/golay.d.ts +0 -6
  25. package/vendor/helm-crypto/golay.js +0 -167
  26. package/vendor/helm-crypto/golay.js.map +0 -1
  27. package/vendor/helm-crypto/grg_forward.d.ts +0 -22
  28. package/vendor/helm-crypto/grg_forward.js +0 -89
  29. package/vendor/helm-crypto/grg_forward.js.map +0 -1
  30. package/vendor/helm-crypto/grg_inverse.d.ts +0 -16
  31. package/vendor/helm-crypto/grg_inverse.js +0 -118
  32. package/vendor/helm-crypto/grg_inverse.js.map +0 -1
  33. package/vendor/helm-crypto/grg_pipeline.d.ts +0 -13
  34. package/vendor/helm-crypto/grg_pipeline.js +0 -66
  35. package/vendor/helm-crypto/grg_pipeline.js.map +0 -1
  36. package/vendor/helm-crypto/index.d.ts +0 -5
  37. package/vendor/helm-crypto/index.js +0 -17
  38. package/vendor/helm-crypto/index.js.map +0 -1
  39. package/vendor/helm-crypto/logger.d.ts +0 -6
  40. package/vendor/helm-crypto/logger.js +0 -11
  41. package/vendor/helm-crypto/logger.js.map +0 -1
  42. package/vendor/helm-crypto/reed_solomon.d.ts +0 -37
  43. package/vendor/helm-crypto/reed_solomon.js +0 -210
  44. package/vendor/helm-crypto/reed_solomon.js.map +0 -1
@@ -1,179 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ReedSolomon = void 0;
4
- class ReedSolomon {
5
- static expTable = new Uint8Array(256);
6
- static logTable = new Uint8Array(256);
7
- static initialized = false;
8
- static init() {
9
- if (this.initialized)
10
- return;
11
- let x = 1;
12
- for (let i = 0; i < 255; i++) {
13
- this.expTable[i] = x;
14
- this.logTable[x] = i;
15
- x <<= 1;
16
- if (x & 0x100) {
17
- x ^= 0x11D; // x^8 + x^4 + x^3 + x^2 + 1
18
- }
19
- }
20
- this.expTable[255] = this.expTable[0];
21
- this.logTable[0] = 0;
22
- this.initialized = true;
23
- }
24
- static mul(a, b) {
25
- if (a === 0 || b === 0)
26
- return 0;
27
- return this.expTable[(this.logTable[a] + this.logTable[b]) % 255];
28
- }
29
- static div(a, b) {
30
- if (b === 0)
31
- throw new Error("Division by zero");
32
- if (a === 0)
33
- return 0;
34
- return this.expTable[(this.logTable[a] - this.logTable[b] + 255) % 255];
35
- }
36
- static invertMatrix(matrix) {
37
- const n = matrix.length;
38
- const aug = [];
39
- for (let i = 0; i < n; i++) {
40
- aug[i] = [];
41
- for (let j = 0; j < n; j++) {
42
- aug[i][j] = matrix[i][j];
43
- }
44
- for (let j = 0; j < n; j++) {
45
- aug[i][j + n] = (i === j) ? 1 : 0;
46
- }
47
- }
48
- for (let i = 0; i < n; i++) {
49
- let pivot = i;
50
- while (pivot < n && aug[pivot][i] === 0) {
51
- pivot++;
52
- }
53
- if (pivot === n)
54
- throw new Error("Singular matrix");
55
- if (pivot !== i) {
56
- const temp = aug[i];
57
- aug[i] = aug[pivot];
58
- aug[pivot] = temp;
59
- }
60
- const pivotVal = aug[i][i];
61
- if (pivotVal !== 1) {
62
- for (let j = i; j < 2 * n; j++) {
63
- aug[i][j] = this.div(aug[i][j], pivotVal);
64
- }
65
- }
66
- for (let j = 0; j < n; j++) {
67
- if (i !== j && aug[j][i] !== 0) {
68
- const factor = aug[j][i];
69
- for (let k = i; k < 2 * n; k++) {
70
- aug[j][k] ^= this.mul(factor, aug[i][k]);
71
- }
72
- }
73
- }
74
- }
75
- const inv = [];
76
- for (let i = 0; i < n; i++) {
77
- inv[i] = [];
78
- for (let j = 0; j < n; j++) {
79
- inv[i][j] = aug[i][j + n];
80
- }
81
- }
82
- return inv;
83
- }
84
- static buildVandermonde(rows, cols) {
85
- const V = [];
86
- for (let r = 0; r < rows; r++) {
87
- V[r] = [];
88
- const x = r + 1;
89
- for (let c = 0; c < cols; c++) {
90
- if (c === 0) {
91
- V[r][c] = 1;
92
- }
93
- else {
94
- V[r][c] = this.mul(V[r][c - 1], x);
95
- }
96
- }
97
- }
98
- const V_top = [];
99
- for (let i = 0; i < cols; i++) {
100
- V_top.push([...V[i]]);
101
- }
102
- const V_top_inv = this.invertMatrix(V_top);
103
- const G = [];
104
- for (let r = 0; r < rows; r++) {
105
- G[r] = [];
106
- for (let c = 0; c < cols; c++) {
107
- let val = 0;
108
- for (let k = 0; k < cols; k++) {
109
- val ^= this.mul(V[r][k], V_top_inv[k][c]);
110
- }
111
- G[r][c] = val;
112
- }
113
- }
114
- return G;
115
- }
116
- static encode(data, dataShards = 4, parityShards = 2) {
117
- this.init();
118
- const totalShards = dataShards + parityShards;
119
- const rawShardSize = Math.ceil(data.length / dataShards);
120
- const shardSize = Math.ceil(rawShardSize / 3) * 3;
121
- const matrix = this.buildVandermonde(totalShards, dataShards);
122
- const shards = [];
123
- for (let i = 0; i < totalShards; i++) {
124
- shards.push(new Uint8Array(shardSize));
125
- }
126
- for (let i = 0; i < dataShards; i++) {
127
- shards[i].set(data.subarray(i * shardSize, Math.min((i + 1) * shardSize, data.length)));
128
- }
129
- for (let c = 0; c < shardSize; c++) {
130
- for (let r = dataShards; r < totalShards; r++) {
131
- let val = 0;
132
- for (let j = 0; j < dataShards; j++) {
133
- val ^= this.mul(matrix[r][j], shards[j][c]);
134
- }
135
- shards[r][c] = val;
136
- }
137
- }
138
- return shards;
139
- }
140
- static decode(shards, dataShards = 4, parityShards = 2) {
141
- this.init();
142
- const totalShards = dataShards + parityShards;
143
- if (shards.length !== totalShards) {
144
- throw new Error(`[RS] Expected ${totalShards} shards, got ${shards.length}`);
145
- }
146
- const presentIndices = [];
147
- const presentShards = [];
148
- for (let i = 0; i < totalShards; i++) {
149
- if (shards[i] !== null && shards[i] !== undefined) {
150
- presentIndices.push(i);
151
- presentShards.push(shards[i]);
152
- if (presentIndices.length === dataShards)
153
- break;
154
- }
155
- }
156
- if (presentIndices.length < dataShards) {
157
- throw new Error(`[RS] Not enough shards for recovery: need ${dataShards}, got ${presentIndices.length}`);
158
- }
159
- const shardSize = presentShards[0].length;
160
- const origMatrix = this.buildVandermonde(totalShards, dataShards);
161
- const subMatrix = [];
162
- for (let i = 0; i < dataShards; i++) {
163
- subMatrix.push([...origMatrix[presentIndices[i]]]);
164
- }
165
- const invMatrix = this.invertMatrix(subMatrix);
166
- const result = new Uint8Array(shardSize * dataShards);
167
- for (let c = 0; c < shardSize; c++) {
168
- for (let r = 0; r < dataShards; r++) {
169
- let val = 0;
170
- for (let j = 0; j < dataShards; j++) {
171
- val ^= this.mul(invMatrix[r][j], presentShards[j][c]);
172
- }
173
- result[r * shardSize + c] = val;
174
- }
175
- }
176
- return result;
177
- }
178
- }
179
- exports.ReedSolomon = ReedSolomon;
@@ -1,6 +0,0 @@
1
- export declare function golayEncode(data: Uint8Array): Uint8Array;
2
- export declare function golayDecode(encoded: Uint8Array): {
3
- data: Uint8Array;
4
- corrected: number;
5
- uncorrectable: boolean;
6
- };
@@ -1,167 +0,0 @@
1
- "use strict";
2
- // sdk/src/golay.ts
3
- // 🔱 Golay(24,12) Extended Binary Golay Code (RE-SURGERY)
4
- // Corrects up to 3 bit errors, detects 4 bit errors.
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.golayEncode = golayEncode;
7
- exports.golayDecode = golayDecode;
8
- // Standard Golay Parity Matrix P (12x12) - Systematic Form [I12 | P]
9
- // Every row has weight 7.
10
- // Verified: P*P^T = I (mod 2), all rows weight 7.
11
- // Derived from g(x) = x^11+x^10+x^6+x^5+x^4+x^2+1 with parity extension.
12
- const P = [
13
- 0xC75, 0x49F, 0xD4B, 0x6E3, 0x9B3, 0xB66,
14
- 0xECC, 0x1ED, 0x3DA, 0x7B4, 0xB1D, 0xE3A
15
- ];
16
- // R7-P0-1: P^T (transpose of P) — required for second syndrome computation.
17
- // P is NOT symmetric, so s2 = s*P^T must use this separate matrix.
18
- // Without this, decodeWord fails ~24% of weight-2 and ~50% of weight-3 error patterns.
19
- const PT = [
20
- 0xAE3, 0xF25, 0x16F, 0x2DE, 0x5BC, 0xB78,
21
- 0x9D5, 0xC8F, 0x63B, 0xC76, 0x7C9, 0xF92
22
- ];
23
- const UNIT_VECTORS = [
24
- 0x800, 0x400, 0x200, 0x100, 0x080, 0x040,
25
- 0x020, 0x010, 0x008, 0x004, 0x002, 0x001
26
- ];
27
- /**
28
- * Calculates weight (number of set bits).
29
- */
30
- function weight(n) {
31
- let count = 0;
32
- let temp = n & 0xFFF;
33
- while (temp > 0) {
34
- temp &= (temp - 1);
35
- count++;
36
- }
37
- return count;
38
- }
39
- /**
40
- * Multiplies a 12-bit vector by the parity matrix P.
41
- */
42
- function multiplyP(v) {
43
- let res = 0;
44
- for (let i = 0; i < 12; i++) {
45
- if ((v >> (11 - i)) & 1) {
46
- res ^= P[i];
47
- }
48
- }
49
- return res & 0xFFF;
50
- }
51
- /**
52
- * R7-P0-1: Multiplies a 12-bit vector by P^T (transpose of P).
53
- * Required for second syndrome computation in decodeWord steps 3-5.
54
- */
55
- function multiplyPT(v) {
56
- let res = 0;
57
- for (let i = 0; i < 12; i++) {
58
- if ((v >> (11 - i)) & 1) {
59
- res ^= PT[i];
60
- }
61
- }
62
- return res & 0xFFF;
63
- }
64
- function encodeWord(msg) {
65
- const parity = multiplyP(msg & 0xFFF);
66
- return ((msg & 0xFFF) << 12) | parity;
67
- }
68
- /**
69
- * Full Syndrome Decoding for Golay(24,12)
70
- */
71
- function decodeWord(received) {
72
- let r_m = (received >> 12) & 0xFFF;
73
- const r_p = received & 0xFFF;
74
- // Syndrome s = r_m * P + r_p
75
- const s = multiplyP(r_m) ^ r_p;
76
- if (s === 0)
77
- return { msg: r_m, corrected: 0, uncorrectable: false };
78
- // 1. wt(s) <= 3 -> Error in parity
79
- if (weight(s) <= 3) {
80
- return { msg: r_m, corrected: weight(s), uncorrectable: false };
81
- }
82
- // 2. wt(s + P_i) <= 2 -> Error in msg bit i (+ possible 1-2 errors in parity)
83
- for (let i = 0; i < 12; i++) {
84
- if (weight(s ^ P[i]) <= 2) {
85
- return { msg: r_m ^ UNIT_VECTORS[i], corrected: weight(s ^ P[i]) + 1, uncorrectable: false };
86
- }
87
- }
88
- // 3. R7-P0-1: Second syndrome s2 = s * P^T (NOT s * P — P is not symmetric!)
89
- const s2 = multiplyPT(s);
90
- // 4. wt(s2) <= 3 -> Error in message
91
- if (weight(s2) <= 3) {
92
- return { msg: r_m ^ s2, corrected: weight(s2), uncorrectable: false };
93
- }
94
- // 5. wt(s2 + PT_i) <= 2 -> Error in parity bit i + message errors
95
- // R7-P0-1: Must use PT rows (columns of P), not P rows
96
- for (let i = 0; i < 12; i++) {
97
- if (weight(s2 ^ PT[i]) <= 2) {
98
- const error_m = s2 ^ PT[i];
99
- return { msg: r_m ^ error_m, corrected: weight(s2 ^ PT[i]) + 1, uncorrectable: false };
100
- }
101
- }
102
- return { msg: r_m, corrected: 0, uncorrectable: true };
103
- }
104
- function golayEncode(data) {
105
- // B1-6: Correct output size calculation to avoid buffer overflow
106
- const out = new Uint8Array(Math.ceil(data.length / 3) * 6);
107
- let outIdx = 0;
108
- // Process in 3-byte blocks -> two 12-bit words -> two 24-bit (3-byte) codewords
109
- for (let i = 0; i < data.length; i += 3) {
110
- const b1 = data[i];
111
- const b2 = i + 1 < data.length ? data[i + 1] : 0;
112
- const b3 = i + 2 < data.length ? data[i + 2] : 0;
113
- // Word 1 (12 bits)
114
- const w1 = (b1 << 4) | (b2 >> 4);
115
- const c1 = encodeWord(w1);
116
- out[outIdx++] = (c1 >> 16) & 0xFF;
117
- out[outIdx++] = (c1 >> 8) & 0xFF;
118
- out[outIdx++] = c1 & 0xFF;
119
- if (i + 1 < data.length) {
120
- // Word 2 (12 bits)
121
- const w2 = ((b2 & 0x0F) << 8) | b3;
122
- const c2 = encodeWord(w2);
123
- out[outIdx++] = (c2 >> 16) & 0xFF;
124
- out[outIdx++] = (c2 >> 8) & 0xFF;
125
- out[outIdx++] = c2 & 0xFF;
126
- }
127
- }
128
- return out;
129
- }
130
- function golayDecode(encoded) {
131
- // B1-6: Throws if length is not a multiple of 6
132
- if (encoded.length % 6 !== 0) {
133
- throw new Error("Invalid Golay encoded data: length must be multiple of 6");
134
- }
135
- const outLen = Math.floor(encoded.length / 2);
136
- const out = new Uint8Array(outLen);
137
- let outIdx = 0;
138
- let totalCorrected = 0;
139
- let anyUncorrectable = false;
140
- for (let i = 0; i < encoded.length; i += 6) {
141
- const c1 = (encoded[i] << 16) | (encoded[i + 1] << 8) | encoded[i + 2];
142
- const res1 = decodeWord(c1);
143
- totalCorrected += res1.corrected;
144
- if (res1.uncorrectable)
145
- anyUncorrectable = true;
146
- if (outIdx < outLen)
147
- out[outIdx++] = (res1.msg >> 4) & 0xFF;
148
- const b2_high = (res1.msg & 0x0F) << 4;
149
- if (i + 3 < encoded.length) {
150
- const c2 = (encoded[i + 3] << 16) | (encoded[i + 4] << 8) | encoded[i + 5];
151
- const res2 = decodeWord(c2);
152
- totalCorrected += res2.corrected;
153
- if (res2.uncorrectable)
154
- anyUncorrectable = true;
155
- if (outIdx < outLen)
156
- out[outIdx++] = b2_high | (res2.msg >> 8);
157
- if (outIdx < outLen)
158
- out[outIdx++] = res2.msg & 0xFF;
159
- }
160
- else {
161
- if (outIdx < outLen)
162
- out[outIdx++] = b2_high;
163
- }
164
- }
165
- return { data: out, corrected: totalCorrected, uncorrectable: anyUncorrectable };
166
- }
167
- //# sourceMappingURL=golay.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"golay.js","sourceRoot":"","sources":["../src/golay.ts"],"names":[],"mappings":";AAAA,mBAAmB;AACnB,0DAA0D;AAC1D,qDAAqD;;AAiHrD,kCA4BC;AAED,kCAkCC;AA/KD,qEAAqE;AACrE,0BAA0B;AAC1B,kDAAkD;AAClD,yEAAyE;AACzE,MAAM,CAAC,GAAG;IACN,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK;IACxC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK;CAC3C,CAAC;AAEF,4EAA4E;AAC5E,mEAAmE;AACnE,uFAAuF;AACvF,MAAM,EAAE,GAAG;IACP,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK;IACxC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK;CAC3C,CAAC;AAEF,MAAM,YAAY,GAAG;IACjB,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK;IACxC,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK;CAC3C,CAAC;AAEF;;GAEG;AACH,SAAS,MAAM,CAAC,CAAS;IACrB,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,IAAI,GAAG,CAAC,GAAG,KAAK,CAAC;IACrB,OAAO,IAAI,GAAG,CAAC,EAAE,CAAC;QACd,IAAI,IAAI,CAAC,IAAI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IAAI,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC;YAC/D,IAAI,MAAM,GAAG,MAAM;gBAAE,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC;QACzD,CAAC;aAAM,CAAC;YACJ,IAAI,MAAM,GAAG,MAAM;gBAAE,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC;QACjD,CAAC;IACL,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,cAAc,EAAE,aAAa,EAAE,gBAAgB,EAAE,CAAC;AACrF,CAAC"}
@@ -1,22 +0,0 @@
1
- export declare class GrgForward {
2
- /**
3
- * Golomb-Rice encoding.
4
- *
5
- * Golomb-Rice is optimal for small-value biased distributions (e.g., timestamp deltas).
6
- * For uniformly distributed data (random bytes), expect ~50% expansion.
7
- * The primary purpose in the GRG pipeline is structured encoding for error correction,
8
- * not compression.
9
- *
10
- * @param data Raw bytes to encode.
11
- * @param m Golomb divisor (must be a power of 2, default 16).
12
- */
13
- static golombEncode(data: Uint8Array, m?: number): Uint8Array;
14
- static redstuffEncode(data: Uint8Array, shards?: number, parity?: number): Uint8Array[];
15
- /**
16
- * Derives an HMAC key from GRG payload context (chainId + poolAddress).
17
- * Both parameters are required — no default key fallback.
18
- */
19
- static deriveHmacKey(chainId: number, poolAddress: string): Buffer;
20
- static golayEncodeWrapper(data: Uint8Array, hmacKey: Buffer): Uint8Array;
21
- static encode(data: Uint8Array, chainId: number, poolAddress: string): Uint8Array[];
22
- }
@@ -1,89 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GrgForward = void 0;
4
- const crypto_1 = require("crypto");
5
- const ethers_1 = require("ethers");
6
- const golay_1 = require("./golay");
7
- const reed_solomon_1 = require("./reed_solomon");
8
- class GrgForward {
9
- /**
10
- * Golomb-Rice encoding.
11
- *
12
- * Golomb-Rice is optimal for small-value biased distributions (e.g., timestamp deltas).
13
- * For uniformly distributed data (random bytes), expect ~50% expansion.
14
- * The primary purpose in the GRG pipeline is structured encoding for error correction,
15
- * not compression.
16
- *
17
- * @param data Raw bytes to encode.
18
- * @param m Golomb divisor (must be a power of 2, default 16).
19
- */
20
- static golombEncode(data, m = 16) {
21
- if (m < 2)
22
- throw new Error("[GRG] Golomb parameter m must be >= 2");
23
- const k = Math.log2(m);
24
- if (!Number.isInteger(k))
25
- throw new Error("M must be power of 2");
26
- const bits = [];
27
- for (const byte of data) {
28
- const q = Math.floor(byte / m);
29
- const r = byte % m;
30
- for (let i = 0; i < q; i++)
31
- bits.push(1);
32
- bits.push(0);
33
- for (let i = k - 1; i >= 0; i--)
34
- bits.push((r >> i) & 1);
35
- }
36
- const out = new Uint8Array(Math.ceil(bits.length / 8));
37
- for (let i = 0; i < bits.length; i++) {
38
- if (bits[i])
39
- out[i >> 3] |= (0x80 >> (i & 7));
40
- }
41
- return out;
42
- }
43
- // 2. RedStuff Erasure Coding (Reed-Solomon GF(2^8))
44
- static redstuffEncode(data, shards = 4, parity = 2) {
45
- return reed_solomon_1.ReedSolomon.encode(data, shards, parity);
46
- }
47
- /**
48
- * Derives an HMAC key from GRG payload context (chainId + poolAddress).
49
- * Both parameters are required — no default key fallback.
50
- */
51
- static deriveHmacKey(chainId, poolAddress) {
52
- if (chainId === undefined || chainId === null || !poolAddress) {
53
- throw new Error("[GRG] chainId and poolAddress are required for HMAC key derivation. No default key is allowed.");
54
- }
55
- const packed = (0, ethers_1.keccak256)(ethers_1.AbiCoder.defaultAbiCoder().encode(["uint256", "address"], [chainId, poolAddress]));
56
- return Buffer.from(packed.slice(2), "hex"); // 32 bytes
57
- }
58
- // 3. Golay(24,12) Error Correction Encoding
59
- static golayEncodeWrapper(data, hmacKey) {
60
- const encoded = (0, golay_1.golayEncode)(data);
61
- // 🔱 Integrity: Append 8-byte HMAC-SHA256 of the encoded shard (keyed hash)
62
- const key = hmacKey;
63
- const mac = (0, crypto_1.createHmac)("sha256", key).update(Buffer.from(encoded)).digest();
64
- const checksum = mac.subarray(0, 8);
65
- const final = new Uint8Array(encoded.length + 8);
66
- final.set(encoded);
67
- final.set(checksum, encoded.length);
68
- return final;
69
- }
70
- static encode(data, chainId, poolAddress) {
71
- // R3-P0-3: Reject empty input — roundtrip breaks ([] → [0])
72
- if (data.length === 0) {
73
- throw new Error("[GRG] Cannot encode empty input — roundtrip identity violation");
74
- }
75
- const compressed = this.golombEncode(data);
76
- // Prepend original length (4 bytes, big-endian) for exact roundtrip
77
- const withLen = new Uint8Array(4 + compressed.length);
78
- withLen[0] = (data.length >> 24) & 0xFF;
79
- withLen[1] = (data.length >> 16) & 0xFF;
80
- withLen[2] = (data.length >> 8) & 0xFF;
81
- withLen[3] = data.length & 0xFF;
82
- withLen.set(compressed, 4);
83
- const shards = this.redstuffEncode(withLen);
84
- const hmacKey = this.deriveHmacKey(chainId, poolAddress);
85
- return shards.map(s => this.golayEncodeWrapper(s, hmacKey));
86
- }
87
- }
88
- exports.GrgForward = GrgForward;
89
- //# sourceMappingURL=grg_forward.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"grg_forward.js","sourceRoot":"","sources":["../src/grg_forward.ts"],"names":[],"mappings":";;;AAAA,mCAAgD;AAChD,mCAA6C;AAC7C,mCAAsC;AACtC,iDAA6C;AAE7C,MAAa,UAAU;IAErB;;;;;;;;;;OAUG;IACH,MAAM,CAAC,YAAY,CAAC,IAAgB,EAAE,IAAY,EAAE;QAClD,IAAI,CAAC,GAAG,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;QACpE,MAAM,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACvB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAElE,MAAM,IAAI,GAAa,EAAE,CAAC;QAC1B,KAAK,MAAM,IAAI,IA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U,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;QAC3C,oEAAoE;QACpE,MAAM,OAAO,GAAG,IAAI,UAAU,CAAC,CAAC,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC;QACtD,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;QACxC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;QACxC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC;QACvC,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC;QAChC,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC;QAC3B,MAAM,MAAM,GAAG,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;QAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QACzD,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;IAC9D,CAAC;CACF;AArFD,gCAqFC"}
@@ -1,16 +0,0 @@
1
- export declare class GrgInverse {
2
- static golayDecodeWrapper(data: Uint8Array, hmacKey: Buffer): Uint8Array;
3
- static redstuffDecode(shards: (Uint8Array | null)[], dataShardCount?: number, parityShardCount?: number): Uint8Array;
4
- private static readonly MAX_GOLOMB_Q;
5
- /**
6
- * Decode a Golomb-Rice compressed byte stream.
7
- *
8
- * @param data - Golomb-encoded bit-packed bytes
9
- * @param m - Golomb divisor (must be power of 2, default 16)
10
- * @param originalLength - If provided, stop decoding once this many values
11
- * have been emitted. This prevents phantom trailing bytes caused by
12
- * zero-padding in the last byte of the encoded stream.
13
- */
14
- static golombDecode(data: Uint8Array, m?: number, originalLength?: number): Uint8Array;
15
- static verify(data: Uint8Array, originalShards: Uint8Array[], chainId: number, poolAddress: string): boolean;
16
- }
@@ -1,118 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GrgInverse = void 0;
4
- const crypto_1 = require("crypto");
5
- const golay_1 = require("./golay");
6
- const grg_forward_1 = require("./grg_forward");
7
- const logger_1 = require("./logger");
8
- const reed_solomon_1 = require("./reed_solomon");
9
- class GrgInverse {
10
- // 1. Golay Decoding & Integrity Check
11
- static golayDecodeWrapper(data, hmacKey) {
12
- if (data.length < 8)
13
- throw new Error("GRG shard too short for checksum");
14
- // Split data and checksum (last 8 bytes)
15
- const encoded = data.subarray(0, data.length - 8);
16
- const checksum = data.subarray(data.length - 8);
17
- // Verify HMAC-SHA256 Checksum (keyed hash, B1-5: 8 bytes truncated)
18
- const key = hmacKey;
19
- const mac = (0, crypto_1.createHmac)("sha256", key).update(Buffer.from(encoded)).digest();
20
- const expected = mac.subarray(0, 8);
21
- if (!Buffer.from(checksum).equals(Buffer.from(expected))) {
22
- throw new Error("GRG tamper detected: HMAC-SHA256 checksum mismatch");
23
- }
24
- // Proceed to Golay decode
25
- const res = (0, golay_1.golayDecode)(encoded);
26
- if (res.uncorrectable) {
27
- throw new Error("GRG tamper detected: uncorrectable bit errors in Golay codeword");
28
- }
29
- return res.data;
30
- }
31
- // 2. RedStuff Decoding (Reed-Solomon GF(2^8))
32
- static redstuffDecode(shards, dataShardCount = 4, parityShardCount = 2) {
33
- return reed_solomon_1.ReedSolomon.decode(shards, dataShardCount, parityShardCount);
34
- }
35
- /**
36
- * Decode a Golomb-Rice compressed byte stream.
37
- *
38
- * @param data - Golomb-encoded bit-packed bytes
39
- * @param m - Golomb divisor (must be power of 2, default 16)
40
- * @param originalLength - If provided, stop decoding once this many values
41
- * have been emitted. This prevents phantom trailing bytes caused by
42
- * zero-padding in the last byte of the encoded stream.
43
- */
44
- static golombDecode(data, m = 16, originalLength) {
45
- if (m < 2)
46
- throw new Error("[GRG] Golomb parameter m must be >= 2");
47
- const k = Math.log2(m);
48
- const totalBits = data.length * 8;
49
- // Helper to read a single bit from the packed byte array
50
- const readBit = (pos) => {
51
- return (data[pos >> 3] >> (7 - (pos & 7))) & 1;
52
- };
53
- const result = [];
54
- let i = 0;
55
- while (i < totalBits) {
56
- // Guard: stop at expected length to avoid processing padding bits
57
- if (originalLength !== undefined && result.length >= originalLength)
58
- break;
59
- // Read unary part: count 1-bits until a 0-bit
60
- let q = 0;
61
- while (i < totalBits && readBit(i) === 1) {
62
- q++;
63
- i++;
64
- if (q > this.MAX_GOLOMB_Q)
65
- throw new Error(`[GRG] Golomb decode: unary run exceeds ${this.MAX_GOLOMB_Q} — malformed or malicious input`);
66
- }
67
- if (i < totalBits && readBit(i) === 0)
68
- i++; // skip the 0 delimiter
69
- // Read k-bit remainder
70
- if (i + k > totalBits)
71
- break;
72
- let r = 0;
73
- for (let j = 0; j < k; j++) {
74
- r = (r << 1) | readBit(i + j);
75
- }
76
- result.push(q * m + r);
77
- i += k;
78
- }
79
- return new Uint8Array(result);
80
- }
81
- static verify(data, originalShards, chainId, poolAddress) {
82
- try {
83
- const hmacKey = grg_forward_1.GrgForward.deriveHmacKey(chainId, poolAddress);
84
- const decodedShards = originalShards.map(s => {
85
- try {
86
- return this.golayDecodeWrapper(s, hmacKey);
87
- }
88
- catch {
89
- return null;
90
- }
91
- });
92
- const withLen = this.redstuffDecode(decodedShards);
93
- // Extract original length
94
- if (withLen.length < 4)
95
- return false;
96
- const origLen = (withLen[0] << 24) | (withLen[1] << 16) | (withLen[2] << 8) | withLen[3];
97
- const compressed = withLen.subarray(4);
98
- const decoded = this.golombDecode(compressed);
99
- const final = decoded.subarray(0, origLen);
100
- if (final.length !== data.length)
101
- return false;
102
- for (let i = 0; i < data.length; i++) {
103
- if (final[i] !== data[i])
104
- return false;
105
- }
106
- return true;
107
- }
108
- catch (e) {
109
- logger_1.logger.warn(`[GRG Inverse] Verification failed: ${e}`);
110
- return false;
111
- }
112
- }
113
- }
114
- exports.GrgInverse = GrgInverse;
115
- // 3. Golomb-Rice Decompression
116
- // R4-P2-3: Max unary run length to prevent amplification DoS
117
- GrgInverse.MAX_GOLOMB_Q = 1000000;
118
- //# sourceMappingURL=grg_inverse.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"grg_inverse.js","sourceRoot":"","sources":["../src/grg_inverse.ts"],"names":[],"mappings":";;;AAAA,mCAAgD;AAChD,mCAAsC;AACtC,+CAA2C;AAC3C,qCAAkC;AAClC,iDAA6C;AAE7C,MAAa,UAAU;IAErB,sCAAsC;IACtC,MAAM,CAAC,kBAAkB,CAAC,IAAgB,EAAE,OAAe;QACzD,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;QAEzE,yCAAyC;QACzC,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAClD,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAEhD,oEAAoE;QACpE,MAAM,GAAG,GAAG,OAAO,CAAC;QACpB,MAAM,GAAG,GAAG,IAAA,mBAAU,EAAC,QAAQ,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;QAC5E,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAEpC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC;YACzD,MAAM,IAAI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IZE: number;
3
- /**
4
- * Runs the full forward pipeline:
5
- * Golomb-Rice -> RedStuff (Erasure) -> Golay(24,12)
6
- */
7
- static processForward(data: Uint8Array, chainId: number, poolAddress: string): Uint8Array[];
8
- /**
9
- * Runs the full inverse pipeline:
10
- * Golay(24,12) -> RedStuff (Reconstruction) -> Golomb-Rice Decompression
11
- */
12
- static processInverse(shards: Uint8Array[], originalLength: number, chainId: number, poolAddress: string): Uint8Array;
13
- }