-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_test.go
431 lines (398 loc) · 41.6 KB
/
crypto_test.go
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func fixedTestTime() int64 {
return 1623517380172
}
func TestProcessPassword(t *testing.T) {
r := new(rsa)
r.init()
r.rng.resetPool()
r.rng.time = fixedTestTime
tests := []struct {
name string
input string
t1Assertion string
output string
}{
{
name: "test 1",
input: "AAAA",
t1Assertion: "ozzutFJrd0LBW45xy1kS",
output: "UGHzEeAP30kBQ3RhCnScMsTkBs4czq4aYffk961oZXZDEt9enDtjz3umyFundtQ0+NAPF88oVXZO5T9ztc7lR9iqmHx1s8uVsyMBTgH9v8Q/UJrcIP6GQjdjJ+cH0YN956fNWpSpAKZvN6vQ0Gl3/1ri4npbw6bb4D385xhNIeKUV7GXC5HmetDGpJearHtiI7eYbls+h1qJ09DcqCqcM6Aos9AMQLFSACm+NX+5D4ZzCFReWKjzGeFU2bpXba5GTnyIyimufZx3Q48nQPTTQbgXtit1pZoWzeBdb0ZOnAbAIk2TJQZ5+fg2fL+0L+q/pgjA24OoMJ7YvmxZsDcE5Q==",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := r.processPassword(tt.input, tt.t1Assertion)
assert.Equal(t, tt.output, actual)
})
}
}
func TestRsaSetPublic(t *testing.T) {
k := rsaKey{}
k.setPublic(
"A6CA1BB4BD803E5704A071E8F7370FD68F2A42CAB574A765693F0F54796CB8AD2CF1B624005119FE651227F7992FF6A6D1979C9B72EA0EAD789F1CBADAB9851779CB8F5F82F40BC71C5C303A10298ED6DC5657E3401AE5720F06836F098366441AC30AB35F13FAB8B6CE81955A1181FCA0AD4EA471CC09C51EAE8EDA42E8C615F933483449CBC67883F407430CB856E4EEC1919BFDD38850CCF5837EC67D8CF802EC30836099592FCDB6CEF4D4AB8EC7F95229B6B262DC6F9A62BFD082CCF98D8FC73FADFA2CCBDDBD17126206E0EC41FE85ECDB9B7631A7EDEF193E4971ADA3E4AB3FFE05F5146907255AD29D0AFB91160C95E225514E1CD07E35BA157A44D1",
"10001",
)
assert.Equal(t, int64(65537), k.e)
assert.Equal(t, bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456,
F1: 24,
F2: 4,
FV: 4503599627370496,
arr: []int64{91899089, 132340641, 21896400, 157164117, 194057740, 220844207, 151463258, 6246726, 78331902, 119200318, 253312585, 52068062, 215718774, 69199966, 34005228, 198275366, 170707933, 208927455, 217681295, 201132076, 208640610, 191571501, 133780009, 223000812, 230084340, 160797436, 204505952, 147816494, 58639997, 84725592, 201184136, 250353945, 213407460, 255882288, 197556355, 75711644, 102103347, 228863628, 85896846, 119324828, 11357860, 18358218, 243373402, 262900588, 179527443, 71412784, 252281702, 15755318, 1762674, 90537524, 160356060, 50569474, 197598300, 100151104, 125422479, 229349457, 144645306, 245426903, 127703922, 258632985, 133667119, 266752290, 67129625, 47127394, 158120109, 66123079, 78079337, 70036311, 265719594, 244282224, 117743729, 198706149, 113908660, 10},
}, k.n)
assert.Equal(t, 2048, k.n.bitLength())
}
func TestRsaEncrypt(t *testing.T) {
t1Assertion := "ozzutFJrd0LBW45xy1kS"
k := rsaKey{}
k.setPublic(
"A6CA1BB4BD803E5704A071E8F7370FD68F2A42CAB574A765693F0F54796CB8AD2CF1B624005119FE651227F7992FF6A6D1979C9B72EA0EAD789F1CBADAB9851779CB8F5F82F40BC71C5C303A10298ED6DC5657E3401AE5720F06836F098366441AC30AB35F13FAB8B6CE81955A1181FCA0AD4EA471CC09C51EAE8EDA42E8C615F933483449CBC67883F407430CB856E4EEC1919BFDD38850CCF5837EC67D8CF802EC30836099592FCDB6CEF4D4AB8EC7F95229B6B262DC6F9A62BFD082CCF98D8FC73FADFA2CCBDDBD17126206E0EC41FE85ECDB9B7631A7EDEF193E4971ADA3E4AB3FFE05F5146907255AD29D0AFB91160C95E225514E1CD07E35BA157A44D1",
"10001",
)
rng := randNumGen{}
rng.resetPool()
rng.time = fixedTestTime
actual := k.encrypt(&rng, "AAAA"+"|"+t1Assertion)
assert.Equal(t,
"5061f311e00fdf49014374610a749c32c4e406ce1cceae1a61f7e4f7ad6865764312df5e9c3b63cf7ba6c85ba776d434f8d00f17cf2855764ee53f73b5cee547d8aa987c75b3cb95b323014e01fdbfc43f509adc20fe8642376327e707d1837de7a7cd5a94a900a66f37abd0d06977ff5ae2e27a5bc3a6dbe03dfce7184d21e29457b1970b91e67ad0c6a4979aac7b6223b7986e5b3e875a89d3d0dca82a9c33a028b3d00c40b1520029be357fb90f867308545e58a8f319e154d9ba576dae464e7c88ca29ae7d9c77438f2740f4d341b817b62b75a59a16cde05d6f464e9c06c0224d93250679f9f8367cbfb42feabfa608c0db83a8309ed8be6c59b03704e5",
actual)
}
func TestHex2B64(t *testing.T) {
expected := `UGHzEeAP30kBQ3RhCnScMsTkBs4czq4aYffk961oZXZDEt9enDtjz3umyFundtQ0+NAPF88oVXZO5T9ztc7lR9iqmHx1s8uVsyMBTgH9v8Q/UJrcIP6GQjdjJ+cH0YN956fNWpSpAKZvN6vQ0Gl3/1ri4npbw6bb4D385xhNIeKUV7GXC5HmetDGpJearHtiI7eYbls+h1qJ09DcqCqcM6Aos9AMQLFSACm+NX+5D4ZzCFReWKjzGeFU2bpXba5GTnyIyimufZx3Q48nQPTTQbgXtit1pZoWzeBdb0ZOnAbAIk2TJQZ5+fg2fL+0L+q/pgjA24OoMJ7YvmxZsDcE5Q==`
actual := hex2b64(`5061f311e00fdf49014374610a749c32c4e406ce1cceae1a61f7e4f7ad6865764312df5e9c3b63cf7ba6c85ba776d434f8d00f17cf2855764ee53f73b5cee547d8aa987c75b3cb95b323014e01fdbfc43f509adc20fe8642376327e707d1837de7a7cd5a94a900a66f37abd0d06977ff5ae2e27a5bc3a6dbe03dfce7184d21e29457b1970b91e67ad0c6a4979aac7b6223b7986e5b3e875a89d3d0dca82a9c33a028b3d00c40b1520029be357fb90f867308545e58a8f319e154d9ba576dae464e7c88ca29ae7d9c77438f2740f4d341b817b62b75a59a16cde05d6f464e9c06c0224d93250679f9f8367cbfb42feabfa608c0db83a8309ed8be6c59b03704e5`)
assert.Equal(t, expected, actual)
}
func TestNBits(t *testing.T) {
tests := []struct {
name string
input int64
output int
}{
{name: "test 1", input: 1, output: 1},
{name: "test 2", input: 2, output: 2},
{name: "test 3", input: 3, output: 2},
{name: "test 4", input: 4, output: 3},
{name: "test 5", input: 8, output: 4},
{name: "test 6", input: 2048, output: 12},
{name: "test 7", input: 65537, output: 17},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := nbits(tt.input)
assert.Equal(t, tt.output, actual)
})
}
}
func TestPkcs1pad2(t *testing.T) {
r := new(randNumGen)
r.resetPool()
r.time = fixedTestTime
actual := pkcs1pad2(r, `AAAA|ozzutFJrd0LBW45xy1kS`, 256)
assert.Equal(t, bigInteger{
t: 73, s: 0,
DB: 28, DM: 268435455, DV: 268435456, FV: 4503599627370496, F1: 24, F2: 4,
arr: []int64{154233683, 54744967, 4997719, 78063171, 175469638, 130480039, 21053761, 161587204, 268394322, 121434164, 78418508, 3550946, 12455929, 24891754, 185073407, 228296779, 244302159, 107191025, 116453635, 123234106, 173845849, 224383917, 99891756, 191404168, 52664173, 1980702, 201686014, 136607849, 22370042, 4648679, 172316499, 36552800, 151325320, 54152351, 158085758, 100144992, 126304478, 252283695, 75706, 13934210, 127988695, 244294746, 137659364, 58017546, 172378819, 95552461, 9528036, 65724189, 172768429, 66297275, 136154050, 164523179, 22702920, 226522046, 66709101, 110240193, 210752781, 190639350, 171925521, 204453797, 100106205, 47008966, 55805816, 258696058, 224370946, 217025253, 7518543, 251152916, 82614777, 18006155, 125838249, 35991357, 195526, 0},
}, actual)
}
func TestBigIntegerAbs(t *testing.T) {
x := bigInteger{
t: 73, s: 0,
DB: 28, DM: 268435455, DV: 268435456, FV: 4503599627370496, F1: 24, F2: 4,
arr: []int64{154233683, 54744967, 4997719, 78063171, 175469638, 130480039, 21053761, 161587204, 268394322, 121434164, 78418508, 3550946, 12455929, 24891754, 185073407, 228296779, 244302159, 107191025, 116453635, 123234106, 173845849, 224383917, 99891756, 191404168, 52664173, 1980702, 201686014, 136607849, 22370042, 4648679, 172316499, 36552800, 151325320, 54152351, 158085758, 100144992, 126304478, 252283695, 75706, 13934210, 127988695, 244294746, 137659364, 58017546, 172378819, 95552461, 9528036, 65724189, 172768429, 66297275, 136154050, 164523179, 22702920, 226522046, 66709101, 110240193, 210752781, 190639350, 171925521, 204453797, 100106205, 47008966, 55805816, 258696058, 224370946, 217025253, 7518543, 251152916, 82614777, 18006155, 125838249, 35991357, 195526, 0},
}
actual := x.abs()
assert.Equal(t, bigInteger{
s: 0,
t: 73,
DB: 28, DM: 268435455, DV: 268435456, FV: 4503599627370496, F1: 24, F2: 4,
arr: []int64{154233683, 54744967, 4997719, 78063171, 175469638, 130480039, 21053761, 161587204, 268394322, 121434164, 78418508, 3550946, 12455929, 24891754, 185073407, 228296779, 244302159, 107191025, 116453635, 123234106, 173845849, 224383917, 99891756, 191404168, 52664173, 1980702, 201686014, 136607849, 22370042, 4648679, 172316499, 36552800, 151325320, 54152351, 158085758, 100144992, 126304478, 252283695, 75706, 13934210, 127988695, 244294746, 137659364, 58017546, 172378819, 95552461, 9528036, 65724189, 172768429, 66297275, 136154050, 164523179, 22702920, 226522046, 66709101, 110240193, 210752781, 190639350, 171925521, 204453797, 100106205, 47008966, 55805816, 258696058, 224370946, 217025253, 7518543, 251152916, 82614777, 18006155, 125838249, 35991357, 195526, 0},
}, actual)
}
func TestBigIntegerDlShiftTo(t *testing.T) {
x := bigInteger{
s: 0,
t: 73,
DB: 28, DM: 268435455, DV: 268435456, FV: 4503599627370496, F1: 24, F2: 4,
arr: []int64{154233683, 54744967, 4997719, 78063171, 175469638, 130480039, 21053761, 161587204, 268394322, 121434164, 78418508, 3550946, 12455929, 24891754, 185073407, 228296779, 244302159, 107191025, 116453635, 123234106, 173845849, 224383917, 99891756, 191404168, 52664173, 1980702, 201686014, 136607849, 22370042, 4648679, 172316499, 36552800, 151325320, 54152351, 158085758, 100144992, 126304478, 252283695, 75706, 13934210, 127988695, 244294746, 137659364, 58017546, 172378819, 95552461, 9528036, 65724189, 172768429, 66297275, 136154050, 164523179, 22702920, 226522046, 66709101, 110240193, 210752781, 190639350, 171925521, 204453797, 100106205, 47008966, 55805816, 258696058, 224370946, 217025253, 7518543, 251152916, 82614777, 18006155, 125838249, 35991357, 195526, 0},
}
actual := nbi()
x.dlShiftTo(74, actual)
assert.Equal(t, bigInteger{
s: 0,
t: 147,
DB: 28, DM: 268435455, DV: 268435456, FV: 4503599627370496, F1: 24, F2: 4,
arr: []int64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 154233683, 54744967, 4997719, 78063171, 175469638, 130480039, 21053761, 161587204, 268394322, 121434164, 78418508, 3550946, 12455929, 24891754, 185073407, 228296779, 244302159, 107191025, 116453635, 123234106, 173845849, 224383917, 99891756, 191404168, 52664173, 1980702, 201686014, 136607849, 22370042, 4648679, 172316499, 36552800, 151325320, 54152351, 158085758, 100144992, 126304478, 252283695, 75706, 13934210, 127988695, 244294746, 137659364, 58017546, 172378819, 95552461, 9528036, 65724189, 172768429, 66297275, 136154050, 164523179, 22702920, 226522046, 66709101, 110240193, 210752781, 190639350, 171925521, 204453797, 100106205, 47008966, 55805816, 258696058, 224370946, 217025253, 7518543, 251152916, 82614777, 18006155, 125838249, 35991357, 195526},
}, *actual)
}
func TestNbv(t *testing.T) {
assert.Equal(t, bigInteger{
t: 1, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{1},
}, *nbv(1))
}
func TestBigIntegerToString(t *testing.T) {
m := bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{91899089, 132340641, 21896400, 157164117, 194057740, 220844207, 151463258, 6246726, 78331902, 119200318, 253312585, 52068062, 215718774, 69199966, 34005228, 198275366, 170707933, 208927455, 217681295, 201132076, 208640610, 191571501, 133780009, 223000812, 230084340, 160797436, 204505952, 147816494, 58639997, 84725592, 201184136, 250353945, 213407460, 255882288, 197556355, 75711644, 102103347, 228863628, 85896846, 119324828, 11357860, 18358218, 243373402, 262900588, 179527443, 71412784, 252281702, 15755318, 1762674, 90537524, 160356060, 50569474, 197598300, 100151104, 125422479, 229349457, 144645306, 245426903, 127703922, 258632985, 133667119, 266752290, 67129625, 47127394, 158120109, 66123079, 78079337, 70036311, 265719594, 244282224, 117743729, 198706149, 113908660, 10},
}
actual := m.toString(16)
assert.Equal(t, "a6ca1bb4bd803e5704a071e8f7370fd68f2a42cab574a765693f0f54796cb8ad2cf1b624005119fe651227f7992ff6a6d1979c9b72ea0ead789f1cbadab9851779cb8f5f82f40bc71c5c303a10298ed6dc5657e3401ae5720f06836f098366441ac30ab35f13fab8b6ce81955a1181fca0ad4ea471cc09c51eae8eda42e8c615f933483449cbc67883f407430cb856e4eec1919bfdd38850ccf5837ec67d8cf802ec30836099592fcdb6cef4d4ab8ec7f95229b6b262dc6f9a62bfd082ccf98d8fc73fadfa2ccbddbd17126206e0ec41fe85ecdb9b7631a7edef193e4971ada3e4ab3ffe05f5146907255ad29d0afb91160c95e225514e1cd07e35ba157a44d1", actual)
assert.Equal(t, "0", bigIntegerZero.toString(16))
assert.Equal(t, "1", bigIntegerOne.toString(16))
}
func TestBigIntegerClamp(t *testing.T) {
pm := bigInteger{
t: 75, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{16777216, 22520909, 8271290, 85254605, 211149349, 263786848, 181574922, 110129749, 235271444, 239776767, 158444963, 250713060, 103917549, 248363447, 205651589, 102788622, 230496018, 262327485, 264716205, 214931672, 46125186, 231143846, 162968162, 209687842, 81046414, 215706863, 10049839, 247662646, 227342338, 137882727, 139513077, 163568952, 82755985, 13337966, 66324291, 213673864, 55063625, 207708051, 249185000, 206695144, 74566665, 168482026, 168919548, 216537429, 66762934, 11220465, 105126595, 116430902, 34539139, 67219031, 206985187, 43576685, 204487184, 12349893, 257917684, 24616120, 182106501, 126480843, 48893613, 158976439, 267822801, 41908626, 167666962, 37750033, 221049270, 127323018, 155127636, 122320470, 172149429, 16607474, 32044855, 91245063, 79527998, 174891451, 0},
}
pm.clamp()
assert.Equal(t,
bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{16777216, 22520909, 8271290, 85254605, 211149349, 263786848, 181574922, 110129749, 235271444, 239776767, 158444963, 250713060, 103917549, 248363447, 205651589, 102788622, 230496018, 262327485, 264716205, 214931672, 46125186, 231143846, 162968162, 209687842, 81046414, 215706863, 10049839, 247662646, 227342338, 137882727, 139513077, 163568952, 82755985, 13337966, 66324291, 213673864, 55063625, 207708051, 249185000, 206695144, 74566665, 168482026, 168919548, 216537429, 66762934, 11220465, 105126595, 116430902, 34539139, 67219031, 206985187, 43576685, 204487184, 12349893, 257917684, 24616120, 182106501, 126480843, 48893613, 158976439, 267822801, 41908626, 167666962, 37750033, 221049270, 127323018, 155127636, 122320470, 172149429, 16607474, 32044855, 91245063, 79527998, 174891451, 0},
}, pm)
}
/*
const pm = parseBigInt('a6ca1bb4bd803e5704a071e8f7370fd68f2a42cab574a765693f0f54796cb8ad2cf1b624005119fe651227f7992ff6a6d1979c9b72ea0ead789f1cbadab9851779cb8f5f82f40bc71c5c303a10298ed6dc5657e3401ae5720f06836f098366441ac30ab35f13fab8b6ce81955a1181fca0ad4ea471cc09c51eae8eda42e8c615f933483449cbc67883f407430cb856e4eec1919bfdd38850ccf5837ec67d8cf802ec30836099592fcdb6cef4d4ab8ec7f95229b6b262dc6f9a62bfd082ccf98d8fc73fadfa2ccbddbd17126206e0ec41fe85ecdb9b7631a7edef193e4971ada3e4ab3ffe05f5146907255ad29d0afb91160c95e225514e1cd07e35ba157a44d1', 16)
const nsh = 24
const y = nbi()
// console.log({ ...pm })
// console.log(nsh)
console.log({ ...y })
console.log('-------------')
pm.lShiftTo(nsh, y)
console.log('-------------')
// console.log({ ...pm })
// console.log(nsh)
console.log({ ...y })
console.log(toGo(y))
console.log('-------------')
*/
func TestBigIntegerLShiftTo(t *testing.T) {
pm := bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{91899089, 132340641, 21896400, 157164117, 194057740, 220844207, 151463258, 6246726, 78331902, 119200318, 253312585, 52068062, 215718774, 69199966, 34005228, 198275366, 170707933, 208927455, 217681295, 201132076, 208640610, 191571501, 133780009, 223000812, 230084340, 160797436, 204505952, 147816494, 58639997, 84725592, 201184136, 250353945, 213407460, 255882288, 197556355, 75711644, 102103347, 228863628, 85896846, 119324828, 11357860, 18358218, 243373402, 262900588, 179527443, 71412784, 252281702, 15755318, 1762674, 90537524, 160356060, 50569474, 197598300, 100151104, 125422479, 229349457, 144645306, 245426903, 127703922, 258632985, 133667119, 266752290, 67129625, 47127394, 158120109, 66123079, 78079337, 70036311, 265719594, 244282224, 117743729, 198706149, 113908660, 10},
}
nsh := 24
y := nbi()
pm.lShiftTo(nsh, y)
assert.Equal(t, bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{16777216, 22520909, 8271290, 85254605, 211149349, 263786848, 181574922, 110129749, 235271444, 239776767, 158444963, 250713060, 103917549, 248363447, 205651589, 102788622, 230496018, 262327485, 264716205, 214931672, 46125186, 231143846, 162968162, 209687842, 81046414, 215706863, 10049839, 247662646, 227342338, 137882727, 139513077, 163568952, 82755985, 13337966, 66324291, 213673864, 55063625, 207708051, 249185000, 206695144, 74566665, 168482026, 168919548, 216537429, 66762934, 11220465, 105126595, 116430902, 34539139, 67219031, 206985187, 43576685, 204487184, 12349893, 257917684, 24616120, 182106501, 126480843, 48893613, 158976439, 267822801, 41908626, 167666962, 37750033, 221049270, 127323018, 155127636, 122320470, 172149429, 16607474, 32044855, 91245063, 79527998, 174891451, 0},
}, *y)
}
/*
const m = parseBigInt("A6CA1BB4BD803E5704A071E8F7370FD68F2A42CAB574A765693F0F54796CB8AD2CF1B624005119FE651227F7992FF6A6D1979C9B72EA0EAD789F1CBADAB9851779CB8F5F82F40BC71C5C303A10298ED6DC5657E3401AE5720F06836F098366441AC30AB35F13FAB8B6CE81955A1181FCA0AD4EA471CC09C51EAE8EDA42E8C615F933483449CBC67883F407430CB856E4EEC1919BFDD38850CCF5837EC67D8CF802EC30836099592FCDB6CEF4D4AB8EC7F95229B6B262DC6F9A62BFD082CCF98D8FC73FADFA2CCBDDBD17126206E0EC41FE85ECDB9B7631A7EDEF193E4971ADA3E4AB3FFE05F5146907255AD29D0AFB91160C95E225514E1CD07E35BA157A44D1", 16)
const z = new Montgomery(m)
const x = pkcs1pad2(`AAAA|ozzutFJrd0LBW45xy1kS`, 256)
const r = nbi()
console.log('-----------')
x.abs().dlShiftTo(74, r)
console.log({ ...m })
console.log({ ...r })
console.log('-----------')
debugger
r.divRemTo(m, null, r)
console.log({ ...m })
console.log({ ...r })
console.log(toGo(r))
*/
func TestBigIntegerDivRemTo(t *testing.T) {
m := bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456,
F1: 24,
F2: 4,
FV: 4503599627370496,
arr: []int64{91899089, 132340641, 21896400, 157164117, 194057740, 220844207, 151463258, 6246726, 78331902, 119200318, 253312585, 52068062, 215718774, 69199966, 34005228, 198275366, 170707933, 208927455, 217681295, 201132076, 208640610, 191571501, 133780009, 223000812, 230084340, 160797436, 204505952, 147816494, 58639997, 84725592, 201184136, 250353945, 213407460, 255882288, 197556355, 75711644, 102103347, 228863628, 85896846, 119324828, 11357860, 18358218, 243373402, 262900588, 179527443, 71412784, 252281702, 15755318, 1762674, 90537524, 160356060, 50569474, 197598300, 100151104, 125422479, 229349457, 144645306, 245426903, 127703922, 258632985, 133667119, 266752290, 67129625, 47127394, 158120109, 66123079, 78079337, 70036311, 265719594, 244282224, 117743729, 198706149, 113908660, 10},
}
r := &bigInteger{
s: 0,
t: 147,
DB: 28, DM: 268435455, DV: 268435456, FV: 4503599627370496, F1: 24, F2: 4,
arr: []int64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 154233683, 54744967, 4997719, 78063171, 175469638, 130480039, 21053761, 161587204, 268394322, 121434164, 78418508, 3550946, 12455929, 24891754, 185073407, 228296779, 244302159, 107191025, 116453635, 123234106, 173845849, 224383917, 99891756, 191404168, 52664173, 1980702, 201686014, 136607849, 22370042, 4648679, 172316499, 36552800, 151325320, 54152351, 158085758, 100144992, 126304478, 252283695, 75706, 13934210, 127988695, 244294746, 137659364, 58017546, 172378819, 95552461, 9528036, 65724189, 172768429, 66297275, 136154050, 164523179, 22702920, 226522046, 66709101, 110240193, 210752781, 190639350, 171925521, 204453797, 100106205, 47008966, 55805816, 258696058, 224370946, 217025253, 7518543, 251152916, 82614777, 18006155, 125838249, 35991357, 195526},
}
r.divRemTo(m, nil, r)
assert.Equal(t, bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{209773657, 226446907, 152388036, 174309560, 205056592, 77959934, 265294427, 49937091, 36049544, 241647745, 238142818, 230454931, 128720791, 220133128, 248693216, 154118189, 247219102, 13421605, 170546559, 156541741, 152257897, 142827003, 139524786, 187563347, 145400166, 205906977, 194901979, 245911980, 183171354, 229625478, 37175496, 117546847, 19292568, 25041223, 69386072, 851203, 192819676, 132704088, 223458668, 218664099, 16429066, 124492615, 217433053, 46973246, 249488663, 89463743, 70030115, 156482442, 262000883, 190564865, 214414604, 82098643, 159787017, 107897947, 29141587, 191993709, 68333250, 242359535, 2448060, 146015266, 205991168, 200889494, 162589797, 224299709, 141084186, 250132441, 185988049, 76673743, 46139833, 251304458, 164196384, 203364614, 32579725, 9, 83941111, 52335636, 173064576, 202248966, 46885449, 14317686, 236567515, 160963604, 129652172, 197126990, 183596112, 47555437, 17476055, 262067971, 161679094, 206033598, 241095451, 133864413, 135597429, 94021220, 9891920, 211080553, 151481965, 149555429, 54708811, 263491612, 138264416, 189406020, 110261288, 169331488, 75832019, 257214216, 258762176, 58529953, 241695623, 206594185, 150999015, 14003982, 17278812, 19780137, 141729942, 141682564, 188994911, 268026753, 197495567, 161845799, 101410096, 224621604, 188151407, 127297334, 43325688, 89933441, 721283, 67751082, 26418271, 149449090, 206290444, 38153975, 197939414, 158550640, 176439882, 186298985, 39017519, 11278796, 112433356, 23003171, 204456702, 185826629, 205187174, 15440890, 242217219, 136779331, 184375460, 18756},
}, *r)
}
func TestMontgomeryConvert(t *testing.T) {
m := bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456,
F1: 24,
F2: 4,
FV: 4503599627370496,
arr: []int64{91899089, 132340641, 21896400, 157164117, 194057740, 220844207, 151463258, 6246726, 78331902, 119200318, 253312585, 52068062, 215718774, 69199966, 34005228, 198275366, 170707933, 208927455, 217681295, 201132076, 208640610, 191571501, 133780009, 223000812, 230084340, 160797436, 204505952, 147816494, 58639997, 84725592, 201184136, 250353945, 213407460, 255882288, 197556355, 75711644, 102103347, 228863628, 85896846, 119324828, 11357860, 18358218, 243373402, 262900588, 179527443, 71412784, 252281702, 15755318, 1762674, 90537524, 160356060, 50569474, 197598300, 100151104, 125422479, 229349457, 144645306, 245426903, 127703922, 258632985, 133667119, 266752290, 67129625, 47127394, 158120109, 66123079, 78079337, 70036311, 265719594, 244282224, 117743729, 198706149, 113908660, 10},
}
z := newMontgomery(m)
x := bigInteger{
t: 73, s: 0,
DB: 28, DM: 268435455, DV: 268435456, FV: 4503599627370496, F1: 24, F2: 4,
arr: []int64{154233683, 54744967, 4997719, 78063171, 175469638, 130480039, 21053761, 161587204, 268394322, 121434164, 78418508, 3550946, 12455929, 24891754, 185073407, 228296779, 244302159, 107191025, 116453635, 123234106, 173845849, 224383917, 99891756, 191404168, 52664173, 1980702, 201686014, 136607849, 22370042, 4648679, 172316499, 36552800, 151325320, 54152351, 158085758, 100144992, 126304478, 252283695, 75706, 13934210, 127988695, 244294746, 137659364, 58017546, 172378819, 95552461, 9528036, 65724189, 172768429, 66297275, 136154050, 164523179, 22702920, 226522046, 66709101, 110240193, 210752781, 190639350, 171925521, 204453797, 100106205, 47008966, 55805816, 258696058, 224370946, 217025253, 7518543, 251152916, 82614777, 18006155, 125838249, 35991357, 195526, 0},
}
actual := z.convert(x)
assert.Equal(t, bigInteger{
s: 0,
t: 74,
DB: 28, DM: 268435455, DV: 268435456, FV: 4503599627370496, F1: 24, F2: 4,
arr: []int64{209773657, 226446907, 152388036, 174309560, 205056592, 77959934, 265294427, 49937091, 36049544, 241647745, 238142818, 230454931, 128720791, 220133128, 248693216, 154118189, 247219102, 13421605, 170546559, 156541741, 152257897, 142827003, 139524786, 187563347, 145400166, 205906977, 194901979, 245911980, 183171354, 229625478, 37175496, 117546847, 19292568, 25041223, 69386072, 851203, 192819676, 132704088, 223458668, 218664099, 16429066, 124492615, 217433053, 46973246, 249488663, 89463743, 70030115, 156482442, 262000883, 190564865, 214414604, 82098643, 159787017, 107897947, 29141587, 191993709, 68333250, 242359535, 2448060, 146015266, 205991168, 200889494, 162589797, 224299709, 141084186, 250132441, 185988049, 76673743, 46139833, 251304458, 164196384, 203364614, 32579725, 9, 83941111, 52335636, 173064576, 202248966, 46885449, 14317686, 236567515, 160963604, 129652172, 197126990, 183596112, 47555437, 17476055, 262067971, 161679094, 206033598, 241095451, 133864413, 135597429, 94021220, 9891920, 211080553, 151481965, 149555429, 54708811, 263491612, 138264416, 189406020, 110261288, 169331488, 75832019, 257214216, 258762176, 58529953, 241695623, 206594185, 150999015, 14003982, 17278812, 19780137, 141729942, 141682564, 188994911, 268026753, 197495567, 161845799, 101410096, 224621604, 188151407, 127297334, 43325688, 89933441, 721283, 67751082, 26418271, 149449090, 206290444, 38153975, 197939414, 158550640, 176439882, 186298985, 39017519, 11278796, 112433356, 23003171, 204456702, 185826629, 205187174, 15440890, 242217219, 136779331, 184375460, 18756},
}, actual)
}
func TestAm(t *testing.T) {
y := bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{251658240, 245914546, 260164165, 183180850, 57286106, 4648607, 86860533, 158305706, 33164011, 28658688, 109990492, 17722395, 164517906, 20072008, 62783866, 165646833, 37939437, 6107970, 3719250, 53503783, 222310269, 37291609, 105467293, 58747613, 187389041, 52728592, 258385616, 20772809, 41093117, 130552728, 128922378, 104866503, 185679470, 255097489, 202111164, 54761591, 213371830, 60727404, 19250455, 61740311, 193868790, 99953429, 99515907, 51898026, 201672521, 257214990, 163308860, 152004553, 233896316, 201216424, 61450268, 224858770, 63948271, 256085562, 10517771, 243819335, 86328954, 141954612, 219541842, 109459016, 612654, 226526829, 100768493, 230685422, 47386185, 141112437, 113307819, 146114985, 96286026, 251827981, 236390600, 177190392, 188907457, 93544004, 0},
}
tests := []struct {
i int
x int64
w *bigInteger
j int
c int64
n int
expected int64
}{
// {i: 0, x: 209773657, w: &bigInteger{
// t: 148, s: 0,
// DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
// arr: []int64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
// }, j: 0, c: 0, n: 1, expected: 163931351},
{i: 1, x: 0, w: &bigInteger{
t: 0, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{0},
}, j: 1, c: 0, n: 72, expected: 0},
}
for i, tt := range tests {
t.Run("", func(t *testing.T) {
if i == 74 {
fmt.Println("const y = parseBigInt('", y.toString(16), "', 16)")
fmt.Println("const i =", tt.i)
fmt.Println("const x =", tt.x)
fmt.Println("const w = parseBigInt('", tt.w.toString(16), "', 16)")
fmt.Println("const j =", tt.j)
fmt.Println("const c =", tt.c)
fmt.Println("const n =", tt.n)
fmt.Println("console.log(y.am(i, x, w, j, c, n))")
// runtime.Breakpoint()
}
actual := y.am(tt.i, tt.x, tt.w, tt.j, tt.c, tt.n)
assert.Equal(t, tt.expected, actual)
})
}
}
func TestMontgomeryReduce(t *testing.T) {
tests := []struct {
z montgomery
x bigInteger
expected bigInteger
}{
{
z: newMontgomery(bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{91899089, 132340641, 21896400, 157164117, 194057740, 220844207, 151463258, 6246726, 78331902, 119200318, 253312585, 52068062, 215718774, 69199966, 34005228, 198275366, 170707933, 208927455, 217681295, 201132076, 208640610, 191571501, 133780009, 223000812, 230084340, 160797436, 204505952, 147816494, 58639997, 84725592, 201184136, 250353945, 213407460, 255882288, 197556355, 75711644, 102103347, 228863628, 85896846, 119324828, 11357860, 18358218, 243373402, 262900588, 179527443, 71412784, 252281702, 15755318, 1762674, 90537524, 160356060, 50569474, 197598300, 100151104, 125422479, 229349457, 144645306, 245426903, 127703922, 258632985, 133667119, 266752290, 67129625, 47127394, 158120109, 66123079, 78079337, 70036311, 265719594, 244282224, 117743729, 198706149, 113908660, 10},
}),
x: bigInteger{
t: 149, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{212772593, 44229085, 192366432, 110136921, 227121492, 200257226, 110976332, 151913112, 12540453, 195207495, 97380258, 249160817, 260150795, 262929319, 55593787, 128890057, 80343752, 128309026, 88307147, 128344983, 241358930, 32806317, 241104509, 149399329, 143501672, 87358830, 222674982, 139058551, 117564239, 54275244, 21978218, 197089064, 184075200, 15460221, 252856840, 70085879, 122152034, 183616842, 195665110, 10806454, 216433278, 71264393, 232792319, 255370397, 26399430, 170537338, 165381842, 30537040, 209210170, 124082274, 149722658, 200810669, 128328873, 196769304, 7410569, 119523825, 223361273, 4438911, 265408224, 265615012, 79123225, 185008113, 268406427, 248970951, 194226400, 151428995, 10643213, 55627155, 245539311, 241519472, 125071977, 228418703, 87832376, 219591714, 70371568, 165313438, 231765714, 263489768, 69092022, 86926455, 126636607, 176299331, 112027822, 85111040, 208409521, 136948422, 32016838, 139600384, 155831623, 487843, 189892427, 82355318, 220027651, 255610026, 9988458, 61176333, 166944118, 247590726, 24139245, 100975879, 148095133, 93223529, 161351082, 94250680, 226887946, 266399025, 131099584, 87397969, 58428978, 95196403, 225660566, 25380535, 26139124, 203676034, 187786901, 257091523, 83983242, 211622411, 219769062, 265285320, 240008404, 64415602, 78653361, 178487741, 23446379, 187965763, 95671445, 162631067, 106449793, 94678034, 63712520, 177905949, 203997973, 107046637, 148637101, 210145236, 221221258, 147136200, 231595761, 43709440, 197463410, 89517135, 36275472, 92798736, 79596901, 53518318, 83, 0, 0},
},
expected: bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{180420020, 157457209, 191345529, 192609246, 40814436, 11621838, 130357867, 237432792, 87933400, 268280114, 227063021, 241978008, 175314965, 51871469, 51934967, 77750178, 31438140, 14354557, 147604859, 140216863, 236218558, 114430638, 17073350, 225249488, 45481005, 250586200, 225741704, 12050962, 98045995, 263843310, 154846961, 219877441, 194513554, 8177928, 209710170, 48677900, 265898890, 176059360, 147086584, 70950780, 2565752, 166025933, 21002040, 192068355, 45155928, 180871007, 6520138, 192390605, 145736842, 107992727, 87032440, 44410593, 26872404, 149801549, 7131551, 41210847, 247238280, 99687208, 207902256, 212290167, 145752816, 133834096, 198900231, 20890055, 71632001, 64928078, 248424351, 234571620, 75383735, 214485644, 238958461, 85420579, 168506970, 8, 180420020, 157457209, 191345529, 192609246, 40814436, 11621838, 130357867, 237432792, 87933400, 268280114, 227063021, 241978008, 175314965, 51871469, 51934967, 77750178, 31438140, 14354557, 147604859, 140216863, 236218558, 114430638, 17073350, 225249488, 45481005, 250586200, 225741704, 12050962, 98045995, 263843310, 154846961, 219877441, 194513554, 8177928, 209710170, 48677900, 265898890, 176059360, 147086584, 70950780, 2565752, 166025933, 21002040, 192068355, 45155928, 180871007, 6520138, 192390605, 145736842, 107992727, 87032440, 44410593, 26872404, 149801549, 7131551, 41210847, 247238280, 99687208, 207902256, 212290167, 145752816, 133834096, 198900231, 20890055, 71632001, 64928078, 248424351, 234571620, 75383735, 214485644, 238958461, 85420579, 168506970, 8, 0},
},
},
{
z: newMontgomery(bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{91899089, 132340641, 21896400, 157164117, 194057740, 220844207, 151463258, 6246726, 78331902, 119200318, 253312585, 52068062, 215718774, 69199966, 34005228, 198275366, 170707933, 208927455, 217681295, 201132076, 208640610, 191571501, 133780009, 223000812, 230084340, 160797436, 204505952, 147816494, 58639997, 84725592, 201184136, 250353945, 213407460, 255882288, 197556355, 75711644, 102103347, 228863628, 85896846, 119324828, 11357860, 18358218, 243373402, 262900588, 179527443, 71412784, 252281702, 15755318, 1762674, 90537524, 160356060, 50569474, 197598300, 100151104, 125422479, 229349457, 144645306, 245426903, 127703922, 258632985, 133667119, 266752290, 67129625, 47127394, 158120109, 66123079, 78079337, 70036311, 265719594, 244282224, 117743729, 198706149, 113908660, 10},
}),
x: bigInteger{
t: 149, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{158156432, 171132705, 7424271, 113475393, 262104253, 89030838, 202285150, 122497652, 210240605, 245229729, 9813036, 7210836, 139244298, 45570276, 165403512, 121993309, 186226100, 191397610, 59197355, 47165851, 134521965, 2589284, 251226997, 176989680, 230993795, 122041164, 221715522, 166355492, 171511448, 138604154, 20490993, 31134705, 120377306, 227619435, 208006102, 122374071, 214416992, 158863970, 78333629, 133088898, 173511335, 256294262, 2131670, 68623288, 134601412, 32546994, 113586197, 113776993, 221435387, 942178, 144115375, 63688469, 28367992, 168852776, 6348880, 71158092, 74808081, 221402468, 102158175, 3151657, 245354686, 263714779, 246993255, 5840101, 245603569, 226323565, 142056704, 144406909, 197491984, 234889244, 195042437, 266166644, 127939037, 11645068, 173262005, 61278612, 10169838, 152809304, 261482770, 230859993, 30121188, 124981415, 104921468, 198112895, 139782065, 42370160, 178853489, 228034964, 16802730, 3511943, 124242196, 133516115, 205162248, 164313205, 205979317, 76121166, 83805050, 94551959, 154508330, 226812825, 130863557, 131554464, 61529788, 54090686, 182687704, 50033820, 234404111, 100302435, 47686040, 164537444, 183017732, 119091508, 16434078, 266971026, 137638816, 119733725, 82864080, 36597884, 181448173, 146145132, 227497570, 187989647, 6157018, 47519113, 139493963, 60164278, 191473656, 208824284, 135843927, 2970180, 92380820, 46053818, 15864830, 66335610, 120254501, 191248295, 153472118, 216071213, 231592473, 242015156, 38936127, 40846187, 232449048, 128146624, 119611736, 117535089, 74, 0, 0},
},
expected: bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{35100958, 6375474, 140221205, 205218361, 97622390, 258188275, 82430824, 90537829, 185444140, 21639133, 189925292, 88274348, 180729513, 152386183, 131114061, 77412443, 82017277, 166452214, 129210497, 63273129, 113543903, 108995784, 4739850, 111599907, 206013542, 164919432, 117685421, 250235474, 57928446, 233983610, 171963170, 235240224, 195993565, 260633810, 127471843, 132300492, 234766095, 71716409, 97867332, 101143186, 26861115, 40260749, 88722543, 228562588, 67045991, 180581244, 170086347, 148199385, 148646128, 66825105, 226311868, 232105996, 14930891, 66865154, 94191594, 98783853, 80697004, 52952511, 249216738, 212604906, 171643938, 46816469, 135818360, 192079925, 216466822, 182990333, 19292245, 1435820, 68112492, 162672658, 56159700, 90153765, 53688784, 3, 35100958, 6375474, 140221205, 205218361, 97622390, 258188275, 82430824, 90537829, 185444140, 21639133, 189925292, 88274348, 180729513, 152386183, 131114061, 77412443, 82017277, 166452214, 129210497, 63273129, 113543903, 108995784, 4739850, 111599907, 206013542, 164919432, 117685421, 250235474, 57928446, 233983610, 171963170, 235240224, 195993565, 260633810, 127471843, 132300492, 234766095, 71716409, 97867332, 101143186, 26861115, 40260749, 88722543, 228562588, 67045991, 180581244, 170086347, 148199385, 148646128, 66825105, 226311868, 232105996, 14930891, 66865154, 94191594, 98783853, 80697004, 52952511, 249216738, 212604906, 171643938, 46816469, 135818360, 192079925, 216466822, 182990333, 19292245, 1435820, 68112492, 162672658, 56159700, 90153765, 53688784, 3, 0},
},
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
tt.z.reduce(&tt.x)
assert.Equal(t, tt.expected, tt.x)
})
}
}
/*
const x = parseBigInt('2fbc62252f3d78023a9112c08b4ec99f9ef84a14072b94fcef8ae5d5fa102f6b637a35387782cd4cc65f77fddc2fb7a5a3f6011b5cecf6c8fd50d69221c13f9e66dd8073be15a6b489ce6cab81d8bc23f39dbba4c3cad3eadf1d09162e45b203cda464ac3375470a83483e4e8fa45a7a0f3d70d49e8200127baf098b2f78740de5f8176096c327e33a4c9f9050a8822dc060a455753046eee715556fa8247869c057bfe01e391e323976db6898885f43a2cd5fd3ada5cad59758673a6f0f1036639af1e8fc14fd9b884bb07feff17bd16a0be0ff90362ee24ac924c73cf034fff5f529a1a00414141417c6f7a7a7574464a7264304c425734357879316b53', 16)
// console.log(toGo(x))
const e = 65537
const m = parseBigInt('a6ca1bb4bd803e5704a071e8f7370fd68f2a42cab574a765693f0f54796cb8ad2cf1b624005119fe651227f7992ff6a6d1979c9b72ea0ead789f1cbadab9851779cb8f5f82f40bc71c5c303a10298ed6dc5657e3401ae5720f06836f098366441ac30ab35f13fab8b6ce81955a1181fca0ad4ea471cc09c51eae8eda42e8c615f933483449cbc67883f407430cb856e4eec1919bfdd38850ccf5837ec67d8cf802ec30836099592fcdb6cef4d4ab8ec7f95229b6b262dc6f9a62bfd082ccf98d8fc73fadfa2ccbddbd17126206e0ec41fe85ecdb9b7631a7edef193e4971ada3e4ab3ffe05f5146907255ad29d0afb91160c95e225514e1cd07e35ba157a44d1', 16)
// console.log(toGo(m))
const z = new Montgomery(m)
const actual = x.exp(e, z)
// console.log(actual.toString(16))
// console.log(toGo(actual))
*/
func TestBigIntegerExp(t *testing.T) {
x := bigInteger{
t: 73, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{154233683, 54744967, 4997719, 78063171, 175469638, 130480039, 21053761, 161587204, 268394322, 121434164, 78418508, 3550946, 12455929, 24891754, 185073407, 228296779, 244302159, 107191025, 116453635, 123234106, 173845849, 224383917, 99891756, 191404168, 52664173, 1980702, 201686014, 136607849, 22370042, 4648679, 172316499, 36552800, 151325320, 54152351, 158085758, 100144992, 126304478, 252283695, 75706, 13934210, 127988695, 244294746, 137659364, 58017546, 172378819, 95552461, 9528036, 65724189, 172768429, 66297275, 136154050, 164523179, 22702920, 226522046, 66709101, 110240193, 210752781, 190639350, 171925521, 204453797, 100106205, 47008966, 55805816, 258696058, 224370946, 217025253, 7518543, 251152916, 82614777, 18006155, 125838249, 35991357, 195526},
}
e := int64(65537)
z := newMontgomery(bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{91899089, 132340641, 21896400, 157164117, 194057740, 220844207, 151463258, 6246726, 78331902, 119200318, 253312585, 52068062, 215718774, 69199966, 34005228, 198275366, 170707933, 208927455, 217681295, 201132076, 208640610, 191571501, 133780009, 223000812, 230084340, 160797436, 204505952, 147816494, 58639997, 84725592, 201184136, 250353945, 213407460, 255882288, 197556355, 75711644, 102103347, 228863628, 85896846, 119324828, 11357860, 18358218, 243373402, 262900588, 179527443, 71412784, 252281702, 15755318, 1762674, 90537524, 160356060, 50569474, 197598300, 100151104, 125422479, 229349457, 144645306, 245426903, 127703922, 258632985, 133667119, 266752290, 67129625, 47127394, 158120109, 66123079, 78079337, 70036311, 265719594, 244282224, 117743729, 198706149, 113908660, 10},
})
// fmt.Println(len(x.arr))
// t.FailNow()
// fmt.Println(x.toString(16))
// fmt.Println(e)
// fmt.Println(z.m.toString(16))
actual := x.exp(e, z)
// fmt.Println(actual.toString(16))
assert.Equal(t, bigInteger{
t: 74, s: 0,
DB: 28, DM: 268435455, DV: 268435456, F1: 24, F2: 4, FV: 4503599627370496,
arr: []int64{3605733, 199673243, 137404120, 202225722, 180332040, 201016062, 167261820, 38823839, 2248083, 82428012, 6123334, 161574110, 103511461, 68911483, 121697491, 125057266, 162430364, 130583714, 229525070, 228304246, 52027732, 98929295, 108202068, 133927160, 2735669, 67835168, 146001932, 163789314, 14460970, 94936381, 240860807, 37452166, 179075938, 208292217, 31881936, 186216633, 31626327, 242320594, 199245308, 96221805, 182641274, 110591989, 128700624, 681715, 224040105, 132020860, 117952899, 58077822, 16680514, 84520386, 230671423, 1368095, 194360099, 130505532, 131639960, 190639700, 249905011, 42293092, 989135, 222515085, 140224374, 217561708, 245119843, 70331893, 224945526, 259936122, 246291041, 7135692, 204653796, 101754697, 151077748, 234946036, 6419217, 5, 3605733, 199673243, 137404120, 202225722, 180332040, 201016062, 167261820, 38823839, 2248083, 82428012, 6123334, 161574110, 103511461, 68911483, 121697491, 125057266, 162430364, 130583714, 229525070, 228304246, 52027732, 98929295, 108202068, 133927160, 2735669, 67835168, 146001932, 163789314, 14460970, 94936381, 240860807, 37452166, 179075938, 208292217, 31881936, 186216633, 31626327, 242320594, 199245308, 96221805, 182641274, 110591989, 128700624, 681715, 224040105, 132020860, 117952899, 58077822, 16680514, 84520386, 230671423, 1368095, 194360099, 130505532, 131639960, 190639700, 249905011, 42293092, 989135, 222515085, 140224374, 217561708, 245119843, 70331893, 224945526, 259936122, 246291041, 7135692, 204653796, 101754697, 151077748, 234946036, 6419217, 5, 0},
}, actual)
}
func TestIntAt(t *testing.T) {
tests := map[rune]int{
48: 0, 49: 1, 50: 2, 51: 3, 52: 4, 53: 5, 54: 6, 55: 7, 56: 8, 57: 9,
65: 10, 66: 11, 67: 12, 68: 13, 69: 14, 70: 15, 71: 16, 72: 17, 73: 18, 74: 19, 75: 20, 76: 21, 77: 22, 78: 23, 79: 24, 80: 25, 81: 26, 82: 27, 83: 28, 84: 29, 85: 30, 86: 31, 87: 32, 88: 33, 89: 34, 90: 35,
97: 10, 98: 11, 99: 12, 100: 13, 101: 14, 102: 15, 103: 16, 104: 17, 105: 18, 106: 19, 107: 20, 108: 21, 109: 22, 110: 23, 111: 24, 112: 25, 113: 26, 114: 27, 115: 28, 116: 29, 117: 30, 118: 31, 119: 32, 120: 33, 121: 34, 122: 35,
}
for r, expected := range tests {
t.Run("", func(t *testing.T) {
actual := runeIndex(r)
assert.Equal(t, expected, actual)
})
}
}