Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: to-bits and to-radix for > 128 bits #1312

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/noirc_driver/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::program::{deserialize_circuit, serialize_circuit};
use acvm::acir::circuit::Circuit;
use noirc_abi::Abi;
use serde::{Deserialize, Serialize};
use crate::program::{serialize_circuit, deserialize_circuit};

/// Describes the types of smart contract functions that are allowed.
/// Unlike the similar enum in noirc_frontend, 'open' and 'unconstrained'
Expand Down
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