diff --git a/ecc/bls12-377/twistededwards/point.go b/ecc/bls12-377/twistededwards/point.go index 9ec9e07416..da542968bb 100644 --- a/ecc/bls12-377/twistededwards/point.go +++ b/ecc/bls12-377/twistededwards/point.go @@ -182,8 +182,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -313,8 +313,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -470,9 +471,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bls12-377/twistededwards/point_test.go b/ecc/bls12-377/twistededwards/point_test.go index 950ea15ac7..e165093e40 100644 --- a/ecc/bls12-377/twistededwards/point_test.go +++ b/ecc/bls12-377/twistededwards/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/ecc/bls12-378/twistededwards/point.go b/ecc/bls12-378/twistededwards/point.go index 7db08b4cb8..7027d605db 100644 --- a/ecc/bls12-378/twistededwards/point.go +++ b/ecc/bls12-378/twistededwards/point.go @@ -182,8 +182,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -313,8 +313,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -470,9 +471,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bls12-378/twistededwards/point_test.go b/ecc/bls12-378/twistededwards/point_test.go index b3baa3e279..3316bba913 100644 --- a/ecc/bls12-378/twistededwards/point_test.go +++ b/ecc/bls12-378/twistededwards/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/ecc/bls12-381/bandersnatch/point.go b/ecc/bls12-381/bandersnatch/point.go index 3c2db78161..d0b98b3c35 100644 --- a/ecc/bls12-381/bandersnatch/point.go +++ b/ecc/bls12-381/bandersnatch/point.go @@ -181,8 +181,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -312,8 +312,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -445,9 +446,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bls12-381/bandersnatch/point_test.go b/ecc/bls12-381/bandersnatch/point_test.go index d8b8fbc9ca..07281c8aa9 100644 --- a/ecc/bls12-381/bandersnatch/point_test.go +++ b/ecc/bls12-381/bandersnatch/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/ecc/bls12-381/twistededwards/point.go b/ecc/bls12-381/twistededwards/point.go index ec42a001a2..eb08d0b057 100644 --- a/ecc/bls12-381/twistededwards/point.go +++ b/ecc/bls12-381/twistededwards/point.go @@ -182,8 +182,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -313,8 +313,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -470,9 +471,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bls12-381/twistededwards/point_test.go b/ecc/bls12-381/twistededwards/point_test.go index 0d5bf5cb0f..35d2ef0e9c 100644 --- a/ecc/bls12-381/twistededwards/point_test.go +++ b/ecc/bls12-381/twistededwards/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/ecc/bls24-315/twistededwards/point.go b/ecc/bls24-315/twistededwards/point.go index a460738f3b..edf4524f89 100644 --- a/ecc/bls24-315/twistededwards/point.go +++ b/ecc/bls24-315/twistededwards/point.go @@ -182,8 +182,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -313,8 +313,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -470,9 +471,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bls24-315/twistededwards/point_test.go b/ecc/bls24-315/twistededwards/point_test.go index 6c96a37f9f..4ce82c1884 100644 --- a/ecc/bls24-315/twistededwards/point_test.go +++ b/ecc/bls24-315/twistededwards/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/ecc/bls24-317/twistededwards/point.go b/ecc/bls24-317/twistededwards/point.go index 0193e627bb..42e5f32423 100644 --- a/ecc/bls24-317/twistededwards/point.go +++ b/ecc/bls24-317/twistededwards/point.go @@ -182,8 +182,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -313,8 +313,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -470,9 +471,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bls24-317/twistededwards/point_test.go b/ecc/bls24-317/twistededwards/point_test.go index 2da327d286..b11d1792d8 100644 --- a/ecc/bls24-317/twistededwards/point_test.go +++ b/ecc/bls24-317/twistededwards/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/ecc/bn254/twistededwards/point.go b/ecc/bn254/twistededwards/point.go index 09303663c3..a741c3eeb7 100644 --- a/ecc/bn254/twistededwards/point.go +++ b/ecc/bn254/twistededwards/point.go @@ -182,8 +182,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -313,8 +313,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -470,9 +471,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bn254/twistededwards/point_test.go b/ecc/bn254/twistededwards/point_test.go index c8434cff61..59ccb3e894 100644 --- a/ecc/bn254/twistededwards/point_test.go +++ b/ecc/bn254/twistededwards/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/ecc/bw6-633/twistededwards/point.go b/ecc/bw6-633/twistededwards/point.go index 45c401d155..c263ac338c 100644 --- a/ecc/bw6-633/twistededwards/point.go +++ b/ecc/bw6-633/twistededwards/point.go @@ -182,8 +182,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -313,8 +313,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -470,9 +471,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bw6-633/twistededwards/point_test.go b/ecc/bw6-633/twistededwards/point_test.go index 0ffa874543..dd488885f7 100644 --- a/ecc/bw6-633/twistededwards/point_test.go +++ b/ecc/bw6-633/twistededwards/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/ecc/bw6-756/twistededwards/point.go b/ecc/bw6-756/twistededwards/point.go index a0454e8b97..62b707cc0c 100644 --- a/ecc/bw6-756/twistededwards/point.go +++ b/ecc/bw6-756/twistededwards/point.go @@ -182,8 +182,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -313,8 +313,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -470,9 +471,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bw6-756/twistededwards/point_test.go b/ecc/bw6-756/twistededwards/point_test.go index fc5347fb00..b853e78c12 100644 --- a/ecc/bw6-756/twistededwards/point_test.go +++ b/ecc/bw6-756/twistededwards/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/ecc/bw6-761/twistededwards/point.go b/ecc/bw6-761/twistededwards/point.go index 5a494f8289..a653ef0fc0 100644 --- a/ecc/bw6-761/twistededwards/point.go +++ b/ecc/bw6-761/twistededwards/point.go @@ -182,8 +182,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -313,8 +313,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -470,9 +471,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/ecc/bw6-761/twistededwards/point_test.go b/ecc/bw6-761/twistededwards/point_test.go index 56225dc1bd..e3cf90aa62 100644 --- a/ecc/bw6-761/twistededwards/point_test.go +++ b/ecc/bw6-761/twistededwards/point_test.go @@ -779,3 +779,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} diff --git a/internal/generator/edwards/template/point.go.tmpl b/internal/generator/edwards/template/point.go.tmpl index 7c2249d532..3eb72370bc 100644 --- a/internal/generator/edwards/template/point.go.tmpl +++ b/internal/generator/edwards/template/point.go.tmpl @@ -166,8 +166,8 @@ func (p *PointAffine) IsOnCurve() bool { // Neg sets p to -p1 and returns it func (p *PointAffine) Neg(p1 *PointAffine) *PointAffine { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y return p } @@ -297,8 +297,9 @@ func (p *PointProj) IsZero() bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointProj) Neg(p1 *PointProj) *PointProj { - p.Set(p1) - p.X.Neg(&p.X) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z return p } @@ -458,9 +459,10 @@ func (p *PointExtended) Equal(p1 *PointExtended) bool { // Neg negates point (x,y) on a twisted Edwards curve with parameters a, d // modifies p func (p *PointExtended) Neg(p1 *PointExtended) *PointExtended { - p.Set(p1) - p.X.Neg(&p.X) - p.T.Neg(&p.T) + p.X.Neg(&p1.X) + p.Y = p1.Y + p.Z = p1.Z + p.T.Neg(&p1.T) return p } diff --git a/internal/generator/edwards/template/tests/point.go.tmpl b/internal/generator/edwards/template/tests/point.go.tmpl index 650dd93d4a..6f56dd25db 100644 --- a/internal/generator/edwards/template/tests/point.go.tmpl +++ b/internal/generator/edwards/template/tests/point.go.tmpl @@ -763,3 +763,41 @@ func BenchmarkScalarMulProjective(b *testing.B) { doubleAndAdd.ScalarMultiplication(&a, &s) } } + +func BenchmarkNeg(b *testing.B) { + params := GetEdwardsCurve() + var s big.Int + s.SetString("52435875175126190479447705081859658376581184513", 10) + + b.Run("Affine", func(b *testing.B) { + var point PointAffine + point.ScalarMultiplication(¶ms.Base, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Projective", func(b *testing.B) { + var baseProj PointProj + baseProj.FromAffine(¶ms.Base) + var point PointProj + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) + b.Run("Extended", func(b *testing.B) { + var baseProj PointExtended + baseProj.FromAffine(¶ms.Base) + var point PointExtended + point.ScalarMultiplication(&baseProj, &s) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + point.Neg(&point) + } + }) +} \ No newline at end of file