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: zero out input to to_radix calls if inactive #4116

Merged
merged 4 commits into from
Jan 25, 2024
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
5 changes: 3 additions & 2 deletions compiler/noirc_evaluator/src/ssa/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ impl Intrinsic {
match self {
Intrinsic::AssertConstant | Intrinsic::ApplyRangeConstraint => true,

// These apply a constraint that the input must fit into a specified number of limbs.
Intrinsic::ToBits(_) | Intrinsic::ToRadix(_) => true,

Intrinsic::Sort
| Intrinsic::ArrayLen
| Intrinsic::SlicePushBack
Expand All @@ -91,8 +94,6 @@ impl Intrinsic {
| Intrinsic::SliceInsert
| Intrinsic::SliceRemove
| Intrinsic::StrAsBytes
| Intrinsic::ToBits(_)
| Intrinsic::ToRadix(_)
| Intrinsic::FromField
| Intrinsic::AsField => false,

Expand Down
26 changes: 24 additions & 2 deletions compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ use crate::ssa::{
dfg::{CallStack, InsertInstructionResult},
function::Function,
function_inserter::FunctionInserter,
instruction::{BinaryOp, Instruction, InstructionId, TerminatorInstruction},
instruction::{BinaryOp, Instruction, InstructionId, Intrinsic, TerminatorInstruction},
types::Type,
value::ValueId,
value::{Value, ValueId},
},
ssa_gen::Ssa,
};
Expand Down Expand Up @@ -683,6 +683,28 @@ impl<'f> Context<'f> {
);
Instruction::RangeCheck { value, max_bit_size, assert_message }
}
Instruction::Call { func, mut arguments } => match self.inserter.function.dfg[func]
{
Value::Intrinsic(Intrinsic::ToBits(_) | Intrinsic::ToRadix(_)) => {
let field = arguments[0];
let argument_type = self.inserter.function.dfg.type_of_value(field);

let casted_condition = self.insert_instruction(
Instruction::Cast(condition, argument_type),
call_stack.clone(),
);
let field = self.insert_instruction(
Instruction::binary(BinaryOp::Mul, field, casted_condition),
call_stack.clone(),
);

arguments[0] = field;

Instruction::Call { func, arguments }
}

_ => Instruction::Call { func, arguments },
},
other => other,
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions test_programs/compile_success_empty/intrinsic_die/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use dep::std;
// This test checks that we perform dead-instruction-elimination on intrinsic functions.
fn main(x: Field) {
let bytes = x.to_be_bytes(32);

let hash = std::hash::pedersen_commitment([x]);
let _p1 = std::scalar_mul::fixed_base_embedded_curve(x, 0);
}
1 change: 1 addition & 0 deletions test_programs/execution_success/to_le_bytes/Prover.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
x = "2040124"
cond = false
9 changes: 8 additions & 1 deletion test_programs/execution_success/to_le_bytes/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main(x: Field) -> pub [u8; 31] {
fn main(x: Field, cond: bool) -> pub [u8; 31] {
// The result of this byte array will be little-endian
let byte_array = x.to_le_bytes(31);
assert(byte_array.len() == 31);
Expand All @@ -7,5 +7,12 @@ fn main(x: Field) -> pub [u8; 31] {
for i in 0..31 {
bytes[i] = byte_array[i];
}

if cond {
// We've set x = "2040124" so we shouldn't be able to represent this as a single byte.
let bad_byte_array = x.to_le_bytes(1);
assert_eq(bad_byte_array.len(), 1);
}

bytes
}
Loading