int_test.go 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. // 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. package big
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "fmt"
  9. "math/rand"
  10. "strconv"
  11. "strings"
  12. "testing"
  13. "testing/quick"
  14. )
  15. func isNormalized(x *Int) bool {
  16. if len(x.abs) == 0 {
  17. return !x.neg
  18. }
  19. // len(x.abs) > 0
  20. return x.abs[len(x.abs)-1] != 0
  21. }
  22. type funZZ func(z, x, y *Int) *Int
  23. type argZZ struct {
  24. z, x, y *Int
  25. }
  26. var sumZZ = []argZZ{
  27. {NewInt(0), NewInt(0), NewInt(0)},
  28. {NewInt(1), NewInt(1), NewInt(0)},
  29. {NewInt(1111111110), NewInt(123456789), NewInt(987654321)},
  30. {NewInt(-1), NewInt(-1), NewInt(0)},
  31. {NewInt(864197532), NewInt(-123456789), NewInt(987654321)},
  32. {NewInt(-1111111110), NewInt(-123456789), NewInt(-987654321)},
  33. }
  34. var prodZZ = []argZZ{
  35. {NewInt(0), NewInt(0), NewInt(0)},
  36. {NewInt(0), NewInt(1), NewInt(0)},
  37. {NewInt(1), NewInt(1), NewInt(1)},
  38. {NewInt(-991 * 991), NewInt(991), NewInt(-991)},
  39. // TODO(gri) add larger products
  40. }
  41. func TestSignZ(t *testing.T) {
  42. var zero Int
  43. for _, a := range sumZZ {
  44. s := a.z.Sign()
  45. e := a.z.Cmp(&zero)
  46. if s != e {
  47. t.Errorf("got %d; want %d for z = %v", s, e, a.z)
  48. }
  49. }
  50. }
  51. func TestSetZ(t *testing.T) {
  52. for _, a := range sumZZ {
  53. var z Int
  54. z.Set(a.z)
  55. if !isNormalized(&z) {
  56. t.Errorf("%v is not normalized", z)
  57. }
  58. if (&z).Cmp(a.z) != 0 {
  59. t.Errorf("got z = %v; want %v", z, a.z)
  60. }
  61. }
  62. }
  63. func TestAbsZ(t *testing.T) {
  64. var zero Int
  65. for _, a := range sumZZ {
  66. var z Int
  67. z.Abs(a.z)
  68. var e Int
  69. e.Set(a.z)
  70. if e.Cmp(&zero) < 0 {
  71. e.Sub(&zero, &e)
  72. }
  73. if z.Cmp(&e) != 0 {
  74. t.Errorf("got z = %v; want %v", z, e)
  75. }
  76. }
  77. }
  78. func testFunZZ(t *testing.T, msg string, f funZZ, a argZZ) {
  79. var z Int
  80. f(&z, a.x, a.y)
  81. if !isNormalized(&z) {
  82. t.Errorf("%s%v is not normalized", msg, z)
  83. }
  84. if (&z).Cmp(a.z) != 0 {
  85. t.Errorf("%s%+v\n\tgot z = %v; want %v", msg, a, &z, a.z)
  86. }
  87. }
  88. func TestSumZZ(t *testing.T) {
  89. AddZZ := func(z, x, y *Int) *Int { return z.Add(x, y) }
  90. SubZZ := func(z, x, y *Int) *Int { return z.Sub(x, y) }
  91. for _, a := range sumZZ {
  92. arg := a
  93. testFunZZ(t, "AddZZ", AddZZ, arg)
  94. arg = argZZ{a.z, a.y, a.x}
  95. testFunZZ(t, "AddZZ symmetric", AddZZ, arg)
  96. arg = argZZ{a.x, a.z, a.y}
  97. testFunZZ(t, "SubZZ", SubZZ, arg)
  98. arg = argZZ{a.y, a.z, a.x}
  99. testFunZZ(t, "SubZZ symmetric", SubZZ, arg)
  100. }
  101. }
  102. func TestProdZZ(t *testing.T) {
  103. MulZZ := func(z, x, y *Int) *Int { return z.Mul(x, y) }
  104. for _, a := range prodZZ {
  105. arg := a
  106. testFunZZ(t, "MulZZ", MulZZ, arg)
  107. arg = argZZ{a.z, a.y, a.x}
  108. testFunZZ(t, "MulZZ symmetric", MulZZ, arg)
  109. }
  110. }
  111. // mulBytes returns x*y via grade school multiplication. Both inputs
  112. // and the result are assumed to be in big-endian representation (to
  113. // match the semantics of Int.Bytes and Int.SetBytes).
  114. func mulBytes(x, y []byte) []byte {
  115. z := make([]byte, len(x)+len(y))
  116. // multiply
  117. k0 := len(z) - 1
  118. for j := len(y) - 1; j >= 0; j-- {
  119. d := int(y[j])
  120. if d != 0 {
  121. k := k0
  122. carry := 0
  123. for i := len(x) - 1; i >= 0; i-- {
  124. t := int(z[k]) + int(x[i])*d + carry
  125. z[k], carry = byte(t), t>>8
  126. k--
  127. }
  128. z[k] = byte(carry)
  129. }
  130. k0--
  131. }
  132. // normalize (remove leading 0's)
  133. i := 0
  134. for i < len(z) && z[i] == 0 {
  135. i++
  136. }
  137. return z[i:]
  138. }
  139. func checkMul(a, b []byte) bool {
  140. var x, y, z1 Int
  141. x.SetBytes(a)
  142. y.SetBytes(b)
  143. z1.Mul(&x, &y)
  144. var z2 Int
  145. z2.SetBytes(mulBytes(a, b))
  146. return z1.Cmp(&z2) == 0
  147. }
  148. func TestMul(t *testing.T) {
  149. if err := quick.Check(checkMul, nil); err != nil {
  150. t.Error(err)
  151. }
  152. }
  153. var mulRangesZ = []struct {
  154. a, b int64
  155. prod string
  156. }{
  157. // entirely positive ranges are covered by mulRangesN
  158. {-1, 1, "0"},
  159. {-2, -1, "2"},
  160. {-3, -2, "6"},
  161. {-3, -1, "-6"},
  162. {1, 3, "6"},
  163. {-10, -10, "-10"},
  164. {0, -1, "1"}, // empty range
  165. {-1, -100, "1"}, // empty range
  166. {-1, 1, "0"}, // range includes 0
  167. {-1e9, 0, "0"}, // range includes 0
  168. {-1e9, 1e9, "0"}, // range includes 0
  169. {-10, -1, "3628800"}, // 10!
  170. {-20, -2, "-2432902008176640000"}, // -20!
  171. {-99, -1,
  172. "-933262154439441526816992388562667004907159682643816214685929" +
  173. "638952175999932299156089414639761565182862536979208272237582" +
  174. "511852109168640000000000000000000000", // -99!
  175. },
  176. }
  177. func TestMulRangeZ(t *testing.T) {
  178. var tmp Int
  179. // test entirely positive ranges
  180. for i, r := range mulRangesN {
  181. prod := tmp.MulRange(int64(r.a), int64(r.b)).String()
  182. if prod != r.prod {
  183. t.Errorf("#%da: got %s; want %s", i, prod, r.prod)
  184. }
  185. }
  186. // test other ranges
  187. for i, r := range mulRangesZ {
  188. prod := tmp.MulRange(r.a, r.b).String()
  189. if prod != r.prod {
  190. t.Errorf("#%db: got %s; want %s", i, prod, r.prod)
  191. }
  192. }
  193. }
  194. func TestBinomial(t *testing.T) {
  195. var z Int
  196. for _, test := range []struct {
  197. n, k int64
  198. want string
  199. }{
  200. {0, 0, "1"},
  201. {0, 1, "0"},
  202. {1, 0, "1"},
  203. {1, 1, "1"},
  204. {1, 10, "0"},
  205. {4, 0, "1"},
  206. {4, 1, "4"},
  207. {4, 2, "6"},
  208. {4, 3, "4"},
  209. {4, 4, "1"},
  210. {10, 1, "10"},
  211. {10, 9, "10"},
  212. {10, 5, "252"},
  213. {11, 5, "462"},
  214. {11, 6, "462"},
  215. {100, 10, "17310309456440"},
  216. {100, 90, "17310309456440"},
  217. {1000, 10, "263409560461970212832400"},
  218. {1000, 990, "263409560461970212832400"},
  219. } {
  220. if got := z.Binomial(test.n, test.k).String(); got != test.want {
  221. t.Errorf("Binomial(%d, %d) = %s; want %s", test.n, test.k, got, test.want)
  222. }
  223. }
  224. }
  225. func BenchmarkBinomial(b *testing.B) {
  226. var z Int
  227. for i := b.N - 1; i >= 0; i-- {
  228. z.Binomial(1000, 990)
  229. }
  230. }
  231. // Examples from the Go Language Spec, section "Arithmetic operators"
  232. var divisionSignsTests = []struct {
  233. x, y int64
  234. q, r int64 // T-division
  235. d, m int64 // Euclidean division
  236. }{
  237. {5, 3, 1, 2, 1, 2},
  238. {-5, 3, -1, -2, -2, 1},
  239. {5, -3, -1, 2, -1, 2},
  240. {-5, -3, 1, -2, 2, 1},
  241. {1, 2, 0, 1, 0, 1},
  242. {8, 4, 2, 0, 2, 0},
  243. }
  244. func TestDivisionSigns(t *testing.T) {
  245. for i, test := range divisionSignsTests {
  246. x := NewInt(test.x)
  247. y := NewInt(test.y)
  248. q := NewInt(test.q)
  249. r := NewInt(test.r)
  250. d := NewInt(test.d)
  251. m := NewInt(test.m)
  252. q1 := new(Int).Quo(x, y)
  253. r1 := new(Int).Rem(x, y)
  254. if !isNormalized(q1) {
  255. t.Errorf("#%d Quo: %v is not normalized", i, *q1)
  256. }
  257. if !isNormalized(r1) {
  258. t.Errorf("#%d Rem: %v is not normalized", i, *r1)
  259. }
  260. if q1.Cmp(q) != 0 || r1.Cmp(r) != 0 {
  261. t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q1, r1, q, r)
  262. }
  263. q2, r2 := new(Int).QuoRem(x, y, new(Int))
  264. if !isNormalized(q2) {
  265. t.Errorf("#%d Quo: %v is not normalized", i, *q2)
  266. }
  267. if !isNormalized(r2) {
  268. t.Errorf("#%d Rem: %v is not normalized", i, *r2)
  269. }
  270. if q2.Cmp(q) != 0 || r2.Cmp(r) != 0 {
  271. t.Errorf("#%d QuoRem: got (%s, %s), want (%s, %s)", i, q2, r2, q, r)
  272. }
  273. d1 := new(Int).Div(x, y)
  274. m1 := new(Int).Mod(x, y)
  275. if !isNormalized(d1) {
  276. t.Errorf("#%d Div: %v is not normalized", i, *d1)
  277. }
  278. if !isNormalized(m1) {
  279. t.Errorf("#%d Mod: %v is not normalized", i, *m1)
  280. }
  281. if d1.Cmp(d) != 0 || m1.Cmp(m) != 0 {
  282. t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d1, m1, d, m)
  283. }
  284. d2, m2 := new(Int).DivMod(x, y, new(Int))
  285. if !isNormalized(d2) {
  286. t.Errorf("#%d Div: %v is not normalized", i, *d2)
  287. }
  288. if !isNormalized(m2) {
  289. t.Errorf("#%d Mod: %v is not normalized", i, *m2)
  290. }
  291. if d2.Cmp(d) != 0 || m2.Cmp(m) != 0 {
  292. t.Errorf("#%d DivMod: got (%s, %s), want (%s, %s)", i, d2, m2, d, m)
  293. }
  294. }
  295. }
  296. func norm(x nat) nat {
  297. i := len(x)
  298. for i > 0 && x[i-1] == 0 {
  299. i--
  300. }
  301. return x[:i]
  302. }
  303. func TestBits(t *testing.T) {
  304. for _, test := range []nat{
  305. nil,
  306. {0},
  307. {1},
  308. {0, 1, 2, 3, 4},
  309. {4, 3, 2, 1, 0},
  310. {4, 3, 2, 1, 0, 0, 0, 0},
  311. } {
  312. var z Int
  313. z.neg = true
  314. got := z.SetBits(test)
  315. want := norm(test)
  316. if got.abs.cmp(want) != 0 {
  317. t.Errorf("SetBits(%v) = %v; want %v", test, got.abs, want)
  318. }
  319. if got.neg {
  320. t.Errorf("SetBits(%v): got negative result", test)
  321. }
  322. bits := nat(z.Bits())
  323. if bits.cmp(want) != 0 {
  324. t.Errorf("%v.Bits() = %v; want %v", z.abs, bits, want)
  325. }
  326. }
  327. }
  328. func checkSetBytes(b []byte) bool {
  329. hex1 := hex.EncodeToString(new(Int).SetBytes(b).Bytes())
  330. hex2 := hex.EncodeToString(b)
  331. for len(hex1) < len(hex2) {
  332. hex1 = "0" + hex1
  333. }
  334. for len(hex1) > len(hex2) {
  335. hex2 = "0" + hex2
  336. }
  337. return hex1 == hex2
  338. }
  339. func TestSetBytes(t *testing.T) {
  340. if err := quick.Check(checkSetBytes, nil); err != nil {
  341. t.Error(err)
  342. }
  343. }
  344. func checkBytes(b []byte) bool {
  345. // trim leading zero bytes since Bytes() won't return them
  346. // (was issue 12231)
  347. for len(b) > 0 && b[0] == 0 {
  348. b = b[1:]
  349. }
  350. b2 := new(Int).SetBytes(b).Bytes()
  351. return bytes.Equal(b, b2)
  352. }
  353. func TestBytes(t *testing.T) {
  354. if err := quick.Check(checkBytes, nil); err != nil {
  355. t.Error(err)
  356. }
  357. }
  358. func checkQuo(x, y []byte) bool {
  359. u := new(Int).SetBytes(x)
  360. v := new(Int).SetBytes(y)
  361. if len(v.abs) == 0 {
  362. return true
  363. }
  364. r := new(Int)
  365. q, r := new(Int).QuoRem(u, v, r)
  366. if r.Cmp(v) >= 0 {
  367. return false
  368. }
  369. uprime := new(Int).Set(q)
  370. uprime.Mul(uprime, v)
  371. uprime.Add(uprime, r)
  372. return uprime.Cmp(u) == 0
  373. }
  374. var quoTests = []struct {
  375. x, y string
  376. q, r string
  377. }{
  378. {
  379. "476217953993950760840509444250624797097991362735329973741718102894495832294430498335824897858659711275234906400899559094370964723884706254265559534144986498357",
  380. "9353930466774385905609975137998169297361893554149986716853295022578535724979483772383667534691121982974895531435241089241440253066816724367338287092081996",
  381. "50911",
  382. "1",
  383. },
  384. {
  385. "11510768301994997771168",
  386. "1328165573307167369775",
  387. "8",
  388. "885443715537658812968",
  389. },
  390. }
  391. func TestQuo(t *testing.T) {
  392. if err := quick.Check(checkQuo, nil); err != nil {
  393. t.Error(err)
  394. }
  395. for i, test := range quoTests {
  396. x, _ := new(Int).SetString(test.x, 10)
  397. y, _ := new(Int).SetString(test.y, 10)
  398. expectedQ, _ := new(Int).SetString(test.q, 10)
  399. expectedR, _ := new(Int).SetString(test.r, 10)
  400. r := new(Int)
  401. q, r := new(Int).QuoRem(x, y, r)
  402. if q.Cmp(expectedQ) != 0 || r.Cmp(expectedR) != 0 {
  403. t.Errorf("#%d got (%s, %s) want (%s, %s)", i, q, r, expectedQ, expectedR)
  404. }
  405. }
  406. }
  407. func TestQuoStepD6(t *testing.T) {
  408. // See Knuth, Volume 2, section 4.3.1, exercise 21. This code exercises
  409. // a code path which only triggers 1 in 10^{-19} cases.
  410. u := &Int{false, nat{0, 0, 1 + 1<<(_W-1), _M ^ (1 << (_W - 1))}}
  411. v := &Int{false, nat{5, 2 + 1<<(_W-1), 1 << (_W - 1)}}
  412. r := new(Int)
  413. q, r := new(Int).QuoRem(u, v, r)
  414. const expectedQ64 = "18446744073709551613"
  415. const expectedR64 = "3138550867693340382088035895064302439801311770021610913807"
  416. const expectedQ32 = "4294967293"
  417. const expectedR32 = "39614081266355540837921718287"
  418. if q.String() != expectedQ64 && q.String() != expectedQ32 ||
  419. r.String() != expectedR64 && r.String() != expectedR32 {
  420. t.Errorf("got (%s, %s) want (%s, %s) or (%s, %s)", q, r, expectedQ64, expectedR64, expectedQ32, expectedR32)
  421. }
  422. }
  423. func BenchmarkQuoRem(b *testing.B) {
  424. x, _ := new(Int).SetString("153980389784927331788354528594524332344709972855165340650588877572729725338415474372475094155672066328274535240275856844648695200875763869073572078279316458648124537905600131008790701752441155668003033945258023841165089852359980273279085783159654751552359397986180318708491098942831252291841441726305535546071", 0)
  425. y, _ := new(Int).SetString("7746362281539803897849273317883545285945243323447099728551653406505888775727297253384154743724750941556720663282745352402758568446486952008757638690735720782793164586481245379056001310087907017524411556680030339452580238411650898523599802732790857831596547515523593979861803187084910989428312522918414417263055355460715745539358014631136245887418412633787074173796862711588221766398229333338511838891484974940633857861775630560092874987828057333663969469797013996401149696897591265769095952887917296740109742927689053276850469671231961384715398038978492733178835452859452433234470997285516534065058887757272972533841547437247509415567206632827453524027585684464869520087576386907357207827931645864812453790560013100879070175244115566800303394525802384116508985235998027327908578315965475155235939798618031870849109894283125229184144172630553554607112725169432413343763989564437170644270643461665184965150423819594083121075825", 0)
  426. q := new(Int)
  427. r := new(Int)
  428. b.ResetTimer()
  429. for i := 0; i < b.N; i++ {
  430. q.QuoRem(y, x, r)
  431. }
  432. }
  433. var bitLenTests = []struct {
  434. in string
  435. out int
  436. }{
  437. {"-1", 1},
  438. {"0", 0},
  439. {"1", 1},
  440. {"2", 2},
  441. {"4", 3},
  442. {"0xabc", 12},
  443. {"0x8000", 16},
  444. {"0x80000000", 32},
  445. {"0x800000000000", 48},
  446. {"0x8000000000000000", 64},
  447. {"0x80000000000000000000", 80},
  448. {"-0x4000000000000000000000", 87},
  449. }
  450. func TestBitLen(t *testing.T) {
  451. for i, test := range bitLenTests {
  452. x, ok := new(Int).SetString(test.in, 0)
  453. if !ok {
  454. t.Errorf("#%d test input invalid: %s", i, test.in)
  455. continue
  456. }
  457. if n := x.BitLen(); n != test.out {
  458. t.Errorf("#%d got %d want %d", i, n, test.out)
  459. }
  460. }
  461. }
  462. var expTests = []struct {
  463. x, y, m string
  464. out string
  465. }{
  466. // y <= 0
  467. {"0", "0", "", "1"},
  468. {"1", "0", "", "1"},
  469. {"-10", "0", "", "1"},
  470. {"1234", "-1", "", "1"},
  471. {"1234", "-1", "0", "1"},
  472. {"17", "-100", "1234", "865"},
  473. {"2", "-100", "1234", ""},
  474. // m == 1
  475. {"0", "0", "1", "0"},
  476. {"1", "0", "1", "0"},
  477. {"-10", "0", "1", "0"},
  478. {"1234", "-1", "1", "0"},
  479. // misc
  480. {"5", "1", "3", "2"},
  481. {"5", "-7", "", "1"},
  482. {"-5", "-7", "", "1"},
  483. {"5", "0", "", "1"},
  484. {"-5", "0", "", "1"},
  485. {"5", "1", "", "5"},
  486. {"-5", "1", "", "-5"},
  487. {"-5", "1", "7", "2"},
  488. {"-2", "3", "2", "0"},
  489. {"5", "2", "", "25"},
  490. {"1", "65537", "2", "1"},
  491. {"0x8000000000000000", "2", "", "0x40000000000000000000000000000000"},
  492. {"0x8000000000000000", "2", "6719", "4944"},
  493. {"0x8000000000000000", "3", "6719", "5447"},
  494. {"0x8000000000000000", "1000", "6719", "1603"},
  495. {"0x8000000000000000", "1000000", "6719", "3199"},
  496. {"0x8000000000000000", "-1000000", "6719", "3663"}, // 3663 = ModInverse(3199, 6719) Issue #25865
  497. {"0xffffffffffffffffffffffffffffffff", "0x12345678123456781234567812345678123456789", "0x01112222333344445555666677778889", "0x36168FA1DB3AAE6C8CE647E137F97A"},
  498. {
  499. "2938462938472983472983659726349017249287491026512746239764525612965293865296239471239874193284792387498274256129746192347",
  500. "298472983472983471903246121093472394872319615612417471234712061",
  501. "29834729834729834729347290846729561262544958723956495615629569234729836259263598127342374289365912465901365498236492183464",
  502. "23537740700184054162508175125554701713153216681790245129157191391322321508055833908509185839069455749219131480588829346291",
  503. },
  504. // test case for issue 8822
  505. {
  506. "11001289118363089646017359372117963499250546375269047542777928006103246876688756735760905680604646624353196869572752623285140408755420374049317646428185270079555372763503115646054602867593662923894140940837479507194934267532831694565516466765025434902348314525627418515646588160955862839022051353653052947073136084780742729727874803457643848197499548297570026926927502505634297079527299004267769780768565695459945235586892627059178884998772989397505061206395455591503771677500931269477503508150175717121828518985901959919560700853226255420793148986854391552859459511723547532575574664944815966793196961286234040892865",
  507. "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD",
  508. "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73",
  509. "21484252197776302499639938883777710321993113097987201050501182909581359357618579566746556372589385361683610524730509041328855066514963385522570894839035884713051640171474186548713546686476761306436434146475140156284389181808675016576845833340494848283681088886584219750554408060556769486628029028720727393293111678826356480455433909233520504112074401376133077150471237549474149190242010469539006449596611576612573955754349042329130631128234637924786466585703488460540228477440853493392086251021228087076124706778899179648655221663765993962724699135217212118535057766739392069738618682722216712319320435674779146070442",
  510. },
  511. {
  512. "-0x1BCE04427D8032319A89E5C4136456671AC620883F2C4139E57F91307C485AD2D6204F4F87A58262652DB5DBBAC72B0613E51B835E7153BEC6068F5C8D696B74DBD18FEC316AEF73985CF0475663208EB46B4F17DD9DA55367B03323E5491A70997B90C059FB34809E6EE55BCFBD5F2F52233BFE62E6AA9E4E26A1D4C2439883D14F2633D55D8AA66A1ACD5595E778AC3A280517F1157989E70C1A437B849F1877B779CC3CDDEDE2DAA6594A6C66D181A00A5F777EE60596D8773998F6E988DEAE4CCA60E4DDCF9590543C89F74F603259FCAD71660D30294FBBE6490300F78A9D63FA660DC9417B8B9DDA28BEB3977B621B988E23D4D954F322C3540541BC649ABD504C50FADFD9F0987D58A2BF689313A285E773FF02899A6EF887D1D4A0D2",
  513. "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD",
  514. "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73",
  515. "21484252197776302499639938883777710321993113097987201050501182909581359357618579566746556372589385361683610524730509041328855066514963385522570894839035884713051640171474186548713546686476761306436434146475140156284389181808675016576845833340494848283681088886584219750554408060556769486628029028720727393293111678826356480455433909233520504112074401376133077150471237549474149190242010469539006449596611576612573955754349042329130631128234637924786466585703488460540228477440853493392086251021228087076124706778899179648655221663765993962724699135217212118535057766739392069738618682722216712319320435674779146070442",
  516. },
  517. // test cases for issue 13907
  518. {"0xffffffff00000001", "0xffffffff00000001", "0xffffffff00000001", "0"},
  519. {"0xffffffffffffffff00000001", "0xffffffffffffffff00000001", "0xffffffffffffffff00000001", "0"},
  520. {"0xffffffffffffffffffffffff00000001", "0xffffffffffffffffffffffff00000001", "0xffffffffffffffffffffffff00000001", "0"},
  521. {"0xffffffffffffffffffffffffffffffff00000001", "0xffffffffffffffffffffffffffffffff00000001", "0xffffffffffffffffffffffffffffffff00000001", "0"},
  522. {
  523. "2",
  524. "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD",
  525. "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73", // odd
  526. "0x6AADD3E3E424D5B713FCAA8D8945B1E055166132038C57BBD2D51C833F0C5EA2007A2324CE514F8E8C2F008A2F36F44005A4039CB55830986F734C93DAF0EB4BAB54A6A8C7081864F44346E9BC6F0A3EB9F2C0146A00C6A05187D0C101E1F2D038CDB70CB5E9E05A2D188AB6CBB46286624D4415E7D4DBFAD3BCC6009D915C406EED38F468B940F41E6BEDC0430DD78E6F19A7DA3A27498A4181E24D738B0072D8F6ADB8C9809A5B033A09785814FD9919F6EF9F83EEA519BEC593855C4C10CBEEC582D4AE0792158823B0275E6AEC35242740468FAF3D5C60FD1E376362B6322F78B7ED0CA1C5BBCD2B49734A56C0967A1D01A100932C837B91D592CE08ABFF",
  527. },
  528. {
  529. "2",
  530. "0xB08FFB20760FFED58FADA86DFEF71AD72AA0FA763219618FE022C197E54708BB1191C66470250FCE8879487507CEE41381CA4D932F81C2B3F1AB20B539D50DCD",
  531. "0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF72", // even
  532. "0x7858794B5897C29F4ED0B40913416AB6C48588484E6A45F2ED3E26C941D878E923575AAC434EE2750E6439A6976F9BB4D64CEDB2A53CE8D04DD48CADCDF8E46F22747C6B81C6CEA86C0D873FBF7CEF262BAAC43A522BD7F32F3CDAC52B9337C77B3DCFB3DB3EDD80476331E82F4B1DF8EFDC1220C92656DFC9197BDC1877804E28D928A2A284B8DED506CBA304435C9D0133C246C98A7D890D1DE60CBC53A024361DA83A9B8775019083D22AC6820ED7C3C68F8E801DD4EC779EE0A05C6EB682EF9840D285B838369BA7E148FA27691D524FAEAF7C6ECE2A4B99A294B9F2C241857B5B90CC8BFFCFCF18DFA7D676131D5CD3855A5A3E8EBFA0CDFADB4D198B4A",
  533. },
  534. }
  535. func TestExp(t *testing.T) {
  536. for i, test := range expTests {
  537. x, ok1 := new(Int).SetString(test.x, 0)
  538. y, ok2 := new(Int).SetString(test.y, 0)
  539. var ok3, ok4 bool
  540. var out, m *Int
  541. if len(test.out) == 0 {
  542. out, ok3 = nil, true
  543. } else {
  544. out, ok3 = new(Int).SetString(test.out, 0)
  545. }
  546. if len(test.m) == 0 {
  547. m, ok4 = nil, true
  548. } else {
  549. m, ok4 = new(Int).SetString(test.m, 0)
  550. }
  551. if !ok1 || !ok2 || !ok3 || !ok4 {
  552. t.Errorf("#%d: error in input", i)
  553. continue
  554. }
  555. z1 := new(Int).Exp(x, y, m)
  556. if z1 != nil && !isNormalized(z1) {
  557. t.Errorf("#%d: %v is not normalized", i, *z1)
  558. }
  559. if !(z1 == nil && out == nil || z1.Cmp(out) == 0) {
  560. t.Errorf("#%d: got %x want %x", i, z1, out)
  561. }
  562. if m == nil {
  563. // The result should be the same as for m == 0;
  564. // specifically, there should be no div-zero panic.
  565. m = &Int{abs: nat{}} // m != nil && len(m.abs) == 0
  566. z2 := new(Int).Exp(x, y, m)
  567. if z2.Cmp(z1) != 0 {
  568. t.Errorf("#%d: got %x want %x", i, z2, z1)
  569. }
  570. }
  571. }
  572. }
  573. func BenchmarkExp(b *testing.B) {
  574. x, _ := new(Int).SetString("11001289118363089646017359372117963499250546375269047542777928006103246876688756735760905680604646624353196869572752623285140408755420374049317646428185270079555372763503115646054602867593662923894140940837479507194934267532831694565516466765025434902348314525627418515646588160955862839022051353653052947073136084780742729727874803457643848197499548297570026926927502505634297079527299004267769780768565695459945235586892627059178884998772989397505061206395455591503771677500931269477503508150175717121828518985901959919560700853226255420793148986854391552859459511723547532575574664944815966793196961286234040892865", 0)
  575. y, _ := new(Int).SetString("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF72", 0)
  576. n, _ := new(Int).SetString("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73", 0)
  577. out := new(Int)
  578. for i := 0; i < b.N; i++ {
  579. out.Exp(x, y, n)
  580. }
  581. }
  582. func BenchmarkExp2(b *testing.B) {
  583. x, _ := new(Int).SetString("2", 0)
  584. y, _ := new(Int).SetString("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF72", 0)
  585. n, _ := new(Int).SetString("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73", 0)
  586. out := new(Int)
  587. for i := 0; i < b.N; i++ {
  588. out.Exp(x, y, n)
  589. }
  590. }
  591. func checkGcd(aBytes, bBytes []byte) bool {
  592. x := new(Int)
  593. y := new(Int)
  594. a := new(Int).SetBytes(aBytes)
  595. b := new(Int).SetBytes(bBytes)
  596. d := new(Int).GCD(x, y, a, b)
  597. x.Mul(x, a)
  598. y.Mul(y, b)
  599. x.Add(x, y)
  600. return x.Cmp(d) == 0
  601. }
  602. // euclidExtGCD is a reference implementation of Euclid's
  603. // extended GCD algorithm for testing against optimized algorithms.
  604. // Requirements: a, b > 0
  605. func euclidExtGCD(a, b *Int) (g, x, y *Int) {
  606. A := new(Int).Set(a)
  607. B := new(Int).Set(b)
  608. // A = Ua*a + Va*b
  609. // B = Ub*a + Vb*b
  610. Ua := new(Int).SetInt64(1)
  611. Va := new(Int)
  612. Ub := new(Int)
  613. Vb := new(Int).SetInt64(1)
  614. q := new(Int)
  615. temp := new(Int)
  616. r := new(Int)
  617. for len(B.abs) > 0 {
  618. q, r = q.QuoRem(A, B, r)
  619. A, B, r = B, r, A
  620. // Ua, Ub = Ub, Ua-q*Ub
  621. temp.Set(Ub)
  622. Ub.Mul(Ub, q)
  623. Ub.Sub(Ua, Ub)
  624. Ua.Set(temp)
  625. // Va, Vb = Vb, Va-q*Vb
  626. temp.Set(Vb)
  627. Vb.Mul(Vb, q)
  628. Vb.Sub(Va, Vb)
  629. Va.Set(temp)
  630. }
  631. return A, Ua, Va
  632. }
  633. func checkLehmerGcd(aBytes, bBytes []byte) bool {
  634. a := new(Int).SetBytes(aBytes)
  635. b := new(Int).SetBytes(bBytes)
  636. if a.Sign() <= 0 || b.Sign() <= 0 {
  637. return true // can only test positive arguments
  638. }
  639. d := new(Int).lehmerGCD(nil, nil, a, b)
  640. d0, _, _ := euclidExtGCD(a, b)
  641. return d.Cmp(d0) == 0
  642. }
  643. func checkLehmerExtGcd(aBytes, bBytes []byte) bool {
  644. a := new(Int).SetBytes(aBytes)
  645. b := new(Int).SetBytes(bBytes)
  646. x := new(Int)
  647. y := new(Int)
  648. if a.Sign() <= 0 || b.Sign() <= 0 {
  649. return true // can only test positive arguments
  650. }
  651. d := new(Int).lehmerGCD(x, y, a, b)
  652. d0, x0, y0 := euclidExtGCD(a, b)
  653. return d.Cmp(d0) == 0 && x.Cmp(x0) == 0 && y.Cmp(y0) == 0
  654. }
  655. var gcdTests = []struct {
  656. d, x, y, a, b string
  657. }{
  658. // a <= 0 || b <= 0
  659. {"0", "0", "0", "0", "0"},
  660. {"7", "0", "1", "0", "7"},
  661. {"7", "0", "-1", "0", "-7"},
  662. {"11", "1", "0", "11", "0"},
  663. {"7", "-1", "-2", "-77", "35"},
  664. {"935", "-3", "8", "64515", "24310"},
  665. {"935", "-3", "-8", "64515", "-24310"},
  666. {"935", "3", "-8", "-64515", "-24310"},
  667. {"1", "-9", "47", "120", "23"},
  668. {"7", "1", "-2", "77", "35"},
  669. {"935", "-3", "8", "64515", "24310"},
  670. {"935000000000000000", "-3", "8", "64515000000000000000", "24310000000000000000"},
  671. {"1", "-221", "22059940471369027483332068679400581064239780177629666810348940098015901108344", "98920366548084643601728869055592650835572950932266967461790948584315647051443", "991"},
  672. }
  673. func testGcd(t *testing.T, d, x, y, a, b *Int) {
  674. var X *Int
  675. if x != nil {
  676. X = new(Int)
  677. }
  678. var Y *Int
  679. if y != nil {
  680. Y = new(Int)
  681. }
  682. D := new(Int).GCD(X, Y, a, b)
  683. if D.Cmp(d) != 0 {
  684. t.Errorf("GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, D, d)
  685. }
  686. if x != nil && X.Cmp(x) != 0 {
  687. t.Errorf("GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, X, x)
  688. }
  689. if y != nil && Y.Cmp(y) != 0 {
  690. t.Errorf("GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, Y, y)
  691. }
  692. // check results in presence of aliasing (issue #11284)
  693. a2 := new(Int).Set(a)
  694. b2 := new(Int).Set(b)
  695. a2.GCD(X, Y, a2, b2) // result is same as 1st argument
  696. if a2.Cmp(d) != 0 {
  697. t.Errorf("aliased z = a GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, a2, d)
  698. }
  699. if x != nil && X.Cmp(x) != 0 {
  700. t.Errorf("aliased z = a GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, X, x)
  701. }
  702. if y != nil && Y.Cmp(y) != 0 {
  703. t.Errorf("aliased z = a GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, Y, y)
  704. }
  705. a2 = new(Int).Set(a)
  706. b2 = new(Int).Set(b)
  707. b2.GCD(X, Y, a2, b2) // result is same as 2nd argument
  708. if b2.Cmp(d) != 0 {
  709. t.Errorf("aliased z = b GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, b2, d)
  710. }
  711. if x != nil && X.Cmp(x) != 0 {
  712. t.Errorf("aliased z = b GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, X, x)
  713. }
  714. if y != nil && Y.Cmp(y) != 0 {
  715. t.Errorf("aliased z = b GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, Y, y)
  716. }
  717. a2 = new(Int).Set(a)
  718. b2 = new(Int).Set(b)
  719. D = new(Int).GCD(a2, b2, a2, b2) // x = a, y = b
  720. if D.Cmp(d) != 0 {
  721. t.Errorf("aliased x = a, y = b GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, D, d)
  722. }
  723. if x != nil && a2.Cmp(x) != 0 {
  724. t.Errorf("aliased x = a, y = b GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, a2, x)
  725. }
  726. if y != nil && b2.Cmp(y) != 0 {
  727. t.Errorf("aliased x = a, y = b GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, b2, y)
  728. }
  729. a2 = new(Int).Set(a)
  730. b2 = new(Int).Set(b)
  731. D = new(Int).GCD(b2, a2, a2, b2) // x = b, y = a
  732. if D.Cmp(d) != 0 {
  733. t.Errorf("aliased x = b, y = a GCD(%s, %s, %s, %s): got d = %s, want %s", x, y, a, b, D, d)
  734. }
  735. if x != nil && b2.Cmp(x) != 0 {
  736. t.Errorf("aliased x = b, y = a GCD(%s, %s, %s, %s): got x = %s, want %s", x, y, a, b, b2, x)
  737. }
  738. if y != nil && a2.Cmp(y) != 0 {
  739. t.Errorf("aliased x = b, y = a GCD(%s, %s, %s, %s): got y = %s, want %s", x, y, a, b, a2, y)
  740. }
  741. }
  742. func TestGcd(t *testing.T) {
  743. for _, test := range gcdTests {
  744. d, _ := new(Int).SetString(test.d, 0)
  745. x, _ := new(Int).SetString(test.x, 0)
  746. y, _ := new(Int).SetString(test.y, 0)
  747. a, _ := new(Int).SetString(test.a, 0)
  748. b, _ := new(Int).SetString(test.b, 0)
  749. testGcd(t, d, nil, nil, a, b)
  750. testGcd(t, d, x, nil, a, b)
  751. testGcd(t, d, nil, y, a, b)
  752. testGcd(t, d, x, y, a, b)
  753. }
  754. if err := quick.Check(checkGcd, nil); err != nil {
  755. t.Error(err)
  756. }
  757. if err := quick.Check(checkLehmerGcd, nil); err != nil {
  758. t.Error(err)
  759. }
  760. if err := quick.Check(checkLehmerExtGcd, nil); err != nil {
  761. t.Error(err)
  762. }
  763. }
  764. type intShiftTest struct {
  765. in string
  766. shift uint
  767. out string
  768. }
  769. var rshTests = []intShiftTest{
  770. {"0", 0, "0"},
  771. {"-0", 0, "0"},
  772. {"0", 1, "0"},
  773. {"0", 2, "0"},
  774. {"1", 0, "1"},
  775. {"1", 1, "0"},
  776. {"1", 2, "0"},
  777. {"2", 0, "2"},
  778. {"2", 1, "1"},
  779. {"-1", 0, "-1"},
  780. {"-1", 1, "-1"},
  781. {"-1", 10, "-1"},
  782. {"-100", 2, "-25"},
  783. {"-100", 3, "-13"},
  784. {"-100", 100, "-1"},
  785. {"4294967296", 0, "4294967296"},
  786. {"4294967296", 1, "2147483648"},
  787. {"4294967296", 2, "1073741824"},
  788. {"18446744073709551616", 0, "18446744073709551616"},
  789. {"18446744073709551616", 1, "9223372036854775808"},
  790. {"18446744073709551616", 2, "4611686018427387904"},
  791. {"18446744073709551616", 64, "1"},
  792. {"340282366920938463463374607431768211456", 64, "18446744073709551616"},
  793. {"340282366920938463463374607431768211456", 128, "1"},
  794. }
  795. func TestRsh(t *testing.T) {
  796. for i, test := range rshTests {
  797. in, _ := new(Int).SetString(test.in, 10)
  798. expected, _ := new(Int).SetString(test.out, 10)
  799. out := new(Int).Rsh(in, test.shift)
  800. if !isNormalized(out) {
  801. t.Errorf("#%d: %v is not normalized", i, *out)
  802. }
  803. if out.Cmp(expected) != 0 {
  804. t.Errorf("#%d: got %s want %s", i, out, expected)
  805. }
  806. }
  807. }
  808. func TestRshSelf(t *testing.T) {
  809. for i, test := range rshTests {
  810. z, _ := new(Int).SetString(test.in, 10)
  811. expected, _ := new(Int).SetString(test.out, 10)
  812. z.Rsh(z, test.shift)
  813. if !isNormalized(z) {
  814. t.Errorf("#%d: %v is not normalized", i, *z)
  815. }
  816. if z.Cmp(expected) != 0 {
  817. t.Errorf("#%d: got %s want %s", i, z, expected)
  818. }
  819. }
  820. }
  821. var lshTests = []intShiftTest{
  822. {"0", 0, "0"},
  823. {"0", 1, "0"},
  824. {"0", 2, "0"},
  825. {"1", 0, "1"},
  826. {"1", 1, "2"},
  827. {"1", 2, "4"},
  828. {"2", 0, "2"},
  829. {"2", 1, "4"},
  830. {"2", 2, "8"},
  831. {"-87", 1, "-174"},
  832. {"4294967296", 0, "4294967296"},
  833. {"4294967296", 1, "8589934592"},
  834. {"4294967296", 2, "17179869184"},
  835. {"18446744073709551616", 0, "18446744073709551616"},
  836. {"9223372036854775808", 1, "18446744073709551616"},
  837. {"4611686018427387904", 2, "18446744073709551616"},
  838. {"1", 64, "18446744073709551616"},
  839. {"18446744073709551616", 64, "340282366920938463463374607431768211456"},
  840. {"1", 128, "340282366920938463463374607431768211456"},
  841. }
  842. func TestLsh(t *testing.T) {
  843. for i, test := range lshTests {
  844. in, _ := new(Int).SetString(test.in, 10)
  845. expected, _ := new(Int).SetString(test.out, 10)
  846. out := new(Int).Lsh(in, test.shift)
  847. if !isNormalized(out) {
  848. t.Errorf("#%d: %v is not normalized", i, *out)
  849. }
  850. if out.Cmp(expected) != 0 {
  851. t.Errorf("#%d: got %s want %s", i, out, expected)
  852. }
  853. }
  854. }
  855. func TestLshSelf(t *testing.T) {
  856. for i, test := range lshTests {
  857. z, _ := new(Int).SetString(test.in, 10)
  858. expected, _ := new(Int).SetString(test.out, 10)
  859. z.Lsh(z, test.shift)
  860. if !isNormalized(z) {
  861. t.Errorf("#%d: %v is not normalized", i, *z)
  862. }
  863. if z.Cmp(expected) != 0 {
  864. t.Errorf("#%d: got %s want %s", i, z, expected)
  865. }
  866. }
  867. }
  868. func TestLshRsh(t *testing.T) {
  869. for i, test := range rshTests {
  870. in, _ := new(Int).SetString(test.in, 10)
  871. out := new(Int).Lsh(in, test.shift)
  872. out = out.Rsh(out, test.shift)
  873. if !isNormalized(out) {
  874. t.Errorf("#%d: %v is not normalized", i, *out)
  875. }
  876. if in.Cmp(out) != 0 {
  877. t.Errorf("#%d: got %s want %s", i, out, in)
  878. }
  879. }
  880. for i, test := range lshTests {
  881. in, _ := new(Int).SetString(test.in, 10)
  882. out := new(Int).Lsh(in, test.shift)
  883. out.Rsh(out, test.shift)
  884. if !isNormalized(out) {
  885. t.Errorf("#%d: %v is not normalized", i, *out)
  886. }
  887. if in.Cmp(out) != 0 {
  888. t.Errorf("#%d: got %s want %s", i, out, in)
  889. }
  890. }
  891. }
  892. // Entries must be sorted by value in ascending order.
  893. var cmpAbsTests = []string{
  894. "0",
  895. "1",
  896. "2",
  897. "10",
  898. "10000000",
  899. "2783678367462374683678456387645876387564783686583485",
  900. "2783678367462374683678456387645876387564783686583486",
  901. "32957394867987420967976567076075976570670947609750670956097509670576075067076027578341538",
  902. }
  903. func TestCmpAbs(t *testing.T) {
  904. values := make([]*Int, len(cmpAbsTests))
  905. var prev *Int
  906. for i, s := range cmpAbsTests {
  907. x, ok := new(Int).SetString(s, 0)
  908. if !ok {
  909. t.Fatalf("SetString(%s, 0) failed", s)
  910. }
  911. if prev != nil && prev.Cmp(x) >= 0 {
  912. t.Fatal("cmpAbsTests entries not sorted in ascending order")
  913. }
  914. values[i] = x
  915. prev = x
  916. }
  917. for i, x := range values {
  918. for j, y := range values {
  919. // try all combinations of signs for x, y
  920. for k := 0; k < 4; k++ {
  921. var a, b Int
  922. a.Set(x)
  923. b.Set(y)
  924. if k&1 != 0 {
  925. a.Neg(&a)
  926. }
  927. if k&2 != 0 {
  928. b.Neg(&b)
  929. }
  930. got := a.CmpAbs(&b)
  931. want := 0
  932. switch {
  933. case i > j:
  934. want = 1
  935. case i < j:
  936. want = -1
  937. }
  938. if got != want {
  939. t.Errorf("absCmp |%s|, |%s|: got %d; want %d", &a, &b, got, want)
  940. }
  941. }
  942. }
  943. }
  944. }
  945. func TestIntCmpSelf(t *testing.T) {
  946. for _, s := range cmpAbsTests {
  947. x, ok := new(Int).SetString(s, 0)
  948. if !ok {
  949. t.Fatalf("SetString(%s, 0) failed", s)
  950. }
  951. got := x.Cmp(x)
  952. want := 0
  953. if got != want {
  954. t.Errorf("x = %s: x.Cmp(x): got %d; want %d", x, got, want)
  955. }
  956. }
  957. }
  958. var int64Tests = []string{
  959. // int64
  960. "0",
  961. "1",
  962. "-1",
  963. "4294967295",
  964. "-4294967295",
  965. "4294967296",
  966. "-4294967296",
  967. "9223372036854775807",
  968. "-9223372036854775807",
  969. "-9223372036854775808",
  970. // not int64
  971. "0x8000000000000000",
  972. "-0x8000000000000001",
  973. "38579843757496759476987459679745",
  974. "-38579843757496759476987459679745",
  975. }
  976. func TestInt64(t *testing.T) {
  977. for _, s := range int64Tests {
  978. var x Int
  979. _, ok := x.SetString(s, 0)
  980. if !ok {
  981. t.Errorf("SetString(%s, 0) failed", s)
  982. continue
  983. }
  984. want, err := strconv.ParseInt(s, 0, 64)
  985. if err != nil {
  986. if err.(*strconv.NumError).Err == strconv.ErrRange {
  987. if x.IsInt64() {
  988. t.Errorf("IsInt64(%s) succeeded unexpectedly", s)
  989. }
  990. } else {
  991. t.Errorf("ParseInt(%s) failed", s)
  992. }
  993. continue
  994. }
  995. if !x.IsInt64() {
  996. t.Errorf("IsInt64(%s) failed unexpectedly", s)
  997. }
  998. got := x.Int64()
  999. if got != want {
  1000. t.Errorf("Int64(%s) = %d; want %d", s, got, want)
  1001. }
  1002. }
  1003. }
  1004. var uint64Tests = []string{
  1005. // uint64
  1006. "0",
  1007. "1",
  1008. "4294967295",
  1009. "4294967296",
  1010. "8589934591",
  1011. "8589934592",
  1012. "9223372036854775807",
  1013. "9223372036854775808",
  1014. "0x08000000000000000",
  1015. // not uint64
  1016. "0x10000000000000000",
  1017. "-0x08000000000000000",
  1018. "-1",
  1019. }
  1020. func TestUint64(t *testing.T) {
  1021. for _, s := range uint64Tests {
  1022. var x Int
  1023. _, ok := x.SetString(s, 0)
  1024. if !ok {
  1025. t.Errorf("SetString(%s, 0) failed", s)
  1026. continue
  1027. }
  1028. want, err := strconv.ParseUint(s, 0, 64)
  1029. if err != nil {
  1030. // check for sign explicitly (ErrRange doesn't cover signed input)
  1031. if s[0] == '-' || err.(*strconv.NumError).Err == strconv.ErrRange {
  1032. if x.IsUint64() {
  1033. t.Errorf("IsUint64(%s) succeeded unexpectedly", s)
  1034. }
  1035. } else {
  1036. t.Errorf("ParseUint(%s) failed", s)
  1037. }
  1038. continue
  1039. }
  1040. if !x.IsUint64() {
  1041. t.Errorf("IsUint64(%s) failed unexpectedly", s)
  1042. }
  1043. got := x.Uint64()
  1044. if got != want {
  1045. t.Errorf("Uint64(%s) = %d; want %d", s, got, want)
  1046. }
  1047. }
  1048. }
  1049. var bitwiseTests = []struct {
  1050. x, y string
  1051. and, or, xor, andNot string
  1052. }{
  1053. {"0x00", "0x00", "0x00", "0x00", "0x00", "0x00"},
  1054. {"0x00", "0x01", "0x00", "0x01", "0x01", "0x00"},
  1055. {"0x01", "0x00", "0x00", "0x01", "0x01", "0x01"},
  1056. {"-0x01", "0x00", "0x00", "-0x01", "-0x01", "-0x01"},
  1057. {"-0xaf", "-0x50", "-0xf0", "-0x0f", "0xe1", "0x41"},
  1058. {"0x00", "-0x01", "0x00", "-0x01", "-0x01", "0x00"},
  1059. {"0x01", "0x01", "0x01", "0x01", "0x00", "0x00"},
  1060. {"-0x01", "-0x01", "-0x01", "-0x01", "0x00", "0x00"},
  1061. {"0x07", "0x08", "0x00", "0x0f", "0x0f", "0x07"},
  1062. {"0x05", "0x0f", "0x05", "0x0f", "0x0a", "0x00"},
  1063. {"0xff", "-0x0a", "0xf6", "-0x01", "-0xf7", "0x09"},
  1064. {"0x013ff6", "0x9a4e", "0x1a46", "0x01bffe", "0x01a5b8", "0x0125b0"},
  1065. {"-0x013ff6", "0x9a4e", "0x800a", "-0x0125b2", "-0x01a5bc", "-0x01c000"},
  1066. {"-0x013ff6", "-0x9a4e", "-0x01bffe", "-0x1a46", "0x01a5b8", "0x8008"},
  1067. {
  1068. "0x1000009dc6e3d9822cba04129bcbe3401",
  1069. "0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
  1070. "0x1000001186210100001000009048c2001",
  1071. "0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
  1072. "0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
  1073. "0x8c40c2d8822caa04120b8321400",
  1074. },
  1075. {
  1076. "0x1000009dc6e3d9822cba04129bcbe3401",
  1077. "-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
  1078. "0x8c40c2d8822caa04120b8321401",
  1079. "-0xb9bd7d543685789d57ca918e82229142459020483cd2014001fd",
  1080. "-0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fe",
  1081. "0x1000001186210100001000009048c2000",
  1082. },
  1083. {
  1084. "-0x1000009dc6e3d9822cba04129bcbe3401",
  1085. "-0xb9bd7d543685789d57cb918e833af352559021483cdb05cc21fd",
  1086. "-0xb9bd7d543685789d57cb918e8bfeff7fddb2ebe87dfbbdfe35fd",
  1087. "-0x1000001186210100001000009048c2001",
  1088. "0xb9bd7d543685789d57ca918e8ae69d6fcdb2eae87df2b97215fc",
  1089. "0xb9bd7d543685789d57ca918e82229142459020483cd2014001fc",
  1090. },
  1091. }
  1092. type bitFun func(z, x, y *Int) *Int
  1093. func testBitFun(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
  1094. expected := new(Int)
  1095. expected.SetString(exp, 0)
  1096. out := f(new(Int), x, y)
  1097. if out.Cmp(expected) != 0 {
  1098. t.Errorf("%s: got %s want %s", msg, out, expected)
  1099. }
  1100. }
  1101. func testBitFunSelf(t *testing.T, msg string, f bitFun, x, y *Int, exp string) {
  1102. self := new(Int)
  1103. self.Set(x)
  1104. expected := new(Int)
  1105. expected.SetString(exp, 0)
  1106. self = f(self, self, y)
  1107. if self.Cmp(expected) != 0 {
  1108. t.Errorf("%s: got %s want %s", msg, self, expected)
  1109. }
  1110. }
  1111. func altBit(x *Int, i int) uint {
  1112. z := new(Int).Rsh(x, uint(i))
  1113. z = z.And(z, NewInt(1))
  1114. if z.Cmp(new(Int)) != 0 {
  1115. return 1
  1116. }
  1117. return 0
  1118. }
  1119. func altSetBit(z *Int, x *Int, i int, b uint) *Int {
  1120. one := NewInt(1)
  1121. m := one.Lsh(one, uint(i))
  1122. switch b {
  1123. case 1:
  1124. return z.Or(x, m)
  1125. case 0:
  1126. return z.AndNot(x, m)
  1127. }
  1128. panic("set bit is not 0 or 1")
  1129. }
  1130. func testBitset(t *testing.T, x *Int) {
  1131. n := x.BitLen()
  1132. z := new(Int).Set(x)
  1133. z1 := new(Int).Set(x)
  1134. for i := 0; i < n+10; i++ {
  1135. old := z.Bit(i)
  1136. old1 := altBit(z1, i)
  1137. if old != old1 {
  1138. t.Errorf("bitset: inconsistent value for Bit(%s, %d), got %v want %v", z1, i, old, old1)
  1139. }
  1140. z := new(Int).SetBit(z, i, 1)
  1141. z1 := altSetBit(new(Int), z1, i, 1)
  1142. if z.Bit(i) == 0 {
  1143. t.Errorf("bitset: bit %d of %s got 0 want 1", i, x)
  1144. }
  1145. if z.Cmp(z1) != 0 {
  1146. t.Errorf("bitset: inconsistent value after SetBit 1, got %s want %s", z, z1)
  1147. }
  1148. z.SetBit(z, i, 0)
  1149. altSetBit(z1, z1, i, 0)
  1150. if z.Bit(i) != 0 {
  1151. t.Errorf("bitset: bit %d of %s got 1 want 0", i, x)
  1152. }
  1153. if z.Cmp(z1) != 0 {
  1154. t.Errorf("bitset: inconsistent value after SetBit 0, got %s want %s", z, z1)
  1155. }
  1156. altSetBit(z1, z1, i, old)
  1157. z.SetBit(z, i, old)
  1158. if z.Cmp(z1) != 0 {
  1159. t.Errorf("bitset: inconsistent value after SetBit old, got %s want %s", z, z1)
  1160. }
  1161. }
  1162. if z.Cmp(x) != 0 {
  1163. t.Errorf("bitset: got %s want %s", z, x)
  1164. }
  1165. }
  1166. var bitsetTests = []struct {
  1167. x string
  1168. i int
  1169. b uint
  1170. }{
  1171. {"0", 0, 0},
  1172. {"0", 200, 0},
  1173. {"1", 0, 1},
  1174. {"1", 1, 0},
  1175. {"-1", 0, 1},
  1176. {"-1", 200, 1},
  1177. {"0x2000000000000000000000000000", 108, 0},
  1178. {"0x2000000000000000000000000000", 109, 1},
  1179. {"0x2000000000000000000000000000", 110, 0},
  1180. {"-0x2000000000000000000000000001", 108, 1},
  1181. {"-0x2000000000000000000000000001", 109, 0},
  1182. {"-0x2000000000000000000000000001", 110, 1},
  1183. }
  1184. func TestBitSet(t *testing.T) {
  1185. for _, test := range bitwiseTests {
  1186. x := new(Int)
  1187. x.SetString(test.x, 0)
  1188. testBitset(t, x)
  1189. x = new(Int)
  1190. x.SetString(test.y, 0)
  1191. testBitset(t, x)
  1192. }
  1193. for i, test := range bitsetTests {
  1194. x := new(Int)
  1195. x.SetString(test.x, 0)
  1196. b := x.Bit(test.i)
  1197. if b != test.b {
  1198. t.Errorf("#%d got %v want %v", i, b, test.b)
  1199. }
  1200. }
  1201. z := NewInt(1)
  1202. z.SetBit(NewInt(0), 2, 1)
  1203. if z.Cmp(NewInt(4)) != 0 {
  1204. t.Errorf("destination leaked into result; got %s want 4", z)
  1205. }
  1206. }
  1207. var tzbTests = []struct {
  1208. in string
  1209. out uint
  1210. }{
  1211. {"0", 0},
  1212. {"1", 0},
  1213. {"-1", 0},
  1214. {"4", 2},
  1215. {"-8", 3},
  1216. {"0x4000000000000000000", 74},
  1217. {"-0x8000000000000000000", 75},
  1218. }
  1219. func TestTrailingZeroBits(t *testing.T) {
  1220. for i, test := range tzbTests {
  1221. in, _ := new(Int).SetString(test.in, 0)
  1222. want := test.out
  1223. got := in.TrailingZeroBits()
  1224. if got != want {
  1225. t.Errorf("#%d: got %v want %v", i, got, want)
  1226. }
  1227. }
  1228. }
  1229. func BenchmarkBitset(b *testing.B) {
  1230. z := new(Int)
  1231. z.SetBit(z, 512, 1)
  1232. b.ResetTimer()
  1233. b.StartTimer()
  1234. for i := b.N - 1; i >= 0; i-- {
  1235. z.SetBit(z, i&512, 1)
  1236. }
  1237. }
  1238. func BenchmarkBitsetNeg(b *testing.B) {
  1239. z := NewInt(-1)
  1240. z.SetBit(z, 512, 0)
  1241. b.ResetTimer()
  1242. b.StartTimer()
  1243. for i := b.N - 1; i >= 0; i-- {
  1244. z.SetBit(z, i&512, 0)
  1245. }
  1246. }
  1247. func BenchmarkBitsetOrig(b *testing.B) {
  1248. z := new(Int)
  1249. altSetBit(z, z, 512, 1)
  1250. b.ResetTimer()
  1251. b.StartTimer()
  1252. for i := b.N - 1; i >= 0; i-- {
  1253. altSetBit(z, z, i&512, 1)
  1254. }
  1255. }
  1256. func BenchmarkBitsetNegOrig(b *testing.B) {
  1257. z := NewInt(-1)
  1258. altSetBit(z, z, 512, 0)
  1259. b.ResetTimer()
  1260. b.StartTimer()
  1261. for i := b.N - 1; i >= 0; i-- {
  1262. altSetBit(z, z, i&512, 0)
  1263. }
  1264. }
  1265. // tri generates the trinomial 2**(n*2) - 2**n - 1, which is always 3 mod 4 and
  1266. // 7 mod 8, so that 2 is always a quadratic residue.
  1267. func tri(n uint) *Int {
  1268. x := NewInt(1)
  1269. x.Lsh(x, n)
  1270. x2 := new(Int).Lsh(x, n)
  1271. x2.Sub(x2, x)
  1272. x2.Sub(x2, intOne)
  1273. return x2
  1274. }
  1275. func BenchmarkModSqrt225_Tonelli(b *testing.B) {
  1276. p := tri(225)
  1277. x := NewInt(2)
  1278. for i := 0; i < b.N; i++ {
  1279. x.SetUint64(2)
  1280. x.modSqrtTonelliShanks(x, p)
  1281. }
  1282. }
  1283. func BenchmarkModSqrt225_3Mod4(b *testing.B) {
  1284. p := tri(225)
  1285. x := new(Int).SetUint64(2)
  1286. for i := 0; i < b.N; i++ {
  1287. x.SetUint64(2)
  1288. x.modSqrt3Mod4Prime(x, p)
  1289. }
  1290. }
  1291. func BenchmarkModSqrt231_Tonelli(b *testing.B) {
  1292. p := tri(231)
  1293. p.Sub(p, intOne)
  1294. p.Sub(p, intOne) // tri(231) - 2 is a prime == 5 mod 8
  1295. x := new(Int).SetUint64(7)
  1296. for i := 0; i < b.N; i++ {
  1297. x.SetUint64(7)
  1298. x.modSqrtTonelliShanks(x, p)
  1299. }
  1300. }
  1301. func BenchmarkModSqrt231_5Mod8(b *testing.B) {
  1302. p := tri(231)
  1303. p.Sub(p, intOne)
  1304. p.Sub(p, intOne) // tri(231) - 2 is a prime == 5 mod 8
  1305. x := new(Int).SetUint64(7)
  1306. for i := 0; i < b.N; i++ {
  1307. x.SetUint64(7)
  1308. x.modSqrt5Mod8Prime(x, p)
  1309. }
  1310. }
  1311. func TestBitwise(t *testing.T) {
  1312. x := new(Int)
  1313. y := new(Int)
  1314. for _, test := range bitwiseTests {
  1315. x.SetString(test.x, 0)
  1316. y.SetString(test.y, 0)
  1317. testBitFun(t, "and", (*Int).And, x, y, test.and)
  1318. testBitFunSelf(t, "and", (*Int).And, x, y, test.and)
  1319. testBitFun(t, "andNot", (*Int).AndNot, x, y, test.andNot)
  1320. testBitFunSelf(t, "andNot", (*Int).AndNot, x, y, test.andNot)
  1321. testBitFun(t, "or", (*Int).Or, x, y, test.or)
  1322. testBitFunSelf(t, "or", (*Int).Or, x, y, test.or)
  1323. testBitFun(t, "xor", (*Int).Xor, x, y, test.xor)
  1324. testBitFunSelf(t, "xor", (*Int).Xor, x, y, test.xor)
  1325. }
  1326. }
  1327. var notTests = []struct {
  1328. in string
  1329. out string
  1330. }{
  1331. {"0", "-1"},
  1332. {"1", "-2"},
  1333. {"7", "-8"},
  1334. {"0", "-1"},
  1335. {"-81910", "81909"},
  1336. {
  1337. "298472983472983471903246121093472394872319615612417471234712061",
  1338. "-298472983472983471903246121093472394872319615612417471234712062",
  1339. },
  1340. }
  1341. func TestNot(t *testing.T) {
  1342. in := new(Int)
  1343. out := new(Int)
  1344. expected := new(Int)
  1345. for i, test := range notTests {
  1346. in.SetString(test.in, 10)
  1347. expected.SetString(test.out, 10)
  1348. out = out.Not(in)
  1349. if out.Cmp(expected) != 0 {
  1350. t.Errorf("#%d: got %s want %s", i, out, expected)
  1351. }
  1352. out = out.Not(out)
  1353. if out.Cmp(in) != 0 {
  1354. t.Errorf("#%d: got %s want %s", i, out, in)
  1355. }
  1356. }
  1357. }
  1358. var modInverseTests = []struct {
  1359. element string
  1360. modulus string
  1361. }{
  1362. {"1234567", "458948883992"},
  1363. {"239487239847", "2410312426921032588552076022197566074856950548502459942654116941958108831682612228890093858261341614673227141477904012196503648957050582631942730706805009223062734745341073406696246014589361659774041027169249453200378729434170325843778659198143763193776859869524088940195577346119843545301547043747207749969763750084308926339295559968882457872412993810129130294592999947926365264059284647209730384947211681434464714438488520940127459844288859336526896320919633919"},
  1364. {"-10", "13"}, // issue #16984
  1365. {"10", "-13"},
  1366. {"-17", "-13"},
  1367. }
  1368. func TestModInverse(t *testing.T) {
  1369. var element, modulus, gcd, inverse Int
  1370. one := NewInt(1)
  1371. for _, test := range modInverseTests {
  1372. (&element).SetString(test.element, 10)
  1373. (&modulus).SetString(test.modulus, 10)
  1374. (&inverse).ModInverse(&element, &modulus)
  1375. (&inverse).Mul(&inverse, &element)
  1376. (&inverse).Mod(&inverse, &modulus)
  1377. if (&inverse).Cmp(one) != 0 {
  1378. t.Errorf("ModInverse(%d,%d)*%d%%%d=%d, not 1", &element, &modulus, &element, &modulus, &inverse)
  1379. }
  1380. }
  1381. // exhaustive test for small values
  1382. for n := 2; n < 100; n++ {
  1383. (&modulus).SetInt64(int64(n))
  1384. for x := 1; x < n; x++ {
  1385. (&element).SetInt64(int64(x))
  1386. (&gcd).GCD(nil, nil, &element, &modulus)
  1387. if (&gcd).Cmp(one) != 0 {
  1388. continue
  1389. }
  1390. (&inverse).ModInverse(&element, &modulus)
  1391. (&inverse).Mul(&inverse, &element)
  1392. (&inverse).Mod(&inverse, &modulus)
  1393. if (&inverse).Cmp(one) != 0 {
  1394. t.Errorf("ModInverse(%d,%d)*%d%%%d=%d, not 1", &element, &modulus, &element, &modulus, &inverse)
  1395. }
  1396. }
  1397. }
  1398. }
  1399. func BenchmarkModInverse(b *testing.B) {
  1400. p := new(Int).SetInt64(1) // Mersenne prime 2**1279 -1
  1401. p.abs = p.abs.shl(p.abs, 1279)
  1402. p.Sub(p, intOne)
  1403. x := new(Int).Sub(p, intOne)
  1404. z := new(Int)
  1405. for i := 0; i < b.N; i++ {
  1406. z.ModInverse(x, p)
  1407. }
  1408. }
  1409. // testModSqrt is a helper for TestModSqrt,
  1410. // which checks that ModSqrt can compute a square-root of elt^2.
  1411. func testModSqrt(t *testing.T, elt, mod, sq, sqrt *Int) bool {
  1412. var sqChk, sqrtChk, sqrtsq Int
  1413. sq.Mul(elt, elt)
  1414. sq.Mod(sq, mod)
  1415. z := sqrt.ModSqrt(sq, mod)
  1416. if z != sqrt {
  1417. t.Errorf("ModSqrt returned wrong value %s", z)
  1418. }
  1419. // test ModSqrt arguments outside the range [0,mod)
  1420. sqChk.Add(sq, mod)
  1421. z = sqrtChk.ModSqrt(&sqChk, mod)
  1422. if z != &sqrtChk || z.Cmp(sqrt) != 0 {
  1423. t.Errorf("ModSqrt returned inconsistent value %s", z)
  1424. }
  1425. sqChk.Sub(sq, mod)
  1426. z = sqrtChk.ModSqrt(&sqChk, mod)
  1427. if z != &sqrtChk || z.Cmp(sqrt) != 0 {
  1428. t.Errorf("ModSqrt returned inconsistent value %s", z)
  1429. }
  1430. // test x aliasing z
  1431. z = sqrtChk.ModSqrt(sqrtChk.Set(sq), mod)
  1432. if z != &sqrtChk || z.Cmp(sqrt) != 0 {
  1433. t.Errorf("ModSqrt returned inconsistent value %s", z)
  1434. }
  1435. // make sure we actually got a square root
  1436. if sqrt.Cmp(elt) == 0 {
  1437. return true // we found the "desired" square root
  1438. }
  1439. sqrtsq.Mul(sqrt, sqrt) // make sure we found the "other" one
  1440. sqrtsq.Mod(&sqrtsq, mod)
  1441. return sq.Cmp(&sqrtsq) == 0
  1442. }
  1443. func TestModSqrt(t *testing.T) {
  1444. var elt, mod, modx4, sq, sqrt Int
  1445. r := rand.New(rand.NewSource(9))
  1446. for i, s := range primes[1:] { // skip 2, use only odd primes
  1447. mod.SetString(s, 10)
  1448. modx4.Lsh(&mod, 2)
  1449. // test a few random elements per prime
  1450. for x := 1; x < 5; x++ {
  1451. elt.Rand(r, &modx4)
  1452. elt.Sub(&elt, &mod) // test range [-mod, 3*mod)
  1453. if !testModSqrt(t, &elt, &mod, &sq, &sqrt) {
  1454. t.Errorf("#%d: failed (sqrt(e) = %s)", i, &sqrt)
  1455. }
  1456. }
  1457. if testing.Short() && i > 2 {
  1458. break
  1459. }
  1460. }
  1461. if testing.Short() {
  1462. return
  1463. }
  1464. // exhaustive test for small values
  1465. for n := 3; n < 100; n++ {
  1466. mod.SetInt64(int64(n))
  1467. if !mod.ProbablyPrime(10) {
  1468. continue
  1469. }
  1470. isSquare := make([]bool, n)
  1471. // test all the squares
  1472. for x := 1; x < n; x++ {
  1473. elt.SetInt64(int64(x))
  1474. if !testModSqrt(t, &elt, &mod, &sq, &sqrt) {
  1475. t.Errorf("#%d: failed (sqrt(%d,%d) = %s)", x, &elt, &mod, &sqrt)
  1476. }
  1477. isSquare[sq.Uint64()] = true
  1478. }
  1479. // test all non-squares
  1480. for x := 1; x < n; x++ {
  1481. sq.SetInt64(int64(x))
  1482. z := sqrt.ModSqrt(&sq, &mod)
  1483. if !isSquare[x] && z != nil {
  1484. t.Errorf("#%d: failed (sqrt(%d,%d) = nil)", x, &sqrt, &mod)
  1485. }
  1486. }
  1487. }
  1488. }
  1489. func TestJacobi(t *testing.T) {
  1490. testCases := []struct {
  1491. x, y int64
  1492. result int
  1493. }{
  1494. {0, 1, 1},
  1495. {0, -1, 1},
  1496. {1, 1, 1},
  1497. {1, -1, 1},
  1498. {0, 5, 0},
  1499. {1, 5, 1},
  1500. {2, 5, -1},
  1501. {-2, 5, -1},
  1502. {2, -5, -1},
  1503. {-2, -5, 1},
  1504. {3, 5, -1},
  1505. {5, 5, 0},
  1506. {-5, 5, 0},
  1507. {6, 5, 1},
  1508. {6, -5, 1},
  1509. {-6, 5, 1},
  1510. {-6, -5, -1},
  1511. }
  1512. var x, y Int
  1513. for i, test := range testCases {
  1514. x.SetInt64(test.x)
  1515. y.SetInt64(test.y)
  1516. expected := test.result
  1517. actual := Jacobi(&x, &y)
  1518. if actual != expected {
  1519. t.Errorf("#%d: Jacobi(%d, %d) = %d, but expected %d", i, test.x, test.y, actual, expected)
  1520. }
  1521. }
  1522. }
  1523. func TestJacobiPanic(t *testing.T) {
  1524. const failureMsg = "test failure"
  1525. defer func() {
  1526. msg := recover()
  1527. if msg == nil || msg == failureMsg {
  1528. panic(msg)
  1529. }
  1530. t.Log(msg)
  1531. }()
  1532. x := NewInt(1)
  1533. y := NewInt(2)
  1534. // Jacobi should panic when the second argument is even.
  1535. Jacobi(x, y)
  1536. panic(failureMsg)
  1537. }
  1538. func TestIssue2607(t *testing.T) {
  1539. // This code sequence used to hang.
  1540. n := NewInt(10)
  1541. n.Rand(rand.New(rand.NewSource(9)), n)
  1542. }
  1543. func TestSqrt(t *testing.T) {
  1544. root := 0
  1545. r := new(Int)
  1546. for i := 0; i < 10000; i++ {
  1547. if (root+1)*(root+1) <= i {
  1548. root++
  1549. }
  1550. n := NewInt(int64(i))
  1551. r.SetInt64(-2)
  1552. r.Sqrt(n)
  1553. if r.Cmp(NewInt(int64(root))) != 0 {
  1554. t.Errorf("Sqrt(%v) = %v, want %v", n, r, root)
  1555. }
  1556. }
  1557. for i := 0; i < 1000; i += 10 {
  1558. n, _ := new(Int).SetString("1"+strings.Repeat("0", i), 10)
  1559. r := new(Int).Sqrt(n)
  1560. root, _ := new(Int).SetString("1"+strings.Repeat("0", i/2), 10)
  1561. if r.Cmp(root) != 0 {
  1562. t.Errorf("Sqrt(1e%d) = %v, want 1e%d", i, r, i/2)
  1563. }
  1564. }
  1565. // Test aliasing.
  1566. r.SetInt64(100)
  1567. r.Sqrt(r)
  1568. if r.Int64() != 10 {
  1569. t.Errorf("Sqrt(100) = %v, want 10 (aliased output)", r.Int64())
  1570. }
  1571. }
  1572. // We can't test this together with the other Exp tests above because
  1573. // it requires a different receiver setup.
  1574. func TestIssue22830(t *testing.T) {
  1575. one := new(Int).SetInt64(1)
  1576. base, _ := new(Int).SetString("84555555300000000000", 10)
  1577. mod, _ := new(Int).SetString("66666670001111111111", 10)
  1578. want, _ := new(Int).SetString("17888885298888888889", 10)
  1579. var tests = []int64{
  1580. 0, 1, -1,
  1581. }
  1582. for _, n := range tests {
  1583. m := NewInt(n)
  1584. if got := m.Exp(base, one, mod); got.Cmp(want) != 0 {
  1585. t.Errorf("(%v).Exp(%s, 1, %s) = %s, want %s", n, base, mod, got, want)
  1586. }
  1587. }
  1588. }
  1589. func BenchmarkSqrt(b *testing.B) {
  1590. n, _ := new(Int).SetString("1"+strings.Repeat("0", 1001), 10)
  1591. b.ResetTimer()
  1592. t := new(Int)
  1593. for i := 0; i < b.N; i++ {
  1594. t.Sqrt(n)
  1595. }
  1596. }
  1597. func benchmarkIntSqr(b *testing.B, nwords int) {
  1598. x := new(Int)
  1599. x.abs = rndNat(nwords)
  1600. t := new(Int)
  1601. b.ResetTimer()
  1602. for i := 0; i < b.N; i++ {
  1603. t.Mul(x, x)
  1604. }
  1605. }
  1606. func BenchmarkIntSqr(b *testing.B) {
  1607. for _, n := range sqrBenchSizes {
  1608. if isRaceBuilder && n > 1e3 {
  1609. continue
  1610. }
  1611. b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
  1612. benchmarkIntSqr(b, n)
  1613. })
  1614. }
  1615. }
  1616. func benchmarkDiv(b *testing.B, aSize, bSize int) {
  1617. var r = rand.New(rand.NewSource(1234))
  1618. aa := randInt(r, uint(aSize))
  1619. bb := randInt(r, uint(bSize))
  1620. if aa.Cmp(bb) < 0 {
  1621. aa, bb = bb, aa
  1622. }
  1623. x := new(Int)
  1624. y := new(Int)
  1625. b.ResetTimer()
  1626. for i := 0; i < b.N; i++ {
  1627. x.DivMod(aa, bb, y)
  1628. }
  1629. }
  1630. func BenchmarkDiv(b *testing.B) {
  1631. sizes := []int{
  1632. 10, 20, 50, 100, 200, 500, 1000,
  1633. 1e4, 1e5, 1e6, 1e7,
  1634. }
  1635. for _, i := range sizes {
  1636. j := 2 * i
  1637. b.Run(fmt.Sprintf("%d/%d", j, i), func(b *testing.B) {
  1638. benchmarkDiv(b, j, i)
  1639. })
  1640. }
  1641. }
  1642. func TestFillBytes(t *testing.T) {
  1643. checkResult := func(t *testing.T, buf []byte, want *Int) {
  1644. t.Helper()
  1645. got := new(Int).SetBytes(buf)
  1646. if got.CmpAbs(want) != 0 {
  1647. t.Errorf("got 0x%x, want 0x%x: %x", got, want, buf)
  1648. }
  1649. }
  1650. panics := func(f func()) (panic bool) {
  1651. defer func() { panic = recover() != nil }()
  1652. f()
  1653. return
  1654. }
  1655. for _, n := range []string{
  1656. "0",
  1657. "1000",
  1658. "0xffffffff",
  1659. "-0xffffffff",
  1660. "0xffffffffffffffff",
  1661. "0x10000000000000000",
  1662. "0xabababababababababababababababababababababababababa",
  1663. "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  1664. } {
  1665. t.Run(n, func(t *testing.T) {
  1666. t.Logf(n)
  1667. x, ok := new(Int).SetString(n, 0)
  1668. if !ok {
  1669. panic("invalid test entry")
  1670. }
  1671. // Perfectly sized buffer.
  1672. byteLen := (x.BitLen() + 7) / 8
  1673. buf := make([]byte, byteLen)
  1674. checkResult(t, x.FillBytes(buf), x)
  1675. // Way larger, checking all bytes get zeroed.
  1676. buf = make([]byte, 100)
  1677. for i := range buf {
  1678. buf[i] = 0xff
  1679. }
  1680. checkResult(t, x.FillBytes(buf), x)
  1681. // Too small.
  1682. if byteLen > 0 {
  1683. buf = make([]byte, byteLen-1)
  1684. if !panics(func() { x.FillBytes(buf) }) {
  1685. t.Errorf("expected panic for small buffer and value %x", x)
  1686. }
  1687. }
  1688. })
  1689. }
  1690. }