1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
package p503
import (
. "github.com/cloudflare/sidh/internal/isogeny"
"math/big"
"testing"
"testing/quick"
)
//------------------------------------------------------------------------------
// Extended Field
//------------------------------------------------------------------------------
func TestOneFp2ToBytes(t *testing.T) {
var x = P503_OneFp2
var xBytes [2 * P503_Bytelen]byte
kCurveOps.Fp2ToBytes(xBytes[:], &x)
if xBytes[0] != 1 {
t.Error("Expected 1, got", xBytes[0])
}
for i := 1; i < 2*P503_Bytelen; i++ {
if xBytes[i] != 0 {
t.Error("Expected 0, got", xBytes[0])
}
}
}
func TestFp2ElementToBytesRoundTrip(t *testing.T) {
roundTrips := func(x GeneratedTestParams) bool {
var xBytes [2 * P503_Bytelen]byte
var xPrime Fp2Element
kCurveOps.Fp2ToBytes(xBytes[:], &x.ExtElem)
kCurveOps.Fp2FromBytes(&xPrime, xBytes[:])
return VartimeEqFp2(&xPrime, &x.ExtElem)
}
if err := quick.Check(roundTrips, quickCheckConfig); err != nil {
t.Error(err)
}
}
func TestFp2ElementMulDistributesOverAdd(t *testing.T) {
mulDistributesOverAdd := func(x, y, z GeneratedTestParams) bool {
// Compute t1 = (x+y)*z
t1 := new(Fp2Element)
kFieldOps.Add(t1, &x.ExtElem, &y.ExtElem)
kFieldOps.Mul(t1, t1, &z.ExtElem)
// Compute t2 = x*z + y*z
t2 := new(Fp2Element)
t3 := new(Fp2Element)
kFieldOps.Mul(t2, &x.ExtElem, &z.ExtElem)
kFieldOps.Mul(t3, &y.ExtElem, &z.ExtElem)
kFieldOps.Add(t2, t2, t3)
return VartimeEqFp2(t1, t2)
}
if err := quick.Check(mulDistributesOverAdd, quickCheckConfig); err != nil {
t.Error(err)
}
}
func TestFp2ElementMulIsAssociative(t *testing.T) {
isAssociative := func(x, y, z GeneratedTestParams) bool {
// Compute t1 = (x*y)*z
t1 := new(Fp2Element)
kFieldOps.Mul(t1, &x.ExtElem, &y.ExtElem)
kFieldOps.Mul(t1, t1, &z.ExtElem)
// Compute t2 = (y*z)*x
t2 := new(Fp2Element)
kFieldOps.Mul(t2, &y.ExtElem, &z.ExtElem)
kFieldOps.Mul(t2, t2, &x.ExtElem)
return VartimeEqFp2(t1, t2)
}
if err := quick.Check(isAssociative, quickCheckConfig); err != nil {
t.Error(err)
}
}
func TestFp2ElementSquareMatchesMul(t *testing.T) {
sqrMatchesMul := func(x GeneratedTestParams) bool {
// Compute t1 = (x*x)
t1 := new(Fp2Element)
kFieldOps.Mul(t1, &x.ExtElem, &x.ExtElem)
// Compute t2 = x^2
t2 := new(Fp2Element)
kFieldOps.Square(t2, &x.ExtElem)
return VartimeEqFp2(t1, t2)
}
if err := quick.Check(sqrMatchesMul, quickCheckConfig); err != nil {
t.Error(err)
}
}
func TestFp2ElementInv(t *testing.T) {
inverseIsCorrect := func(x GeneratedTestParams) bool {
z := new(Fp2Element)
kFieldOps.Inv(z, &x.ExtElem)
// Now z = (1/x), so (z * x) * x == x
kFieldOps.Mul(z, z, &x.ExtElem)
kFieldOps.Mul(z, z, &x.ExtElem)
return VartimeEqFp2(z, &x.ExtElem)
}
// This is more expensive; run fewer tests
var quickCheckConfig = &quick.Config{MaxCount: (1 << (8 + quickCheckScaleFactor))}
if err := quick.Check(inverseIsCorrect, quickCheckConfig); err != nil {
t.Error(err)
}
}
func TestFp2ElementBatch3Inv(t *testing.T) {
batchInverseIsCorrect := func(x1, x2, x3 GeneratedTestParams) bool {
var x1Inv, x2Inv, x3Inv Fp2Element
kFieldOps.Inv(&x1Inv, &x1.ExtElem)
kFieldOps.Inv(&x2Inv, &x2.ExtElem)
kFieldOps.Inv(&x3Inv, &x3.ExtElem)
var y1, y2, y3 Fp2Element
kCurveOps.Fp2Batch3Inv(&x1.ExtElem, &x2.ExtElem, &x3.ExtElem, &y1, &y2, &y3)
return (VartimeEqFp2(&x1Inv, &y1) && VartimeEqFp2(&x2Inv, &y2) && VartimeEqFp2(&x3Inv, &y3))
}
// This is more expensive; run fewer tests
var quickCheckConfig = &quick.Config{MaxCount: (1 << (5 + quickCheckScaleFactor))}
if err := quick.Check(batchInverseIsCorrect, quickCheckConfig); err != nil {
t.Error(err)
}
}
//------------------------------------------------------------------------------
// Prime Field
//------------------------------------------------------------------------------
func TestPrimeFieldElementMulVersusBigInt(t *testing.T) {
mulMatchesBigInt := func(x, y primeFieldElement) bool {
z := new(primeFieldElement)
z.Mul(&x, &y)
check := new(big.Int)
check.Mul(toBigInt(&x.A), toBigInt(&y.A))
check.Mod(check, p503BigIntPrime)
return check.Cmp(toBigInt(&z.A)) == 0
}
if err := quick.Check(mulMatchesBigInt, quickCheckConfig); err != nil {
t.Error(err)
}
}
func TestPrimeFieldElementP34VersusBigInt(t *testing.T) {
var p34, _ = new(big.Int).SetString("3293960789226779345209813229049836260623046691894590999611415869258960983005190308379728727886506087902151787597521914245745576582754898490288559357951", 10)
p34MatchesBigInt := func(x primeFieldElement) bool {
z := new(primeFieldElement)
z.P34(&x)
check := toBigInt(&x.A)
check.Exp(check, p34, p503BigIntPrime)
return check.Cmp(toBigInt(&z.A)) == 0
}
// This is more expensive; run fewer tests
var quickCheckConfig = &quick.Config{MaxCount: (1 << (8 + quickCheckScaleFactor))}
if err := quick.Check(p34MatchesBigInt, quickCheckConfig); err != nil {
t.Error(err)
}
}
func TestPrimeFieldElementToBigInt(t *testing.T) {
// Chosen so that p < xR < 2p
x := primeFieldElement{A: FpElement{
1, 1, 1, 1, 1, 1, 1, 36028797018963968,
},
}
// Computed using Sage:
// sage: p = 2^e2 * 3^e3 - 1
// sage: R = 2^512
// sage: from_radix_64 = lambda xs: sum((xi * (2**64)**i for i,xi in enumerate(xs)))
// sage: xR = from_radix_64([1]*7 + [2^55])
// sage: assert(p < xR)
// sage: assert(xR < 2*p)
// sage: (xR / R) % p
xBig, _ := new(big.Int).SetString("9018685569593152305590037326062904046918870374552508285127709347526265324701162612011653377441752634975109935373869185819144129719824212073345315986301", 10)
if xBig.Cmp(toBigInt(&x.A)) != 0 {
t.Error("Expected", xBig, "found", toBigInt(&x.A))
}
}
func TestFpElementConditionalSwap(t *testing.T) {
var one = FpElement{1, 1, 1, 1, 1, 1, 1, 1}
var two = FpElement{2, 2, 2, 2, 2, 2, 2, 2}
var x = one
var y = two
fp503ConditionalSwap(&x, &y, 0)
if !(x == one && y == two) {
t.Error("Found", x, "expected", one)
}
fp503ConditionalSwap(&x, &y, 1)
if !(x == two && y == one) {
t.Error("Found", x, "expected", two)
}
}
func BenchmarkFp2ElementMul(b *testing.B) {
z := &Fp2Element{A: bench_x, B: bench_y}
w := new(Fp2Element)
for n := 0; n < b.N; n++ {
kFieldOps.Mul(w, z, z)
}
}
func BenchmarkFp2ElementInv(b *testing.B) {
z := &Fp2Element{A: bench_x, B: bench_y}
w := new(Fp2Element)
for n := 0; n < b.N; n++ {
kFieldOps.Inv(w, z)
}
}
func BenchmarkFp2ElementSquare(b *testing.B) {
z := &Fp2Element{A: bench_x, B: bench_y}
w := new(Fp2Element)
for n := 0; n < b.N; n++ {
kFieldOps.Square(w, z)
}
}
func BenchmarkFp2ElementAdd(b *testing.B) {
z := &Fp2Element{A: bench_x, B: bench_y}
w := new(Fp2Element)
for n := 0; n < b.N; n++ {
kFieldOps.Add(w, z, z)
}
}
func BenchmarkFp2ElementSub(b *testing.B) {
z := &Fp2Element{A: bench_x, B: bench_y}
w := new(Fp2Element)
for n := 0; n < b.N; n++ {
kFieldOps.Sub(w, z, z)
}
}
func BenchmarkPrimeFieldElementMul(b *testing.B) {
z := &primeFieldElement{A: bench_x}
w := new(primeFieldElement)
for n := 0; n < b.N; n++ {
w.Mul(z, z)
}
}
// --- field operation functions
func BenchmarkFp503Multiply(b *testing.B) {
for n := 0; n < b.N; n++ {
fp503Mul(&benchmarkFpElementX2, &bench_x, &bench_y)
}
}
func BenchmarkFp503MontgomeryReduce(b *testing.B) {
z := bench_z
// This benchmark actually computes garbage, because
// fp503MontgomeryReduce mangles its input, but since it's
// constant-time that shouldn't matter for the benchmarks.
for n := 0; n < b.N; n++ {
fp503MontgomeryReduce(&benchmarkFpElement, &z)
}
}
func BenchmarkFp503AddReduced(b *testing.B) {
for n := 0; n < b.N; n++ {
fp503AddReduced(&benchmarkFpElement, &bench_x, &bench_y)
}
}
func BenchmarkFp503SubReduced(b *testing.B) {
for n := 0; n < b.N; n++ {
fp503SubReduced(&benchmarkFpElement, &bench_x, &bench_y)
}
}
func BenchmarkFp503ConditionalSwap(b *testing.B) {
x, y := bench_x, bench_y
for n := 0; n < b.N; n++ {
fp503ConditionalSwap(&x, &y, 1)
fp503ConditionalSwap(&x, &y, 0)
}
}
func BenchmarkFp503StrongReduce(b *testing.B) {
x := bench_x
for n := 0; n < b.N; n++ {
fp503StrongReduce(&x)
}
}
func BenchmarkFp503AddLazy(b *testing.B) {
var z FpElement
x, y := bench_x, bench_y
for n := 0; n < b.N; n++ {
fp503AddLazy(&z, &x, &y)
}
}
func BenchmarkFp503X2AddLazy(b *testing.B) {
x, y, z := bench_z, bench_z, bench_z
for n := 0; n < b.N; n++ {
fp503X2AddLazy(&x, &y, &z)
}
}
func BenchmarkFp503X2SubLazy(b *testing.B) {
x, y, z := bench_z, bench_z, bench_z
for n := 0; n < b.N; n++ {
fp503X2SubLazy(&x, &y, &z)
}
}
|