openttt 0.1.2 → 0.2.0
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.
- package/README.md +52 -30
- package/dist/adaptive_switch.d.ts +22 -7
- package/dist/adaptive_switch.js +52 -15
- package/dist/auto_mint.d.ts +22 -7
- package/dist/auto_mint.js +107 -30
- package/dist/ct_log.d.ts +47 -0
- package/dist/ct_log.js +107 -0
- package/dist/dynamic_fee.d.ts +13 -2
- package/dist/dynamic_fee.js +62 -11
- package/dist/errors.d.ts +44 -25
- package/dist/errors.js +58 -42
- package/dist/evm_connector.d.ts +28 -1
- package/dist/evm_connector.js +124 -32
- package/dist/grg_api_client.d.ts +41 -0
- package/dist/grg_api_client.js +116 -0
- package/dist/http_client.d.ts +98 -0
- package/dist/http_client.js +252 -0
- package/dist/index.d.ts +5 -5
- package/dist/index.js +5 -5
- package/dist/logger.d.ts +36 -4
- package/dist/logger.js +70 -11
- package/dist/networks.d.ts +21 -0
- package/dist/networks.js +30 -4
- package/dist/pool_registry.d.ts +9 -0
- package/dist/pool_registry.js +37 -0
- package/dist/pot_signer.d.ts +15 -0
- package/dist/pot_signer.js +28 -0
- package/dist/protocol_fee.d.ts +42 -26
- package/dist/protocol_fee.js +77 -54
- package/dist/revenue_tiers.d.ts +36 -0
- package/dist/revenue_tiers.js +83 -0
- package/dist/signer.d.ts +1 -2
- package/dist/signer.js +72 -14
- package/dist/time_synthesis.d.ts +38 -0
- package/dist/time_synthesis.js +131 -21
- package/dist/trust_store.d.ts +49 -0
- package/dist/trust_store.js +89 -0
- package/dist/ttt_builder.d.ts +1 -1
- package/dist/ttt_builder.js +2 -2
- package/dist/ttt_client.d.ts +42 -29
- package/dist/ttt_client.js +117 -28
- package/dist/types.d.ts +46 -3
- package/dist/v4_hook.d.ts +10 -2
- package/dist/v4_hook.js +10 -2
- package/dist/x402_enforcer.d.ts +17 -2
- package/dist/x402_enforcer.js +27 -2
- package/package.json +6 -2
- package/dist/golay.d.ts +0 -6
- package/dist/golay.js +0 -166
- package/dist/grg_forward.d.ts +0 -11
- package/dist/grg_forward.js +0 -74
- package/dist/grg_inverse.d.ts +0 -7
- package/dist/grg_inverse.js +0 -100
- package/dist/grg_pipeline.d.ts +0 -13
- package/dist/grg_pipeline.js +0 -64
- package/dist/reed_solomon.d.ts +0 -12
- package/dist/reed_solomon.js +0 -179
- package/vendor/helm-crypto/golay.d.ts +0 -6
- package/vendor/helm-crypto/golay.js +0 -167
- package/vendor/helm-crypto/golay.js.map +0 -1
- package/vendor/helm-crypto/grg_forward.d.ts +0 -22
- package/vendor/helm-crypto/grg_forward.js +0 -89
- package/vendor/helm-crypto/grg_forward.js.map +0 -1
- package/vendor/helm-crypto/grg_inverse.d.ts +0 -16
- package/vendor/helm-crypto/grg_inverse.js +0 -118
- package/vendor/helm-crypto/grg_inverse.js.map +0 -1
- package/vendor/helm-crypto/grg_pipeline.d.ts +0 -13
- package/vendor/helm-crypto/grg_pipeline.js +0 -66
- package/vendor/helm-crypto/grg_pipeline.js.map +0 -1
- package/vendor/helm-crypto/index.d.ts +0 -5
- package/vendor/helm-crypto/index.js +0 -17
- package/vendor/helm-crypto/index.js.map +0 -1
- package/vendor/helm-crypto/logger.d.ts +0 -6
- package/vendor/helm-crypto/logger.js +0 -11
- package/vendor/helm-crypto/logger.js.map +0 -1
- package/vendor/helm-crypto/reed_solomon.d.ts +0 -37
- package/vendor/helm-crypto/reed_solomon.js +0 -210
- package/vendor/helm-crypto/reed_solomon.js.map +0 -1
package/dist/golay.js
DELETED
|
@@ -1,166 +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
|
-
}
|
package/dist/grg_forward.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export declare class GrgForward {
|
|
2
|
-
static golombEncode(data: Uint8Array, m?: number): Uint8Array;
|
|
3
|
-
static redstuffEncode(data: Uint8Array, shards?: number, parity?: number): Uint8Array[];
|
|
4
|
-
/**
|
|
5
|
-
* Derives an HMAC key from GRG payload context (chainId + poolAddress).
|
|
6
|
-
* Falls back to a static domain-separation key when no context is provided.
|
|
7
|
-
*/
|
|
8
|
-
static deriveHmacKey(chainId?: number, poolAddress?: string): Buffer;
|
|
9
|
-
static golayEncodeWrapper(data: Uint8Array, hmacKey?: Buffer): Uint8Array;
|
|
10
|
-
static encode(data: Uint8Array, chainId?: number, poolAddress?: string): Uint8Array[];
|
|
11
|
-
}
|
package/dist/grg_forward.js
DELETED
|
@@ -1,74 +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
|
-
// 1. Golomb-Rice Compression
|
|
10
|
-
static golombEncode(data, m = 16) {
|
|
11
|
-
if (m < 2)
|
|
12
|
-
throw new Error("[GRG] Golomb parameter m must be >= 2");
|
|
13
|
-
const k = Math.log2(m);
|
|
14
|
-
if (!Number.isInteger(k))
|
|
15
|
-
throw new Error("M must be power of 2");
|
|
16
|
-
let bits = "";
|
|
17
|
-
for (const byte of data) {
|
|
18
|
-
const q = Math.floor(byte / m);
|
|
19
|
-
const r = byte % m;
|
|
20
|
-
bits += "1".repeat(q) + "0" + r.toString(2).padStart(k, "0");
|
|
21
|
-
}
|
|
22
|
-
const bytes = [];
|
|
23
|
-
for (let i = 0; i < bits.length; i += 8) {
|
|
24
|
-
bytes.push(parseInt(bits.substring(i, i + 8).padEnd(8, "0"), 2));
|
|
25
|
-
}
|
|
26
|
-
return new Uint8Array(bytes);
|
|
27
|
-
}
|
|
28
|
-
// 2. RedStuff Erasure Coding (Reed-Solomon GF(2^8))
|
|
29
|
-
static redstuffEncode(data, shards = 4, parity = 2) {
|
|
30
|
-
return reed_solomon_1.ReedSolomon.encode(data, shards, parity);
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Derives an HMAC key from GRG payload context (chainId + poolAddress).
|
|
34
|
-
* Falls back to a static domain-separation key when no context is provided.
|
|
35
|
-
*/
|
|
36
|
-
static deriveHmacKey(chainId, poolAddress) {
|
|
37
|
-
if (chainId !== undefined && poolAddress) {
|
|
38
|
-
const packed = (0, ethers_1.keccak256)(ethers_1.AbiCoder.defaultAbiCoder().encode(["uint256", "address"], [chainId, poolAddress]));
|
|
39
|
-
return Buffer.from(packed.slice(2), "hex"); // 32 bytes
|
|
40
|
-
}
|
|
41
|
-
// Default domain-separation key when no context is available
|
|
42
|
-
return Buffer.from("grg-integrity-hmac-default-key-v1");
|
|
43
|
-
}
|
|
44
|
-
// 3. Golay(24,12) Error Correction Encoding
|
|
45
|
-
static golayEncodeWrapper(data, hmacKey) {
|
|
46
|
-
const encoded = (0, golay_1.golayEncode)(data);
|
|
47
|
-
// 🔱 Integrity: Append 8-byte HMAC-SHA256 of the encoded shard (keyed hash)
|
|
48
|
-
const key = hmacKey || this.deriveHmacKey();
|
|
49
|
-
const mac = (0, crypto_1.createHmac)("sha256", key).update(Buffer.from(encoded)).digest();
|
|
50
|
-
const checksum = mac.subarray(0, 8);
|
|
51
|
-
const final = new Uint8Array(encoded.length + 8);
|
|
52
|
-
final.set(encoded);
|
|
53
|
-
final.set(checksum, encoded.length);
|
|
54
|
-
return final;
|
|
55
|
-
}
|
|
56
|
-
static encode(data, chainId, poolAddress) {
|
|
57
|
-
// R3-P0-3: Reject empty input — roundtrip breaks ([] → [0])
|
|
58
|
-
if (data.length === 0) {
|
|
59
|
-
throw new Error("[GRG] Cannot encode empty input — roundtrip identity violation");
|
|
60
|
-
}
|
|
61
|
-
const compressed = this.golombEncode(data);
|
|
62
|
-
// Prepend original length (4 bytes, big-endian) for exact roundtrip
|
|
63
|
-
const withLen = new Uint8Array(4 + compressed.length);
|
|
64
|
-
withLen[0] = (data.length >> 24) & 0xFF;
|
|
65
|
-
withLen[1] = (data.length >> 16) & 0xFF;
|
|
66
|
-
withLen[2] = (data.length >> 8) & 0xFF;
|
|
67
|
-
withLen[3] = data.length & 0xFF;
|
|
68
|
-
withLen.set(compressed, 4);
|
|
69
|
-
const shards = this.redstuffEncode(withLen);
|
|
70
|
-
const hmacKey = this.deriveHmacKey(chainId, poolAddress);
|
|
71
|
-
return shards.map(s => this.golayEncodeWrapper(s, hmacKey));
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
exports.GrgForward = GrgForward;
|
package/dist/grg_inverse.d.ts
DELETED
|
@@ -1,7 +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
|
-
static golombDecode(data: Uint8Array, m?: number): Uint8Array;
|
|
6
|
-
static verify(data: Uint8Array, originalShards: Uint8Array[], chainId?: number, poolAddress?: string): boolean;
|
|
7
|
-
}
|
package/dist/grg_inverse.js
DELETED
|
@@ -1,100 +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 || grg_forward_1.GrgForward.deriveHmacKey();
|
|
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
|
-
// 3. Golomb-Rice Decompression
|
|
36
|
-
// R4-P2-3: Max unary run length to prevent amplification DoS
|
|
37
|
-
static MAX_GOLOMB_Q = 1_000_000;
|
|
38
|
-
static golombDecode(data, m = 16) {
|
|
39
|
-
if (m < 2)
|
|
40
|
-
throw new Error("[GRG] Golomb parameter m must be >= 2");
|
|
41
|
-
const k = Math.log2(m);
|
|
42
|
-
let bits = "";
|
|
43
|
-
for (const byte of data) {
|
|
44
|
-
bits += byte.toString(2).padStart(8, "0");
|
|
45
|
-
}
|
|
46
|
-
const result = [];
|
|
47
|
-
let i = 0;
|
|
48
|
-
while (i < bits.length) {
|
|
49
|
-
let q = 0;
|
|
50
|
-
while (bits[i] === "1") {
|
|
51
|
-
q++;
|
|
52
|
-
i++;
|
|
53
|
-
if (q > this.MAX_GOLOMB_Q)
|
|
54
|
-
throw new Error(`[GRG] Golomb decode: unary run exceeds ${this.MAX_GOLOMB_Q} — malformed or malicious input`);
|
|
55
|
-
}
|
|
56
|
-
if (bits[i] === "0")
|
|
57
|
-
i++;
|
|
58
|
-
const rStr = bits.substring(i, i + k);
|
|
59
|
-
if (rStr.length < k)
|
|
60
|
-
break;
|
|
61
|
-
const r = parseInt(rStr, 2);
|
|
62
|
-
result.push(q * m + r);
|
|
63
|
-
i += k;
|
|
64
|
-
}
|
|
65
|
-
return new Uint8Array(result);
|
|
66
|
-
}
|
|
67
|
-
static verify(data, originalShards, chainId, poolAddress) {
|
|
68
|
-
try {
|
|
69
|
-
const hmacKey = grg_forward_1.GrgForward.deriveHmacKey(chainId, poolAddress);
|
|
70
|
-
const decodedShards = originalShards.map(s => {
|
|
71
|
-
try {
|
|
72
|
-
return this.golayDecodeWrapper(s, hmacKey);
|
|
73
|
-
}
|
|
74
|
-
catch {
|
|
75
|
-
return null;
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
const withLen = this.redstuffDecode(decodedShards);
|
|
79
|
-
// Extract original length
|
|
80
|
-
if (withLen.length < 4)
|
|
81
|
-
return false;
|
|
82
|
-
const origLen = (withLen[0] << 24) | (withLen[1] << 16) | (withLen[2] << 8) | withLen[3];
|
|
83
|
-
const compressed = withLen.subarray(4);
|
|
84
|
-
const decoded = this.golombDecode(compressed);
|
|
85
|
-
const final = decoded.subarray(0, origLen);
|
|
86
|
-
if (final.length !== data.length)
|
|
87
|
-
return false;
|
|
88
|
-
for (let i = 0; i < data.length; i++) {
|
|
89
|
-
if (final[i] !== data[i])
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
catch (e) {
|
|
95
|
-
logger_1.logger.warn(`[GRG Inverse] Verification failed: ${e}`);
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
exports.GrgInverse = GrgInverse;
|
package/dist/grg_pipeline.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare class GrgPipeline {
|
|
2
|
-
static readonly MAX_INPUT_SIZE: number;
|
|
3
|
-
/**
|
|
4
|
-
* Runs the full forward pipeline:
|
|
5
|
-
* Golomb-Rice -> RedStuff (Erasure) -> Golay(24,12)
|
|
6
|
-
*/
|
|
7
|
-
static processForward(data: Uint8Array): 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): Uint8Array;
|
|
13
|
-
}
|
package/dist/grg_pipeline.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GrgPipeline = void 0;
|
|
4
|
-
const grg_forward_1 = require("./grg_forward");
|
|
5
|
-
const grg_inverse_1 = require("./grg_inverse");
|
|
6
|
-
const logger_1 = require("./logger");
|
|
7
|
-
class GrgPipeline {
|
|
8
|
-
// P1-3: Max input size to prevent OOM attacks (100 MB)
|
|
9
|
-
static MAX_INPUT_SIZE = 100 * 1024 * 1024;
|
|
10
|
-
/**
|
|
11
|
-
* Runs the full forward pipeline:
|
|
12
|
-
* Golomb-Rice -> RedStuff (Erasure) -> Golay(24,12)
|
|
13
|
-
*/
|
|
14
|
-
static processForward(data) {
|
|
15
|
-
if (data.length > this.MAX_INPUT_SIZE) {
|
|
16
|
-
throw new Error(`[GRG] Input size ${data.length} exceeds MAX_INPUT_SIZE ${this.MAX_INPUT_SIZE}`);
|
|
17
|
-
}
|
|
18
|
-
logger_1.logger.info("Starting GRG forward pipeline...");
|
|
19
|
-
try {
|
|
20
|
-
const shards = grg_forward_1.GrgForward.encode(data);
|
|
21
|
-
logger_1.logger.info(`GRG forward pipeline complete. Generated ${shards.length} shards.`);
|
|
22
|
-
return shards;
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
logger_1.logger.error(`GRG forward pipeline failed: ${error}`);
|
|
26
|
-
throw error;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Runs the full inverse pipeline:
|
|
31
|
-
* Golay(24,12) -> RedStuff (Reconstruction) -> Golomb-Rice Decompression
|
|
32
|
-
*/
|
|
33
|
-
static processInverse(shards, originalLength) {
|
|
34
|
-
logger_1.logger.info("Starting GRG inverse pipeline...");
|
|
35
|
-
try {
|
|
36
|
-
const decodedShards = shards.map(s => {
|
|
37
|
-
try {
|
|
38
|
-
return grg_inverse_1.GrgInverse.golayDecodeWrapper(s);
|
|
39
|
-
}
|
|
40
|
-
catch (e) {
|
|
41
|
-
logger_1.logger.warn(`Golay decode failed for a shard: ${e}`);
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
const withLen = grg_inverse_1.GrgInverse.redstuffDecode(decodedShards);
|
|
46
|
-
// Extract original length from the first 4 bytes
|
|
47
|
-
const decodedLength = (withLen[0] << 24) | (withLen[1] << 16) | (withLen[2] << 8) | withLen[3];
|
|
48
|
-
const compressed = withLen.subarray(4);
|
|
49
|
-
const decompressed = grg_inverse_1.GrgInverse.golombDecode(compressed);
|
|
50
|
-
const final = decompressed.subarray(0, decodedLength);
|
|
51
|
-
// P1-4 FIX: Length mismatch is a corruption signal — throw instead of warn
|
|
52
|
-
if (final.length !== originalLength) {
|
|
53
|
-
throw new Error(`[GRG] Length mismatch in inverse: expected ${originalLength}, got ${final.length}`);
|
|
54
|
-
}
|
|
55
|
-
logger_1.logger.info("GRG inverse pipeline complete.");
|
|
56
|
-
return final;
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
logger_1.logger.error(`GRG inverse pipeline failed: ${error}`);
|
|
60
|
-
throw error;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
exports.GrgPipeline = GrgPipeline;
|
package/dist/reed_solomon.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export declare class ReedSolomon {
|
|
2
|
-
private static expTable;
|
|
3
|
-
private static logTable;
|
|
4
|
-
private static initialized;
|
|
5
|
-
static init(): void;
|
|
6
|
-
static mul(a: number, b: number): number;
|
|
7
|
-
static div(a: number, b: number): number;
|
|
8
|
-
private static invertMatrix;
|
|
9
|
-
private static buildVandermonde;
|
|
10
|
-
static encode(data: Uint8Array, dataShards?: number, parityShards?: number): Uint8Array[];
|
|
11
|
-
static decode(shards: (Uint8Array | null)[], dataShards?: number, parityShards?: number): Uint8Array;
|
|
12
|
-
}
|
package/dist/reed_solomon.js
DELETED
|
@@ -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;
|