diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b3917b6..97808ad 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - go: [ '1.23.2', '1.22.8', '1.21.13' ] + go: [ '1.23', '1.22', '1.21' ] uses: bytemare/workflows/.github/workflows/test-go.yml@f572ea606a74fe011e68a23c19f8d4f5daf58488 with: command: cd .github && make test diff --git a/element.go b/element.go index 7bf40d3..6b37af8 100644 --- a/element.go +++ b/element.go @@ -87,13 +87,13 @@ func (e *Element) Multiply(scalar *Scalar) *Element { return e } -// Equal returns 1 if the elements are equivalent, and 0 otherwise. -func (e *Element) Equal(element *Element) int { +// Equal returns true if the elements are equivalent, and false otherwise. +func (e *Element) Equal(element *Element) bool { if element == nil { - return 0 + return false } - return e.Element.Equal(element.Element) + return e.Element.Equal(element.Element) == 1 } // IsIdentity returns whether the Element is the point at infinity of the Group's underlying curve. diff --git a/scalar.go b/scalar.go index f92b6f5..52cf2c7 100644 --- a/scalar.go +++ b/scalar.go @@ -100,13 +100,13 @@ func (s *Scalar) Invert() *Scalar { return s } -// Equal returns 1 if the scalars are equal, and 0 otherwise. -func (s *Scalar) Equal(scalar *Scalar) int { +// Equal returns true if the elements are equivalent, and false otherwise. +func (s *Scalar) Equal(scalar *Scalar) bool { if scalar == nil { - return 0 + return false } - return s.Scalar.Equal(scalar.Scalar) + return s.Scalar.Equal(scalar.Scalar) == 1 } // LessOrEqual returns 1 if s <= scalar, and 0 otherwise. diff --git a/tests/element_test.go b/tests/element_test.go index 2ffd116..62bc48d 100644 --- a/tests/element_test.go +++ b/tests/element_test.go @@ -33,23 +33,23 @@ func testElementCopySet(t *testing.T, element, other *crypto.Element) { } // Verify whether they are equivalent - if element.Equal(other) != 1 { + if !element.Equal(other) { t.Fatalf("Expected equality") } // Verify than operations on one don't affect the other element.Add(element) - if element.Equal(other) == 1 { + if element.Equal(other) { t.Fatalf(errUnExpectedEquality) } other.Double().Double() - if element.Equal(other) == 1 { + if element.Equal(other) { t.Fatalf(errUnExpectedEquality) } // Verify setting to nil sets to identity - if element.Set(nil).Equal(other.Identity()) != 1 { + if !element.Set(nil).Equal(other.Identity()) { t.Error(errExpectedEquality) } } @@ -87,7 +87,7 @@ func TestElement_WrongInput(t *testing.T) { } } - equal := func(f func(*crypto.Element) int, arg *crypto.Element) func() { + equal := func(f func(*crypto.Element) bool, arg *crypto.Element) func() { return func() { f(arg) } @@ -254,7 +254,7 @@ func TestElement_Vectors_Add(t *testing.T) { for _, mult := range group.multBase { e := decodeElement(t, group.group, mult) - if e.Equal(acc) != 1 { + if !e.Equal(acc) { t.Fatal("expected equality") } @@ -262,11 +262,11 @@ func TestElement_Vectors_Add(t *testing.T) { } base.Add(group.group.NewElement()) - if base.Equal(group.group.Base()) != 1 { + if !base.Equal(group.group.Base()) { t.Fatal(errExpectedEquality) } - if group.group.NewElement().Add(base).Equal(base) != 1 { + if !group.group.NewElement().Add(base).Equal(base) { t.Fatal(errExpectedEquality) } }) @@ -287,7 +287,7 @@ func TestElement_Vectors_Double(t *testing.T) { e.Double() v := decodeElement(t, group.group, group.multBase[multiple-1]) - if v.Equal(e) != 1 { + if !v.Equal(e) { t.Fatalf("expected equality for %d", multiple) } } @@ -302,7 +302,7 @@ func TestElement_Vectors_Mult(t *testing.T) { for i, mult := range group.multBase { e := decodeElement(t, group.group, mult) - if e.Equal(base) != 1 { + if !e.Equal(base) { t.Fatalf("expected equality for %d", i) } @@ -328,17 +328,17 @@ func elementTestEqual(t *testing.T, g crypto.Group) { base := g.Base() base2 := g.Base() - if base.Equal(nil) != 0 { + if base.Equal(nil) { t.Fatal(errUnExpectedEquality) } - if base.Equal(base2) != 1 { + if !base.Equal(base2) { t.Fatal(errExpectedEquality) } random := g.NewElement().Multiply(g.NewScalar().Random()) cpy := random.Copy() - if random.Equal(cpy) != 1 { + if !random.Equal(cpy) { t.Fatal() } } @@ -347,7 +347,7 @@ func elementTestAdd(t *testing.T, g crypto.Group) { // Verify whether add yields the same element when given nil base := g.Base() cpy := base.Copy() - if cpy.Add(nil).Equal(base) != 1 { + if !cpy.Add(nil).Equal(base) { t.Fatal(errExpectedEquality) } @@ -355,14 +355,14 @@ func elementTestAdd(t *testing.T, g crypto.Group) { base = g.Base() cpy = base.Copy() cpy.Add(g.NewElement()) - if cpy.Equal(base) != 1 { + if !cpy.Equal(base) { t.Fatal(errExpectedEquality) } // Verify whether add yields the same when adding to identity base = g.Base() identity := g.NewElement() - if identity.Add(base).Equal(base) != 1 { + if !identity.Add(base).Equal(base) { t.Fatal(errExpectedEquality) } @@ -370,14 +370,14 @@ func elementTestAdd(t *testing.T, g crypto.Group) { base = g.Base() negative := g.Base().Negate() identity = g.NewElement() - if base.Add(negative).Equal(identity) != 1 { + if !base.Add(negative).Equal(identity) { t.Fatal(errExpectedEquality) } // Verify whether add yields the double when adding to itself base = g.Base() double := g.Base().Double() - if base.Add(base).Equal(double) != 1 { + if !base.Add(base).Equal(double) { t.Fatal(errExpectedEquality) } @@ -389,7 +389,7 @@ func elementTestAdd(t *testing.T, g crypto.Group) { mult := g.Base().Multiply(three) e := g.Base().Add(g.Base()).Add(g.Base()) - if e.Equal(mult) != 1 { + if !e.Equal(mult) { t.Fatal(errExpectedEquality) } } @@ -399,7 +399,7 @@ func elementTestNegate(t *testing.T, g crypto.Group) { id := g.NewElement().Identity() negId := g.NewElement().Identity().Negate() - if id.Equal(negId) != 1 { + if !id.Equal(negId) { t.Fatal("expected equality when negating identity element") } @@ -416,7 +416,7 @@ func elementTestNegate(t *testing.T, g crypto.Group) { b = g.NewElement().Base() negB = g.NewElement().Base().Negate().Negate() - if b.Equal(negB) != 1 { + if !b.Equal(negB) { t.Fatal("expected equality -(-b) = b") } } @@ -425,13 +425,13 @@ func elementTestDouble(t *testing.T, g crypto.Group) { // Verify whether double works like adding base := g.Base() double := g.Base().Add(g.Base()) - if double.Equal(base.Double()) != 1 { + if !double.Equal(base.Double()) { t.Fatal(errExpectedEquality) } two := g.NewScalar().One().Add(g.NewScalar().One()) mult := g.Base().Multiply(two) - if mult.Equal(double) != 1 { + if !mult.Equal(double) { t.Fatal(errExpectedEquality) } } @@ -440,13 +440,13 @@ func elementTestSubstract(t *testing.T, g crypto.Group) { base := g.Base() // Verify whether subtracting yields the same element when given nil. - if base.Subtract(nil).Equal(base) != 1 { + if !base.Subtract(nil).Equal(base) { t.Fatal(errExpectedEquality) } // Verify whether subtracting and then adding yields the same element. base2 := base.Add(base).Subtract(base) - if base.Equal(base2) != 1 { + if !base.Equal(base2) { t.Fatal(errExpectedEquality) } } @@ -457,7 +457,7 @@ func elementTestMultiply(t *testing.T, g crypto.Group) { // base = base * 1 base := g.Base() mult := g.Base().Multiply(scalar.One()) - if base.Equal(mult) != 1 { + if !base.Equal(mult) { t.Fatal(errExpectedEquality) } @@ -473,7 +473,7 @@ func elementTestMultiply(t *testing.T, g crypto.Group) { two := g.NewScalar().One().Add(g.NewScalar().One()) mult = g.Base().Multiply(two) - if mult.Equal(twoG) != 1 { + if !mult.Equal(twoG) { t.Fatal(errExpectedEquality) } @@ -495,7 +495,7 @@ func elementTestIdentity(t *testing.T, g crypto.Group) { } base := g.Base() - if id.Equal(base.Subtract(base)) != 1 { + if !id.Equal(base.Subtract(base)) { log.Printf("id : %v", id.Encode()) log.Printf("ba : %v", base.Encode()) t.Fatal(errExpectedIdentity) @@ -503,22 +503,22 @@ func elementTestIdentity(t *testing.T, g crypto.Group) { sub1 := g.Base().Double().Negate().Add(g.Base().Double()) sub2 := g.Base().Subtract(g.Base()) - if sub1.Equal(sub2) != 1 { + if !sub1.Equal(sub2) { t.Fatal(errExpectedEquality) } - if id.Equal(base.Multiply(nil)) != 1 { + if !id.Equal(base.Multiply(nil)) { t.Fatal(errExpectedIdentity) } - if id.Equal(base.Multiply(g.NewScalar().Zero())) != 1 { + if !id.Equal(base.Multiply(g.NewScalar().Zero())) { t.Fatal(errExpectedIdentity) } base = g.Base() neg := base.Copy().Negate() base.Add(neg) - if id.Equal(base) != 1 { + if !id.Equal(base) { t.Fatal(errExpectedIdentity) } } diff --git a/tests/encoding_test.go b/tests/encoding_test.go index 2499c17..5c16d3c 100644 --- a/tests/encoding_test.go +++ b/tests/encoding_test.go @@ -137,7 +137,7 @@ func testScalarEncodings(g crypto.Group, f makeEncodeTest) error { return err } - if source.Equal(receiver) != 1 { + if !source.Equal(receiver) { return errors.New(errExpectedEquality) } @@ -152,7 +152,7 @@ func testElementEncodings(g crypto.Group, f makeEncodeTest) error { return err } - if source.Equal(receiver) != 1 { + if !source.Equal(receiver) { return errors.New(errExpectedEquality) } @@ -248,7 +248,7 @@ func TestEncoding_Hex_Fails(t *testing.T) { t.Fatalf("unexpected error on valid encoding: %s", err) } - if s.Equal(scalar) != 1 { + if !s.Equal(scalar) { t.Fatal(errExpectedEquality) } @@ -258,7 +258,7 @@ func TestEncoding_Hex_Fails(t *testing.T) { t.Fatalf("unexpected error on valid encoding: %s", err) } - if e.Equal(element) != 1 { + if !e.Equal(element) { t.Fatal(errExpectedEquality) } }) diff --git a/tests/groups_test.go b/tests/groups_test.go index 92f3e60..9e83d01 100644 --- a/tests/groups_test.go +++ b/tests/groups_test.go @@ -154,7 +154,7 @@ func TestHashToScalar(t *testing.T) { sv := decodeScalar(t, group.group, group.hashToCurve.hashToScalar) s := group.group.HashToScalar(group.hashToCurve.input, group.hashToCurve.dst) - if s.Equal(sv) != 1 { + if !s.Equal(sv) { t.Error(errExpectedEquality) } }) @@ -185,7 +185,7 @@ func TestHashToGroup(t *testing.T) { ev := decodeElement(t, group.group, group.hashToCurve.hashToGroup) e := group.group.HashToGroup(group.hashToCurve.input, group.hashToCurve.dst) - if e.Equal(ev) != 1 { + if !e.Equal(ev) { t.Error(errExpectedEquality) } }) diff --git a/tests/scalar_test.go b/tests/scalar_test.go index 872415d..6b74a01 100644 --- a/tests/scalar_test.go +++ b/tests/scalar_test.go @@ -38,7 +38,7 @@ func TestScalar_WrongInput(t *testing.T) { } } - equal := func(f func(*crypto.Scalar) int, arg *crypto.Scalar) func() { + equal := func(f func(*crypto.Scalar) bool, arg *crypto.Scalar) func() { return func() { f(arg) } @@ -87,23 +87,23 @@ func testScalarCopySet(t *testing.T, scalar, other *crypto.Scalar) { } // Verify whether they are equivalent - if scalar.Equal(other) != 1 { + if !scalar.Equal(other) { t.Fatalf("Expected equality") } // Verify than operations on one don't affect the other scalar.Add(scalar) - if scalar.Equal(other) == 1 { + if scalar.Equal(other) { t.Fatalf(errUnExpectedEquality) } other.Invert() - if scalar.Equal(other) == 1 { + if scalar.Equal(other) { t.Fatalf(errUnExpectedEquality) } // Verify setting to nil sets to 0 - if scalar.Set(nil).Equal(other.Zero()) != 1 { + if !scalar.Set(nil).Equal(other.Zero()) { t.Error(errExpectedEquality) } } @@ -185,7 +185,7 @@ func TestScalar_SetUInt64(t *testing.T) { } s.SetUInt64(1) - if s.Equal(group.group.NewScalar().One()) != 1 { + if !s.Equal(group.group.NewScalar().One()) { t.Fatal("expected 1") } @@ -290,12 +290,12 @@ func scalarTestZero(t *testing.T, g crypto.Group) { } s = g.NewScalar().Random() - if s.Add(zero).Equal(s) != 1 { + if !s.Add(zero).Equal(s) { t.Fatal("expected no change in adding zero scalar") } s = g.NewScalar().Random() - if s.Add(zero).Equal(s) != 1 { + if !s.Add(zero).Equal(s) { t.Fatal("not equal") } } @@ -303,14 +303,14 @@ func scalarTestZero(t *testing.T, g crypto.Group) { func scalarTestOne(t *testing.T, g crypto.Group) { one := g.NewScalar().One() m := one.Copy() - if one.Equal(m.Multiply(m)) != 1 { + if !one.Equal(m.Multiply(m)) { t.Fatal(errExpectedEquality) } } func scalarTestRandom(t *testing.T, g crypto.Group) { r := g.NewScalar().Random() - if r.Equal(g.NewScalar().Zero()) == 1 { + if r.Equal(g.NewScalar().Zero()) { t.Fatalf("random scalar is zero: %v", r.Hex()) } } @@ -319,22 +319,22 @@ func scalarTestEqual(t *testing.T, g crypto.Group) { zero := g.NewScalar().Zero() zero2 := g.NewScalar().Zero() - if g.NewScalar().Random().Equal(nil) != 0 { + if g.NewScalar().Random().Equal(nil) { t.Fatal(errUnExpectedEquality) } - if zero.Equal(zero2) != 1 { + if !zero.Equal(zero2) { t.Fatal(errExpectedEquality) } random := g.NewScalar().Random() cpy := random.Copy() - if random.Equal(cpy) != 1 { + if !random.Equal(cpy) { t.Fatal(errExpectedEquality) } random2 := g.NewScalar().Random() - if random.Equal(random2) == 1 { + if random.Equal(random2) { t.Fatal(errUnExpectedEquality) } } @@ -385,7 +385,7 @@ func scalarTestLessOrEqual(t *testing.T, g crypto.Group) { func scalarTestAdd(t *testing.T, g crypto.Group) { r := g.NewScalar().Random() cpy := r.Copy() - if r.Add(nil).Equal(cpy) != 1 { + if !r.Add(nil).Equal(cpy) { t.Fatal(errExpectedEquality) } } @@ -393,7 +393,7 @@ func scalarTestAdd(t *testing.T, g crypto.Group) { func scalarTestSubtract(t *testing.T, g crypto.Group) { r := g.NewScalar().Random() cpy := r.Copy() - if r.Subtract(nil).Equal(cpy) != 1 { + if !r.Subtract(nil).Equal(cpy) { t.Fatal(errExpectedEquality) } } @@ -408,21 +408,21 @@ func scalarTestMultiply(t *testing.T, g crypto.Group) { func scalarTestPow(t *testing.T, g crypto.Group) { // s**nil = 1 s := g.NewScalar().Random() - if s.Pow(nil).Equal(g.NewScalar().One()) != 1 { + if !s.Pow(nil).Equal(g.NewScalar().One()) { t.Fatal("expected s**nil = 1") } // s**0 = 1 s = g.NewScalar().Random() zero := g.NewScalar().Zero() - if s.Pow(zero).Equal(g.NewScalar().One()) != 1 { + if !s.Pow(zero).Equal(g.NewScalar().One()) { t.Fatal("expected s**0 = 1") } // s**1 = s s = g.NewScalar().Random() exp := g.NewScalar().One() - if s.Copy().Pow(exp).Equal(s) != 1 { + if !s.Copy().Pow(exp).Equal(s) { t.Fatal("expected s**1 = s") } @@ -432,7 +432,7 @@ func scalarTestPow(t *testing.T, g crypto.Group) { s2 := s.Copy().Multiply(s) exp.SetUInt64(2) - if s.Pow(exp).Equal(s2) != 1 { + if !s.Pow(exp).Equal(s2) { t.Fatal("expected s**2 = s*s") } @@ -442,7 +442,7 @@ func scalarTestPow(t *testing.T, g crypto.Group) { s3.Multiply(s) exp.SetUInt64(3) - if s.Pow(exp).Equal(s3) != 1 { + if !s.Pow(exp).Equal(s3) { t.Fatal("expected s**3 = s*s*s") } @@ -452,7 +452,7 @@ func scalarTestPow(t *testing.T, g crypto.Group) { exp.SetUInt64(7) res := s.Pow(exp) - if res.Equal(result) != 1 { + if !res.Equal(result) { t.Fatal("expected 5**7 = 78125") } @@ -465,7 +465,7 @@ func scalarTestPow(t *testing.T, g crypto.Group) { exp.SetUInt64(255) res = s.Pow(exp) - if res.Equal(result) != 1 { + if !res.Equal(result) { t.Fatal( "expected 3**255 = " + "11F1B08E87EC42C5D83C3218FC83C41DCFD9F4428F4F92AF1AAA80AA46162B1F71E981273601F4AD1DD4709B5ACA650265A6AB", @@ -481,7 +481,7 @@ func scalarTestPow(t *testing.T, g crypto.Group) { exp.SetUInt64(513) res = s.Pow(exp) - if res.Equal(result) != 1 { + if !res.Equal(result) { t.Fatal("expect equality on 7945232487465**513") } @@ -513,7 +513,7 @@ func scalarTestPow(t *testing.T, g crypto.Group) { result = bigIntExp(t, g, iBase, iExp) - if s.Pow(exp).Equal(result) != 1 { + if !s.Pow(exp).Equal(result) { t.Fatal("expected equality on random numbers") } } @@ -546,14 +546,14 @@ func scalarTestInvert(t *testing.T, g crypto.Group) { sqr := s.Copy().Multiply(s) i := s.Copy().Invert().Multiply(sqr) - if i.Equal(s) != 1 { + if !i.Equal(s) { t.Fatal(errExpectedEquality) } s = g.NewScalar().Random() square := s.Copy().Multiply(s) inv := square.Copy().Invert() - if s.One().Equal(square.Multiply(inv)) != 1 { + if !s.One().Equal(square.Multiply(inv)) { t.Fatal(errExpectedEquality) } }