-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
114 lines (101 loc) · 2.77 KB
/
test.js
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
import t from './index.js';
dchest_tests();
smhasher_verification_value();
standard_test();
function dchest_tests() {
const cases = [
{
run: () => t.hash(0.00000000000000001),
h: '0000000000000000'
},
{
run: () => t.hash(0.00000000000000002),
h: '00000000ffffffff'
},
{
run: () => t.hash(0.00000000000000003),
h: '00000000ffffffff'
},
{
run: () => t.hash(0.00000000000000004),
h: '00000000ffffffff'
},
{
run: () => t.hash(0.00000000000000005),
h: '00000000fdffffff'
},
{
run: () => t.hash(1e17),
h: '5555555597d44646'
},
{
run: () => t.hash(1e18),
h: '55555555ac43d2d1'
},
{
run: () => t.hash(1e19),
h: '55555555acd2b64f'
},
{
run: () => t.hash('\x00'),
h: '0000000000000000'
},
{
run: () => t.hash('\x00\x00'),
h: '5555555592244992'
},
{
run: () => t.hash('\x00\x00\x00'),
h: '0000000000000000'
},
{
run: () => t.hash('\x01'),
h: '9224499200000000'
},
{
run: () => t.hash('\x02'),
h: '33333333dedddddd'
}
];
cases.forEach( (c,i) => {
const r = c.run();
console.log( `Running case ${i}`, c.run );
if ( r == c.h ) {
console.log( `Case ${i} verified: ${r} == ${c.h}` );
} else {
console.log( `ALTERATION: ${r}` );
console.log( `Case ${i} altered: ${r} !== ${c.h}` );
}
});
}
function smhasher_verification_value() {
// Copied from <smhasher_repo>/src/KeysetTests.cpp
const hashbytes = 8;
const key = new Uint8Array( 256 );
const hashes = new Uint8Array( 256 * hashbytes );
const spec8 = {
out_format: 'bytes',
bits: hashbytes*8
};
// Hash keys of the form {0}, {0,1}, {0,1,2}... up to N=255,using 256-N as
// the seed
for ( let i = 0; i < 256; i++ ) {
key[i] = i;
const seed = 256-i;
const hash = t.hash(key.slice(0,i), spec8, seed );
hashes.set( hash, i*hashbytes );
}
// Then hash the result array
const final32 = t.hash(hashes,{ out_format: 'uint32s', bits: 64 }, 0);
const final8 = t.hash(hashes,{ out_format: 'bytes', bits: 64 }, 0);
let v_val8 = (final8[0] << 0) | (final8[1] << 8) | (final8[2] << 16) | (final8[3] << 24);
v_val8 = t.pad(8, v_val8.toString(16));
let v_val32 = t.pad(8, final32[0].toString(16));
// note this is different to the C++ value because
// of casting differences between JS and C
console.log( `JS Verification value ${v_val8} ${v_val32}` );
return v_val32;
}
function standard_test() {
t.test();
}