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(ssa): Remove padding from ToRadix call with constant inputs #2479

Merged
merged 1 commit into from
Aug 29, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "to_bytes_consistent"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x = "2040124"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This test aims to check that we have consistent behavior
// between a `to_be_bytes` call (which is radix decomposition under the hood)
// with constant inputs or with witness inputs.

// x = 2040124
fn main(x : Field) {
let byte_array = x.to_be_bytes(31);
let x_as_constant = 2040124;
let constant_byte_array = x_as_constant.to_be_bytes(31);
assert(constant_byte_array.len() == byte_array.len());
for i in 0..constant_byte_array.len() {
assert(constant_byte_array[i] == byte_array[i]);
}
}
9 changes: 0 additions & 9 deletions crates/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@

/// Slices have a tuple structure (slice length, slice contents) to enable logic
/// that uses dynamic slice lengths (such as with merging slices in the flattening pass).
/// This method codegens an update to the slice length.

Check warning on line 224 in crates/noirc_evaluator/src/ssa/ir/instruction/call.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (codegens)
///
/// The binary operation performed on the slice length is always an addition or subtraction of `1`.
/// This is because the slice length holds the user length (length as displayed by a `.len()` call),
Expand Down Expand Up @@ -334,15 +334,6 @@
limbs.reverse();
}

// For legacy reasons (see #617) the to_radix interface supports 256 bits even though
// FieldElement::max_num_bits() is only 254 bits. Any limbs beyond the specified count
// become zero padding.
let max_decomposable_bits: u32 = 256;
let limb_count_with_padding = max_decomposable_bits / bit_size;
while limbs.len() < limb_count_with_padding as usize {
limbs.push(FieldElement::zero());
}

make_constant_array(dfg, limbs, Type::unsigned(bit_size))
}

Expand Down
4 changes: 2 additions & 2 deletions crates/noirc_evaluator/src/ssa/ssa_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ mod tests {

#[test]
fn insert_constant_call() {
// `bits` should be an array of constants [1, 1, 1, 0...]:
// `bits` should be an array of constants [1, 1, 1, 0...] of length 8:
// let x = 7;
// let bits = x.to_le_bits(8);
let func_id = Id::test_new(0);
Expand All @@ -404,7 +404,7 @@ mod tests {
Value::NumericConstant { constant, .. } => *constant,
_ => panic!(),
};
assert_eq!(slice_len, FieldElement::from(256u128));
assert_eq!(slice_len, FieldElement::from(8_u128));

let slice = match &builder.current_function.dfg[call_results[1]] {
Value::Array { array, .. } => array,
Expand Down