add equality for field elements

This commit is contained in:
George Tankersley 2017-06-25 20:20:59 -04:00
parent ca2029ab22
commit 7ae6271616
3 changed files with 28 additions and 0 deletions

View File

@ -7,3 +7,8 @@ const (
// The vaule 2^51-1, used in carry propagation
maskLow51Bits = uint64(1)<<51 - 1
)
var (
FieldZero FieldElement = [5]uint64{0, 0, 0, 0, 0}
FieldOne FieldElement = [5]uint64{1, 0, 0, 0, 0}
)

View File

@ -210,6 +210,14 @@ func FeCSwap(a, b *FieldElement, c uint64) {
b[4] ^= t[4]
}
func FeEqual(a, b *FieldElement) uint64 {
var result uint64
for i := 0; i < 5; i++ {
result |= a[i] ^ b[i]
}
return (result ^ 0)
}
func FeFromBytes(v *FieldElement, x *[32]byte) {
v[0] = uint64(x[0])
v[0] |= uint64(x[1]) << 8

View File

@ -144,6 +144,21 @@ func vartimeEqual(x, y FieldElement) bool {
return true
}
func TestFeEqual(t *testing.T) {
var x FieldElement = [5]uint64{1, 1, 1, 1, 1}
var y FieldElement = [5]uint64{5, 4, 3, 2, 1}
eq := FeEqual(&x, &x)
if eq != 0 {
t.Errorf("wrong about equality")
}
eq = FeEqual(&x, &y)
if eq == 0 {
t.Errorf("wrong about inequality")
}
}
func TestFeInvert(t *testing.T) {
var x FieldElement = [5]uint64{1, 1, 1, 1, 1}
var one FieldElement = [5]uint64{1, 0, 0, 0, 0}