Skip to content

Commit

Permalink
Change Affine.ToProjective implementation to be in Go instead of call…
Browse files Browse the repository at this point in the history
…ing down to cpp
  • Loading branch information
jeremyfelder committed Sep 19, 2024
1 parent b58af44 commit 8a004d3
Show file tree
Hide file tree
Showing 29 changed files with 354 additions and 30 deletions.
4 changes: 4 additions & 0 deletions wrappers/golang/core/internal/mock_curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (a *MockAffine) Zero() MockAffine {
return *a
}

func (a *MockAffine) IsZero() bool {
return a.X.IsZero() && a.Y.IsZero()
}

func (a *MockAffine) FromLimbs(x, y []uint32) MockAffine {
a.X.FromLimbs(x)
a.Y.FromLimbs(y)
Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/core/internal/mock_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func (f *MockBaseField) Zero() MockBaseField {
return *f
}

func (f *MockBaseField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *MockBaseField) One() MockBaseField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/curves/bls12377/base_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func (f *BaseField) Zero() BaseField {
return *f
}

func (f *BaseField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *BaseField) One() BaseField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
20 changes: 17 additions & 3 deletions wrappers/golang/curves/bls12377/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (a *Affine) Zero() Affine {
return *a
}

func (a *Affine) IsZero() bool {
return a.X.IsZero() && a.Y.IsZero()
}

func (a *Affine) FromLimbs(x, y []uint32) Affine {
a.X.FromLimbs(x)
a.Y.FromLimbs(y)
Expand All @@ -106,9 +110,19 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
func (a Affine) ToProjective() Projective {
var p Projective

cA := (*C.affine_t)(unsafe.Pointer(&a))
cP := (*C.projective_t)(unsafe.Pointer(&p))
C.bls12_377_from_affine(cA, cP)
// TODO - Figure out why this sometimes returns an empty projective point, i.e. {x:0, y:0, z:0}
// cA := (*C.affine_t)(unsafe.Pointer(&a))
// cP := (*C.projective_t)(unsafe.Pointer(&p))
// C.bls12_377_from_affine(cA, cP)

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

return p
}

Expand Down
20 changes: 17 additions & 3 deletions wrappers/golang/curves/bls12377/g2/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (a *G2Affine) Zero() G2Affine {
return *a
}

func (a *G2Affine) IsZero() bool {
return a.X.IsZero() && a.Y.IsZero()
}

func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
a.X.FromLimbs(x)
a.Y.FromLimbs(y)
Expand All @@ -106,9 +110,19 @@ func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
func (a G2Affine) ToProjective() G2Projective {
var p G2Projective

cA := (*C.g2_affine_t)(unsafe.Pointer(&a))
cP := (*C.g2_projective_t)(unsafe.Pointer(&p))
C.bls12_377_g2_from_affine(cA, cP)
// TODO - Figure out why this sometimes returns an empty projective point, i.e. {x:0, y:0, z:0}
// cA := (*C.g2_affine_t)(unsafe.Pointer(&a))
// cP := (*C.g2_projective_t)(unsafe.Pointer(&p))
// C.bls12_377_g2_from_affine(cA, cP)

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

return p
}

Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/curves/bls12377/g2/g2base_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func (f *G2BaseField) Zero() G2BaseField {
return *f
}

func (f *G2BaseField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *G2BaseField) One() G2BaseField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/curves/bls12377/scalar_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func (f *ScalarField) Zero() ScalarField {
return *f
}

func (f *ScalarField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *ScalarField) One() ScalarField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/curves/bls12381/base_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func (f *BaseField) Zero() BaseField {
return *f
}

func (f *BaseField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *BaseField) One() BaseField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
20 changes: 17 additions & 3 deletions wrappers/golang/curves/bls12381/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (a *Affine) Zero() Affine {
return *a
}

func (a *Affine) IsZero() bool {
return a.X.IsZero() && a.Y.IsZero()
}

func (a *Affine) FromLimbs(x, y []uint32) Affine {
a.X.FromLimbs(x)
a.Y.FromLimbs(y)
Expand All @@ -106,9 +110,19 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
func (a Affine) ToProjective() Projective {
var p Projective

cA := (*C.affine_t)(unsafe.Pointer(&a))
cP := (*C.projective_t)(unsafe.Pointer(&p))
C.bls12_381_from_affine(cA, cP)
// TODO - Figure out why this sometimes returns an empty projective point, i.e. {x:0, y:0, z:0}
// cA := (*C.affine_t)(unsafe.Pointer(&a))
// cP := (*C.projective_t)(unsafe.Pointer(&p))
// C.bls12_381_from_affine(cA, cP)

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

return p
}

Expand Down
20 changes: 17 additions & 3 deletions wrappers/golang/curves/bls12381/g2/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (a *G2Affine) Zero() G2Affine {
return *a
}

func (a *G2Affine) IsZero() bool {
return a.X.IsZero() && a.Y.IsZero()
}

func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
a.X.FromLimbs(x)
a.Y.FromLimbs(y)
Expand All @@ -106,9 +110,19 @@ func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
func (a G2Affine) ToProjective() G2Projective {
var p G2Projective

cA := (*C.g2_affine_t)(unsafe.Pointer(&a))
cP := (*C.g2_projective_t)(unsafe.Pointer(&p))
C.bls12_381_g2_from_affine(cA, cP)
// TODO - Figure out why this sometimes returns an empty projective point, i.e. {x:0, y:0, z:0}
// cA := (*C.g2_affine_t)(unsafe.Pointer(&a))
// cP := (*C.g2_projective_t)(unsafe.Pointer(&p))
// C.bls12_381_g2_from_affine(cA, cP)

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

return p
}

Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/curves/bls12381/g2/g2base_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func (f *G2BaseField) Zero() G2BaseField {
return *f
}

func (f *G2BaseField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *G2BaseField) One() G2BaseField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/curves/bls12381/scalar_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func (f *ScalarField) Zero() ScalarField {
return *f
}

func (f *ScalarField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *ScalarField) One() ScalarField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/curves/bn254/base_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func (f *BaseField) Zero() BaseField {
return *f
}

func (f *BaseField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *BaseField) One() BaseField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
20 changes: 17 additions & 3 deletions wrappers/golang/curves/bn254/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (a *Affine) Zero() Affine {
return *a
}

func (a *Affine) IsZero() bool {
return a.X.IsZero() && a.Y.IsZero()
}

func (a *Affine) FromLimbs(x, y []uint32) Affine {
a.X.FromLimbs(x)
a.Y.FromLimbs(y)
Expand All @@ -106,9 +110,19 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine {
func (a Affine) ToProjective() Projective {
var p Projective

cA := (*C.affine_t)(unsafe.Pointer(&a))
cP := (*C.projective_t)(unsafe.Pointer(&p))
C.bn254_from_affine(cA, cP)
// TODO - Figure out why this sometimes returns an empty projective point, i.e. {x:0, y:0, z:0}
// cA := (*C.affine_t)(unsafe.Pointer(&a))
// cP := (*C.projective_t)(unsafe.Pointer(&p))
// C.bn254_from_affine(cA, cP)

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

return p
}

Expand Down
20 changes: 17 additions & 3 deletions wrappers/golang/curves/bn254/g2/curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (a *G2Affine) Zero() G2Affine {
return *a
}

func (a *G2Affine) IsZero() bool {
return a.X.IsZero() && a.Y.IsZero()
}

func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
a.X.FromLimbs(x)
a.Y.FromLimbs(y)
Expand All @@ -106,9 +110,19 @@ func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine {
func (a G2Affine) ToProjective() G2Projective {
var p G2Projective

cA := (*C.g2_affine_t)(unsafe.Pointer(&a))
cP := (*C.g2_projective_t)(unsafe.Pointer(&p))
C.bn254_g2_from_affine(cA, cP)
// TODO - Figure out why this sometimes returns an empty projective point, i.e. {x:0, y:0, z:0}
// cA := (*C.g2_affine_t)(unsafe.Pointer(&a))
// cP := (*C.g2_projective_t)(unsafe.Pointer(&p))
// C.bn254_g2_from_affine(cA, cP)

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

return p
}

Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/curves/bn254/g2/g2base_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ func (f *G2BaseField) Zero() G2BaseField {
return *f
}

func (f *G2BaseField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *G2BaseField) One() G2BaseField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
10 changes: 10 additions & 0 deletions wrappers/golang/curves/bn254/scalar_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func (f *ScalarField) Zero() ScalarField {
return *f
}

func (f *ScalarField) IsZero() bool {
for _, limb := range f.limbs {
if limb != 0 {
return false
}
}

return true
}

func (f *ScalarField) One() ScalarField {
for i := range f.limbs {
f.limbs[i] = 0
Expand Down
Loading

0 comments on commit 8a004d3

Please sign in to comment.