Skip to content

Commit

Permalink
fix: to-bits and to-radix for > 128 bits (#1312)
Browse files Browse the repository at this point in the history
* fix: to-bits and to-radix for > 128 bits

* revert: use earlier overflow check that ensures unicity
  • Loading branch information
shuklaayush authored May 11, 2023
1 parent e123aa7 commit 12f3e7e
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions crates/noirc_evaluator/src/ssa/optimizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::ssa::{
},
};
use acvm::FieldElement;
use num_bigint::ToBigUint;
use num_bigint::BigUint;

pub(super) fn simplify_id(ctx: &mut SsaContext, ins_id: NodeId) -> Result<(), RuntimeError> {
let mut ins = ctx.instruction(ins_id).clone();
Expand Down Expand Up @@ -74,22 +74,24 @@ pub(super) fn simplify(ctx: &mut SsaContext, ins: &mut Instruction) -> Result<()
fn evaluate_intrinsic(
ctx: &mut SsaContext,
op: builtin::Opcode,
args: Vec<u128>,
args: Vec<FieldElement>,
res_type: &ObjectType,
block_id: BlockId,
) -> Result<Vec<NodeId>, RuntimeErrorKind> {
match op {
builtin::Opcode::ToBits(_) => {
let bit_count = args[1] as u32;
let bit_count = args[1].to_u128() as u32;
let mut result = Vec::new();
let mut bits = args[0].bits();
bits.reverse();

if let ObjectType::ArrayPointer(a) = res_type {
for i in 0..bit_count {
let index = ctx.get_or_create_const(
FieldElement::from(i as i128),
ObjectType::native_field(),
);
let op = if args[0] & (1 << i) != 0 {
let op = if i < bits.len() as u32 && bits[i as usize] {
Operation::Store {
array_id: *a,
index,
Expand All @@ -116,9 +118,10 @@ fn evaluate_intrinsic(
);
}
builtin::Opcode::ToRadix(endian) => {
let mut element = args[0].to_biguint().unwrap().to_radix_le(args[1] as u32);
let byte_count = args[2] as u32;
let diff = if byte_count > element.len() as u32 {
let mut element = BigUint::from_bytes_be(&args[0].to_be_bytes())
.to_radix_le(args[1].to_u128() as u32);
let byte_count = args[2].to_u128() as u32;
let diff = if byte_count >= element.len() as u32 {
byte_count - element.len() as u32
} else {
return Err(RuntimeErrorKind::ArrayOutOfBounds {
Expand Down Expand Up @@ -532,9 +535,8 @@ fn cse_block_with_anchor(
// We do not simplify print statements
builtin::Opcode::Println(_) => (),
_ => {
let args = args.iter().map(|arg| {
NodeEval::from_id(ctx, *arg).into_const_value().map(|f| f.to_u128())
});
let args =
args.iter().map(|arg| NodeEval::from_id(ctx, *arg).into_const_value());

if let Some(args) = args.collect() {
update2.mark = Mark::Deleted;
Expand Down

0 comments on commit 12f3e7e

Please sign in to comment.