@@ -79,7 +79,6 @@ pub comptime fn derive_curve_impl(
7979 let typ = struct_def .as_type ();
8080 let CurveJ = quote { $crate ::curve_jac::CurveJ };
8181 let AffineTranscript = quote { $crate ::curve_jac::AffineTranscript };
82- let ScalarField = quote { $crate ::scalar_field::ScalarField };
8382
8483 quote {
8584 impl $crate ::BigCurve <$field_type > for $typ {
@@ -184,12 +183,16 @@ pub comptime fn derive_curve_impl(
184183 // Expensive witness generation! Avoid if possible
185184 impl std::ops::Add for $typ {
186185 fn add (self , other : Self ) -> Self {
187- let lhsJ = $CurveJ ::<$field_type , $typ >::from (self );
188- let rhsJ = $CurveJ ::<$field_type , $typ >::from (other );
189-
190- let transcript = unsafe { $AffineTranscript ::from_j (lhsJ .add (rhsJ ).1 ) };
191-
192- $crate ::add_with_hint ::<$field_type , $typ >(self ,other , transcript )
186+ let lhsJ = $crate ::curve_jac::CurveJ ::<$field_type , $typ >::from (self );
187+ let rhsJ = $crate ::curve_jac::CurveJ ::<$field_type , $typ >::from (other );
188+ let (result_jac , j_transcript ) = unsafe { lhsJ .add (rhsJ ) };
189+ let transcript = unsafe { $crate ::curve_jac::AffineTranscript ::from_j (j_transcript ) };
190+ if std::runtime:: is_unconstrained () {
191+ $typ:: from_coordinates (transcript .x3 , transcript .y3 , result_jac .is_infinity )
192+
193+ } else {
194+ $crate ::add_with_hint ::<$field_type , $typ >(self , other , transcript )
195+ }
193196 }
194197 }
195198
@@ -204,12 +207,17 @@ pub comptime fn derive_curve_impl(
204207
205208 impl std::ops::Sub for $typ {
206209 fn sub (self , other : Self ) -> Self {
207- let lhsJ = $CurveJ ::<$field_type , $typ >::from (self );
208- let rhsJ = $CurveJ ::<$field_type , $typ >::from (other );
209-
210- let transcript = unsafe { $AffineTranscript ::from_j (lhsJ .sub (rhsJ ).1 ) };
211-
212- $crate ::sub_with_hint ::<$field_type , $typ >(self , other , transcript )
210+ let lhsJ = $crate ::curve_jac::CurveJ ::<$field_type , $typ >::from (self );
211+ let rhsJ = $crate ::curve_jac::CurveJ ::<$field_type , $typ >::from (other );
212+ let (result_jac , j_transcript ) = unsafe { lhsJ .sub (rhsJ ) };
213+
214+ // Convert back to affine coordinates using the transcript
215+ let transcript = unsafe { $crate ::curve_jac::AffineTranscript ::from_j (j_transcript ) };
216+ if std::runtime:: is_unconstrained () {
217+ $typ:: from_coordinates (transcript .x3 , transcript .y3 , result_jac .is_infinity )
218+ } else {
219+ $crate ::sub_with_hint ::<$field_type , $typ >(self , other , transcript )
220+ }
213221 }
214222 }
215223
@@ -549,10 +557,10 @@ pub(crate) fn add_with_hint<B: BigNum, P: BigCurve<B>>(
549557
550558 // If we are skipping the evaluation of a group operation (x2 == x1, y2 == -y1 OR any input points are at infinity),
551559 // set input operands to 0!
552- let (x1 , y1 , x2 , y2 ) = if evaluate_group_operation_predicate {
553- (x1 , y1 , x2 , y2 )
560+ let (x1 , y1 , x2 , y2 , x3 , y3 ) = if evaluate_group_operation_predicate {
561+ (x1 , y1 , x2 , y2 , x3 , y3 )
554562 } else {
555- (B ::zero (), B ::zero (), B ::zero (), B ::zero ())
563+ (B ::zero (), B ::zero (), B ::zero (), B ::zero (), B :: zero (), B :: zero () )
556564 };
557565
558566 // lambda * 2y - 3x * x = 0
@@ -678,10 +686,10 @@ pub(crate) fn sub_with_hint<B: BigNum, P: BigCurve<B>>(
678686
679687 // If we are skipping the evaluation of a group operation (x2 == x1, y2 == -y1 OR any input points are at infinity),
680688 // set input operands to 0!
681- let (x1 , y1 , x2 , y2 ) = if evaluate_group_operation_predicate {
682- (x1 , y1 , x2 , y2 )
689+ let (x1 , y1 , x2 , y2 , x3 , y3 ) = if evaluate_group_operation_predicate {
690+ (x1 , y1 , x2 , y2 , x3 , y3 )
683691 } else {
684- (B ::zero (), B ::zero (), B ::zero (), B ::zero ())
692+ (B ::zero (), B ::zero (), B ::zero (), B ::zero (), B :: zero (), B :: zero () )
685693 };
686694
687695 // lambda * 2y - 3x*x = 0
0 commit comments