Repository navigation
Expand file tree
/
Copy pathcross-validate.js
More file actions
333 lines (280 loc) · 9.78 KB
/
Copy pathcross-validate.js
File metadata and controls
333 lines (280 loc) · 9.78 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* Cross-validation against Python reference implementation
*/
const { execSync, spawnSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const blake3 = require('./blake3.js');
const bao = require('./bao.js');
// Check Python availability
function checkPython() {
try {
execSync('python --version', { stdio: 'pipe' });
return 'python';
} catch {
try {
execSync('python3 --version', { stdio: 'pipe' });
return 'python3';
} catch {
return null;
}
}
}
const PYTHON = checkPython();
function toHex(bytes) {
return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
}
function fromHex(hex) {
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
}
return bytes;
}
// Generate test input (same as test vectors: 4-byte LE counter)
function generateInput(length) {
const input = new Uint8Array(length);
let counter = 1;
for (let i = 0; i < length; i += 4) {
const remaining = Math.min(4, length - i);
for (let j = 0; j < remaining; j++) {
input[i + j] = (counter >> (j * 8)) & 0xff;
}
counter++;
}
return input;
}
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(`PASS: ${name}`);
passed++;
} catch (e) {
console.log(`FAIL: ${name}`);
console.log(` Error: ${e.message}`);
failed++;
}
}
function assertEqual(actual, expected, msg) {
if (actual !== expected) {
throw new Error(`${msg}\n Expected: ${expected}\n Got: ${actual}`);
}
}
// Run Python bao_hash function
function pythonHash(inputBytes) {
const tempFile = path.join(__dirname, '.temp_input');
fs.writeFileSync(tempFile, Buffer.from(inputBytes));
try {
const result = spawnSync(PYTHON, ['-c', `
import sys
sys.path.insert(0, '${__dirname.replace(/\\/g, '/')}')
from docs import bao_reference
import io
with open('${tempFile.replace(/\\/g, '/')}', 'rb') as f:
data = f.read()
hash_result = bao_reference.bao_hash(io.BytesIO(data))
print(hash_result.hex())
`], { encoding: 'utf8', cwd: __dirname });
if (result.error) throw result.error;
if (result.status !== 0) throw new Error(result.stderr);
return result.stdout.trim().replace(/\r/g, '');
} finally {
try { fs.unlinkSync(tempFile); } catch {}
}
}
// Run Python bao_encode function
function pythonEncode(inputBytes, outboard = false) {
const tempFile = path.join(__dirname, '.temp_input');
fs.writeFileSync(tempFile, Buffer.from(inputBytes));
try {
const result = spawnSync(PYTHON, ['-c', `
import sys
sys.path.insert(0, '${__dirname.replace(/\\/g, '/')}')
from docs import bao_reference
with open('${tempFile.replace(/\\/g, '/')}', 'rb') as f:
data = f.read()
encoded, hash_result = bao_reference.bao_encode(data, outboard=${outboard ? 'True' : 'False'})
print(hash_result.hex())
print(encoded.hex())
`], { encoding: 'utf8', cwd: __dirname });
if (result.error) throw result.error;
if (result.status !== 0) throw new Error(result.stderr);
const lines = result.stdout.trim().replace(/\r/g, '').split('\n');
return {
hash: lines[0],
encoded: lines[1]
};
} finally {
try { fs.unlinkSync(tempFile); } catch {}
}
}
// Run Python bao_slice function
function pythonSlice(inputBytes, sliceStart, sliceLen) {
const tempFile = path.join(__dirname, '.temp_input');
const tempEncoded = path.join(__dirname, '.temp_encoded');
// First encode the input
const { encoded: encodedHex } = pythonEncode(inputBytes, false);
fs.writeFileSync(tempEncoded, Buffer.from(encodedHex, 'hex'));
try {
const result = spawnSync(PYTHON, ['-c', `
import sys
sys.path.insert(0, '${__dirname.replace(/\\/g, '/')}')
from docs import bao_reference
import io
with open('${tempEncoded.replace(/\\/g, '/')}', 'rb') as f:
encoded_data = f.read()
output = io.BytesIO()
bao_reference.bao_slice(
io.BytesIO(encoded_data),
output,
${sliceStart},
${sliceLen}
)
print(output.getvalue().hex())
`], { encoding: 'utf8', cwd: __dirname });
if (result.error) throw result.error;
if (result.status !== 0) throw new Error(result.stderr);
return result.stdout.trim().replace(/\r/g, '');
} finally {
try { fs.unlinkSync(tempFile); } catch {}
try { fs.unlinkSync(tempEncoded); } catch {}
}
}
console.log('Cross-Validation Against Python Reference');
console.log('==========================================\n');
if (!PYTHON) {
console.log('ERROR: Python not found. Cannot run cross-validation.');
console.log('Please install Python 3 and ensure it is in PATH.');
process.exit(1);
}
console.log(`Using Python: ${PYTHON}\n`);
// Create docs/__init__.py if needed for Python imports
const initFile = path.join(__dirname, 'docs', '__init__.py');
if (!fs.existsSync(initFile)) {
fs.writeFileSync(initFile, '');
}
// Rename bao-reference.py to bao_reference.py for Python import
const refOriginal = path.join(__dirname, 'docs', 'bao-reference.py');
const refRenamed = path.join(__dirname, 'docs', 'bao_reference.py');
if (fs.existsSync(refOriginal) && !fs.existsSync(refRenamed)) {
fs.copyFileSync(refOriginal, refRenamed);
}
// ============================
// BLAKE3 Hash Tests
// ============================
console.log('--- BLAKE3 Hash Comparison ---\n');
const hashTests = [
{ name: 'empty', data: new Uint8Array(0) },
{ name: '"hello"', data: new TextEncoder().encode('hello') },
{ name: '1 KB', data: generateInput(1024) },
{ name: '1025 bytes (2 chunks)', data: generateInput(1025) },
{ name: '2048 bytes', data: generateInput(2048) },
{ name: '10 KB', data: generateInput(10240) },
];
for (const tc of hashTests) {
test(`Hash ${tc.name}`, () => {
const jsHash = blake3.hashHex(tc.data);
const pyHash = pythonHash(tc.data);
assertEqual(jsHash, pyHash, `Hash mismatch for ${tc.name}`);
});
}
// ============================
// Bao Encode Tests
// ============================
console.log('\n--- Bao Encode Comparison ---\n');
const encodeTests = [
{ name: 'empty', data: new Uint8Array(0) },
{ name: '1 byte', data: new Uint8Array([0x42]) },
{ name: '100 bytes', data: generateInput(100) },
{ name: '1024 bytes (1 chunk)', data: generateInput(1024) },
{ name: '1025 bytes (2 chunks)', data: generateInput(1025) },
{ name: '2048 bytes', data: generateInput(2048) },
{ name: '2049 bytes (3 chunks)', data: generateInput(2049) },
{ name: '5000 bytes', data: generateInput(5000) },
];
for (const tc of encodeTests) {
test(`Encode ${tc.name} - hash`, () => {
const jsResult = bao.baoEncode(tc.data);
const pyResult = pythonEncode(tc.data, false);
assertEqual(toHex(jsResult.hash), pyResult.hash, `Hash mismatch for ${tc.name}`);
});
test(`Encode ${tc.name} - encoded bytes`, () => {
const jsResult = bao.baoEncode(tc.data);
const pyResult = pythonEncode(tc.data, false);
assertEqual(toHex(jsResult.encoded), pyResult.encoded, `Encoded mismatch for ${tc.name}`);
});
}
// ============================
// Bao Outboard Encode Tests
// ============================
console.log('\n--- Bao Outboard Encode Comparison ---\n');
for (const tc of encodeTests) {
test(`Outboard ${tc.name} - hash`, () => {
const jsResult = bao.baoEncode(tc.data, true);
const pyResult = pythonEncode(tc.data, true);
assertEqual(toHex(jsResult.hash), pyResult.hash, `Outboard hash mismatch for ${tc.name}`);
});
test(`Outboard ${tc.name} - encoded bytes`, () => {
const jsResult = bao.baoEncode(tc.data, true);
const pyResult = pythonEncode(tc.data, true);
assertEqual(toHex(jsResult.encoded), pyResult.encoded, `Outboard encoded mismatch for ${tc.name}`);
});
}
// ============================
// Bao Slice Tests
// ============================
console.log('\n--- Bao Slice Comparison ---\n');
const sliceTests = [
{ name: '2KB file, [0, 1024)', data: generateInput(2048), start: 0, len: 1024 },
{ name: '2KB file, [1024, 1024)', data: generateInput(2048), start: 1024, len: 1024 },
{ name: '5KB file, [1000, 2000)', data: generateInput(5120), start: 1000, len: 2000 },
{ name: '5KB file, [0, 100)', data: generateInput(5120), start: 0, len: 100 },
{ name: '10KB file, [5000, 1000)', data: generateInput(10240), start: 5000, len: 1000 },
];
for (const tc of sliceTests) {
test(`Slice ${tc.name}`, () => {
const { encoded } = bao.baoEncode(tc.data);
const jsSlice = bao.baoSlice(encoded, tc.start, tc.len);
const pySlice = pythonSlice(tc.data, tc.start, tc.len);
assertEqual(toHex(jsSlice), pySlice, `Slice mismatch for ${tc.name}`);
});
}
// ============================
// Round-trip Verification
// ============================
console.log('\n--- Round-trip Verification ---\n');
for (const tc of encodeTests) {
test(`Round-trip ${tc.name}`, () => {
// Encode with Python, decode with JS
const pyResult = pythonEncode(tc.data, false);
const pyEncoded = fromHex(pyResult.encoded);
const pyHash = fromHex(pyResult.hash);
// Decode with our JS implementation
const decoded = bao.baoDecode(pyEncoded, pyHash);
// Verify data matches
if (decoded.length !== tc.data.length) {
throw new Error(`Length mismatch: ${decoded.length} vs ${tc.data.length}`);
}
for (let i = 0; i < decoded.length; i++) {
if (decoded[i] !== tc.data[i]) {
throw new Error(`Data mismatch at index ${i}`);
}
}
});
}
// ============================
// Summary
// ============================
console.log('\n==========================================');
console.log(`Results: ${passed} passed, ${failed} failed`);
if (failed === 0) {
console.log('\nAll cross-validation tests passed!');
console.log('JavaScript implementation matches Python reference exactly.');
} else {
console.log('\nSome tests failed. Investigation needed.');
process.exit(1);
}
// Cleanup
try { fs.unlinkSync(initFile); } catch {}