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 refactor): pad radix result #1730

Merged
merged 3 commits into from
Jun 16, 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,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[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 @@
use dep::std;

fn main(x : Field) -> pub [u8; 4] {
// The result of this byte array will be little-endian
let byte_array = x.to_le_bytes(31);
let mut first_four_bytes = [0; 4];
for i in 0..4 {
first_four_bytes[i] = byte_array[i];
}
// Issue #617 fix
// We were incorrectly mapping our output array from bit decomposition functions during acir generation
first_four_bytes[3] = byte_array[31];
first_four_bytes
}
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,8 @@ impl AcirContext {

let input_expr = &self.vars[input_var].to_expression();

let limbs = self.acir_ir.radix_le_decompose(input_expr, radix, limb_count)?;
let bit_size = u32::BITS - (radix - 1).leading_zeros();
let limbs = self.acir_ir.radix_le_decompose(input_expr, radix, limb_count, bit_size)?;

let mut limb_vars = vecmap(limbs, |witness| {
let witness = self.add_data(AcirVarData::Witness(witness));
Expand All @@ -603,6 +604,16 @@ impl AcirContext {
limb_vars.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;
let zero = self.add_constant(FieldElement::zero());
while limb_vars.len() < limb_count_with_padding as usize {
limb_vars.push(AcirValue::Var(zero, result_element_type));
}

Ok(vec![AcirValue::Array(limb_vars.into())])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ impl GeneratedAcir {
input_expr: &Expression,
radix: u32,
limb_count: u32,
bit_size: u32,
) -> Result<Vec<Witness>, AcirGenError> {
let bit_size = u32::BITS - (radix - 1).leading_zeros();
let radix_big = BigUint::from(radix);
assert_eq!(
BigUint::from(2u128).pow(bit_size),
Expand Down