diff --git a/icicle/src/curves/extern.cu b/icicle/src/curves/extern.cu index 8ea5bce2f..a40d24ffd 100644 --- a/icicle/src/curves/extern.cu +++ b/icicle/src/curves/extern.cu @@ -20,6 +20,11 @@ extern "C" void CONCAT_EXPAND(CURVE, to_affine)(projective_t* point, affine_t* p *point_out = projective_t::to_affine(*point); } +extern "C" void CONCAT_EXPAND(CURVE, from_affine)(affine_t* point, projective_t* point_out) +{ + *point_out = projective_t::from_affine(*point); +} + extern "C" void CONCAT_EXPAND(CURVE, generate_projective_points)(projective_t* points, int size) { projective_t::rand_host_many(points, size); diff --git a/icicle/src/curves/extern_g2.cu b/icicle/src/curves/extern_g2.cu index e8daa5e11..6d1e44146 100644 --- a/icicle/src/curves/extern_g2.cu +++ b/icicle/src/curves/extern_g2.cu @@ -20,6 +20,11 @@ extern "C" void CONCAT_EXPAND(CURVE, g2_to_affine)(g2_projective_t* point, g2_af *point_out = g2_projective_t::to_affine(*point); } +extern "C" void CONCAT_EXPAND(CURVE, g2_from_affine)(g2_affine_t* point, g2_projective_t* point_out) +{ + *point_out = g2_projective_t::from_affine(*point); +} + extern "C" void CONCAT_EXPAND(CURVE, g2_generate_projective_points)(g2_projective_t* points, int size) { g2_projective_t::rand_host_many(points, size); diff --git a/wrappers/golang/core/internal/mock_curve.go b/wrappers/golang/core/internal/mock_curve.go index de21b3d92..dddbddee7 100644 --- a/wrappers/golang/core/internal/mock_curve.go +++ b/wrappers/golang/core/internal/mock_curve.go @@ -28,21 +28,6 @@ func (p *MockProjective) FromLimbs(x, y, z []uint32) MockProjective { return *p } -func (p *MockProjective) FromAffine(a MockAffine) MockProjective { - z := MockBaseField{} - 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.One() - } - - return *p -} - type MockAffine struct { X, Y MockBaseField } @@ -68,18 +53,3 @@ func (a *MockAffine) FromLimbs(x, y []uint32) MockAffine { return *a } - -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, - Z: z.One(), - } -} diff --git a/wrappers/golang/curves/bls12377/curve.go b/wrappers/golang/curves/bls12377/curve.go index 8083669ab..43184ff56 100644 --- a/wrappers/golang/curves/bls12377/curve.go +++ b/wrappers/golang/curves/bls12377/curve.go @@ -40,17 +40,10 @@ func (p *Projective) FromLimbs(x, y, z []uint32) Projective { } func (p *Projective) FromAffine(a Affine) Projective { - z := 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.One() - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(p)) + C.bls12_377_from_affine(cA, cP) return *p } @@ -65,7 +58,7 @@ func (p *Projective) ProjectiveToAffine() Affine { var a Affine cA := (*C.affine_t)(unsafe.Pointer(&a)) - cP := (*C.projective_t)(unsafe.Pointer(&p)) + cP := (*C.projective_t)(unsafe.Pointer(p)) C.bls12_377_to_affine(cP, cA) return a } @@ -111,18 +104,12 @@ 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() - } + var p Projective - return Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(&p)) + C.bls12_377_from_affine(cA, cP) + return p } func AffineFromProjective(p *Projective) Affine { diff --git a/wrappers/golang/curves/bls12377/g2/curve.go b/wrappers/golang/curves/bls12377/g2/curve.go index 7da1d2717..4544e9735 100644 --- a/wrappers/golang/curves/bls12377/g2/curve.go +++ b/wrappers/golang/curves/bls12377/g2/curve.go @@ -40,17 +40,10 @@ func (p *G2Projective) FromLimbs(x, y, z []uint32) G2Projective { } func (p *G2Projective) FromAffine(a G2Affine) G2Projective { - z := G2BaseField{} - 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.One() - } + cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) + cP := (*C.g2_projective_t)(unsafe.Pointer(p)) + C.bls12_377_g2_from_affine(cA, cP) return *p } @@ -65,7 +58,7 @@ func (p *G2Projective) ProjectiveToAffine() G2Affine { var a G2Affine cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) - cP := (*C.g2_projective_t)(unsafe.Pointer(&p)) + cP := (*C.g2_projective_t)(unsafe.Pointer(p)) C.bls12_377_g2_to_affine(cP, cA) return a } @@ -111,18 +104,12 @@ 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() - } + var p G2Projective - return G2Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } + cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) + cP := (*C.g2_projective_t)(unsafe.Pointer(&p)) + C.bls12_377_g2_from_affine(cA, cP) + return p } func G2AffineFromProjective(p *G2Projective) G2Affine { diff --git a/wrappers/golang/curves/bls12377/g2/include/curve.h b/wrappers/golang/curves/bls12377/g2/include/curve.h index b136de9a8..57d5e148f 100644 --- a/wrappers/golang/curves/bls12377/g2/include/curve.h +++ b/wrappers/golang/curves/bls12377/g2/include/curve.h @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool bls12_377_g2_eq(g2_projective_t* point1, g2_projective_t* point2); void bls12_377_g2_to_affine(g2_projective_t* point, g2_affine_t* point_out); +void bls12_377_g2_from_affine(g2_affine_t* point, g2_projective_t* point_out); void bls12_377_g2_generate_projective_points(g2_projective_t* points, int size); void bls12_377_g2_generate_affine_points(g2_affine_t* points, int size); cudaError_t bls12_377_g2_affine_convert_montgomery(g2_affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/golang/curves/bls12377/include/curve.h b/wrappers/golang/curves/bls12377/include/curve.h index 87a0229b6..6f32d1c36 100644 --- a/wrappers/golang/curves/bls12377/include/curve.h +++ b/wrappers/golang/curves/bls12377/include/curve.h @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool bls12_377_eq(projective_t* point1, projective_t* point2); void bls12_377_to_affine(projective_t* point, affine_t* point_out); +void bls12_377_from_affine(affine_t* point, projective_t* point_out); void bls12_377_generate_projective_points(projective_t* points, int size); void bls12_377_generate_affine_points(affine_t* points, int size); cudaError_t bls12_377_affine_convert_montgomery(affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/golang/curves/bls12381/curve.go b/wrappers/golang/curves/bls12381/curve.go index 02cee7b64..3b1b59ea2 100644 --- a/wrappers/golang/curves/bls12381/curve.go +++ b/wrappers/golang/curves/bls12381/curve.go @@ -40,17 +40,10 @@ func (p *Projective) FromLimbs(x, y, z []uint32) Projective { } func (p *Projective) FromAffine(a Affine) Projective { - z := 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.One() - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(p)) + C.bls12_381_from_affine(cA, cP) return *p } @@ -65,7 +58,7 @@ func (p *Projective) ProjectiveToAffine() Affine { var a Affine cA := (*C.affine_t)(unsafe.Pointer(&a)) - cP := (*C.projective_t)(unsafe.Pointer(&p)) + cP := (*C.projective_t)(unsafe.Pointer(p)) C.bls12_381_to_affine(cP, cA) return a } @@ -111,18 +104,12 @@ 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() - } + var p Projective - return Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(&p)) + C.bls12_381_from_affine(cA, cP) + return p } func AffineFromProjective(p *Projective) Affine { diff --git a/wrappers/golang/curves/bls12381/g2/curve.go b/wrappers/golang/curves/bls12381/g2/curve.go index d4fced658..b711be658 100644 --- a/wrappers/golang/curves/bls12381/g2/curve.go +++ b/wrappers/golang/curves/bls12381/g2/curve.go @@ -40,17 +40,10 @@ func (p *G2Projective) FromLimbs(x, y, z []uint32) G2Projective { } func (p *G2Projective) FromAffine(a G2Affine) G2Projective { - z := G2BaseField{} - 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.One() - } + cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) + cP := (*C.g2_projective_t)(unsafe.Pointer(p)) + C.bls12_381_g2_from_affine(cA, cP) return *p } @@ -65,7 +58,7 @@ func (p *G2Projective) ProjectiveToAffine() G2Affine { var a G2Affine cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) - cP := (*C.g2_projective_t)(unsafe.Pointer(&p)) + cP := (*C.g2_projective_t)(unsafe.Pointer(p)) C.bls12_381_g2_to_affine(cP, cA) return a } @@ -111,18 +104,13 @@ func (a *G2Affine) FromLimbs(x, y []uint32) G2Affine { } func (a G2Affine) ToProjective() G2Projective { - var z G2BaseField + var p G2Projective - if (a.X == z.Zero()) && (a.Y == z.Zero()) { - var p G2Projective - return p.Zero() - } + cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) + cP := (*C.g2_projective_t)(unsafe.Pointer(&p)) + C.bls12_381_g2_from_affine(cA, cP) + return p - return G2Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } } func G2AffineFromProjective(p *G2Projective) G2Affine { diff --git a/wrappers/golang/curves/bls12381/g2/include/curve.h b/wrappers/golang/curves/bls12381/g2/include/curve.h index b7710244d..274f3ec16 100644 --- a/wrappers/golang/curves/bls12381/g2/include/curve.h +++ b/wrappers/golang/curves/bls12381/g2/include/curve.h @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool bls12_381_g2_eq(g2_projective_t* point1, g2_projective_t* point2); void bls12_381_g2_to_affine(g2_projective_t* point, g2_affine_t* point_out); +void bls12_381_g2_from_affine(g2_affine_t* point, g2_projective_t* point_out); void bls12_381_g2_generate_projective_points(g2_projective_t* points, int size); void bls12_381_g2_generate_affine_points(g2_affine_t* points, int size); cudaError_t bls12_381_g2_affine_convert_montgomery(g2_affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/golang/curves/bls12381/include/curve.h b/wrappers/golang/curves/bls12381/include/curve.h index 1cb3bd61e..b9a50675c 100644 --- a/wrappers/golang/curves/bls12381/include/curve.h +++ b/wrappers/golang/curves/bls12381/include/curve.h @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool bls12_381_eq(projective_t* point1, projective_t* point2); void bls12_381_to_affine(projective_t* point, affine_t* point_out); +void bls12_381_from_affine(affine_t* point, projective_t* point_out); void bls12_381_generate_projective_points(projective_t* points, int size); void bls12_381_generate_affine_points(affine_t* points, int size); cudaError_t bls12_381_affine_convert_montgomery(affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/golang/curves/bn254/curve.go b/wrappers/golang/curves/bn254/curve.go index 3bc94a8f0..f78e02840 100644 --- a/wrappers/golang/curves/bn254/curve.go +++ b/wrappers/golang/curves/bn254/curve.go @@ -40,17 +40,10 @@ func (p *Projective) FromLimbs(x, y, z []uint32) Projective { } func (p *Projective) FromAffine(a Affine) Projective { - z := 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.One() - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(p)) + C.bn254_from_affine(cA, cP) return *p } @@ -65,7 +58,7 @@ func (p *Projective) ProjectiveToAffine() Affine { var a Affine cA := (*C.affine_t)(unsafe.Pointer(&a)) - cP := (*C.projective_t)(unsafe.Pointer(&p)) + cP := (*C.projective_t)(unsafe.Pointer(p)) C.bn254_to_affine(cP, cA) return a } @@ -111,18 +104,13 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine { } func (a Affine) ToProjective() Projective { - var z BaseField + var p Projective - if (a.X == z.Zero()) && (a.Y == z.Zero()) { - var p Projective - return p.Zero() - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(&p)) + C.bn254_from_affine(cA, cP) + return p - return Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } } func AffineFromProjective(p *Projective) Affine { diff --git a/wrappers/golang/curves/bn254/g2/curve.go b/wrappers/golang/curves/bn254/g2/curve.go index 7ee0ec7f9..72245e02b 100644 --- a/wrappers/golang/curves/bn254/g2/curve.go +++ b/wrappers/golang/curves/bn254/g2/curve.go @@ -40,17 +40,10 @@ func (p *G2Projective) FromLimbs(x, y, z []uint32) G2Projective { } func (p *G2Projective) FromAffine(a G2Affine) G2Projective { - z := G2BaseField{} - 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.One() - } + cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) + cP := (*C.g2_projective_t)(unsafe.Pointer(p)) + C.bn254_g2_from_affine(cA, cP) return *p } @@ -65,7 +58,7 @@ func (p *G2Projective) ProjectiveToAffine() G2Affine { var a G2Affine cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) - cP := (*C.g2_projective_t)(unsafe.Pointer(&p)) + cP := (*C.g2_projective_t)(unsafe.Pointer(p)) C.bn254_g2_to_affine(cP, cA) return a } @@ -111,18 +104,12 @@ 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() - } + var p G2Projective - return G2Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } + cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) + cP := (*C.g2_projective_t)(unsafe.Pointer(&p)) + C.bn254_g2_from_affine(cA, cP) + return p } func G2AffineFromProjective(p *G2Projective) G2Affine { diff --git a/wrappers/golang/curves/bn254/g2/include/curve.h b/wrappers/golang/curves/bn254/g2/include/curve.h index e8863f1ef..c24deb307 100644 --- a/wrappers/golang/curves/bn254/g2/include/curve.h +++ b/wrappers/golang/curves/bn254/g2/include/curve.h @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool bn254_g2_eq(g2_projective_t* point1, g2_projective_t* point2); void bn254_g2_to_affine(g2_projective_t* point, g2_affine_t* point_out); +void bn254_g2_from_affine(g2_affine_t* point, g2_projective_t* point_out); void bn254_g2_generate_projective_points(g2_projective_t* points, int size); void bn254_g2_generate_affine_points(g2_affine_t* points, int size); cudaError_t bn254_g2_affine_convert_montgomery(g2_affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/golang/curves/bn254/include/curve.h b/wrappers/golang/curves/bn254/include/curve.h index 069600aa4..ce29f587a 100644 --- a/wrappers/golang/curves/bn254/include/curve.h +++ b/wrappers/golang/curves/bn254/include/curve.h @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool bn254_eq(projective_t* point1, projective_t* point2); void bn254_to_affine(projective_t* point, affine_t* point_out); +void bn254_from_affine(affine_t* point, projective_t* point_out); void bn254_generate_projective_points(projective_t* points, int size); void bn254_generate_affine_points(affine_t* points, int size); cudaError_t bn254_affine_convert_montgomery(affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/golang/curves/bw6761/curve.go b/wrappers/golang/curves/bw6761/curve.go index 2e8518be9..38a68fd94 100644 --- a/wrappers/golang/curves/bw6761/curve.go +++ b/wrappers/golang/curves/bw6761/curve.go @@ -40,17 +40,10 @@ func (p *Projective) FromLimbs(x, y, z []uint32) Projective { } func (p *Projective) FromAffine(a Affine) Projective { - z := 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.One() - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(p)) + C.bw6_761_from_affine(cA, cP) return *p } @@ -65,7 +58,7 @@ func (p *Projective) ProjectiveToAffine() Affine { var a Affine cA := (*C.affine_t)(unsafe.Pointer(&a)) - cP := (*C.projective_t)(unsafe.Pointer(&p)) + cP := (*C.projective_t)(unsafe.Pointer(p)) C.bw6_761_to_affine(cP, cA) return a } @@ -111,18 +104,13 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine { } func (a Affine) ToProjective() Projective { - var z BaseField + var p Projective - if (a.X == z.Zero()) && (a.Y == z.Zero()) { - var p Projective - return p.Zero() - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(&p)) + C.bw6_761_from_affine(cA, cP) + return p - return Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } } func AffineFromProjective(p *Projective) Affine { diff --git a/wrappers/golang/curves/bw6761/g2/curve.go b/wrappers/golang/curves/bw6761/g2/curve.go index 1433602a2..f38bf134f 100644 --- a/wrappers/golang/curves/bw6761/g2/curve.go +++ b/wrappers/golang/curves/bw6761/g2/curve.go @@ -40,17 +40,10 @@ func (p *G2Projective) FromLimbs(x, y, z []uint32) G2Projective { } func (p *G2Projective) FromAffine(a G2Affine) G2Projective { - z := G2BaseField{} - 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.One() - } + cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) + cP := (*C.g2_projective_t)(unsafe.Pointer(p)) + C.bw6_761_g2_from_affine(cA, cP) return *p } @@ -65,7 +58,7 @@ func (p *G2Projective) ProjectiveToAffine() G2Affine { var a G2Affine cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) - cP := (*C.g2_projective_t)(unsafe.Pointer(&p)) + cP := (*C.g2_projective_t)(unsafe.Pointer(p)) C.bw6_761_g2_to_affine(cP, cA) return a } @@ -111,18 +104,12 @@ 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() - } + var p G2Projective - return G2Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } + cA := (*C.g2_affine_t)(unsafe.Pointer(&a)) + cP := (*C.g2_projective_t)(unsafe.Pointer(&p)) + C.bw6_761_g2_from_affine(cA, cP) + return p } func G2AffineFromProjective(p *G2Projective) G2Affine { diff --git a/wrappers/golang/curves/bw6761/g2/include/curve.h b/wrappers/golang/curves/bw6761/g2/include/curve.h index b57b55cfc..8d113fc90 100644 --- a/wrappers/golang/curves/bw6761/g2/include/curve.h +++ b/wrappers/golang/curves/bw6761/g2/include/curve.h @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool bw6_761_g2_eq(g2_projective_t* point1, g2_projective_t* point2); void bw6_761_g2_to_affine(g2_projective_t* point, g2_affine_t* point_out); +void bw6_761_g2_from_affine(g2_affine_t* point, g2_projective_t* point_out); void bw6_761_g2_generate_projective_points(g2_projective_t* points, int size); void bw6_761_g2_generate_affine_points(g2_affine_t* points, int size); cudaError_t bw6_761_g2_affine_convert_montgomery(g2_affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/golang/curves/bw6761/include/curve.h b/wrappers/golang/curves/bw6761/include/curve.h index cc6e401cb..ba243029c 100644 --- a/wrappers/golang/curves/bw6761/include/curve.h +++ b/wrappers/golang/curves/bw6761/include/curve.h @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool bw6_761_eq(projective_t* point1, projective_t* point2); void bw6_761_to_affine(projective_t* point, affine_t* point_out); +void bw6_761_from_affine(affine_t* point, projective_t* point_out); void bw6_761_generate_projective_points(projective_t* points, int size); void bw6_761_generate_affine_points(affine_t* points, int size); cudaError_t bw6_761_affine_convert_montgomery(affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/golang/curves/grumpkin/curve.go b/wrappers/golang/curves/grumpkin/curve.go index 1079ce2e4..4ad6e2321 100644 --- a/wrappers/golang/curves/grumpkin/curve.go +++ b/wrappers/golang/curves/grumpkin/curve.go @@ -40,17 +40,10 @@ func (p *Projective) FromLimbs(x, y, z []uint32) Projective { } func (p *Projective) FromAffine(a Affine) Projective { - z := 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.One() - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(p)) + C.grumpkin_from_affine(cA, cP) return *p } @@ -65,7 +58,7 @@ func (p *Projective) ProjectiveToAffine() Affine { var a Affine cA := (*C.affine_t)(unsafe.Pointer(&a)) - cP := (*C.projective_t)(unsafe.Pointer(&p)) + cP := (*C.projective_t)(unsafe.Pointer(p)) C.grumpkin_to_affine(cP, cA) return a } @@ -111,18 +104,13 @@ func (a *Affine) FromLimbs(x, y []uint32) Affine { } func (a Affine) ToProjective() Projective { - var z BaseField + var p Projective - if (a.X == z.Zero()) && (a.Y == z.Zero()) { - var p Projective - return p.Zero() - } + cA := (*C.affine_t)(unsafe.Pointer(&a)) + cP := (*C.projective_t)(unsafe.Pointer(&p)) + C.grumpkin_from_affine(cA, cP) + return p - return Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } } func AffineFromProjective(p *Projective) Affine { diff --git a/wrappers/golang/curves/grumpkin/include/curve.h b/wrappers/golang/curves/grumpkin/include/curve.h index 8466982ed..6005b280c 100644 --- a/wrappers/golang/curves/grumpkin/include/curve.h +++ b/wrappers/golang/curves/grumpkin/include/curve.h @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool grumpkin_eq(projective_t* point1, projective_t* point2); void grumpkin_to_affine(projective_t* point, affine_t* point_out); +void grumpkin_from_affine(affine_t* point, projective_t* point_out); void grumpkin_generate_projective_points(projective_t* points, int size); void grumpkin_generate_affine_points(affine_t* points, int size); cudaError_t grumpkin_affine_convert_montgomery(affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/golang/internal/generator/curves/templates/curve.go.tmpl b/wrappers/golang/internal/generator/curves/templates/curve.go.tmpl index d02fe6a96..c411f63c4 100644 --- a/wrappers/golang/internal/generator/curves/templates/curve.go.tmpl +++ b/wrappers/golang/internal/generator/curves/templates/curve.go.tmpl @@ -39,21 +39,17 @@ func (p *{{.CurvePrefix}}Projective) FromLimbs(x, y, z []uint32) {{.CurvePrefix} return *p } -func (p *{{.CurvePrefix}}Projective) FromAffine(a {{.CurvePrefix}}Affine) {{.CurvePrefix}}Projective { - 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.One() - } + +{{if ne .CurvePrefix "Mock"}} +func (p *{{.CurvePrefix}}Projective) FromAffine(a {{.CurvePrefix}}Affine) {{.CurvePrefix}}Projective { + + cA := (*C.{{toCName .CurvePrefix}}affine_t)(unsafe.Pointer(&a)) + cP := (*C.{{toCName .CurvePrefix}}projective_t)(unsafe.Pointer(p)) + C.{{.Curve}}{{toCNameBackwards .CurvePrefix}}_from_affine(cA, cP) return *p } -{{if ne .CurvePrefix "Mock"}} + func (p {{.CurvePrefix}}Projective) ProjectiveEq(p2 *{{.CurvePrefix}}Projective) bool { cP := (*C.{{toCName .CurvePrefix}}projective_t)(unsafe.Pointer(&p)) cP2 := (*C.{{toCName .CurvePrefix}}projective_t)(unsafe.Pointer(&p2)) @@ -65,7 +61,7 @@ func (p *{{.CurvePrefix}}Projective) ProjectiveToAffine() {{.CurvePrefix}}Affine var a {{.CurvePrefix}}Affine cA := (*C.{{toCName .CurvePrefix}}affine_t)(unsafe.Pointer(&a)) - cP := (*C.{{toCName .CurvePrefix}}projective_t)(unsafe.Pointer(&p)) + cP := (*C.{{toCName .CurvePrefix}}projective_t)(unsafe.Pointer(p)) C.{{.Curve}}{{toCNameBackwards .CurvePrefix}}_to_affine(cP, cA) return a } @@ -110,21 +106,17 @@ func (a *{{.CurvePrefix}}Affine) FromLimbs(x, y []uint32) {{.CurvePrefix}}Affine return *a } -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() - } +{{if ne .CurvePrefix "Mock"}} +func (a {{.CurvePrefix}}Affine) ToProjective() {{.CurvePrefix}}Projective { + var p {{.CurvePrefix}}Projective - return {{.CurvePrefix}}Projective{ - X: a.X, - Y: a.Y, - Z: z.One(), - } + cA := (*C.{{toCName .CurvePrefix}}affine_t)(unsafe.Pointer(&a)) + cP := (*C.{{toCName .CurvePrefix}}projective_t)(unsafe.Pointer(&p)) + C.{{.Curve}}{{toCNameBackwards .CurvePrefix}}_from_affine(cA, cP) + return p } -{{if ne .CurvePrefix "Mock"}} + func {{.CurvePrefix}}AffineFromProjective(p *{{.CurvePrefix}}Projective) {{.CurvePrefix}}Affine { return p.ProjectiveToAffine() } diff --git a/wrappers/golang/internal/generator/curves/templates/curve.h.tmpl b/wrappers/golang/internal/generator/curves/templates/curve.h.tmpl index 22179e6f6..0dffd240f 100644 --- a/wrappers/golang/internal/generator/curves/templates/curve.h.tmpl +++ b/wrappers/golang/internal/generator/curves/templates/curve.h.tmpl @@ -14,6 +14,7 @@ typedef struct DeviceContext DeviceContext; bool {{.Curve}}{{toCNameBackwards .CurvePrefix}}_eq({{toCName .CurvePrefix}}projective_t* point1, {{toCName .CurvePrefix}}projective_t* point2); void {{.Curve}}{{toCNameBackwards .CurvePrefix}}_to_affine({{toCName .CurvePrefix}}projective_t* point, {{toCName .CurvePrefix}}affine_t* point_out); +void {{.Curve}}{{toCNameBackwards .CurvePrefix}}_from_affine({{toCName .CurvePrefix}}affine_t* point, {{toCName .CurvePrefix}}projective_t* point_out); void {{.Curve}}{{toCNameBackwards .CurvePrefix}}_generate_projective_points({{toCName .CurvePrefix}}projective_t* points, int size); void {{.Curve}}{{toCNameBackwards .CurvePrefix}}_generate_affine_points({{toCName .CurvePrefix}}affine_t* points, int size); cudaError_t {{.Curve}}{{toCNameBackwards .CurvePrefix}}_affine_convert_montgomery({{toCName .CurvePrefix}}affine_t* points, size_t n, bool is_into, DeviceContext* ctx); diff --git a/wrappers/rust/icicle-core/src/curve.rs b/wrappers/rust/icicle-core/src/curve.rs index c0b82d113..a0bf70bc6 100644 --- a/wrappers/rust/icicle-core/src/curve.rs +++ b/wrappers/rust/icicle-core/src/curve.rs @@ -22,6 +22,8 @@ pub trait Curve: Debug + PartialEq + Copy + Clone { #[doc(hidden)] fn to_affine(point: *const Projective, point_aff: *mut Affine); #[doc(hidden)] + fn from_affine(point: *const Affine, point_proj: *mut Projective); + #[doc(hidden)] fn generate_random_projective_points(size: usize) -> Vec>; #[doc(hidden)] fn generate_random_affine_points(size: usize) -> Vec>; @@ -79,27 +81,17 @@ impl Affine { } pub fn to_projective(&self) -> Projective { - if *self == Self::zero() { - return Projective::::zero(); - } - Projective { - x: self.x, - y: self.y, - z: C::BaseField::one(), - } + let mut proj = Projective::::zero(); + C::from_affine(self as *const Self, &mut proj as *mut Projective); + proj } } impl From> for Projective { fn from(item: Affine) -> Self { - if item == (Affine::::zero()) { - return Self::zero(); - } - Self { - x: item.x, - y: item.y, - z: C::BaseField::one(), - } + let mut proj = Self::zero(); + C::from_affine(&item as *const Affine, &mut proj as *mut Self); + proj } } @@ -282,6 +274,8 @@ macro_rules! impl_curve { pub(crate) fn eq(point1: *const $projective_type, point2: *const $projective_type) -> bool; #[link_name = concat!($curve_prefix, "_to_affine")] pub(crate) fn proj_to_affine(point: *const $projective_type, point_out: *mut $affine_type); + #[link_name = concat!($curve_prefix, "_from_affine")] + pub(crate) fn proj_from_affine(point: *const $affine_type, point_out: *mut $projective_type); #[link_name = concat!($curve_prefix, "_generate_projective_points")] pub(crate) fn generate_projective_points(points: *mut $projective_type, size: usize); #[link_name = concat!($curve_prefix, "_generate_affine_points")] @@ -315,6 +309,10 @@ macro_rules! impl_curve { unsafe { $curve_prefix_ident::proj_to_affine(point, point_out) }; } + fn from_affine(point: *const $affine_type, point_out: *mut $projective_type) { + unsafe { $curve_prefix_ident::proj_from_affine(point, point_out) }; + } + fn generate_random_projective_points(size: usize) -> Vec<$projective_type> { let mut res = vec![$projective_type::zero(); size]; unsafe {