-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-bao-vectors.js
More file actions
341 lines (287 loc) · 10.7 KB
/
test-bao-vectors.js
File metadata and controls
341 lines (287 loc) · 10.7 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
334
335
336
337
338
339
340
341
/**
* Tests against official Bao test vectors
* https://github.com/oconnor663/bao/blob/master/tests/test_vectors.json
*/
const fs = require('fs');
const path = require('path');
const blake3 = require('./blake3.js');
const bao = require('./bao.js');
// Load official test vectors
const vectors = JSON.parse(fs.readFileSync(path.join(__dirname, 'test-vectors.json'), 'utf8'));
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 as specified in test vectors:
* "Input bytes are generated by incrementing a 4-byte little-endian integer, starting with 1"
*/
function generateInput(length) {
const input = new Uint8Array(length);
let counter = 1;
for (let i = 0; i < length; i += 4) {
// Write counter as 4-byte little-endian
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}`);
}
}
function assertArrayEqual(actual, expected, msg) {
if (actual.length !== expected.length) {
throw new Error(`${msg}\n Length mismatch: ${actual.length} vs ${expected.length}`);
}
for (let i = 0; i < actual.length; i++) {
if (actual[i] !== expected[i]) {
throw new Error(`${msg}\n Mismatch at index ${i}: ${actual[i]} vs ${expected[i]}`);
}
}
}
console.log('Bao Official Test Vectors');
console.log('=========================\n');
// ============================
// Input Generation Verification
// ============================
console.log('--- Input Generation ---\n');
test('Input generation matches spec example', () => {
// "For example, an input of length 10 would be the bytes [1, 0, 0, 0, 2, 0, 0, 0, 3, 0]"
const input = generateInput(10);
const expected = new Uint8Array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0]);
assertArrayEqual(input, expected, 'Input generation');
});
// ============================
// Hash Tests
// ============================
console.log('\n--- Hash Tests (Official Vectors) ---\n');
for (const vec of vectors.hash) {
test(`Hash: ${vec.input_len} bytes`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input);
const actualHash = toHex(result.hash);
assertEqual(actualHash, vec.bao_hash, `Hash for ${vec.input_len} bytes`);
});
}
// ============================
// Encode Tests
// ============================
console.log('\n--- Encode Tests (Official Vectors) ---\n');
for (const vec of vectors.encode) {
test(`Encode size: ${vec.input_len} bytes -> ${vec.output_len} bytes`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input);
assertEqual(result.encoded.length, vec.output_len, `Encoded size for ${vec.input_len} bytes`);
});
test(`Encode hash: ${vec.input_len} bytes`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input);
const actualHash = toHex(result.hash);
assertEqual(actualHash, vec.bao_hash, `Bao hash for ${vec.input_len} bytes`);
});
test(`Encode content hash: ${vec.input_len} bytes`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input);
// The encoded_blake3 is the BLAKE3 hash of the entire encoding
const encodedHash = toHex(blake3.hash(result.encoded));
assertEqual(encodedHash, vec.encoded_blake3, `Encoded BLAKE3 hash for ${vec.input_len} bytes`);
});
}
// ============================
// Corruption Detection Tests
// ============================
console.log('\n--- Corruption Detection (Official Vectors) ---\n');
for (const vec of vectors.encode) {
if (vec.corruptions && vec.corruptions.length > 0) {
test(`Corruption detection: ${vec.input_len} bytes (${vec.corruptions.length} positions)`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input);
for (const pos of vec.corruptions) {
const corrupted = new Uint8Array(result.encoded);
corrupted[pos] ^= 1; // Flip one bit
let detected = false;
try {
bao.baoDecode(corrupted, result.hash);
} catch (e) {
detected = true;
}
if (!detected) {
throw new Error(`Corruption at position ${pos} not detected`);
}
}
});
}
}
// ============================
// Outboard Tests
// ============================
console.log('\n--- Outboard Tests (Official Vectors) ---\n');
for (const vec of vectors.outboard) {
test(`Outboard size: ${vec.input_len} bytes -> ${vec.output_len} bytes`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input, true);
assertEqual(result.encoded.length, vec.output_len, `Outboard size for ${vec.input_len} bytes`);
});
test(`Outboard hash: ${vec.input_len} bytes`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input, true);
const actualHash = toHex(result.hash);
assertEqual(actualHash, vec.bao_hash, `Outboard hash for ${vec.input_len} bytes`);
});
test(`Outboard content hash: ${vec.input_len} bytes`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input, true);
const encodedHash = toHex(blake3.hash(result.encoded));
assertEqual(encodedHash, vec.encoded_blake3, `Outboard BLAKE3 hash for ${vec.input_len} bytes`);
});
}
// ============================
// Outboard Corruption Detection
// ============================
console.log('\n--- Outboard Corruption Detection ---\n');
for (const vec of vectors.outboard) {
// Test encoded_corruptions (corruption in the outboard tree)
if (vec.encoded_corruptions && vec.encoded_corruptions.length > 0) {
test(`Outboard tree corruption: ${vec.input_len} bytes`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input, true);
for (const pos of vec.encoded_corruptions) {
const corrupted = new Uint8Array(result.encoded);
corrupted[pos] ^= 1;
let detected = false;
try {
bao.baoDecode(corrupted, result.hash, input);
} catch (e) {
detected = true;
}
if (!detected) {
throw new Error(`Outboard tree corruption at position ${pos} not detected`);
}
}
});
}
// Test input_corruptions (corruption in the original data)
if (vec.input_corruptions && vec.input_corruptions.length > 0) {
test(`Outboard data corruption: ${vec.input_len} bytes`, () => {
const input = generateInput(vec.input_len);
const result = bao.baoEncode(input, true);
for (const pos of vec.input_corruptions) {
const corruptedInput = new Uint8Array(input);
corruptedInput[pos] ^= 1;
let detected = false;
try {
bao.baoDecode(result.encoded, result.hash, corruptedInput);
} catch (e) {
detected = true;
}
if (!detected) {
throw new Error(`Outboard input corruption at position ${pos} not detected`);
}
}
});
}
}
// ============================
// Slice Tests
// ============================
console.log('\n--- Slice Tests (Official Vectors) ---\n');
for (const vec of vectors.slice) {
for (const slice of vec.slices) {
test(`Slice: ${vec.input_len} bytes, range [${slice.start}, ${slice.start + slice.len})`, () => {
const input = generateInput(vec.input_len);
const { encoded, hash } = bao.baoEncode(input);
// Extract slice
const sliceData = bao.baoSlice(encoded, slice.start, slice.len);
// Verify slice size matches expected
assertEqual(sliceData.length, slice.output_len, `Slice size for [${slice.start}, ${slice.len})`);
// Verify BLAKE3 hash of slice matches expected
const sliceHash = toHex(blake3.hash(sliceData));
assertEqual(sliceHash, slice.output_blake3, `Slice BLAKE3 hash`);
// Verify slice decodes correctly
const decoded = bao.baoDecodeSlice(sliceData, hash, slice.start, slice.len);
// Calculate expected decoded content
const expectedStart = Math.min(slice.start, vec.input_len);
const expectedEnd = Math.min(slice.start + slice.len, vec.input_len);
const expectedLen = Math.max(0, expectedEnd - expectedStart);
assertEqual(decoded.length, expectedLen, `Decoded slice length`);
if (expectedLen > 0) {
assertArrayEqual(decoded, input.subarray(expectedStart, expectedEnd), `Decoded slice content`);
}
});
}
}
// ============================
// Slice Corruption Detection
// ============================
console.log('\n--- Slice Corruption Detection ---\n');
for (const vec of vectors.slice) {
for (const slice of vec.slices) {
if (slice.corruptions && slice.corruptions.length > 0) {
test(`Slice corruption: ${vec.input_len} bytes, [${slice.start}, ${slice.start + slice.len})`, () => {
const input = generateInput(vec.input_len);
const { encoded, hash } = bao.baoEncode(input);
const sliceData = bao.baoSlice(encoded, slice.start, slice.len);
for (const pos of slice.corruptions) {
const corrupted = new Uint8Array(sliceData);
corrupted[pos] ^= 1;
let detected = false;
try {
bao.baoDecodeSlice(corrupted, hash, slice.start, slice.len);
} catch (e) {
detected = true;
}
if (!detected) {
throw new Error(`Slice corruption at position ${pos} not detected`);
}
}
});
}
}
}
// ============================
// Round-trip Verification
// ============================
console.log('\n--- Round-trip Verification ---\n');
for (const vec of vectors.hash) {
test(`Round-trip: ${vec.input_len} bytes`, () => {
const input = generateInput(vec.input_len);
const { encoded, hash } = bao.baoEncode(input);
const decoded = bao.baoDecode(encoded, hash);
assertArrayEqual(decoded, input, `Round-trip for ${vec.input_len} bytes`);
});
}
// ============================
// Summary
// ============================
console.log('\n=========================');
console.log(`Results: ${passed} passed, ${failed} failed`);
if (failed === 0) {
console.log('\nAll official Bao test vectors passed!');
} else {
console.log('\nSome tests failed.');
process.exit(1);
}