@@ -9,7 +9,7 @@ use crate::ssa::{
9
9
} ,
10
10
} ;
11
11
use acvm:: FieldElement ;
12
- use num_bigint:: ToBigUint ;
12
+ use num_bigint:: BigUint ;
13
13
14
14
pub ( super ) fn simplify_id ( ctx : & mut SsaContext , ins_id : NodeId ) -> Result < ( ) , RuntimeError > {
15
15
let mut ins = ctx. instruction ( ins_id) . clone ( ) ;
@@ -74,22 +74,24 @@ pub(super) fn simplify(ctx: &mut SsaContext, ins: &mut Instruction) -> Result<()
74
74
fn evaluate_intrinsic (
75
75
ctx : & mut SsaContext ,
76
76
op : builtin:: Opcode ,
77
- args : Vec < u128 > ,
77
+ args : Vec < FieldElement > ,
78
78
res_type : & ObjectType ,
79
79
block_id : BlockId ,
80
80
) -> Result < Vec < NodeId > , RuntimeErrorKind > {
81
81
match op {
82
82
builtin:: Opcode :: ToBits ( _) => {
83
- let bit_count = args[ 1 ] as u32 ;
83
+ let bit_count = args[ 1 ] . to_u128 ( ) as u32 ;
84
84
let mut result = Vec :: new ( ) ;
85
+ let mut bits = args[ 0 ] . bits ( ) ;
86
+ bits. reverse ( ) ;
85
87
86
88
if let ObjectType :: ArrayPointer ( a) = res_type {
87
89
for i in 0 ..bit_count {
88
90
let index = ctx. get_or_create_const (
89
91
FieldElement :: from ( i as i128 ) ,
90
92
ObjectType :: native_field ( ) ,
91
93
) ;
92
- let op = if args [ 0 ] & ( 1 << i ) != 0 {
94
+ let op = if i < bits . len ( ) as u32 && bits [ i as usize ] {
93
95
Operation :: Store {
94
96
array_id : * a,
95
97
index,
@@ -116,9 +118,10 @@ fn evaluate_intrinsic(
116
118
) ;
117
119
}
118
120
builtin:: Opcode :: ToRadix ( endian) => {
119
- let mut element = args[ 0 ] . to_biguint ( ) . unwrap ( ) . to_radix_le ( args[ 1 ] as u32 ) ;
120
- let byte_count = args[ 2 ] as u32 ;
121
- let diff = if byte_count > element. len ( ) as u32 {
121
+ let mut element = BigUint :: from_bytes_be ( & args[ 0 ] . to_be_bytes ( ) )
122
+ . to_radix_le ( args[ 1 ] . to_u128 ( ) as u32 ) ;
123
+ let byte_count = args[ 2 ] . to_u128 ( ) as u32 ;
124
+ let diff = if byte_count >= element. len ( ) as u32 {
122
125
byte_count - element. len ( ) as u32
123
126
} else {
124
127
return Err ( RuntimeErrorKind :: ArrayOutOfBounds {
@@ -532,9 +535,8 @@ fn cse_block_with_anchor(
532
535
// We do not simplify print statements
533
536
builtin:: Opcode :: Println ( _) => ( ) ,
534
537
_ => {
535
- let args = args. iter ( ) . map ( |arg| {
536
- NodeEval :: from_id ( ctx, * arg) . into_const_value ( ) . map ( |f| f. to_u128 ( ) )
537
- } ) ;
538
+ let args =
539
+ args. iter ( ) . map ( |arg| NodeEval :: from_id ( ctx, * arg) . into_const_value ( ) ) ;
538
540
539
541
if let Some ( args) = args. collect ( ) {
540
542
update2. mark = Mark :: Deleted ;
0 commit comments