MCPcopy Create free account
hub / github.com/cockroachdb/apd / testModSqrt

Function testModSqrt

bigint_test.go:2084–2118  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

2082// testModSqrt is a helper for TestModSqrt,
2083// which checks that ModSqrt can compute a square-root of elt^2.
2084func 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
2120var primes = []string{
2121 "2",

Callers 1

TestBigIntModSqrtFunction · 0.85

Calls 7

MulMethod · 0.95
ModMethod · 0.95
ModSqrtMethod · 0.95
AddMethod · 0.95
SubMethod · 0.95
SetMethod · 0.95
CmpMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…