testModSqrt is a helper for TestModSqrt, which checks that ModSqrt can compute a square-root of elt^2.
(t *testing.T, elt, mod, sq, sqrt *BigInt)
| 2082 | // testModSqrt is a helper for TestModSqrt, |
| 2083 | // which checks that ModSqrt can compute a square-root of elt^2. |
| 2084 | func testModSqrt(t *testing.T, elt, mod, sq, sqrt *BigInt) bool { |
| 2085 | var sqChk, sqrtChk, sqrtsq BigInt |
| 2086 | sq.Mul(elt, elt) |
| 2087 | sq.Mod(sq, mod) |
| 2088 | z := sqrt.ModSqrt(sq, mod) |
| 2089 | if z != sqrt { |
| 2090 | t.Errorf("ModSqrt returned wrong value %s", z) |
| 2091 | } |
| 2092 | |
| 2093 | // test ModSqrt arguments outside the range [0,mod) |
| 2094 | sqChk.Add(sq, mod) |
| 2095 | z = sqrtChk.ModSqrt(&sqChk, mod) |
| 2096 | if z != &sqrtChk || z.Cmp(sqrt) != 0 { |
| 2097 | t.Errorf("ModSqrt returned inconsistent value %s", z) |
| 2098 | } |
| 2099 | sqChk.Sub(sq, mod) |
| 2100 | z = sqrtChk.ModSqrt(&sqChk, mod) |
| 2101 | if z != &sqrtChk || z.Cmp(sqrt) != 0 { |
| 2102 | t.Errorf("ModSqrt returned inconsistent value %s", z) |
| 2103 | } |
| 2104 | |
| 2105 | // test x aliasing z |
| 2106 | z = sqrtChk.ModSqrt(sqrtChk.Set(sq), mod) |
| 2107 | if z != &sqrtChk || z.Cmp(sqrt) != 0 { |
| 2108 | t.Errorf("ModSqrt returned inconsistent value %s", z) |
| 2109 | } |
| 2110 | |
| 2111 | // make sure we actually got a square root |
| 2112 | if sqrt.Cmp(elt) == 0 { |
| 2113 | return true // we found the "desired" square root |
| 2114 | } |
| 2115 | sqrtsq.Mul(sqrt, sqrt) // make sure we found the "other" one |
| 2116 | sqrtsq.Mod(&sqrtsq, mod) |
| 2117 | return sq.Cmp(&sqrtsq) == 0 |
| 2118 | } |
| 2119 | |
| 2120 | var primes = []string{ |
| 2121 | "2", |