@@ -9,7 +9,6 @@ use cairo_lang_sierra::{
99 program_registry:: ProgramRegistry ,
1010} ;
1111use num_traits:: identities:: Zero ;
12- use rand:: Rng ;
1312use smallvec:: smallvec;
1413use starknet_crypto:: Felt ;
1514use starknet_curve:: curve_params:: BETA ;
@@ -113,15 +112,11 @@ fn eval_state_init(
113112 _info : & SignatureOnlyConcreteLibfunc ,
114113 _args : Vec < Value > ,
115114) -> EvalAction {
116- let state = random_ec_point ( ) ;
117-
118115 EvalAction :: NormalBranch (
119116 0 ,
120117 smallvec ! [ Value :: EcState {
121- x0: state. x( ) ,
122- y0: state. y( ) ,
123- x1: state. x( ) ,
124- y1: state. y( ) ,
118+ x: 0 . into( ) ,
119+ y: 0 . into( )
125120 } ] ,
126121 )
127122}
@@ -131,84 +126,65 @@ fn eval_state_add(
131126 _info : & SignatureOnlyConcreteLibfunc ,
132127 args : Vec < Value > ,
133128) -> EvalAction {
134- let [ Value :: EcState { x0 , y0 , x1 , y1 } , Value :: EcPoint { x, y } ] : [ Value ; 2 ] =
129+ let [ Value :: EcState { x : s_x , y : s_y } , Value :: EcPoint { x, y } ] : [ Value ; 2 ] =
135130 args. try_into ( ) . unwrap ( )
136131 else {
137132 panic ! ( )
138133 } ;
139134
140- let mut state = ProjectivePoint :: from_affine ( x0, y0) . unwrap ( ) ;
135+ if s_x. is_zero ( ) && s_y. is_zero ( ) {
136+ return EvalAction :: NormalBranch ( 0 , smallvec ! [ Value :: EcState { x, y } ] ) ;
137+ }
138+ let mut state = ProjectivePoint :: from_affine ( s_x, s_y) . unwrap ( ) ;
141139 let point = AffinePoint :: new ( x, y) . unwrap ( ) ;
142140
143141 state += & point;
144- let state = state. to_affine ( ) . unwrap ( ) ;
145-
146- EvalAction :: NormalBranch (
147- 0 ,
148- smallvec ! [ Value :: EcState {
149- x0: state. x( ) ,
150- y0: state. y( ) ,
151- x1,
152- y1
153- } ] ,
154- )
142+ let ( x, y) = match state. to_affine ( ) {
143+ Ok ( state) => ( state. x ( ) , state. y ( ) ) ,
144+ Err ( _) => ( Felt :: ZERO , Felt :: ZERO ) ,
145+ } ;
146+ EvalAction :: NormalBranch ( 0 , smallvec ! [ Value :: EcState { x, y } ] )
155147}
156148
157149fn eval_state_add_mul (
158150 _registry : & ProgramRegistry < CoreType , CoreLibfunc > ,
159151 _info : & SignatureOnlyConcreteLibfunc ,
160152 args : Vec < Value > ,
161153) -> EvalAction {
162- let [ ec @ Value :: Unit , Value :: EcState { x0 , y0 , x1 , y1 } , Value :: Felt ( scalar) , Value :: EcPoint { x, y } ] : [ Value ; 4 ] =
154+ let [ ec @ Value :: Unit , Value :: EcState { x : s_x , y : s_y } , Value :: Felt ( scalar) , Value :: EcPoint { x, y } ] : [ Value ; 4 ] =
163155 args. try_into ( ) . unwrap ( )
164156 else {
165157 panic ! ( )
166158 } ;
167159
168- let mut state = ProjectivePoint :: from_affine ( x0, y0) . unwrap ( ) ;
160+ let mut state = if s_x. is_zero ( ) && s_y. is_zero ( ) {
161+ ProjectivePoint :: identity ( )
162+ } else {
163+ ProjectivePoint :: from_affine ( s_x, s_y) . unwrap ( )
164+ } ;
169165 let point = ProjectivePoint :: from_affine ( x, y) . unwrap ( ) ;
170166
171167 state += & point. mul ( scalar) ;
172- let state = state. to_affine ( ) . unwrap ( ) ;
173-
174- EvalAction :: NormalBranch (
175- 0 ,
176- smallvec ! [
177- ec,
178- Value :: EcState {
179- x0: state. x( ) ,
180- y0: state. y( ) ,
181- x1,
182- y1
183- }
184- ] ,
185- )
168+ let ( x, y) = match state. to_affine ( ) {
169+ Ok ( state) => ( state. x ( ) , state. y ( ) ) ,
170+ Err ( _) => ( Felt :: ZERO , Felt :: ZERO ) ,
171+ } ;
172+ EvalAction :: NormalBranch ( 0 , smallvec ! [ ec, Value :: EcState { x, y } ] )
186173}
187174
188175fn eval_state_finalize (
189176 _registry : & ProgramRegistry < CoreType , CoreLibfunc > ,
190177 _info : & SignatureOnlyConcreteLibfunc ,
191178 args : Vec < Value > ,
192179) -> EvalAction {
193- let [ Value :: EcState { x0 , y0 , x1 , y1 } ] : [ Value ; 1 ] = args. try_into ( ) . unwrap ( ) else {
180+ let [ Value :: EcState { x , y } ] : [ Value ; 1 ] = args. try_into ( ) . unwrap ( ) else {
194181 panic ! ( )
195182 } ;
196183
197- let state = ProjectivePoint :: from_affine ( x0, y0) . unwrap ( ) ;
198- let random_point = ProjectivePoint :: from_affine ( x1, y1) . unwrap ( ) ;
199-
200- if state. x ( ) == random_point. x ( ) && state. y ( ) == random_point. y ( ) {
184+ if x. is_zero ( ) && y. is_zero ( ) {
201185 EvalAction :: NormalBranch ( 1 , smallvec ! [ ] )
202186 } else {
203- let point = & state - & random_point;
204- let point = point. to_affine ( ) . unwrap ( ) ;
205- EvalAction :: NormalBranch (
206- 0 ,
207- smallvec ! [ Value :: EcPoint {
208- x: point. x( ) ,
209- y: point. y( ) ,
210- } ] ,
211- )
187+ EvalAction :: NormalBranch ( 0 , smallvec ! [ Value :: EcPoint { x, y } ] )
212188 }
213189}
214190
@@ -244,22 +220,6 @@ fn eval_point_from_x(
244220 }
245221}
246222
247- fn random_ec_point ( ) -> AffinePoint {
248- // https://github.com/starkware-libs/cairo/blob/aaad921bba52e729dc24ece07fab2edf09ccfa15/crates/cairo-lang-runner/src/casm_run/mod.rs#L1802
249- let mut rng = rand:: rng ( ) ;
250- let ( random_x, random_y) = loop {
251- // Randominzing 31 bytes to make sure is in range.
252- let x_bytes: [ u8 ; 31 ] = rng. random ( ) ;
253- let random_x = Felt :: from_bytes_be_slice ( & x_bytes) ;
254- let random_y_squared = random_x * random_x * random_x + random_x + BETA ;
255- if let Some ( random_y) = random_y_squared. sqrt ( ) {
256- break ( random_x, random_y) ;
257- }
258- } ;
259-
260- AffinePoint :: new ( random_x, random_y) . unwrap ( )
261- }
262-
263223fn eval_zero (
264224 _registry : & ProgramRegistry < CoreType , CoreLibfunc > ,
265225 _info : & SignatureOnlyConcreteLibfunc ,
0 commit comments