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
|
//go:build (!purego && arm64) || (!purego && amd64)
// +build !purego,arm64 !purego,amd64
package p384
import (
"crypto/elliptic"
"crypto/rand"
"math/big"
"testing"
"github.com/cloudflare/circl/internal/test"
)
func TestFpCmov(t *testing.T) {
var x, y, z fp384
for _, b := range []int{-2, -1, 1, 2} {
_, _ = rand.Read(x[:])
_, _ = rand.Read(y[:])
z = x
fp384Cmov(&z, &y, b)
got := z
want := y
if got != want {
test.ReportError(t, got, want, b, x, y)
}
}
_, _ = rand.Read(x[:])
_, _ = rand.Read(y[:])
z = x
fp384Cmov(&z, &y, 0)
got := z
want := x
if got != want {
test.ReportError(t, got, want, 0, x, y)
}
}
func TestFpNegZero(t *testing.T) {
zero, x := &fp384{}, &fp384{}
fp384Neg(x, zero)
got := x.BigInt()
want := zero.BigInt()
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x)
}
}
func TestFpSetBigInt(t *testing.T) {
P := elliptic.P384().Params().P
neg := big.NewInt(-0xFF) // negative
zero := big.NewInt(0) // zero
one := big.NewInt(1) // one
two96 := new(big.Int).Lsh(one, 96) // 2^96
two384 := new(big.Int).Lsh(one, 384) // 2^384
two384two96 := new(big.Int).Sub(two384, two96) // 2^384-2^96
two768 := new(big.Int).Lsh(one, 768) // 2^768
for id, b := range []*big.Int{
neg, zero, one, two96, two384, two384two96, two768,
} {
var x fp384
x.SetBigInt(b)
got := x.BigInt()
if b.BitLen() > 384 || b.Sign() < 0 {
b.Mod(b, P)
}
want := b
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, id)
}
}
}
func TestMulZero(t *testing.T) {
x, zero := &fp384{}, &fp384{}
_, _ = rand.Read(x[:])
fp384Mul(x, x, zero)
got := x.BigInt()
want := zero.BigInt()
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x)
}
}
func TestFp(t *testing.T) {
P := elliptic.P384().Params().P
x, y, z := &fp384{}, &fp384{}, &fp384{}
testTimes := 1 << 12
var bigR, bigR2, bigRinv big.Int
one := big.NewInt(1)
bigR.Lsh(one, 384).Mod(&bigR, P)
bigR2.Lsh(one, 2*384).Mod(&bigR2, P)
bigRinv.ModInverse(&bigR, P)
t.Run("Encode", func(t *testing.T) {
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(x[:])
bigX := x.BigInt()
// fp384
montEncode(z, x)
got := z.BigInt()
// big.Int
want := bigX.Mul(bigX, &bigR).Mod(bigX, P)
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x)
}
}
})
t.Run("Decode", func(t *testing.T) {
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(x[:])
bigX := x.BigInt()
// fp384
montDecode(z, x)
got := z.BigInt()
// big.Int
want := bigX.Mul(bigX, new(big.Int).ModInverse(&bigR, P)).Mod(bigX, P)
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x)
}
}
})
t.Run("Neg", func(t *testing.T) {
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(x[:])
bigX := x.BigInt()
// fp384
fp384Neg(z, x)
got := z.BigInt()
// big.Int
want := bigX.Neg(bigX).Mod(bigX, P)
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x)
}
}
})
t.Run("Add", func(t *testing.T) {
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(x[:])
_, _ = rand.Read(y[:])
bigX := x.BigInt()
bigY := y.BigInt()
// fp384
fp384Add(z, x, y)
got := z.BigInt()
// big.Int
want := bigX.Add(bigX, bigY)
want = want.Mod(want, P)
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x, y)
}
}
})
t.Run("Sub", func(t *testing.T) {
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(x[:])
_, _ = rand.Read(y[:])
bigX := x.BigInt()
bigY := y.BigInt()
// fp384
fp384Sub(z, x, y)
got := z.BigInt()
// big.Int
want := bigX.Sub(bigX, bigY)
want = want.Mod(want, P)
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x, y)
}
}
})
t.Run("Mul", func(t *testing.T) {
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(x[:])
_, _ = rand.Read(y[:])
bigX := x.BigInt()
bigY := y.BigInt()
// fp384
fp384Mul(z, x, y)
got := z.BigInt()
// big.Int
want := bigX.Mul(bigX, bigY).Mul(bigX, &bigRinv).Mod(bigX, P)
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x, y)
}
}
})
t.Run("Inv", func(t *testing.T) {
for i := 0; i < testTimes; i++ {
_, _ = rand.Read(x[:])
bigX := x.BigInt()
// fp384
fp384Inv(z, x)
got := z.BigInt()
// big.Int
want := bigX.ModInverse(bigX, P).Mul(bigX, &bigR2).Mod(bigX, P)
if got.Cmp(want) != 0 {
test.ReportError(t, got, want, x)
}
}
})
}
func BenchmarkFp(b *testing.B) {
x, y, z := &fp384{}, &fp384{}, &fp384{}
b.Run("Add", func(b *testing.B) {
for i := 0; i < b.N; i++ {
fp384Add(z, x, y)
}
})
b.Run("Sub", func(b *testing.B) {
for i := 0; i < b.N; i++ {
fp384Sub(z, x, y)
}
})
b.Run("Mul", func(b *testing.B) {
for i := 0; i < b.N; i++ {
fp384Mul(z, x, y)
}
})
b.Run("Sqr", func(b *testing.B) {
for i := 0; i < b.N; i++ {
fp384Sqr(z, x)
}
})
b.Run("Inv", func(b *testing.B) {
for i := 0; i < b.N; i++ {
fp384Inv(z, x)
}
})
}
|