Skip to content

Commit

Permalink
Fix affine to projective zero point bug (#552)
Browse files Browse the repository at this point in the history
## Describe the changes

This PR fixes affine to projective functions in bindings by adding a
condition if the point in affine form is zero then return the projective zero

---------

Co-authored-by: Jeremy Felder <jeremy.felder1@gmail.com>
  • Loading branch information
vladfdp and jeremyfelder authored Jul 4, 2024
1 parent 3108346 commit a4b1eb3
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 31 deletions.
15 changes: 12 additions & 3 deletions wrappers/golang/core/internal/mock_curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ func (p *MockProjective) FromAffine(a MockAffine) MockProjective {
z := MockBaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -68,6 +72,11 @@ func (a *MockAffine) FromLimbs(x, y []uint32) MockAffine {
func (a MockAffine) ToProjective() MockProjective {
var z MockBaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p MockProjective
return p.Zero()
}

return MockProjective{
X: a.X,
Y: a.Y,
Expand Down
15 changes: 12 additions & 3 deletions wrappers/golang/curves/bls12377/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *Projective) FromAffine(a Affine) Projective {
z := BaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
func (a Affine) ToProjective() Projective {
var z BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p Projective
return p.Zero()
}

return Projective{
X: a.X,
Y: a.Y,
Expand Down
15 changes: 12 additions & 3 deletions wrappers/golang/curves/bls12377/g2/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *G2Projective) FromAffine(a G2Affine) G2Projective {
z := G2BaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
func (a G2Affine) ToProjective() G2Projective {
var z G2BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p G2Projective
return p.Zero()
}

return G2Projective{
X: a.X,
Y: a.Y,
Expand Down
15 changes: 12 additions & 3 deletions wrappers/golang/curves/bls12381/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *Projective) FromAffine(a Affine) Projective {
z := BaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
func (a Affine) ToProjective() Projective {
var z BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p Projective
return p.Zero()
}

return Projective{
X: a.X,
Y: a.Y,
Expand Down
15 changes: 12 additions & 3 deletions wrappers/golang/curves/bls12381/g2/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *G2Projective) FromAffine(a G2Affine) G2Projective {
z := G2BaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
func (a G2Affine) ToProjective() G2Projective {
var z G2BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p G2Projective
return p.Zero()
}

return G2Projective{
X: a.X,
Y: a.Y,
Expand Down
15 changes: 12 additions & 3 deletions wrappers/golang/curves/bn254/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *Projective) FromAffine(a Affine) Projective {
z := BaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
func (a Affine) ToProjective() Projective {
var z BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p Projective
return p.Zero()
}

return Projective{
X: a.X,
Y: a.Y,
Expand Down
15 changes: 12 additions & 3 deletions wrappers/golang/curves/bn254/g2/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *G2Projective) FromAffine(a G2Affine) G2Projective {
z := G2BaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
func (a G2Affine) ToProjective() G2Projective {
var z G2BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p G2Projective
return p.Zero()
}

return G2Projective{
X: a.X,
Y: a.Y,
Expand Down
15 changes: 12 additions & 3 deletions wrappers/golang/curves/bw6761/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *Projective) FromAffine(a Affine) Projective {
z := BaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
func (a Affine) ToProjective() Projective {
var z BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p Projective
return p.Zero()
}

return Projective{
X: a.X,
Y: a.Y,
Expand Down
15 changes: 12 additions & 3 deletions wrappers/golang/curves/bw6761/g2/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *G2Projective) FromAffine(a G2Affine) G2Projective {
z := G2BaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
func (a G2Affine) ToProjective() G2Projective {
var z G2BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p G2Projective
return p.Zero()
}

return G2Projective{
X: a.X,
Y: a.Y,
Expand Down
15 changes: 12 additions & 3 deletions wrappers/golang/curves/grumpkin/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *Projective) FromAffine(a Affine) Projective {
z := BaseField{}
z.One()

p.X = a.X
p.Y = a.Y
p.Z = z
if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
} else {
p.X = a.X
p.Y = a.Y
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
func (a Affine) ToProjective() Projective {
var z BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p Projective
return p.Zero()
}

return Projective{
X: a.X,
Y: a.Y,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ func (p *{{.CurvePrefix}}Projective) FromAffine(a {{.CurvePrefix}}Affine) {{.Cur
z := {{.CurvePrefix}}BaseField{}
z.One()

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
p.Zero()
}else{
p.X = a.X
p.Y = a.Y
p.Z = z
p.Z = z.One()
}

return *p
}
Expand Down Expand Up @@ -109,6 +113,11 @@ func (a *{{.CurvePrefix}}Affine) FromLimbs(x, y []uint32) {{.CurvePrefix}}Affine
func (a {{.CurvePrefix}}Affine) ToProjective() {{.CurvePrefix}}Projective {
var z {{.CurvePrefix}}BaseField

if (a.X == z.Zero()) && (a.Y == z.Zero()) {
var p {{.CurvePrefix}}Projective
return p.Zero()
}

return {{.CurvePrefix}}Projective{
X: a.X,
Y: a.Y,
Expand Down
3 changes: 3 additions & 0 deletions wrappers/rust/icicle-core/src/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ impl<C: Curve> Affine<C> {
}

pub fn to_projective(&self) -> Projective<C> {
if *self == Self::zero() {
return Projective::<C>::zero();
}
Projective {
x: self.x,
y: self.y,
Expand Down

0 comments on commit a4b1eb3

Please sign in to comment.