Black Lives Matter. Support the Equal Justice Initiative.

# Source file src/math/big/nat_test.go

## Documentation: math/big

```     1  // Copyright 2009 The Go Authors. All rights reserved.
2  // Use of this source code is governed by a BSD-style
4
5  package big
6
7  import (
8  	"fmt"
9  	"runtime"
10  	"strings"
11  	"testing"
12  )
13
14  var cmpTests = []struct {
15  	x, y nat
16  	r    int
17  }{
18  	{nil, nil, 0},
19  	{nil, nat(nil), 0},
20  	{nat(nil), nil, 0},
21  	{nat(nil), nat(nil), 0},
22  	{nat{0}, nat{0}, 0},
23  	{nat{0}, nat{1}, -1},
24  	{nat{1}, nat{0}, 1},
25  	{nat{1}, nat{1}, 0},
26  	{nat{0, _M}, nat{1}, 1},
27  	{nat{1}, nat{0, _M}, -1},
28  	{nat{1, _M}, nat{0, _M}, 1},
29  	{nat{0, _M}, nat{1, _M}, -1},
30  	{nat{16, 571956, 8794, 68}, nat{837, 9146, 1, 754489}, -1},
31  	{nat{34986, 41, 105, 1957}, nat{56, 7458, 104, 1957}, 1},
32  }
33
34  func TestCmp(t *testing.T) {
35  	for i, a := range cmpTests {
36  		r := a.x.cmp(a.y)
37  		if r != a.r {
38  			t.Errorf("#%d got r = %v; want %v", i, r, a.r)
39  		}
40  	}
41  }
42
43  type funNN func(z, x, y nat) nat
44  type argNN struct {
45  	z, x, y nat
46  }
47
48  var sumNN = []argNN{
49  	{},
50  	{nat{1}, nil, nat{1}},
51  	{nat{1111111110}, nat{123456789}, nat{987654321}},
52  	{nat{0, 0, 0, 1}, nil, nat{0, 0, 0, 1}},
53  	{nat{0, 0, 0, 1111111110}, nat{0, 0, 0, 123456789}, nat{0, 0, 0, 987654321}},
54  	{nat{0, 0, 0, 1}, nat{0, 0, _M}, nat{0, 0, 1}},
55  }
56
57  var prodNN = []argNN{
58  	{},
59  	{nil, nil, nil},
60  	{nil, nat{991}, nil},
61  	{nat{991}, nat{991}, nat{1}},
62  	{nat{991 * 991}, nat{991}, nat{991}},
63  	{nat{0, 0, 991 * 991}, nat{0, 991}, nat{0, 991}},
64  	{nat{1 * 991, 2 * 991, 3 * 991, 4 * 991}, nat{1, 2, 3, 4}, nat{991}},
65  	{nat{4, 11, 20, 30, 20, 11, 4}, nat{1, 2, 3, 4}, nat{4, 3, 2, 1}},
66  	// 3^100 * 3^28 = 3^128
67  	{
68  		natFromString("11790184577738583171520872861412518665678211592275841109096961"),
69  		natFromString("515377520732011331036461129765621272702107522001"),
70  		natFromString("22876792454961"),
71  	},
72  	// z = 111....1 (70000 digits)
73  	// x = 10^(99*700) + ... + 10^1400 + 10^700 + 1
74  	// y = 111....1 (700 digits, larger than Karatsuba threshold on 32-bit and 64-bit)
75  	{
76  		natFromString(strings.Repeat("1", 70000)),
77  		natFromString("1" + strings.Repeat(strings.Repeat("0", 699)+"1", 99)),
78  		natFromString(strings.Repeat("1", 700)),
79  	},
80  	// z = 111....1 (20000 digits)
81  	// x = 10^10000 + 1
82  	// y = 111....1 (10000 digits)
83  	{
84  		natFromString(strings.Repeat("1", 20000)),
85  		natFromString("1" + strings.Repeat("0", 9999) + "1"),
86  		natFromString(strings.Repeat("1", 10000)),
87  	},
88  }
89
90  func natFromString(s string) nat {
91  	x, _, _, err := nat(nil).scan(strings.NewReader(s), 0, false)
92  	if err != nil {
93  		panic(err)
94  	}
95  	return x
96  }
97
98  func TestSet(t *testing.T) {
99  	for _, a := range sumNN {
100  		z := nat(nil).set(a.z)
101  		if z.cmp(a.z) != 0 {
102  			t.Errorf("got z = %v; want %v", z, a.z)
103  		}
104  	}
105  }
106
107  func testFunNN(t *testing.T, msg string, f funNN, a argNN) {
108  	z := f(nil, a.x, a.y)
109  	if z.cmp(a.z) != 0 {
110  		t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, z, a.z)
111  	}
112  }
113
114  func TestFunNN(t *testing.T) {
115  	for _, a := range sumNN {
116  		arg := a
118
119  		arg = argNN{a.z, a.y, a.x}
121
122  		arg = argNN{a.x, a.z, a.y}
123  		testFunNN(t, "sub", nat.sub, arg)
124
125  		arg = argNN{a.y, a.z, a.x}
126  		testFunNN(t, "sub symmetric", nat.sub, arg)
127  	}
128
129  	for _, a := range prodNN {
130  		arg := a
131  		testFunNN(t, "mul", nat.mul, arg)
132
133  		arg = argNN{a.z, a.y, a.x}
134  		testFunNN(t, "mul symmetric", nat.mul, arg)
135  	}
136  }
137
138  var mulRangesN = []struct {
139  	a, b uint64
140  	prod string
141  }{
142  	{0, 0, "0"},
143  	{1, 1, "1"},
144  	{1, 2, "2"},
145  	{1, 3, "6"},
146  	{10, 10, "10"},
147  	{0, 100, "0"},
148  	{0, 1e9, "0"},
149  	{1, 0, "1"},                    // empty range
150  	{100, 1, "1"},                  // empty range
151  	{1, 10, "3628800"},             // 10!
152  	{1, 20, "2432902008176640000"}, // 20!
153  	{1, 100,
154  		"933262154439441526816992388562667004907159682643816214685929" +
155  			"638952175999932299156089414639761565182862536979208272237582" +
156  			"51185210916864000000000000000000000000", // 100!
157  	},
158  }
159
160  func TestMulRangeN(t *testing.T) {
161  	for i, r := range mulRangesN {
162  		prod := string(nat(nil).mulRange(r.a, r.b).utoa(10))
163  		if prod != r.prod {
164  			t.Errorf("#%d: got %s; want %s", i, prod, r.prod)
165  		}
166  	}
167  }
168
169  // allocBytes returns the number of bytes allocated by invoking f.
170  func allocBytes(f func()) uint64 {
171  	var stats runtime.MemStats
173  	t := stats.TotalAlloc
174  	f()
176  	return stats.TotalAlloc - t
177  }
178
179  // TestMulUnbalanced tests that multiplying numbers of different lengths
180  // does not cause deep recursion and in turn allocate too much memory.
181  // Test case for issue 3807.
182  func TestMulUnbalanced(t *testing.T) {
183  	defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
184  	x := rndNat(50000)
185  	y := rndNat(40)
186  	allocSize := allocBytes(func() {
187  		nat(nil).mul(x, y)
188  	})
189  	inputSize := uint64(len(x)+len(y)) * _S
190  	if ratio := allocSize / uint64(inputSize); ratio > 10 {
191  		t.Errorf("multiplication uses too much memory (%d > %d times the size of inputs)", allocSize, ratio)
192  	}
193  }
194
195  // rndNat returns a random nat value >= 0 of (usually) n words in length.
196  // In extremely unlikely cases it may be smaller than n words if the top-
197  // most words are 0.
198  func rndNat(n int) nat {
199  	return nat(rndV(n)).norm()
200  }
201
202  // rndNat1 is like rndNat but the result is guaranteed to be > 0.
203  func rndNat1(n int) nat {
204  	x := nat(rndV(n)).norm()
205  	if len(x) == 0 {
206  		x.setWord(1)
207  	}
208  	return x
209  }
210
211  func BenchmarkMul(b *testing.B) {
212  	mulx := rndNat(1e4)
213  	muly := rndNat(1e4)
214  	b.ResetTimer()
215  	for i := 0; i < b.N; i++ {
216  		var z nat
217  		z.mul(mulx, muly)
218  	}
219  }
220
221  func benchmarkNatMul(b *testing.B, nwords int) {
222  	x := rndNat(nwords)
223  	y := rndNat(nwords)
224  	var z nat
225  	b.ResetTimer()
226  	for i := 0; i < b.N; i++ {
227  		z.mul(x, y)
228  	}
229  }
230
231  var mulBenchSizes = []int{10, 100, 1000, 10000, 100000}
232
233  func BenchmarkNatMul(b *testing.B) {
234  	for _, n := range mulBenchSizes {
235  		if isRaceBuilder && n > 1e3 {
236  			continue
237  		}
238  		b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
239  			benchmarkNatMul(b, n)
240  		})
241  	}
242  }
243
244  func TestNLZ(t *testing.T) {
245  	var x Word = _B >> 1
246  	for i := 0; i <= _W; i++ {
247  		if int(nlz(x)) != i {
248  			t.Errorf("failed at %x: got %d want %d", x, nlz(x), i)
249  		}
250  		x >>= 1
251  	}
252  }
253
254  type shiftTest struct {
255  	in    nat
256  	shift uint
257  	out   nat
258  }
259
260  var leftShiftTests = []shiftTest{
261  	{nil, 0, nil},
262  	{nil, 1, nil},
263  	{natOne, 0, natOne},
264  	{natOne, 1, natTwo},
265  	{nat{1 << (_W - 1)}, 1, nat{0}},
266  	{nat{1 << (_W - 1), 0}, 1, nat{0, 1}},
267  }
268
269  func TestShiftLeft(t *testing.T) {
270  	for i, test := range leftShiftTests {
271  		var z nat
272  		z = z.shl(test.in, test.shift)
273  		for j, d := range test.out {
274  			if j >= len(z) || z[j] != d {
275  				t.Errorf("#%d: got: %v want: %v", i, z, test.out)
276  				break
277  			}
278  		}
279  	}
280  }
281
282  var rightShiftTests = []shiftTest{
283  	{nil, 0, nil},
284  	{nil, 1, nil},
285  	{natOne, 0, natOne},
286  	{natOne, 1, nil},
287  	{natTwo, 1, natOne},
288  	{nat{0, 1}, 1, nat{1 << (_W - 1)}},
289  	{nat{2, 1, 1}, 1, nat{1<<(_W-1) + 1, 1 << (_W - 1)}},
290  }
291
292  func TestShiftRight(t *testing.T) {
293  	for i, test := range rightShiftTests {
294  		var z nat
295  		z = z.shr(test.in, test.shift)
296  		for j, d := range test.out {
297  			if j >= len(z) || z[j] != d {
298  				t.Errorf("#%d: got: %v want: %v", i, z, test.out)
299  				break
300  			}
301  		}
302  	}
303  }
304
305  func BenchmarkZeroShifts(b *testing.B) {
306  	x := rndNat(800)
307
308  	b.Run("Shl", func(b *testing.B) {
309  		for i := 0; i < b.N; i++ {
310  			var z nat
311  			z.shl(x, 0)
312  		}
313  	})
314  	b.Run("ShlSame", func(b *testing.B) {
315  		for i := 0; i < b.N; i++ {
316  			x.shl(x, 0)
317  		}
318  	})
319
320  	b.Run("Shr", func(b *testing.B) {
321  		for i := 0; i < b.N; i++ {
322  			var z nat
323  			z.shr(x, 0)
324  		}
325  	})
326  	b.Run("ShrSame", func(b *testing.B) {
327  		for i := 0; i < b.N; i++ {
328  			x.shr(x, 0)
329  		}
330  	})
331  }
332
333  type modWTest struct {
334  	in       string
335  	dividend string
336  	out      string
337  }
338
339  var modWTests32 = []modWTest{
340  	{"23492635982634928349238759823742", "252341", "220170"},
341  }
342
343  var modWTests64 = []modWTest{
344  	{"6527895462947293856291561095690465243862946", "524326975699234", "375066989628668"},
345  }
346
347  func runModWTests(t *testing.T, tests []modWTest) {
348  	for i, test := range tests {
349  		in, _ := new(Int).SetString(test.in, 10)
350  		d, _ := new(Int).SetString(test.dividend, 10)
351  		out, _ := new(Int).SetString(test.out, 10)
352
353  		r := in.abs.modW(d.abs[0])
354  		if r != out.abs[0] {
355  			t.Errorf("#%d failed: got %d want %s", i, r, out)
356  		}
357  	}
358  }
359
360  func TestModW(t *testing.T) {
361  	if _W >= 32 {
362  		runModWTests(t, modWTests32)
363  	}
364  	if _W >= 64 {
365  		runModWTests(t, modWTests64)
366  	}
367  }
368
369  var montgomeryTests = []struct {
370  	x, y, m      string
371  	k0           uint64
372  	out32, out64 string
373  }{
374  	{
375  		"0xffffffffffffffffffffffffffffffffffffffffffffffffe",
376  		"0xffffffffffffffffffffffffffffffffffffffffffffffffe",
377  		"0xfffffffffffffffffffffffffffffffffffffffffffffffff",
378  		1,
379  		"0x1000000000000000000000000000000000000000000",
380  		"0x10000000000000000000000000000000000",
381  	},
382  	{
383  		"0x000000000ffffff5",
384  		"0x000000000ffffff0",
385  		"0x0000000010000001",
386  		0xff0000000fffffff,
387  		"0x000000000bfffff4",
388  		"0x0000000003400001",
389  	},
390  	{
391  		"0x0000000080000000",
392  		"0x00000000ffffffff",
393  		"0x1000000000000001",
394  		0xfffffffffffffff,
395  		"0x0800000008000001",
396  		"0x0800000008000001",
397  	},
398  	{
399  		"0x0000000080000000",
400  		"0x0000000080000000",
401  		"0xffffffff00000001",
402  		0xfffffffeffffffff,
403  		"0xbfffffff40000001",
404  		"0xbfffffff40000001",
405  	},
406  	{
407  		"0x0000000080000000",
408  		"0x0000000080000000",
409  		"0x00ffffff00000001",
410  		0xfffffeffffffff,
411  		"0xbfffff40000001",
412  		"0xbfffff40000001",
413  	},
414  	{
415  		"0x0000000080000000",
416  		"0x0000000080000000",
417  		"0x0000ffff00000001",
418  		0xfffeffffffff,
419  		"0xbfff40000001",
420  		"0xbfff40000001",
421  	},
422  	{
423  		"0x3321ffffffffffffffffffffffffffff00000000000022222623333333332bbbb888c0",
424  		"0x3321ffffffffffffffffffffffffffff00000000000022222623333333332bbbb888c0",
425  		"0x33377fffffffffffffffffffffffffffffffffffffffffffff0000000000022222eee1",
427  		"0x04eb0e11d72329dc0915f86784820fc403275bf2f6620a20e0dd344c5cd0875e50deb5",
428  		"0x0d7144739a7d8e11d72329dc0915f86784820fc403275bf2f61ed96f35dd34dbb3d6a0",
429  	},
430  	{
431  		"0x10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffff00000000000022222223333333333444444444",
432  		"0x10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffffffffffffffffffff999999999999999aaabbbbbbbbcccccccccccc",
433  		"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff33377fffffffffffffffffffffffffffffffffffffffffffff0000000000022222eee1",
435  		"0x5c0d52f451aec609b15da8e5e5626c4eaa88723bdeac9d25ca9b961269400410ca208a16af9c2fb07d7a11c7772cba02c22f9711078d51a3797eb18e691295293284d988e349fa6deba46b25a4ecd9f715",
437  	},
438  }
439
440  func TestMontgomery(t *testing.T) {
441  	one := NewInt(1)
442  	_B := new(Int).Lsh(one, _W)
443  	for i, test := range montgomeryTests {
444  		x := natFromString(test.x)
445  		y := natFromString(test.y)
446  		m := natFromString(test.m)
447  		for len(x) < len(m) {
448  			x = append(x, 0)
449  		}
450  		for len(y) < len(m) {
451  			y = append(y, 0)
452  		}
453
454  		if x.cmp(m) > 0 {
455  			_, r := nat(nil).div(nil, x, m)
456  			t.Errorf("#%d: x > m (0x%s > 0x%s; use 0x%s)", i, x.utoa(16), m.utoa(16), r.utoa(16))
457  		}
458  		if y.cmp(m) > 0 {
459  			_, r := nat(nil).div(nil, x, m)
460  			t.Errorf("#%d: y > m (0x%s > 0x%s; use 0x%s)", i, y.utoa(16), m.utoa(16), r.utoa(16))
461  		}
462
463  		var out nat
464  		if _W == 32 {
465  			out = natFromString(test.out32)
466  		} else {
467  			out = natFromString(test.out64)
468  		}
469
470  		// t.Logf("#%d: len=%d\n", i, len(m))
471
472  		// check output in table
473  		xi := &Int{abs: x}
474  		yi := &Int{abs: y}
475  		mi := &Int{abs: m}
476  		p := new(Int).Mod(new(Int).Mul(xi, new(Int).Mul(yi, new(Int).ModInverse(new(Int).Lsh(one, uint(len(m))*_W), mi))), mi)
477  		if out.cmp(p.abs.norm()) != 0 {
478  			t.Errorf("#%d: out in table=0x%s, computed=0x%s", i, out.utoa(16), p.abs.norm().utoa(16))
479  		}
480
481  		// check k0 in table
482  		k := new(Int).Mod(&Int{abs: m}, _B)
483  		k = new(Int).Sub(_B, k)
484  		k = new(Int).Mod(k, _B)
485  		k0 := Word(new(Int).ModInverse(k, _B).Uint64())
486  		if k0 != Word(test.k0) {
487  			t.Errorf("#%d: k0 in table=%#x, computed=%#x\n", i, test.k0, k0)
488  		}
489
490  		// check montgomery with correct k0 produces correct output
491  		z := nat(nil).montgomery(x, y, m, k0, len(m))
492  		z = z.norm()
493  		if z.cmp(out) != 0 {
494  			t.Errorf("#%d: got 0x%s want 0x%s", i, z.utoa(16), out.utoa(16))
495  		}
496  	}
497  }
498
499  var expNNTests = []struct {
500  	x, y, m string
501  	out     string
502  }{
503  	{"0", "0", "0", "1"},
504  	{"0", "0", "1", "0"},
505  	{"1", "1", "1", "0"},
506  	{"2", "1", "1", "0"},
507  	{"2", "2", "1", "0"},
508  	{"10", "100000000000", "1", "0"},
509  	{"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
510  	{"0x8000000000000000", "2", "6719", "4944"},
511  	{"0x8000000000000000", "3", "6719", "5447"},
512  	{"0x8000000000000000", "1000", "6719", "1603"},
513  	{"0x8000000000000000", "1000000", "6719", "3199"},
514  	{
515  		"2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
516  		"298472983472983471903246121093472394872319615612417471234712061",
517  		"29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
518  		"23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
519  	},
520  	{
521  		"11521922904531591643048817447554701904414021819823889996244743037378330903763518501116638828335352811871131385129455853417360623007349090150042001944696604737499160174391019030572483602867266711107136838523916077674888297896995042968746762200926853379",
522  		"426343618817810911523",
523  		"444747819283133684179",
524  		"42",
525  	},
526  }
527
528  func TestExpNN(t *testing.T) {
529  	for i, test := range expNNTests {
530  		x := natFromString(test.x)
531  		y := natFromString(test.y)
532  		out := natFromString(test.out)
533
534  		var m nat
535  		if len(test.m) > 0 {
536  			m = natFromString(test.m)
537  		}
538
539  		z := nat(nil).expNN(x, y, m)
540  		if z.cmp(out) != 0 {
541  			t.Errorf("#%d got %s want %s", i, z.utoa(10), out.utoa(10))
542  		}
543  	}
544  }
545
546  func BenchmarkExp3Power(b *testing.B) {
547  	const x = 3
548  	for _, y := range []Word{
549  		0x10, 0x40, 0x100, 0x400, 0x1000, 0x4000, 0x10000, 0x40000, 0x100000, 0x400000,
550  	} {
551  		b.Run(fmt.Sprintf("%#x", y), func(b *testing.B) {
552  			var z nat
553  			for i := 0; i < b.N; i++ {
554  				z.expWW(x, y)
555  			}
556  		})
557  	}
558  }
559
560  func fibo(n int) nat {
561  	switch n {
562  	case 0:
563  		return nil
564  	case 1:
565  		return nat{1}
566  	}
567  	f0 := fibo(0)
568  	f1 := fibo(1)
569  	var f2 nat
570  	for i := 1; i < n; i++ {
572  		f0, f1, f2 = f1, f2, f0
573  	}
574  	return f1
575  }
576
577  var fiboNums = []string{
578  	"0",
579  	"55",
580  	"6765",
581  	"832040",
582  	"102334155",
583  	"12586269025",
584  	"1548008755920",
585  	"190392490709135",
586  	"23416728348467685",
587  	"2880067194370816120",
588  	"354224848179261915075",
589  }
590
591  func TestFibo(t *testing.T) {
592  	for i, want := range fiboNums {
593  		n := i * 10
594  		got := string(fibo(n).utoa(10))
595  		if got != want {
596  			t.Errorf("fibo(%d) failed: got %s want %s", n, got, want)
597  		}
598  	}
599  }
600
601  func BenchmarkFibo(b *testing.B) {
602  	for i := 0; i < b.N; i++ {
603  		fibo(1e0)
604  		fibo(1e1)
605  		fibo(1e2)
606  		fibo(1e3)
607  		fibo(1e4)
608  		fibo(1e5)
609  	}
610  }
611
612  var bitTests = []struct {
613  	x    string
614  	i    uint
615  	want uint
616  }{
617  	{"0", 0, 0},
618  	{"0", 1, 0},
619  	{"0", 1000, 0},
620
621  	{"0x1", 0, 1},
622  	{"0x10", 0, 0},
623  	{"0x10", 3, 0},
624  	{"0x10", 4, 1},
625  	{"0x10", 5, 0},
626
627  	{"0x8000000000000000", 62, 0},
628  	{"0x8000000000000000", 63, 1},
629  	{"0x8000000000000000", 64, 0},
630
631  	{"0x3" + strings.Repeat("0", 32), 127, 0},
632  	{"0x3" + strings.Repeat("0", 32), 128, 1},
633  	{"0x3" + strings.Repeat("0", 32), 129, 1},
634  	{"0x3" + strings.Repeat("0", 32), 130, 0},
635  }
636
637  func TestBit(t *testing.T) {
638  	for i, test := range bitTests {
639  		x := natFromString(test.x)
640  		if got := x.bit(test.i); got != test.want {
641  			t.Errorf("#%d: %s.bit(%d) = %v; want %v", i, test.x, test.i, got, test.want)
642  		}
643  	}
644  }
645
646  var stickyTests = []struct {
647  	x    string
648  	i    uint
649  	want uint
650  }{
651  	{"0", 0, 0},
652  	{"0", 1, 0},
653  	{"0", 1000, 0},
654
655  	{"0x1", 0, 0},
656  	{"0x1", 1, 1},
657
658  	{"0x1350", 0, 0},
659  	{"0x1350", 4, 0},
660  	{"0x1350", 5, 1},
661
662  	{"0x8000000000000000", 63, 0},
663  	{"0x8000000000000000", 64, 1},
664
665  	{"0x1" + strings.Repeat("0", 100), 400, 0},
666  	{"0x1" + strings.Repeat("0", 100), 401, 1},
667  }
668
669  func TestSticky(t *testing.T) {
670  	for i, test := range stickyTests {
671  		x := natFromString(test.x)
672  		if got := x.sticky(test.i); got != test.want {
673  			t.Errorf("#%d: %s.sticky(%d) = %v; want %v", i, test.x, test.i, got, test.want)
674  		}
675  		if test.want == 1 {
676  			// all subsequent i's should also return 1
677  			for d := uint(1); d <= 3; d++ {
678  				if got := x.sticky(test.i + d); got != 1 {
679  					t.Errorf("#%d: %s.sticky(%d) = %v; want %v", i, test.x, test.i+d, got, 1)
680  				}
681  			}
682  		}
683  	}
684  }
685
686  func testSqr(t *testing.T, x nat) {
687  	got := make(nat, 2*len(x))
688  	want := make(nat, 2*len(x))
689  	got = got.sqr(x)
690  	want = want.mul(x, x)
691  	if got.cmp(want) != 0 {
692  		t.Errorf("basicSqr(%v), got %v, want %v", x, got, want)
693  	}
694  }
695
696  func TestSqr(t *testing.T) {
697  	for _, a := range prodNN {
698  		if a.x != nil {
699  			testSqr(t, a.x)
700  		}
701  		if a.y != nil {
702  			testSqr(t, a.y)
703  		}
704  		if a.z != nil {
705  			testSqr(t, a.z)
706  		}
707  	}
708  }
709
710  func benchmarkNatSqr(b *testing.B, nwords int) {
711  	x := rndNat(nwords)
712  	var z nat
713  	b.ResetTimer()
714  	for i := 0; i < b.N; i++ {
715  		z.sqr(x)
716  	}
717  }
718
719  var sqrBenchSizes = []int{
720  	1, 2, 3, 5, 8, 10, 20, 30, 50, 80,
721  	100, 200, 300, 500, 800,
722  	1000, 10000, 100000,
723  }
724
725  func BenchmarkNatSqr(b *testing.B) {
726  	for _, n := range sqrBenchSizes {
727  		if isRaceBuilder && n > 1e3 {
728  			continue
729  		}
730  		b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
731  			benchmarkNatSqr(b, n)
732  		})
733  	}
734  }
735
736  func BenchmarkNatSetBytes(b *testing.B) {
737  	const maxLength = 128
738  	lengths := []int{
739  		// No remainder:
740  		8, 24, maxLength,
741  		// With remainder:
742  		7, 23, maxLength - 1,
743  	}
744  	n := make(nat, maxLength/_W) // ensure n doesn't need to grow during the test
745  	buf := make([]byte, maxLength)
746  	for _, l := range lengths {
747  		b.Run(fmt.Sprint(l), func(b *testing.B) {
748  			for i := 0; i < b.N; i++ {
749  				n.setBytes(buf[:l])
750  			}
751  		})
752  	}
753  }
754
755  func TestNatDiv(t *testing.T) {
756  	sizes := []int{
757  		1, 2, 5, 8, 15, 25, 40, 65, 100,
758  		200, 500, 800, 1500, 2500, 4000, 6500, 10000,
759  	}
760  	for _, i := range sizes {
761  		for _, j := range sizes {
762  			a := rndNat1(i)
763  			b := rndNat1(j)
764  			// the test requires b >= 2
765  			if len(b) == 1 && b[0] == 1 {
766  				b[0] = 2
767  			}
768  			// choose a remainder c < b
769  			c := rndNat1(len(b))
770  			if len(c) == len(b) && c[len(c)-1] >= b[len(b)-1] {
771  				c[len(c)-1] = 0
772  				c = c.norm()
773  			}
774  			// compute x = a*b+c
775  			x := nat(nil).mul(a, b)
777
778  			var q, r nat
779  			q, r = q.div(r, x, b)
780  			if q.cmp(a) != 0 {
781  				t.Fatalf("wrong quotient: got %s; want %s for %s/%s", q.utoa(10), a.utoa(10), x.utoa(10), b.utoa(10))
782  			}
783  			if r.cmp(c) != 0 {
784  				t.Fatalf("wrong remainder: got %s; want %s for %s/%s", r.utoa(10), c.utoa(10), x.utoa(10), b.utoa(10))
785  			}
786  		}
787  	}
788  }
789
790  // TestIssue37499 triggers the edge case of divBasic where
791  // the inaccurate estimate of the first word's quotient
792  // happens at the very beginning of the loop.
793  func TestIssue37499(t *testing.T) {
794  	// Choose u and v such that v is slightly larger than u >> N.
795  	// This tricks divBasic into choosing 1 as the first word
796  	// of the quotient. This works in both 32-bit and 64-bit settings.
797  	u := natFromString("0x2b6c385a05be027f5c22005b63c42a1165b79ff510e1706b39f8489c1d28e57bb5ba4ef9fd9387a3e344402c0a453381")
798  	v := natFromString("0x2b6c385a05be027f5c22005b63c42a1165b79ff510e1706c")
799
800  	q := nat(nil).make(8)
801  	q.divBasic(u, v)
802  	q = q.norm()
803  	if s := string(q.utoa(16)); s != "fffffffffffffffffffffffffffffffffffffffffffffffb" {
804  		t.Fatalf("incorrect quotient: %s", s)
805  	}
806  }
807
808  // TestIssue42552 triggers an edge case of recursive division
809  // where the first division loop is never entered, and correcting
810  // the remainder takes exactly two iterations in the final loop.
811  func TestIssue42552(t *testing.T) {