blob: 5b80509a318f59f75e5d7bac01984625c24fd707 [file] [log] [blame]
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -07001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package big
6
Adam Langley65063bc2009-11-05 15:55:41 -08007import (
Robert Griesemer5a1d3322009-12-15 15:33:31 -08008 "bytes"
Rob Pike45e3bcb2011-11-08 15:41:54 -08009 "encoding/gob"
Robert Griesemer5a1d3322009-12-15 15:33:31 -080010 "encoding/hex"
Robert Griesemer13a59b82012-05-22 17:20:37 -070011 "encoding/json"
Michael T. Jones1dc82d22014-02-14 12:57:03 -080012 "encoding/xml"
Robert Griesemerdbb62322010-05-15 10:23:41 -070013 "fmt"
Robert Griesemerfc78c5a2011-12-22 14:15:41 -080014 "math/rand"
Robert Griesemer5a1d3322009-12-15 15:33:31 -080015 "testing"
16 "testing/quick"
Adam Langley65063bc2009-11-05 15:55:41 -080017)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070018
Robert Griesemerb9caa4a2010-05-03 18:48:05 -070019func isNormalized(x *Int) bool {
20 if len(x.abs) == 0 {
21 return !x.neg
22 }
23 // len(x.abs) > 0
24 return x.abs[len(x.abs)-1] != 0
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070025}
26
Robert Griesemere5874222009-08-15 11:43:54 -070027type funZZ func(z, x, y *Int) *Int
Russ Cox650bff62009-10-06 14:55:39 -070028type argZZ struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080029 z, x, y *Int
Russ Cox650bff62009-10-06 14:55:39 -070030}
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070031
32var sumZZ = []argZZ{
Robert Griesemer34788912010-10-22 10:06:33 -070033 {NewInt(0), NewInt(0), NewInt(0)},
34 {NewInt(1), NewInt(1), NewInt(0)},
35 {NewInt(1111111110), NewInt(123456789), NewInt(987654321)},
36 {NewInt(-1), NewInt(-1), NewInt(0)},
37 {NewInt(864197532), NewInt(-123456789), NewInt(987654321)},
38 {NewInt(-1111111110), NewInt(-123456789), NewInt(-987654321)},
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070039}
40
Robert Griesemer88742ef2009-08-18 10:06:15 -070041var prodZZ = []argZZ{
Robert Griesemer34788912010-10-22 10:06:33 -070042 {NewInt(0), NewInt(0), NewInt(0)},
43 {NewInt(0), NewInt(1), NewInt(0)},
44 {NewInt(1), NewInt(1), NewInt(1)},
45 {NewInt(-991 * 991), NewInt(991), NewInt(-991)},
Adam Langley65063bc2009-11-05 15:55:41 -080046 // TODO(gri) add larger products
Robert Griesemer88742ef2009-08-18 10:06:15 -070047}
48
Robert Griesemer97bcf042010-07-12 16:09:27 -070049func TestSignZ(t *testing.T) {
50 var zero Int
51 for _, a := range sumZZ {
52 s := a.z.Sign()
53 e := a.z.Cmp(&zero)
54 if s != e {
55 t.Errorf("got %d; want %d for z = %v", s, e, a.z)
56 }
57 }
58}
59
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070060func TestSetZ(t *testing.T) {
61 for _, a := range sumZZ {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080062 var z Int
63 z.Set(a.z)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -070064 if !isNormalized(&z) {
65 t.Errorf("%v is not normalized", z)
66 }
Adam Langley19418552009-11-11 13:21:37 -080067 if (&z).Cmp(a.z) != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080068 t.Errorf("got z = %v; want %v", z, a.z)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070069 }
70 }
71}
72
Robert Griesemer97bcf042010-07-12 16:09:27 -070073func TestAbsZ(t *testing.T) {
74 var zero Int
75 for _, a := range sumZZ {
76 var z Int
77 z.Abs(a.z)
78 var e Int
79 e.Set(a.z)
80 if e.Cmp(&zero) < 0 {
81 e.Sub(&zero, &e)
82 }
83 if z.Cmp(&e) != 0 {
84 t.Errorf("got z = %v; want %v", z, e)
85 }
86 }
87}
88
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070089func testFunZZ(t *testing.T, msg string, f funZZ, a argZZ) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -080090 var z Int
91 f(&z, a.x, a.y)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -070092 if !isNormalized(&z) {
Rob Pikefa7791e2013-09-27 10:09:15 +100093 t.Errorf("%s%v is not normalized", msg, z)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -070094 }
Adam Langley19418552009-11-11 13:21:37 -080095 if (&z).Cmp(a.z) != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -080096 t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, &z, a.z)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -070097 }
98}
99
Robert Griesemer88742ef2009-08-18 10:06:15 -0700100func TestSumZZ(t *testing.T) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800101 AddZZ := func(z, x, y *Int) *Int { return z.Add(x, y) }
102 SubZZ := func(z, x, y *Int) *Int { return z.Sub(x, y) }
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700103 for _, a := range sumZZ {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800104 arg := a
105 testFunZZ(t, "AddZZ", AddZZ, arg)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700106
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800107 arg = argZZ{a.z, a.y, a.x}
108 testFunZZ(t, "AddZZ symmetric", AddZZ, arg)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700109
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800110 arg = argZZ{a.x, a.z, a.y}
111 testFunZZ(t, "SubZZ", SubZZ, arg)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700112
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800113 arg = argZZ{a.y, a.z, a.x}
114 testFunZZ(t, "SubZZ symmetric", SubZZ, arg)
Robert Griesemerdb3bf9c2009-08-14 11:53:27 -0700115 }
116}
Robert Griesemer88742ef2009-08-18 10:06:15 -0700117
Robert Griesemer88742ef2009-08-18 10:06:15 -0700118func TestProdZZ(t *testing.T) {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800119 MulZZ := func(z, x, y *Int) *Int { return z.Mul(x, y) }
Robert Griesemer88742ef2009-08-18 10:06:15 -0700120 for _, a := range prodZZ {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800121 arg := a
122 testFunZZ(t, "MulZZ", MulZZ, arg)
Robert Griesemer88742ef2009-08-18 10:06:15 -0700123
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800124 arg = argZZ{a.z, a.y, a.x}
125 testFunZZ(t, "MulZZ symmetric", MulZZ, arg)
Robert Griesemer88742ef2009-08-18 10:06:15 -0700126 }
127}
128
Robert Griesemerb2183702010-04-27 19:16:08 -0700129// mulBytes returns x*y via grade school multiplication. Both inputs
130// and the result are assumed to be in big-endian representation (to
131// match the semantics of Int.Bytes and Int.SetBytes).
132func mulBytes(x, y []byte) []byte {
133 z := make([]byte, len(x)+len(y))
Robert Griesemer88742ef2009-08-18 10:06:15 -0700134
Robert Griesemerb2183702010-04-27 19:16:08 -0700135 // multiply
136 k0 := len(z) - 1
137 for j := len(y) - 1; j >= 0; j-- {
138 d := int(y[j])
139 if d != 0 {
140 k := k0
141 carry := 0
142 for i := len(x) - 1; i >= 0; i-- {
143 t := int(z[k]) + int(x[i])*d + carry
144 z[k], carry = byte(t), t>>8
145 k--
146 }
147 z[k] = byte(carry)
Robert Griesemer88742ef2009-08-18 10:06:15 -0700148 }
Robert Griesemerb2183702010-04-27 19:16:08 -0700149 k0--
150 }
151
152 // normalize (remove leading 0's)
153 i := 0
154 for i < len(z) && z[i] == 0 {
155 i++
156 }
157
158 return z[i:]
159}
160
Robert Griesemerb2183702010-04-27 19:16:08 -0700161func checkMul(a, b []byte) bool {
162 var x, y, z1 Int
163 x.SetBytes(a)
164 y.SetBytes(b)
165 z1.Mul(&x, &y)
166
167 var z2 Int
168 z2.SetBytes(mulBytes(a, b))
169
170 return z1.Cmp(&z2) == 0
171}
172
Robert Griesemerb2183702010-04-27 19:16:08 -0700173func TestMul(t *testing.T) {
174 if err := quick.Check(checkMul, nil); err != nil {
175 t.Error(err)
Robert Griesemer88742ef2009-08-18 10:06:15 -0700176 }
177}
Adam Langley65063bc2009-11-05 15:55:41 -0800178
Robert Griesemere3515332010-10-25 17:45:43 -0700179var mulRangesZ = []struct {
Robert Griesemerdbb62322010-05-15 10:23:41 -0700180 a, b int64
181 prod string
Robert Griesemere3515332010-10-25 17:45:43 -0700182}{
Robert Griesemerdbb62322010-05-15 10:23:41 -0700183 // entirely positive ranges are covered by mulRangesN
Robert Griesemer34788912010-10-22 10:06:33 -0700184 {-1, 1, "0"},
185 {-2, -1, "2"},
186 {-3, -2, "6"},
187 {-3, -1, "-6"},
188 {1, 3, "6"},
189 {-10, -10, "-10"},
190 {0, -1, "1"}, // empty range
191 {-1, -100, "1"}, // empty range
192 {-1, 1, "0"}, // range includes 0
193 {-1e9, 0, "0"}, // range includes 0
194 {-1e9, 1e9, "0"}, // range includes 0
195 {-10, -1, "3628800"}, // 10!
196 {-20, -2, "-2432902008176640000"}, // -20!
197 {-99, -1,
Robert Griesemerdbb62322010-05-15 10:23:41 -0700198 "-933262154439441526816992388562667004907159682643816214685929" +
199 "638952175999932299156089414639761565182862536979208272237582" +
200 "511852109168640000000000000000000000", // -99!
201 },
202}
203
Robert Griesemerdbb62322010-05-15 10:23:41 -0700204func TestMulRangeZ(t *testing.T) {
205 var tmp Int
206 // test entirely positive ranges
207 for i, r := range mulRangesN {
208 prod := tmp.MulRange(int64(r.a), int64(r.b)).String()
209 if prod != r.prod {
210 t.Errorf("#%da: got %s; want %s", i, prod, r.prod)
211 }
212 }
213 // test other ranges
214 for i, r := range mulRangesZ {
215 prod := tmp.MulRange(r.a, r.b).String()
216 if prod != r.prod {
217 t.Errorf("#%db: got %s; want %s", i, prod, r.prod)
218 }
219 }
220}
221
Robert Griesemer919a6fb2015-04-01 11:49:12 -0700222func TestBinomial(t *testing.T) {
223 var z Int
224 for _, test := range []struct {
225 n, k int64
226 want string
227 }{
228 {0, 0, "1"},
229 {0, 1, "0"},
230 {1, 0, "1"},
231 {1, 1, "1"},
232 {1, 10, "0"},
233 {4, 0, "1"},
234 {4, 1, "4"},
235 {4, 2, "6"},
236 {4, 3, "4"},
237 {4, 4, "1"},
238 {10, 1, "10"},
239 {10, 9, "10"},
240 {10, 5, "252"},
241 {11, 5, "462"},
242 {11, 6, "462"},
243 {100, 10, "17310309456440"},
244 {100, 90, "17310309456440"},
245 {1000, 10, "263409560461970212832400"},
246 {1000, 990, "263409560461970212832400"},
247 } {
248 if got := z.Binomial(test.n, test.k).String(); got != test.want {
249 t.Errorf("Binomial(%d, %d) = %s; want %s", test.n, test.k, got, test.want)
250 }
251 }
252}
253
254func BenchmarkBinomial(b *testing.B) {
255 var z Int
256 for i := b.N - 1; i >= 0; i-- {
257 z.Binomial(1000, 990)
258 }
259}
260
Robert Griesemere3515332010-10-25 17:45:43 -0700261// Examples from the Go Language Spec, section "Arithmetic operators"
262var divisionSignsTests = []struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800263 x, y int64
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700264 q, r int64 // T-division
265 d, m int64 // Euclidian division
Robert Griesemere3515332010-10-25 17:45:43 -0700266}{
Robert Griesemer34788912010-10-22 10:06:33 -0700267 {5, 3, 1, 2, 1, 2},
268 {-5, 3, -1, -2, -2, 1},
269 {5, -3, -1, 2, -1, 2},
270 {-5, -3, 1, -2, 2, 1},
271 {1, 2, 0, 1, 0, 1},
272 {8, 4, 2, 0, 2, 0},
Adam Langley65063bc2009-11-05 15:55:41 -0800273}
274
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700275func TestDivisionSigns(t *testing.T) {
276 for i, test := range divisionSignsTests {
277 x := NewInt(test.x)
278 y := NewInt(test.y)
279 q := NewInt(test.q)
280 r := NewInt(test.r)
281 d := NewInt(test.d)
282 m := NewInt(test.m)
Adam Langley65063bc2009-11-05 15:55:41 -0800283
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700284 q1 := new(Int).Quo(x, y)
285 r1 := new(Int).Rem(x, y)
286 if !isNormalized(q1) {
287 t.Errorf("#%d Quo: %v is not normalized", i, *q1)
288 }
289 if !isNormalized(r1) {
290 t.Errorf("#%d Rem: %v is not normalized", i, *r1)
291 }
292 if q1.Cmp(q) != 0 || r1.Cmp(r) != 0 {
293 t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q1, r1, q, r)
294 }
295
296 q2, r2 := new(Int).QuoRem(x, y, new(Int))
297 if !isNormalized(q2) {
298 t.Errorf("#%d Quo: %v is not normalized", i, *q2)
299 }
300 if !isNormalized(r2) {
301 t.Errorf("#%d Rem: %v is not normalized", i, *r2)
302 }
303 if q2.Cmp(q) != 0 || r2.Cmp(r) != 0 {
304 t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q2, r2, q, r)
305 }
306
307 d1 := new(Int).Div(x, y)
308 m1 := new(Int).Mod(x, y)
309 if !isNormalized(d1) {
310 t.Errorf("#%d Div: %v is not normalized", i, *d1)
311 }
312 if !isNormalized(m1) {
313 t.Errorf("#%d Mod: %v is not normalized", i, *m1)
314 }
315 if d1.Cmp(d) != 0 || m1.Cmp(m) != 0 {
316 t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d1, m1, d, m)
317 }
318
319 d2, m2 := new(Int).DivMod(x, y, new(Int))
320 if !isNormalized(d2) {
321 t.Errorf("#%d Div: %v is not normalized", i, *d2)
322 }
323 if !isNormalized(m2) {
324 t.Errorf("#%d Mod: %v is not normalized", i, *m2)
325 }
326 if d2.Cmp(d) != 0 || m2.Cmp(m) != 0 {
327 t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d2, m2, d, m)
Adam Langley65063bc2009-11-05 15:55:41 -0800328 }
329 }
330}
331
Robert Griesemerde47e9c2015-01-16 15:11:26 -0800332func norm(x nat) nat {
333 i := len(x)
334 for i > 0 && x[i-1] == 0 {
335 i--
336 }
337 return x[:i]
338}
339
340func TestBits(t *testing.T) {
Robert Griesemerc20a0182015-02-25 10:20:28 -0800341 for _, test := range []nat{
342 nil,
343 {0},
344 {1},
345 {0, 1, 2, 3, 4},
346 {4, 3, 2, 1, 0},
347 {4, 3, 2, 1, 0, 0, 0, 0},
348 } {
Robert Griesemerde47e9c2015-01-16 15:11:26 -0800349 var z Int
350 z.neg = true
351 got := z.SetBits(test)
352 want := norm(test)
353 if got.abs.cmp(want) != 0 {
354 t.Errorf("SetBits(%v) = %v; want %v", test, got.abs, want)
355 }
356
357 if got.neg {
Robert Griesemerc20a0182015-02-25 10:20:28 -0800358 t.Errorf("SetBits(%v): got negative result", test)
Robert Griesemerde47e9c2015-01-16 15:11:26 -0800359 }
360
361 bits := nat(z.Bits())
362 if bits.cmp(want) != 0 {
363 t.Errorf("%v.Bits() = %v; want %v", z.abs, bits, want)
364 }
365 }
366}
367
Adam Langley65063bc2009-11-05 15:55:41 -0800368func checkSetBytes(b []byte) bool {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800369 hex1 := hex.EncodeToString(new(Int).SetBytes(b).Bytes())
370 hex2 := hex.EncodeToString(b)
Adam Langley65063bc2009-11-05 15:55:41 -0800371
372 for len(hex1) < len(hex2) {
Robert Griesemer16989342009-11-09 21:09:34 -0800373 hex1 = "0" + hex1
Adam Langley65063bc2009-11-05 15:55:41 -0800374 }
375
376 for len(hex1) > len(hex2) {
Robert Griesemer16989342009-11-09 21:09:34 -0800377 hex2 = "0" + hex2
Adam Langley65063bc2009-11-05 15:55:41 -0800378 }
379
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800380 return hex1 == hex2
Adam Langley65063bc2009-11-05 15:55:41 -0800381}
382
Adam Langley65063bc2009-11-05 15:55:41 -0800383func TestSetBytes(t *testing.T) {
Robert Griesemerb2183702010-04-27 19:16:08 -0700384 if err := quick.Check(checkSetBytes, nil); err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800385 t.Error(err)
Adam Langley65063bc2009-11-05 15:55:41 -0800386 }
387}
388
Adam Langley65063bc2009-11-05 15:55:41 -0800389func checkBytes(b []byte) bool {
Robert Griesemere2882712015-08-21 11:30:19 -0700390 // trim leading zero bytes since Bytes() won't return them
391 // (was issue 12231)
392 for len(b) > 0 && b[0] == 0 {
393 b = b[1:]
394 }
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800395 b2 := new(Int).SetBytes(b).Bytes()
Matthew Dempsky46811d22013-01-07 10:03:49 +1100396 return bytes.Equal(b, b2)
Adam Langley65063bc2009-11-05 15:55:41 -0800397}
398
Adam Langley65063bc2009-11-05 15:55:41 -0800399func TestBytes(t *testing.T) {
Jeremy Schlatterff1f3a12015-03-27 03:29:06 +0000400 if err := quick.Check(checkBytes, nil); err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800401 t.Error(err)
Adam Langley65063bc2009-11-05 15:55:41 -0800402 }
403}
404
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700405func checkQuo(x, y []byte) bool {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800406 u := new(Int).SetBytes(x)
407 v := new(Int).SetBytes(y)
Adam Langley65063bc2009-11-05 15:55:41 -0800408
409 if len(v.abs) == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800410 return true
Adam Langley65063bc2009-11-05 15:55:41 -0800411 }
412
Evan Shaw76cbbc82010-04-20 20:39:36 -0700413 r := new(Int)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700414 q, r := new(Int).QuoRem(u, v, r)
Adam Langley65063bc2009-11-05 15:55:41 -0800415
Adam Langley19418552009-11-11 13:21:37 -0800416 if r.Cmp(v) >= 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800417 return false
Adam Langley65063bc2009-11-05 15:55:41 -0800418 }
419
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800420 uprime := new(Int).Set(q)
421 uprime.Mul(uprime, v)
422 uprime.Add(uprime, r)
Adam Langley65063bc2009-11-05 15:55:41 -0800423
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800424 return uprime.Cmp(u) == 0
Adam Langley65063bc2009-11-05 15:55:41 -0800425}
426
Robert Griesemere3515332010-10-25 17:45:43 -0700427var quoTests = []struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800428 x, y string
429 q, r string
Robert Griesemere3515332010-10-25 17:45:43 -0700430}{
Robert Griesemer34788912010-10-22 10:06:33 -0700431 {
Adam Langleydb4e48e2009-11-06 11:36:21 -0800432 "476217953993950760840509444250624797097991362735329973741718102894495832294430498335824897858659711275234906400899559094370964723884706254265559534144986498357",
433 "9353930466774385905609975137998169297361893554149986716853295022578535724979483772383667534691121982974895531435241089241440253066816724367338287092081996",
434 "50911",
435 "1",
436 },
Robert Griesemer34788912010-10-22 10:06:33 -0700437 {
Adam Langley19418552009-11-11 13:21:37 -0800438 "11510768301994997771168",
439 "1328165573307167369775",
440 "8",
441 "885443715537658812968",
442 },
Adam Langleydb4e48e2009-11-06 11:36:21 -0800443}
444
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700445func TestQuo(t *testing.T) {
446 if err := quick.Check(checkQuo, nil); err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800447 t.Error(err)
Adam Langley65063bc2009-11-05 15:55:41 -0800448 }
Adam Langleydb4e48e2009-11-06 11:36:21 -0800449
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700450 for i, test := range quoTests {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800451 x, _ := new(Int).SetString(test.x, 10)
452 y, _ := new(Int).SetString(test.y, 10)
453 expectedQ, _ := new(Int).SetString(test.q, 10)
454 expectedR, _ := new(Int).SetString(test.r, 10)
Adam Langleydb4e48e2009-11-06 11:36:21 -0800455
Evan Shaw76cbbc82010-04-20 20:39:36 -0700456 r := new(Int)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700457 q, r := new(Int).QuoRem(x, y, r)
Adam Langleydb4e48e2009-11-06 11:36:21 -0800458
Adam Langley19418552009-11-11 13:21:37 -0800459 if q.Cmp(expectedQ) != 0 || r.Cmp(expectedR) != 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800460 t.Errorf("#%d got (%s, %s) want (%s, %s)", i, q, r, expectedQ, expectedR)
Adam Langleydb4e48e2009-11-06 11:36:21 -0800461 }
462 }
Adam Langley65063bc2009-11-05 15:55:41 -0800463}
464
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700465func TestQuoStepD6(t *testing.T) {
Adam Langley65063bc2009-11-05 15:55:41 -0800466 // See Knuth, Volume 2, section 4.3.1, exercise 21. This code exercises
467 // a code path which only triggers 1 in 10^{-19} cases.
468
Evan Shaw841a32d2010-04-22 16:57:29 -0700469 u := &Int{false, nat{0, 0, 1 + 1<<(_W-1), _M ^ (1 << (_W - 1))}}
470 v := &Int{false, nat{5, 2 + 1<<(_W-1), 1 << (_W - 1)}}
Adam Langley65063bc2009-11-05 15:55:41 -0800471
Evan Shaw76cbbc82010-04-20 20:39:36 -0700472 r := new(Int)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700473 q, r := new(Int).QuoRem(u, v, r)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800474 const expectedQ64 = "18446744073709551613"
475 const expectedR64 = "3138550867693340382088035895064302439801311770021610913807"
476 const expectedQ32 = "4294967293"
477 const expectedR32 = "39614081266355540837921718287"
Adam Langley17021f42009-11-06 09:05:19 -0800478 if q.String() != expectedQ64 && q.String() != expectedQ32 ||
Robert Griesemer56eca9d2009-11-06 11:00:06 -0800479 r.String() != expectedR64 && r.String() != expectedR32 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800480 t.Errorf("got (%s, %s) want (%s, %s) or (%s, %s)", q, r, expectedQ64, expectedR64, expectedQ32, expectedR32)
Adam Langley65063bc2009-11-05 15:55:41 -0800481 }
482}
483
Robert Griesemere3515332010-10-25 17:45:43 -0700484var bitLenTests = []struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800485 in string
486 out int
Robert Griesemere3515332010-10-25 17:45:43 -0700487}{
Robert Griesemer34788912010-10-22 10:06:33 -0700488 {"-1", 1},
489 {"0", 0},
490 {"1", 1},
491 {"2", 2},
492 {"4", 3},
493 {"0xabc", 12},
494 {"0x8000", 16},
495 {"0x80000000", 32},
496 {"0x800000000000", 48},
497 {"0x8000000000000000", 64},
498 {"0x80000000000000000000", 80},
499 {"-0x4000000000000000000000", 87},
Adam Langley65063bc2009-11-05 15:55:41 -0800500}
501
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700502func TestBitLen(t *testing.T) {
503 for i, test := range bitLenTests {
504 x, ok := new(Int).SetString(test.in, 0)
Adam Langley65063bc2009-11-05 15:55:41 -0800505 if !ok {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800506 t.Errorf("#%d test input invalid: %s", i, test.in)
507 continue
Adam Langley65063bc2009-11-05 15:55:41 -0800508 }
509
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700510 if n := x.BitLen(); n != test.out {
Rob Pike1959c3a2010-09-23 13:48:56 +1000511 t.Errorf("#%d got %d want %d", i, n, test.out)
Adam Langley65063bc2009-11-05 15:55:41 -0800512 }
513 }
514}
515
Robert Griesemere3515332010-10-25 17:45:43 -0700516var expTests = []struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800517 x, y, m string
518 out string
Robert Griesemere3515332010-10-25 17:45:43 -0700519}{
Robert Griesemer26533862014-04-21 15:54:51 -0700520 // y <= 0
521 {"0", "0", "", "1"},
522 {"1", "0", "", "1"},
523 {"-10", "0", "", "1"},
524 {"1234", "-1", "", "1"},
525
526 // m == 1
527 {"0", "0", "1", "0"},
528 {"1", "0", "1", "0"},
529 {"-10", "0", "1", "0"},
530 {"1234", "-1", "1", "0"},
531
532 // misc
ALTreee21154f2015-04-06 21:18:37 +0200533 {"5", "1", "3", "2"},
Robert Griesemer75657262012-10-16 13:46:27 -0700534 {"5", "-7", "", "1"},
535 {"-5", "-7", "", "1"},
Robert Griesemer34788912010-10-22 10:06:33 -0700536 {"5", "0", "", "1"},
Robert Griesemer75657262012-10-16 13:46:27 -0700537 {"-5", "0", "", "1"},
Robert Griesemer34788912010-10-22 10:06:33 -0700538 {"5", "1", "", "5"},
539 {"-5", "1", "", "-5"},
Robert Griesemer28ddfb02014-10-02 13:02:25 -0700540 {"-5", "1", "7", "2"},
Robert Griesemer34788912010-10-22 10:06:33 -0700541 {"-2", "3", "2", "0"},
542 {"5", "2", "", "25"},
543 {"1", "65537", "2", "1"},
544 {"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
545 {"0x8000000000000000", "2", "6719", "4944"},
546 {"0x8000000000000000", "3", "6719", "5447"},
547 {"0x8000000000000000", "1000", "6719", "1603"},
548 {"0x8000000000000000", "1000000", "6719", "3199"},
Robert Griesemer75657262012-10-16 13:46:27 -0700549 {"0x8000000000000000", "-1000000", "6719", "1"},
Robert Griesemer34788912010-10-22 10:06:33 -0700550 {
Adam Langley65063bc2009-11-05 15:55:41 -0800551 "2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
552 "298472983472983471903246121093472394872319615612417471234712061",
553 "29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
554 "23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
Adam Langley17021f42009-11-06 09:05:19 -0800555 },
Robert Griesemer28ddfb02014-10-02 13:02:25 -0700556 // test case for issue 8822
557 {
558 "-0x1BCE04427D8032319A89E5C4136456671AC620883F2C4139E57F91307C485AD2D6204F4F87A58262652DB5DBBAC72B0613E51B835E7153BEC6068F5C8D696B74DBD18FEC316AEF73985CF0475663208EB46B4F17DD9DA55367B03323E5491A70997B90C059FB34809E6EE55BCFBD5F2F52233BFE62E6AA9E4E26A1D4C2439883D14F2633D55D8AA66A1ACD5595E778AC3A280517F1157989E70C1A437B849F1877B779CC3CDDEDE2DAA6594A6C66D181A00A5F777EE60596D8773998F6E988DEAE4CCA60E4DDCF9590543C89F74F603259FCAD71660D30294FBBE6490300F78A9D63FA660DC9417B8B9DDA28BEB3977B621B988E23D4D954F322C3540541BC649ABD504C50FADFD9F0987D58A2BF689313A285E773FF02899A6EF887D1D4A0D2",
559 "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD",
560 "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73",
561 "21484252197776302499639938883777710321993113097987201050501182909581359357618579566746556372589385361683610524730509041328855066514963385522570894839035884713051640171474186548713546686476761306436434146475140156284389181808675016576845833340494848283681088886584219750554408060556769486628029028720727393293111678826356480455433909233520504112074401376133077150471237549474149190242010469539006449596611576612573955754349042329130631128234637924786466585703488460540228477440853493392086251021228087076124706778899179648655221663765993962724699135217212118535057766739392069738618682722216712319320435674779146070442",
562 },
Adam Langley65063bc2009-11-05 15:55:41 -0800563}
564
Adam Langley65063bc2009-11-05 15:55:41 -0800565func TestExp(t *testing.T) {
566 for i, test := range expTests {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800567 x, ok1 := new(Int).SetString(test.x, 0)
568 y, ok2 := new(Int).SetString(test.y, 0)
569 out, ok3 := new(Int).SetString(test.out, 0)
Adam Langley65063bc2009-11-05 15:55:41 -0800570
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800571 var ok4 bool
572 var m *Int
Adam Langley65063bc2009-11-05 15:55:41 -0800573
574 if len(test.m) == 0 {
Robert Griesemer40621d52009-11-09 12:07:39 -0800575 m, ok4 = nil, true
Adam Langley65063bc2009-11-05 15:55:41 -0800576 } else {
Robert Griesemer40621d52009-11-09 12:07:39 -0800577 m, ok4 = new(Int).SetString(test.m, 0)
Adam Langley65063bc2009-11-05 15:55:41 -0800578 }
579
580 if !ok1 || !ok2 || !ok3 || !ok4 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700581 t.Errorf("#%d: error in input", i)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800582 continue
Adam Langley65063bc2009-11-05 15:55:41 -0800583 }
584
Robert Griesemer75657262012-10-16 13:46:27 -0700585 z1 := new(Int).Exp(x, y, m)
586 if !isNormalized(z1) {
587 t.Errorf("#%d: %v is not normalized", i, *z1)
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700588 }
Robert Griesemer75657262012-10-16 13:46:27 -0700589 if z1.Cmp(out) != 0 {
590 t.Errorf("#%d: got %s want %s", i, z1, out)
591 }
592
593 if m == nil {
Robert Griesemer28ddfb02014-10-02 13:02:25 -0700594 // The result should be the same as for m == 0;
595 // specifically, there should be no div-zero panic.
Robert Griesemer75657262012-10-16 13:46:27 -0700596 m = &Int{abs: nat{}} // m != nil && len(m.abs) == 0
597 z2 := new(Int).Exp(x, y, m)
598 if z2.Cmp(z1) != 0 {
Robert Griesemer28ddfb02014-10-02 13:02:25 -0700599 t.Errorf("#%d: got %s want %s", i, z2, z1)
Robert Griesemer75657262012-10-16 13:46:27 -0700600 }
Adam Langley65063bc2009-11-05 15:55:41 -0800601 }
602 }
603}
604
Adam Langley65063bc2009-11-05 15:55:41 -0800605func checkGcd(aBytes, bBytes []byte) bool {
Robert Griesemer10b88882012-06-13 13:54:36 -0700606 x := new(Int)
607 y := new(Int)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800608 a := new(Int).SetBytes(aBytes)
609 b := new(Int).SetBytes(bBytes)
Adam Langley65063bc2009-11-05 15:55:41 -0800610
Robert Griesemer10b88882012-06-13 13:54:36 -0700611 d := new(Int).GCD(x, y, a, b)
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800612 x.Mul(x, a)
613 y.Mul(y, b)
614 x.Add(x, y)
Adam Langley65063bc2009-11-05 15:55:41 -0800615
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800616 return x.Cmp(d) == 0
Adam Langley65063bc2009-11-05 15:55:41 -0800617}
618
Robert Griesemere3515332010-10-25 17:45:43 -0700619var gcdTests = []struct {
Robert Griesemer10b88882012-06-13 13:54:36 -0700620 d, x, y, a, b string
Robert Griesemere3515332010-10-25 17:45:43 -0700621}{
Robert Griesemer10b88882012-06-13 13:54:36 -0700622 // a <= 0 || b <= 0
623 {"0", "0", "0", "0", "0"},
624 {"0", "0", "0", "0", "7"},
625 {"0", "0", "0", "11", "0"},
626 {"0", "0", "0", "-77", "35"},
627 {"0", "0", "0", "64515", "-24310"},
628 {"0", "0", "0", "-64515", "-24310"},
629
630 {"1", "-9", "47", "120", "23"},
631 {"7", "1", "-2", "77", "35"},
632 {"935", "-3", "8", "64515", "24310"},
633 {"935000000000000000", "-3", "8", "64515000000000000000", "24310000000000000000"},
634 {"1", "-221", "22059940471369027483332068679400581064239780177629666810348940098015901108344", "98920366548084643601728869055592650835572950932266967461790948584315647051443", "991"},
635
636 // test early exit (after one Euclidean iteration) in binaryGCD
637 {"1", "", "", "1", "98920366548084643601728869055592650835572950932266967461790948584315647051443"},
638}
639
640func testGcd(t *testing.T, d, x, y, a, b *Int) {
641 var X *Int
642 if x != nil {
643 X = new(Int)
644 }
645 var Y *Int
646 if y != nil {
647 Y = new(Int)
648 }
649
650 D := new(Int).GCD(X, Y, a, b)
651 if D.Cmp(d) != 0 {
652 t.Errorf("GCD(%s, %s): got d = %s, want %s", a, b, D, d)
653 }
654 if x != nil && X.Cmp(x) != 0 {
655 t.Errorf("GCD(%s, %s): got x = %s, want %s", a, b, X, x)
656 }
657 if y != nil && Y.Cmp(y) != 0 {
658 t.Errorf("GCD(%s, %s): got y = %s, want %s", a, b, Y, y)
659 }
660
661 // binaryGCD requires a > 0 && b > 0
662 if a.Sign() <= 0 || b.Sign() <= 0 {
663 return
664 }
665
666 D.binaryGCD(a, b)
667 if D.Cmp(d) != 0 {
668 t.Errorf("binaryGcd(%s, %s): got d = %s, want %s", a, b, D, d)
669 }
Robert Griesemer1eb8c4a2015-06-19 12:50:38 -0700670
671 // check results in presence of aliasing (issue #11284)
672 a2 := new(Int).Set(a)
673 b2 := new(Int).Set(b)
674 a2.binaryGCD(a2, b2) // result is same as 1st argument
675 if a2.Cmp(d) != 0 {
676 t.Errorf("binaryGcd(%s, %s): got d = %s, want %s", a, b, a2, d)
677 }
678
679 a2 = new(Int).Set(a)
680 b2 = new(Int).Set(b)
681 b2.binaryGCD(a2, b2) // result is same as 2nd argument
682 if b2.Cmp(d) != 0 {
683 t.Errorf("binaryGcd(%s, %s): got d = %s, want %s", a, b, b2, d)
684 }
Adam Langley65063bc2009-11-05 15:55:41 -0800685}
686
Adam Langley65063bc2009-11-05 15:55:41 -0800687func TestGcd(t *testing.T) {
Robert Griesemer10b88882012-06-13 13:54:36 -0700688 for _, test := range gcdTests {
689 d, _ := new(Int).SetString(test.d, 0)
690 x, _ := new(Int).SetString(test.x, 0)
691 y, _ := new(Int).SetString(test.y, 0)
692 a, _ := new(Int).SetString(test.a, 0)
693 b, _ := new(Int).SetString(test.b, 0)
Adam Langley65063bc2009-11-05 15:55:41 -0800694
Robert Griesemer10b88882012-06-13 13:54:36 -0700695 testGcd(t, d, nil, nil, a, b)
696 testGcd(t, d, x, nil, a, b)
697 testGcd(t, d, nil, y, a, b)
698 testGcd(t, d, x, y, a, b)
Adam Langley65063bc2009-11-05 15:55:41 -0800699 }
700
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800701 quick.Check(checkGcd, nil)
Adam Langley65063bc2009-11-05 15:55:41 -0800702}
Adam Langley19418552009-11-11 13:21:37 -0800703
Adam Langley19418552009-11-11 13:21:37 -0800704var primes = []string{
705 "2",
706 "3",
707 "5",
708 "7",
709 "11",
Russ Coxcfbee342010-01-05 16:49:05 -0800710
711 "13756265695458089029",
712 "13496181268022124907",
713 "10953742525620032441",
714 "17908251027575790097",
715
Brad Fitzpatrick2ae77372015-07-10 17:17:11 -0600716 // https://golang.org/issue/638
Adam Langley308064f2010-03-05 15:55:26 -0500717 "18699199384836356663",
718
Adam Langley19418552009-11-11 13:21:37 -0800719 "98920366548084643601728869055592650835572950932266967461790948584315647051443",
720 "94560208308847015747498523884063394671606671904944666360068158221458669711639",
Russ Coxcfbee342010-01-05 16:49:05 -0800721
Adam Langley19418552009-11-11 13:21:37 -0800722 // http://primes.utm.edu/lists/small/small3.html
723 "449417999055441493994709297093108513015373787049558499205492347871729927573118262811508386655998299074566974373711472560655026288668094291699357843464363003144674940345912431129144354948751003607115263071543163",
724 "230975859993204150666423538988557839555560243929065415434980904258310530753006723857139742334640122533598517597674807096648905501653461687601339782814316124971547968912893214002992086353183070342498989426570593",
725 "5521712099665906221540423207019333379125265462121169655563495403888449493493629943498064604536961775110765377745550377067893607246020694972959780839151452457728855382113555867743022746090187341871655890805971735385789993",
726 "203956878356401977405765866929034577280193993314348263094772646453283062722701277632936616063144088173312372882677123879538709400158306567338328279154499698366071906766440037074217117805690872792848149112022286332144876183376326512083574821647933992961249917319836219304274280243803104015000563790123",
Bryan Fordac615882014-12-19 14:28:44 -0500727
728 // ECC primes: http://tools.ietf.org/html/draft-ladd-safecurves-02
729 "3618502788666131106986593281521497120414687020801267626233049500247285301239", // Curve1174: 2^251-9
730 "57896044618658097711785492504343953926634992332820282019728792003956564819949", // Curve25519: 2^255-19
731 "9850501549098619803069760025035903451269934817616361666987073351061430442874302652853566563721228910201656997576599", // E-382: 2^382-105
732 "42307582002575910332922579714097346549017899709713998034217522897561970639123926132812109468141778230245837569601494931472367", // Curve41417: 2^414-17
733 "6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151", // E-521: 2^521-1
Adam Langley19418552009-11-11 13:21:37 -0800734}
735
Adam Langley19418552009-11-11 13:21:37 -0800736var composites = []string{
Robert Griesemerde47e9c2015-01-16 15:11:26 -0800737 "0",
738 "1",
Adam Langley19418552009-11-11 13:21:37 -0800739 "21284175091214687912771199898307297748211672914763848041968395774954376176754",
740 "6084766654921918907427900243509372380954290099172559290432744450051395395951",
741 "84594350493221918389213352992032324280367711247940675652888030554255915464401",
742 "82793403787388584738507275144194252681",
743}
744
Adam Langley19418552009-11-11 13:21:37 -0800745func TestProbablyPrime(t *testing.T) {
Rob Pike7f9acb52011-03-26 11:25:22 -0700746 nreps := 20
747 if testing.Short() {
748 nreps = 1
749 }
Adam Langley19418552009-11-11 13:21:37 -0800750 for i, s := range primes {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800751 p, _ := new(Int).SetString(s, 10)
Robert Griesemerb80c7e52012-02-02 19:21:55 -0800752 if !p.ProbablyPrime(nreps) {
Adam Langley308064f2010-03-05 15:55:26 -0500753 t.Errorf("#%d prime found to be non-prime (%s)", i, s)
Adam Langley19418552009-11-11 13:21:37 -0800754 }
755 }
756
757 for i, s := range composites {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800758 c, _ := new(Int).SetString(s, 10)
Robert Griesemerb80c7e52012-02-02 19:21:55 -0800759 if c.ProbablyPrime(nreps) {
Adam Langley308064f2010-03-05 15:55:26 -0500760 t.Errorf("#%d composite found to be prime (%s)", i, s)
Adam Langley19418552009-11-11 13:21:37 -0800761 }
Rob Pike7f9acb52011-03-26 11:25:22 -0700762 if testing.Short() {
763 break
764 }
Adam Langley19418552009-11-11 13:21:37 -0800765 }
Shenghou Ma43178692015-01-05 16:39:34 -0500766
767 // check that ProbablyPrime panics if n <= 0
768 c := NewInt(11) // a prime
769 for _, n := range []int{-1, 0, 1} {
770 func() {
771 defer func() {
772 if n <= 0 && recover() == nil {
773 t.Fatalf("expected panic from ProbablyPrime(%d)", n)
774 }
775 }()
776 if !c.ProbablyPrime(n) {
777 t.Fatalf("%v should be a prime", c)
778 }
779 }()
780 }
Adam Langley19418552009-11-11 13:21:37 -0800781}
782
Evan Shaw76cbbc82010-04-20 20:39:36 -0700783type intShiftTest struct {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800784 in string
Evan Shaw76cbbc82010-04-20 20:39:36 -0700785 shift uint
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800786 out string
Adam Langley19418552009-11-11 13:21:37 -0800787}
788
Evan Shaw76cbbc82010-04-20 20:39:36 -0700789var rshTests = []intShiftTest{
Robert Griesemer34788912010-10-22 10:06:33 -0700790 {"0", 0, "0"},
791 {"-0", 0, "0"},
792 {"0", 1, "0"},
793 {"0", 2, "0"},
794 {"1", 0, "1"},
795 {"1", 1, "0"},
796 {"1", 2, "0"},
797 {"2", 0, "2"},
798 {"2", 1, "1"},
799 {"-1", 0, "-1"},
800 {"-1", 1, "-1"},
801 {"-1", 10, "-1"},
802 {"-100", 2, "-25"},
803 {"-100", 3, "-13"},
804 {"-100", 100, "-1"},
805 {"4294967296", 0, "4294967296"},
806 {"4294967296", 1, "2147483648"},
807 {"4294967296", 2, "1073741824"},
808 {"18446744073709551616", 0, "18446744073709551616"},
809 {"18446744073709551616", 1, "9223372036854775808"},
810 {"18446744073709551616", 2, "4611686018427387904"},
811 {"18446744073709551616", 64, "1"},
812 {"340282366920938463463374607431768211456", 64, "18446744073709551616"},
813 {"340282366920938463463374607431768211456", 128, "1"},
Adam Langley19418552009-11-11 13:21:37 -0800814}
815
Adam Langley19418552009-11-11 13:21:37 -0800816func TestRsh(t *testing.T) {
817 for i, test := range rshTests {
Robert Griesemer5a1d3322009-12-15 15:33:31 -0800818 in, _ := new(Int).SetString(test.in, 10)
819 expected, _ := new(Int).SetString(test.out, 10)
820 out := new(Int).Rsh(in, test.shift)
Adam Langley19418552009-11-11 13:21:37 -0800821
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700822 if !isNormalized(out) {
823 t.Errorf("#%d: %v is not normalized", i, *out)
824 }
Adam Langley19418552009-11-11 13:21:37 -0800825 if out.Cmp(expected) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700826 t.Errorf("#%d: got %s want %s", i, out, expected)
Adam Langley19418552009-11-11 13:21:37 -0800827 }
828 }
829}
Evan Shaw76cbbc82010-04-20 20:39:36 -0700830
Evan Shaw76cbbc82010-04-20 20:39:36 -0700831func TestRshSelf(t *testing.T) {
832 for i, test := range rshTests {
833 z, _ := new(Int).SetString(test.in, 10)
834 expected, _ := new(Int).SetString(test.out, 10)
835 z.Rsh(z, test.shift)
836
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700837 if !isNormalized(z) {
838 t.Errorf("#%d: %v is not normalized", i, *z)
839 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700840 if z.Cmp(expected) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700841 t.Errorf("#%d: got %s want %s", i, z, expected)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700842 }
843 }
844}
845
Evan Shaw76cbbc82010-04-20 20:39:36 -0700846var lshTests = []intShiftTest{
Robert Griesemer34788912010-10-22 10:06:33 -0700847 {"0", 0, "0"},
848 {"0", 1, "0"},
849 {"0", 2, "0"},
850 {"1", 0, "1"},
851 {"1", 1, "2"},
852 {"1", 2, "4"},
853 {"2", 0, "2"},
854 {"2", 1, "4"},
855 {"2", 2, "8"},
856 {"-87", 1, "-174"},
857 {"4294967296", 0, "4294967296"},
858 {"4294967296", 1, "8589934592"},
859 {"4294967296", 2, "17179869184"},
860 {"18446744073709551616", 0, "18446744073709551616"},
861 {"9223372036854775808", 1, "18446744073709551616"},
862 {"4611686018427387904", 2, "18446744073709551616"},
863 {"1", 64, "18446744073709551616"},
864 {"18446744073709551616", 64, "340282366920938463463374607431768211456"},
865 {"1", 128, "340282366920938463463374607431768211456"},
Evan Shaw76cbbc82010-04-20 20:39:36 -0700866}
867
Evan Shaw76cbbc82010-04-20 20:39:36 -0700868func TestLsh(t *testing.T) {
869 for i, test := range lshTests {
870 in, _ := new(Int).SetString(test.in, 10)
871 expected, _ := new(Int).SetString(test.out, 10)
872 out := new(Int).Lsh(in, test.shift)
873
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700874 if !isNormalized(out) {
875 t.Errorf("#%d: %v is not normalized", i, *out)
876 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700877 if out.Cmp(expected) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700878 t.Errorf("#%d: got %s want %s", i, out, expected)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700879 }
880 }
881}
882
Evan Shaw76cbbc82010-04-20 20:39:36 -0700883func TestLshSelf(t *testing.T) {
884 for i, test := range lshTests {
885 z, _ := new(Int).SetString(test.in, 10)
886 expected, _ := new(Int).SetString(test.out, 10)
887 z.Lsh(z, test.shift)
888
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700889 if !isNormalized(z) {
890 t.Errorf("#%d: %v is not normalized", i, *z)
891 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700892 if z.Cmp(expected) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700893 t.Errorf("#%d: got %s want %s", i, z, expected)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700894 }
895 }
896}
897
Evan Shaw76cbbc82010-04-20 20:39:36 -0700898func TestLshRsh(t *testing.T) {
899 for i, test := range rshTests {
900 in, _ := new(Int).SetString(test.in, 10)
901 out := new(Int).Lsh(in, test.shift)
902 out = out.Rsh(out, test.shift)
903
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700904 if !isNormalized(out) {
905 t.Errorf("#%d: %v is not normalized", i, *out)
906 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700907 if in.Cmp(out) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700908 t.Errorf("#%d: got %s want %s", i, out, in)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700909 }
910 }
911 for i, test := range lshTests {
912 in, _ := new(Int).SetString(test.in, 10)
913 out := new(Int).Lsh(in, test.shift)
914 out.Rsh(out, test.shift)
915
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700916 if !isNormalized(out) {
917 t.Errorf("#%d: %v is not normalized", i, *out)
918 }
Evan Shaw76cbbc82010-04-20 20:39:36 -0700919 if in.Cmp(out) != 0 {
Robert Griesemerb9caa4a2010-05-03 18:48:05 -0700920 t.Errorf("#%d: got %s want %s", i, out, in)
Evan Shaw76cbbc82010-04-20 20:39:36 -0700921 }
922 }
923}
924
Evan Shaw76cbbc82010-04-20 20:39:36 -0700925var int64Tests = []int64{
926 0,
927 1,
928 -1,
929 4294967295,
930 -4294967295,
931 4294967296,
932 -4294967296,
933 9223372036854775807,
934 -9223372036854775807,
935 -9223372036854775808,
936}
937
938func TestInt64(t *testing.T) {
939 for i, testVal := range int64Tests {
940 in := NewInt(testVal)
941 out := in.Int64()
942
943 if out != testVal {
944 t.Errorf("#%d got %d want %d", i, out, testVal)
945 }
946 }
947}
Evan Shaw4d1b1572010-05-03 11:20:52 -0700948
Luit van Drongelenf4fc1632012-12-11 12:19:10 -0500949var uint64Tests = []uint64{
950 0,
951 1,
952 4294967295,
953 4294967296,
954 8589934591,
955 8589934592,
956 9223372036854775807,
957 9223372036854775808,
958 18446744073709551615, // 1<<64 - 1
959}
960
961func TestUint64(t *testing.T) {
962 in := new(Int)
963 for i, testVal := range uint64Tests {
964 in.SetUint64(testVal)
965 out := in.Uint64()
966
967 if out != testVal {
968 t.Errorf("#%d got %d want %d", i, out, testVal)
969 }
970
971 str := fmt.Sprint(testVal)
972 strOut := in.String()
973 if strOut != str {
974 t.Errorf("#%d.String got %s want %s", i, strOut, str)
975 }
976 }
977}
978
Robert Griesemere3515332010-10-25 17:45:43 -0700979var bitwiseTests = []struct {
Evan Shaw4d1b1572010-05-03 11:20:52 -0700980 x, y string
981 and, or, xor, andNot string
Robert Griesemere3515332010-10-25 17:45:43 -0700982}{
Robert Griesemer34788912010-10-22 10:06:33 -0700983 {"0x00", "0x00", "0x00", "0x00", "0x00", "0x00"},
984 {"0x00", "0x01", "0x00", "0x01", "0x01", "0x00"},
985 {"0x01", "0x00", "0x00", "0x01", "0x01", "0x01"},
986 {"-0x01", "0x00", "0x00", "-0x01", "-0x01", "-0x01"},
987 {"-0xaf", "-0x50", "-0xf0", "-0x0f", "0xe1", "0x41"},
988 {"0x00", "-0x01", "0x00", "-0x01", "-0x01", "0x00"},
989 {"0x01", "0x01", "0x01", "0x01", "0x00", "0x00"},
990 {"-0x01", "-0x01", "-0x01", "-0x01", "0x00", "0x00"},
991 {"0x07", "0x08", "0x00", "0x0f", "0x0f", "0x07"},
992 {"0x05", "0x0f", "0x05", "0x0f", "0x0a", "0x00"},
Keith Randallc6ddca22015-01-15 20:45:07 -0800993 {"0xff", "-0x0a", "0xf6", "-0x01", "-0xf7", "0x09"},
Robert Griesemer34788912010-10-22 10:06:33 -0700994 {"0x013ff6", "0x9a4e", "0x1a46", "0x01bffe", "0x01a5b8", "0x0125b0"},
995 {"-0x013ff6", "0x9a4e", "0x800a", "-0x0125b2", "-0x01a5bc", "-0x01c000"},
996 {"-0x013ff6", "-0x9a4e", "-0x01bffe", "-0x1a46", "0x01a5b8", "0x8008"},
997 {
Evan Shaw4d1b1572010-05-03 11:20:52 -0700998 "0x1000009dc6e3d9822cba04129bcbe3401",
999 "0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
1000 "0x1000001186210100001000009048c2001",
1001 "0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
1002 "0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
1003 "0x8c40c2d8822caa04120b8321400",
1004 },
Robert Griesemer34788912010-10-22 10:06:33 -07001005 {
Evan Shaw4d1b1572010-05-03 11:20:52 -07001006 "0x1000009dc6e3d9822cba04129bcbe3401",
1007 "-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
1008 "0x8c40c2d8822caa04120b8321401",
1009 "-0xb9bd7d543685789d57ca918e82229142459020483cd2014001fd",
1010 "-0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fe",
1011 "0x1000001186210100001000009048c2000",
1012 },
Robert Griesemer34788912010-10-22 10:06:33 -07001013 {
Evan Shaw4d1b1572010-05-03 11:20:52 -07001014 "-0x1000009dc6e3d9822cba04129bcbe3401",
1015 "-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
1016 "-0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
1017 "-0x1000001186210100001000009048c2001",
1018 "0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
1019 "0xb9bd7d543685789d57ca918e82229142459020483cd2014001fc",
1020 },
1021}
1022
Evan Shaw4d1b1572010-05-03 11:20:52 -07001023type bitFun func(z, x, y *Int) *Int
1024
1025func testBitFun(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
1026 expected := new(Int)
Evan Shaw28a09712010-08-09 10:21:54 -07001027 expected.SetString(exp, 0)
Evan Shaw4d1b1572010-05-03 11:20:52 -07001028
1029 out := f(new(Int), x, y)
1030 if out.Cmp(expected) != 0 {
Evan Shaw4d1b1572010-05-03 11:20:52 -07001031 t.Errorf("%s: got %s want %s", msg, out, expected)
1032 }
1033}
1034
Evan Shaw4d1b1572010-05-03 11:20:52 -07001035func testBitFunSelf(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
Evan Shaw28a09712010-08-09 10:21:54 -07001036 self := new(Int)
1037 self.Set(x)
Evan Shaw4d1b1572010-05-03 11:20:52 -07001038 expected := new(Int)
Evan Shaw28a09712010-08-09 10:21:54 -07001039 expected.SetString(exp, 0)
Evan Shaw4d1b1572010-05-03 11:20:52 -07001040
Evan Shaw28a09712010-08-09 10:21:54 -07001041 self = f(self, self, y)
1042 if self.Cmp(expected) != 0 {
1043 t.Errorf("%s: got %s want %s", msg, self, expected)
Evan Shaw4d1b1572010-05-03 11:20:52 -07001044 }
1045}
1046
Roger Peppe83fd82b2011-05-17 13:38:21 -07001047func altBit(x *Int, i int) uint {
1048 z := new(Int).Rsh(x, uint(i))
1049 z = z.And(z, NewInt(1))
1050 if z.Cmp(new(Int)) != 0 {
1051 return 1
1052 }
1053 return 0
1054}
1055
1056func altSetBit(z *Int, x *Int, i int, b uint) *Int {
1057 one := NewInt(1)
1058 m := one.Lsh(one, uint(i))
1059 switch b {
1060 case 1:
1061 return z.Or(x, m)
1062 case 0:
1063 return z.AndNot(x, m)
1064 }
1065 panic("set bit is not 0 or 1")
1066}
1067
1068func testBitset(t *testing.T, x *Int) {
1069 n := x.BitLen()
1070 z := new(Int).Set(x)
1071 z1 := new(Int).Set(x)
1072 for i := 0; i < n+10; i++ {
1073 old := z.Bit(i)
1074 old1 := altBit(z1, i)
1075 if old != old1 {
1076 t.Errorf("bitset: inconsistent value for Bit(%s, %d), got %v want %v", z1, i, old, old1)
1077 }
1078 z := new(Int).SetBit(z, i, 1)
1079 z1 := altSetBit(new(Int), z1, i, 1)
1080 if z.Bit(i) == 0 {
1081 t.Errorf("bitset: bit %d of %s got 0 want 1", i, x)
1082 }
1083 if z.Cmp(z1) != 0 {
1084 t.Errorf("bitset: inconsistent value after SetBit 1, got %s want %s", z, z1)
1085 }
1086 z.SetBit(z, i, 0)
1087 altSetBit(z1, z1, i, 0)
1088 if z.Bit(i) != 0 {
1089 t.Errorf("bitset: bit %d of %s got 1 want 0", i, x)
1090 }
1091 if z.Cmp(z1) != 0 {
1092 t.Errorf("bitset: inconsistent value after SetBit 0, got %s want %s", z, z1)
1093 }
1094 altSetBit(z1, z1, i, old)
1095 z.SetBit(z, i, old)
1096 if z.Cmp(z1) != 0 {
1097 t.Errorf("bitset: inconsistent value after SetBit old, got %s want %s", z, z1)
1098 }
1099 }
1100 if z.Cmp(x) != 0 {
1101 t.Errorf("bitset: got %s want %s", z, x)
1102 }
1103}
1104
1105var bitsetTests = []struct {
1106 x string
1107 i int
1108 b uint
1109}{
1110 {"0", 0, 0},
1111 {"0", 200, 0},
1112 {"1", 0, 1},
1113 {"1", 1, 0},
1114 {"-1", 0, 1},
1115 {"-1", 200, 1},
1116 {"0x2000000000000000000000000000", 108, 0},
1117 {"0x2000000000000000000000000000", 109, 1},
1118 {"0x2000000000000000000000000000", 110, 0},
1119 {"-0x2000000000000000000000000001", 108, 1},
1120 {"-0x2000000000000000000000000001", 109, 0},
1121 {"-0x2000000000000000000000000001", 110, 1},
1122}
1123
1124func TestBitSet(t *testing.T) {
1125 for _, test := range bitwiseTests {
1126 x := new(Int)
1127 x.SetString(test.x, 0)
1128 testBitset(t, x)
1129 x = new(Int)
1130 x.SetString(test.y, 0)
1131 testBitset(t, x)
1132 }
1133 for i, test := range bitsetTests {
1134 x := new(Int)
1135 x.SetString(test.x, 0)
1136 b := x.Bit(test.i)
1137 if b != test.b {
Roger Peppeca6de002011-11-30 09:29:58 -08001138 t.Errorf("#%d got %v want %v", i, b, test.b)
Roger Peppe83fd82b2011-05-17 13:38:21 -07001139 }
1140 }
Roger Peppeca6de002011-11-30 09:29:58 -08001141 z := NewInt(1)
1142 z.SetBit(NewInt(0), 2, 1)
1143 if z.Cmp(NewInt(4)) != 0 {
1144 t.Errorf("destination leaked into result; got %s want 4", z)
1145 }
Roger Peppe83fd82b2011-05-17 13:38:21 -07001146}
1147
1148func BenchmarkBitset(b *testing.B) {
1149 z := new(Int)
1150 z.SetBit(z, 512, 1)
1151 b.ResetTimer()
1152 b.StartTimer()
1153 for i := b.N - 1; i >= 0; i-- {
1154 z.SetBit(z, i&512, 1)
1155 }
1156}
1157
1158func BenchmarkBitsetNeg(b *testing.B) {
1159 z := NewInt(-1)
1160 z.SetBit(z, 512, 0)
1161 b.ResetTimer()
1162 b.StartTimer()
1163 for i := b.N - 1; i >= 0; i-- {
1164 z.SetBit(z, i&512, 0)
1165 }
1166}
1167
1168func BenchmarkBitsetOrig(b *testing.B) {
1169 z := new(Int)
1170 altSetBit(z, z, 512, 1)
1171 b.ResetTimer()
1172 b.StartTimer()
1173 for i := b.N - 1; i >= 0; i-- {
1174 altSetBit(z, z, i&512, 1)
1175 }
1176}
1177
1178func BenchmarkBitsetNegOrig(b *testing.B) {
1179 z := NewInt(-1)
1180 altSetBit(z, z, 512, 0)
1181 b.ResetTimer()
1182 b.StartTimer()
1183 for i := b.N - 1; i >= 0; i-- {
1184 altSetBit(z, z, i&512, 0)
1185 }
1186}
Evan Shaw4d1b1572010-05-03 11:20:52 -07001187
David Leon Gilea0491b2015-06-26 10:29:45 -07001188// tri generates the trinomial 2**(n*2) - 2**n - 1, which is always 3 mod 4 and
1189// 7 mod 8, so that 2 is always a quadratic residue.
1190func tri(n uint) *Int {
1191 x := NewInt(1)
1192 x.Lsh(x, n)
1193 x2 := new(Int).Lsh(x, n)
1194 x2.Sub(x2, x)
1195 x2.Sub(x2, intOne)
1196 return x2
1197}
1198
1199func BenchmarkModSqrt225_Tonelli(b *testing.B) {
1200 p := tri(225)
1201 x := NewInt(2)
1202 for i := 0; i < b.N; i++ {
1203 x.SetUint64(2)
1204 x.modSqrtTonelliShanks(x, p)
1205 }
1206}
1207
1208func BenchmarkModSqrt224_3Mod4(b *testing.B) {
1209 p := tri(225)
1210 x := new(Int).SetUint64(2)
1211 for i := 0; i < b.N; i++ {
1212 x.SetUint64(2)
1213 x.modSqrt3Mod4Prime(x, p)
1214 }
1215}
1216
1217func BenchmarkModSqrt5430_Tonelli(b *testing.B) {
1218 p := tri(5430)
1219 x := new(Int).SetUint64(2)
1220 for i := 0; i < b.N; i++ {
1221 x.SetUint64(2)
1222 x.modSqrtTonelliShanks(x, p)
1223 }
1224}
1225
1226func BenchmarkModSqrt5430_3Mod4(b *testing.B) {
1227 p := tri(5430)
1228 x := new(Int).SetUint64(2)
1229 for i := 0; i < b.N; i++ {
1230 x.SetUint64(2)
1231 x.modSqrt3Mod4Prime(x, p)
1232 }
1233}
1234
Evan Shaw4d1b1572010-05-03 11:20:52 -07001235func TestBitwise(t *testing.T) {
1236 x := new(Int)
1237 y := new(Int)
1238 for _, test := range bitwiseTests {
Evan Shaw28a09712010-08-09 10:21:54 -07001239 x.SetString(test.x, 0)
1240 y.SetString(test.y, 0)
Evan Shaw4d1b1572010-05-03 11:20:52 -07001241
1242 testBitFun(t, "and", (*Int).And, x, y, test.and)
1243 testBitFunSelf(t, "and", (*Int).And, x, y, test.and)
1244 testBitFun(t, "andNot", (*Int).AndNot, x, y, test.andNot)
1245 testBitFunSelf(t, "andNot", (*Int).AndNot, x, y, test.andNot)
1246 testBitFun(t, "or", (*Int).Or, x, y, test.or)
1247 testBitFunSelf(t, "or", (*Int).Or, x, y, test.or)
1248 testBitFun(t, "xor", (*Int).Xor, x, y, test.xor)
1249 testBitFunSelf(t, "xor", (*Int).Xor, x, y, test.xor)
1250 }
1251}
1252
Robert Griesemere3515332010-10-25 17:45:43 -07001253var notTests = []struct {
Evan Shaw4d1b1572010-05-03 11:20:52 -07001254 in string
1255 out string
Robert Griesemere3515332010-10-25 17:45:43 -07001256}{
Robert Griesemer34788912010-10-22 10:06:33 -07001257 {"0", "-1"},
1258 {"1", "-2"},
1259 {"7", "-8"},
1260 {"0", "-1"},
1261 {"-81910", "81909"},
1262 {
Evan Shaw4d1b1572010-05-03 11:20:52 -07001263 "298472983472983471903246121093472394872319615612417471234712061",
1264 "-298472983472983471903246121093472394872319615612417471234712062",
1265 },
1266}
1267
1268func TestNot(t *testing.T) {
1269 in := new(Int)
1270 out := new(Int)
1271 expected := new(Int)
1272 for i, test := range notTests {
1273 in.SetString(test.in, 10)
1274 expected.SetString(test.out, 10)
1275 out = out.Not(in)
1276 if out.Cmp(expected) != 0 {
1277 t.Errorf("#%d: got %s want %s", i, out, expected)
1278 }
1279 out = out.Not(out)
1280 if out.Cmp(in) != 0 {
1281 t.Errorf("#%d: got %s want %s", i, out, in)
1282 }
1283 }
1284}
Adam Langleyf199f292010-05-26 15:58:58 -04001285
Robert Griesemere3515332010-10-25 17:45:43 -07001286var modInverseTests = []struct {
Adam Langleyf199f292010-05-26 15:58:58 -04001287 element string
Keith Randall96d1e4a2014-10-14 14:09:56 -07001288 modulus string
Robert Griesemere3515332010-10-25 17:45:43 -07001289}{
Keith Randall96d1e4a2014-10-14 14:09:56 -07001290 {"1234567", "458948883992"},
Robert Griesemer34788912010-10-22 10:06:33 -07001291 {"239487239847", "2410312426921032588552076022197566074856950548502459942654116941958108831682612228890093858261341614673227141477904012196503648957050582631942730706805009223062734745341073406696246014589361659774041027169249453200378729434170325843778659198143763193776859869524088940195577346119843545301547043747207749969763750084308926339295559968882457872412993810129130294592999947926365264059284647209730384947211681434464714438488520940127459844288859336526896320919633919"},
Adam Langleyf199f292010-05-26 15:58:58 -04001292}
1293
1294func TestModInverse(t *testing.T) {
Keith Randall96d1e4a2014-10-14 14:09:56 -07001295 var element, modulus, gcd, inverse Int
Adam Langleyf199f292010-05-26 15:58:58 -04001296 one := NewInt(1)
1297 for i, test := range modInverseTests {
1298 (&element).SetString(test.element, 10)
Keith Randall96d1e4a2014-10-14 14:09:56 -07001299 (&modulus).SetString(test.modulus, 10)
1300 (&inverse).ModInverse(&element, &modulus)
1301 (&inverse).Mul(&inverse, &element)
1302 (&inverse).Mod(&inverse, &modulus)
1303 if (&inverse).Cmp(one) != 0 {
1304 t.Errorf("#%d: failed (e·e^(-1)=%s)", i, &inverse)
1305 }
1306 }
1307 // exhaustive test for small values
1308 for n := 2; n < 100; n++ {
1309 (&modulus).SetInt64(int64(n))
1310 for x := 1; x < n; x++ {
1311 (&element).SetInt64(int64(x))
1312 (&gcd).GCD(nil, nil, &element, &modulus)
1313 if (&gcd).Cmp(one) != 0 {
1314 continue
1315 }
1316 (&inverse).ModInverse(&element, &modulus)
1317 (&inverse).Mul(&inverse, &element)
1318 (&inverse).Mod(&inverse, &modulus)
1319 if (&inverse).Cmp(one) != 0 {
1320 t.Errorf("ModInverse(%d,%d)*%d%%%d=%d, not 1", &element, &modulus, &element, &modulus, &inverse)
1321 }
Adam Langleyf199f292010-05-26 15:58:58 -04001322 }
1323 }
1324}
Robert Griesemer758d0552011-03-08 17:27:44 -08001325
Bryan Fordac615882014-12-19 14:28:44 -05001326// testModSqrt is a helper for TestModSqrt,
1327// which checks that ModSqrt can compute a square-root of elt^2.
1328func testModSqrt(t *testing.T, elt, mod, sq, sqrt *Int) bool {
1329 var sqChk, sqrtChk, sqrtsq Int
1330 sq.Mul(elt, elt)
1331 sq.Mod(sq, mod)
1332 z := sqrt.ModSqrt(sq, mod)
1333 if z != sqrt {
1334 t.Errorf("ModSqrt returned wrong value %s", z)
1335 }
1336
1337 // test ModSqrt arguments outside the range [0,mod)
1338 sqChk.Add(sq, mod)
1339 z = sqrtChk.ModSqrt(&sqChk, mod)
1340 if z != &sqrtChk || z.Cmp(sqrt) != 0 {
1341 t.Errorf("ModSqrt returned inconsistent value %s", z)
1342 }
1343 sqChk.Sub(sq, mod)
1344 z = sqrtChk.ModSqrt(&sqChk, mod)
1345 if z != &sqrtChk || z.Cmp(sqrt) != 0 {
1346 t.Errorf("ModSqrt returned inconsistent value %s", z)
1347 }
1348
1349 // make sure we actually got a square root
1350 if sqrt.Cmp(elt) == 0 {
1351 return true // we found the "desired" square root
1352 }
1353 sqrtsq.Mul(sqrt, sqrt) // make sure we found the "other" one
1354 sqrtsq.Mod(&sqrtsq, mod)
1355 return sq.Cmp(&sqrtsq) == 0
1356}
1357
1358func TestModSqrt(t *testing.T) {
1359 var elt, mod, modx4, sq, sqrt Int
1360 r := rand.New(rand.NewSource(9))
1361 for i, s := range primes[1:] { // skip 2, use only odd primes
1362 mod.SetString(s, 10)
1363 modx4.Lsh(&mod, 2)
1364
1365 // test a few random elements per prime
1366 for x := 1; x < 5; x++ {
1367 elt.Rand(r, &modx4)
1368 elt.Sub(&elt, &mod) // test range [-mod, 3*mod)
1369 if !testModSqrt(t, &elt, &mod, &sq, &sqrt) {
1370 t.Errorf("#%d: failed (sqrt(e) = %s)", i, &sqrt)
1371 }
1372 }
1373 }
1374
1375 // exhaustive test for small values
1376 for n := 3; n < 100; n++ {
1377 mod.SetInt64(int64(n))
1378 if !mod.ProbablyPrime(10) {
1379 continue
1380 }
1381 isSquare := make([]bool, n)
1382
1383 // test all the squares
1384 for x := 1; x < n; x++ {
1385 elt.SetInt64(int64(x))
1386 if !testModSqrt(t, &elt, &mod, &sq, &sqrt) {
1387 t.Errorf("#%d: failed (sqrt(%d,%d) = %s)", x, &elt, &mod, &sqrt)
1388 }
1389 isSquare[sq.Uint64()] = true
1390 }
1391
1392 // test all non-squares
1393 for x := 1; x < n; x++ {
1394 sq.SetInt64(int64(x))
1395 z := sqrt.ModSqrt(&sq, &mod)
1396 if !isSquare[x] && z != nil {
1397 t.Errorf("#%d: failed (sqrt(%d,%d) = nil)", x, &sqrt, &mod)
1398 }
1399 }
1400 }
1401}
1402
1403func TestJacobi(t *testing.T) {
1404 testCases := []struct {
1405 x, y int64
1406 result int
1407 }{
1408 {0, 1, 1},
1409 {0, -1, 1},
1410 {1, 1, 1},
1411 {1, -1, 1},
1412 {0, 5, 0},
1413 {1, 5, 1},
1414 {2, 5, -1},
1415 {-2, 5, -1},
1416 {2, -5, -1},
1417 {-2, -5, 1},
1418 {3, 5, -1},
1419 {5, 5, 0},
1420 {-5, 5, 0},
1421 {6, 5, 1},
1422 {6, -5, 1},
1423 {-6, 5, 1},
1424 {-6, -5, -1},
1425 }
1426
1427 var x, y Int
1428
1429 for i, test := range testCases {
1430 x.SetInt64(test.x)
1431 y.SetInt64(test.y)
1432 expected := test.result
1433 actual := Jacobi(&x, &y)
1434 if actual != expected {
1435 t.Errorf("#%d: Jacobi(%d, %d) = %d, but expected %d", i, test.x, test.y, actual, expected)
1436 }
1437 }
1438}
1439
1440func TestJacobiPanic(t *testing.T) {
1441 const failureMsg = "test failure"
1442 defer func() {
1443 msg := recover()
1444 if msg == nil || msg == failureMsg {
1445 panic(msg)
1446 }
1447 t.Log(msg)
1448 }()
1449 x := NewInt(1)
1450 y := NewInt(2)
1451 // Jacobi should panic when the second argument is even.
1452 Jacobi(x, y)
1453 panic(failureMsg)
1454}
1455
Robert Griesemer13a59b82012-05-22 17:20:37 -07001456var encodingTests = []string{
1457 "-539345864568634858364538753846587364875430589374589",
1458 "-678645873",
1459 "-100",
1460 "-2",
1461 "-1",
Robert Griesemer758d0552011-03-08 17:27:44 -08001462 "0",
1463 "1",
1464 "2",
1465 "10",
1466 "42",
1467 "1234567890",
1468 "298472983472983471903246121093472394872319615612417471234712061",
1469}
1470
Robert Griesemer21032eb2011-06-08 09:10:01 -07001471func TestIntGobEncoding(t *testing.T) {
Robert Griesemer758d0552011-03-08 17:27:44 -08001472 var medium bytes.Buffer
1473 enc := gob.NewEncoder(&medium)
1474 dec := gob.NewDecoder(&medium)
Robert Griesemer13a59b82012-05-22 17:20:37 -07001475 for _, test := range encodingTests {
1476 medium.Reset() // empty buffer for each test case (in case of failures)
1477 var tx Int
1478 tx.SetString(test, 10)
1479 if err := enc.Encode(&tx); err != nil {
1480 t.Errorf("encoding of %s failed: %s", &tx, err)
1481 }
1482 var rx Int
1483 if err := dec.Decode(&rx); err != nil {
1484 t.Errorf("decoding of %s failed: %s", &tx, err)
1485 }
1486 if rx.Cmp(&tx) != 0 {
1487 t.Errorf("transmission of %s failed: got %s want %s", &tx, &rx, &tx)
1488 }
1489 }
1490}
1491
Rob Pikebc6bb3e2013-08-19 11:22:09 +10001492// Sending a nil Int pointer (inside a slice) on a round trip through gob should yield a zero.
1493// TODO: top-level nils.
1494func TestGobEncodingNilIntInSlice(t *testing.T) {
1495 buf := new(bytes.Buffer)
1496 enc := gob.NewEncoder(buf)
1497 dec := gob.NewDecoder(buf)
1498
1499 var in = make([]*Int, 1)
1500 err := enc.Encode(&in)
1501 if err != nil {
1502 t.Errorf("gob encode failed: %q", err)
1503 }
1504 var out []*Int
1505 err = dec.Decode(&out)
1506 if err != nil {
1507 t.Fatalf("gob decode failed: %q", err)
1508 }
1509 if len(out) != 1 {
1510 t.Fatalf("wrong len; want 1 got %d", len(out))
1511 }
1512 var zero Int
1513 if out[0].Cmp(&zero) != 0 {
1514 t.Errorf("transmission of (*Int)(nill) failed: got %s want 0", out)
1515 }
1516}
1517
Robert Griesemer13a59b82012-05-22 17:20:37 -07001518func TestIntJSONEncoding(t *testing.T) {
1519 for _, test := range encodingTests {
1520 var tx Int
1521 tx.SetString(test, 10)
1522 b, err := json.Marshal(&tx)
1523 if err != nil {
1524 t.Errorf("marshaling of %s failed: %s", &tx, err)
1525 }
1526 var rx Int
1527 if err := json.Unmarshal(b, &rx); err != nil {
1528 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
1529 }
1530 if rx.Cmp(&tx) != 0 {
1531 t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
Robert Griesemer758d0552011-03-08 17:27:44 -08001532 }
1533 }
1534}
Robert Griesemerfc78c5a2011-12-22 14:15:41 -08001535
Michael T. Jones1dc82d22014-02-14 12:57:03 -08001536var intVals = []string{
1537 "-141592653589793238462643383279502884197169399375105820974944592307816406286",
1538 "-1415926535897932384626433832795028841971",
1539 "-141592653589793",
1540 "-1",
1541 "0",
1542 "1",
1543 "141592653589793",
1544 "1415926535897932384626433832795028841971",
1545 "141592653589793238462643383279502884197169399375105820974944592307816406286",
1546}
1547
1548func TestIntJSONEncodingTextMarshaller(t *testing.T) {
1549 for _, num := range intVals {
1550 var tx Int
1551 tx.SetString(num, 0)
1552 b, err := json.Marshal(&tx)
1553 if err != nil {
1554 t.Errorf("marshaling of %s failed: %s", &tx, err)
1555 continue
1556 }
1557 var rx Int
1558 if err := json.Unmarshal(b, &rx); err != nil {
1559 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
1560 continue
1561 }
1562 if rx.Cmp(&tx) != 0 {
1563 t.Errorf("JSON encoding of %s failed: got %s want %s", &tx, &rx, &tx)
1564 }
1565 }
1566}
1567
1568func TestIntXMLEncodingTextMarshaller(t *testing.T) {
1569 for _, num := range intVals {
1570 var tx Int
1571 tx.SetString(num, 0)
1572 b, err := xml.Marshal(&tx)
1573 if err != nil {
1574 t.Errorf("marshaling of %s failed: %s", &tx, err)
1575 continue
1576 }
1577 var rx Int
1578 if err := xml.Unmarshal(b, &rx); err != nil {
1579 t.Errorf("unmarshaling of %s failed: %s", &tx, err)
1580 continue
1581 }
1582 if rx.Cmp(&tx) != 0 {
1583 t.Errorf("XML encoding of %s failed: got %s want %s", &tx, &rx, &tx)
1584 }
1585 }
1586}
1587
Robert Griesemerfc78c5a2011-12-22 14:15:41 -08001588func TestIssue2607(t *testing.T) {
1589 // This code sequence used to hang.
1590 n := NewInt(10)
1591 n.Rand(rand.New(rand.NewSource(9)), n)
1592}