-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathunittests.js
133 lines (111 loc) · 6.46 KB
/
unittests.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* Author: Willem Hengeveld <itsme@xs4all.nl> */
/* https://github.com/nlitsme/bitcoinexplainer */
"use strict";
function runtests()
{
print(el('p'));
print("start");
CHECK("GCD(123, 996)", GCD(123, 996), [3, 81, -10]);
CHECK("GCD(126, 996)", GCD(126, 996), [6, -79, 10]);
CHECK("GCD(996, 126)", GCD(996, 126), [6, 10, -79]);
CHECK("GCD(996, 1)", GCD(996, 1), [1, 0, 1]);
CHECK("GCD(13, 17)", GCD(13, 17), [1, 4, -3]);
CHECK("gcd(996, 126)", gcd(996, 126), 6);
CHECK("lcm(996, 126)", lcm(996, 126), 20916);
CHECK("modinv(111,997)==503", modinv(111, 997), 503);
CHECK("numshr(111)==[1,55]", numshr(111), [1,55]);
CHECK("numshr(110)==[0,55]", numshr(110), [0,55]);
CHECK("numzero(110)==0", numzero(110), 0);
CHECK("numzero(110n)==0n", numzero(110n), 0n);
CHECK("modexp(123,2,997)==174", modexp(123,2,997), 174);
CHECK("modexp(123,900,997)==192", modexp(123,900,997), 192);
var F = new GaloisField(997);
CHECK("123+123==246", F.value(123).add(F.value(123)), F.value(246));
CHECK("100+900==3", F.value(100).add(F.value(900)), F.value(3));
CHECK("100*2==200", F.value(100).mul(F.value(2)), F.value(200));
CHECK("100*2==200", F.value(100).mul(2), F.value(200));
CHECK("900*2==803", F.value(900).mul(2), F.value(803));
CHECK("100*900==270", F.value(100).mul(F.value(900)), F.value(270));
CHECK("123^900==192", F.value(123).pow(F.value(900)), F.value(192));
CHECK("111^2==357", F.value(111).pow(F.value(2)), F.value(357));
CHECK("111/222==499", F.value(111).div(F.value(222)), F.value(499));
CHECK("222*499==111", F.value(222).mul(F.value(499)), F.value(111));
// 997%8==5, %9==7
// case where res==1
CHECK("(.5) 860.sqrt(0)==94", F.value(860).sqrt(0), F.value(94));
CHECK("(.5) 860.sqrt(1)==903", F.value(860).sqrt(1), F.value(903));
// case where res!=1
CHECK("(.5) 555.sqrt(0)==620", F.value(555).sqrt(0), F.value(620));
CHECK("(.5) 555.sqrt(1)==377", F.value(555).sqrt(1), F.value(377));
CHECK("(.7) 111.cubert(0)==749", F.value(111).cubert(0), F.value(749));
CHECK("(.7) 111.cubert(1)==380", F.value(111).cubert(1), F.value(380));
CHECK("(.7) 111.cubert(2)==865", F.value(111).cubert(2), F.value(865));
var F = new GaloisField(977); // %8 ==1
CHECK("(.1) 111.sqrt(0)==452", F.value(111).sqrt(0), F.value(452));
CHECK("(.1) 111.sqrt(1)==525", F.value(111).sqrt(1), F.value(525));
//CHECK("(.1) 111.cubert(0)==840", F.value(111).cubert(0), F.value(840));
var F = new GaloisField(971); // %8==3 %3==2
CHECK("(.3) 111.sqrt(0)==800", F.value(111).sqrt(0), F.value(800));
CHECK("(.3) 111.sqrt(1)==171", F.value(111).sqrt(1), F.value(171));
CHECK("(.2) 111.cubert(0)==349", F.value(111).cubert(0), F.value(349));
var F = new GaloisField(991); // %8==7 %27==19
CHECK("(.7) 111.sqrt(0)==280", F.value(111).sqrt(0), F.value(280));
CHECK("(.7) 111.sqrt(1)==711", F.value(111).sqrt(1), F.value(711));
CHECK("(.19) 111.cubert(0)==770", F.value(111).cubert(0), F.value(770));
CHECK("(.19) 111.cubert(1)==793", F.value(111).cubert(1), F.value(793));
CHECK("(.19) 111.cubert(2)==419", F.value(111).cubert(2), F.value(419));
var F = new GaloisField(947);
var val947 = F.value(263);
CHECK("(.x) 263.pow(0)==1", F.value(263).pow(0), F.value(1));
CHECK("(.x) 263.pow(1)==263", F.value(263).pow(1), F.value(263));
CHECK("(.x) 263.pow(2)==38", F.value(263).pow(2), F.value(38));
CHECK("(.x) 263.pow(237)==274", F.value(263).pow(237), F.value(274));
CHECK("(.x) 263.sqrt(0)==274", F.value(263).sqrt(0), F.value(274));
CHECK("(.x) 263.sqrt(1)==673", F.value(263).sqrt(1), F.value(673));
CHECK("(.x) 684.sqrt(0)==undef", F.value(684).sqrt(0)==undefined);
var F = new GaloisField(967);
CHECK("F.val(val947).p = 967", F.value(val947).field.p, F.p);
CHECK("(.4) 118.cubert(0)==877", F.value(118).cubert(0), F.value(877));
CHECK("(.4) 118.cubert(1)==758", F.value(118).cubert(1), F.value(758));
CHECK("(.4) 118.cubert(2)==299", F.value(118).cubert(2), F.value(299));
var F = new GaloisField(739);
CHECK("(.10) 129.cubert(0)==719", F.value(129).cubert(0), F.value(719));
CHECK("(.10) 129.cubert(1)==251", F.value(129).cubert(1), F.value(251));
CHECK("(.10) 129.cubert(2)==508", F.value(129).cubert(2), F.value(508));
// ec tests
var F = new GaloisField(997);
var E = new EllipticCurve(F, 0, 7);
CHECK("(131,94).ispt", E.point(131, 94).isoncurve());
CHECK("!(131,94).isinf", !E.point(131, 94).isinf());
CHECK("inf.ispt", E.infinity().isoncurve());
CHECK("inf.isinf", E.infinity().isinf());
CHECK("!(131,93).ispt", !E.point(131 , 93).isoncurve());
CHECK("(131,94)==(131,94)", E.point(131 , 94), E.point(131 , 94));
CHECK("-(131,94)==(131,-94)", E.point(131 , 94).neg(), E.point(131, -94));
CHECK("-(131,94)+(131,94) isinf", E.point(131 , 94).neg().add(E.point(131, 94)).isinf());
CHECK("-(131,94)+(131,94) == inf", E.point(131 , 94).neg().add(E.point(131, 94)), E.infinity());
CHECK("-(131,94)==(131,903)", E.point(131 , 94).neg(), E.point(131, 997-94));
CHECK("(131,94)*2==(630,721)", E.point(131 , 94).mul(2), E.point(630, 721));
CHECK("(131,94)+(131,94)==(630,721)", E.point(131 , 94).add(E.point(131 , 94)), E.point(630, 721));
CHECK("(131,94)+(77,393)==(368,609)", E.point(131 , 94).add(E.point(77, 393)), E.point(368, 609));
var F = new GaloisField(997n);
CHECK("100n+900n==3n", F.value(100n).add(F.value(900n)), F.value(3n));
CHECK("(.5) 860n.sqrt(0)==94n", F.value(860n).sqrt(0), F.value(94n));
// case where res!=1
CHECK("(.5) 555n.sqrt(1)==377n", F.value(555n).sqrt(1), F.value(377n));
CHECK("(.7) 111n.cubert(0)==749n", F.value(111n).cubert(0), F.value(749n));
var E = new EllipticCurve(F, 0n, 7n);
CHECK("(131n,94n)*2n==(630n,721n)", E.point(131n , 94n).mul(2n), E.point(630n, 721n));
// for tests: 907: n=967, 967:n=906, 991: n=1009
var F = new GaloisField(991);
var E = new EllipticCurve(F, 0, 7);
CHECK("decomp(1,0) = (1,840)", E.decompress(F.value(1), 0), E.point(1, 840));
CHECK("decomp(1,1) = (1,151)", E.decompress(F.value(1), 1), E.point(1, 151));
var B = secp256k1();
CHECK("decomp(g.x,0) = G", B.ec.decompress(B.G.x, 0), B.G);
CHECK("decomp(g.x,1) = G", B.ec.decompress(B.G.x, 1), B.G.neg());
CHECK("sum(10-primes) = xx", [...genprimes(10)].reduce((a,b)=>a+b), 17);
CHECK("sum(100-primes) = xx", [...genprimes(100)].reduce((a,b)=>a+b), 1060);
print("done", el('br'));
STATS();
}