forked from dsprenkels/sss
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
vectors_to_tests.js
70 lines (56 loc) · 1.89 KB
/
vectors_to_tests.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
function generate_test(number, name, shares, result) {
share_code = shares.map(x=>`
"${x}",`).join('');
parse_code = shares.map((x,i)=> `
unsigned short words_${i}[${x.split(' ').length}];
n = parse_words(shares[${i}], words_${i}, ${x.split(' ').length});`).join('')
recovery_values = shares.map((x,i) => `words_${i}`).join(', ');
return `
void test_vector_${number}(void);
void test_vector_${number}(void) {
char *name = "${name}";
unsigned int n;
char *shares[] = {${share_code}
};
char *expected_result = "${result}";
${parse_code}
const unsigned short *recovery[] = { ${recovery_values} };
unsigned char buffer[32];
char result[256];
//if( strlen(expected_result) > 0 ) {
printf("%s - ", name);
int length = combine_mnemonics(recovery, n, ${shares.length}, "TREZOR", NULL, buffer, 32);
if(length > 0) {
bufToHex(buffer, length, result, 256);
} else {
result[0] = 0;
}
if(strcmp(result,expected_result) != 0) {
printf("fail\\nexpected: '%s'\\ngot: '%s'\\n\\n", expected_result, result);
} else {
printf("pass\\n");
}
//}
}
`
}
vectors = require('./vectors.json')
console.log('#include "slip39.h"')
console.log(`
void bufToHex(unsigned char *buf, unsigned int length, char *output, int out_length);
void bufToHex(unsigned char *buf, unsigned int length, char *output, int out_length) {
for(unsigned int i=0; i<length &&2*i+1<out_length; ++i) {
sprintf(output + 2*i, "%02x", buf[i]);
}
}`)
for (number in vectors) {
[name,shares,result] = vectors[number]
console.log(generate_test(number,name,shares,result))
}
call_tests = vectors.map((x,i) => `test_vector_${i}();`).join(' \n');
console.log(`
int main(void) {
${call_tests}
return 0;
}
`)