From 7ae6271616dd630bad95bf07e47b665aa0a635a9 Mon Sep 17 00:00:00 2001 From: George Tankersley Date: Sun, 25 Jun 2017 20:20:59 -0400 Subject: [PATCH] add equality for field elements --- internal/radix51/const.go | 5 +++++ internal/radix51/fe.go | 8 ++++++++ internal/radix51/fe_test.go | 15 +++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/internal/radix51/const.go b/internal/radix51/const.go index 9b3f6b2..ce3f2ff 100644 --- a/internal/radix51/const.go +++ b/internal/radix51/const.go @@ -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} +) diff --git a/internal/radix51/fe.go b/internal/radix51/fe.go index 09ebc86..ff7e5e1 100644 --- a/internal/radix51/fe.go +++ b/internal/radix51/fe.go @@ -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 diff --git a/internal/radix51/fe_test.go b/internal/radix51/fe_test.go index 217d910..ba6f4c7 100644 --- a/internal/radix51/fe_test.go +++ b/internal/radix51/fe_test.go @@ -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}