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: fix incorrect return type being applied to stdlib functions modulus_be_bytes(), modulus_be_bits(), etc. #5278

Merged
merged 4 commits into from
Jun 20, 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
15 changes: 7 additions & 8 deletions compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,19 +1271,19 @@ impl<'interner> Monomorphizer<'interner> {
}
"modulus_le_bits" => {
let bits = FieldElement::modulus().to_radix_le(2);
Some(self.modulus_array_literal(bits, IntegerBitSize::One, location))
Some(self.modulus_slice_literal(bits, IntegerBitSize::One, location))
}
"modulus_be_bits" => {
let bits = FieldElement::modulus().to_radix_be(2);
Some(self.modulus_array_literal(bits, IntegerBitSize::One, location))
Some(self.modulus_slice_literal(bits, IntegerBitSize::One, location))
}
"modulus_be_bytes" => {
let bytes = FieldElement::modulus().to_bytes_be();
Some(self.modulus_array_literal(bytes, IntegerBitSize::Eight, location))
Some(self.modulus_slice_literal(bytes, IntegerBitSize::Eight, location))
}
"modulus_le_bytes" => {
let bytes = FieldElement::modulus().to_bytes_le();
Some(self.modulus_array_literal(bytes, IntegerBitSize::Eight, location))
Some(self.modulus_slice_literal(bytes, IntegerBitSize::Eight, location))
}
_ => None,
};
Expand All @@ -1292,7 +1292,7 @@ impl<'interner> Monomorphizer<'interner> {
None
}

fn modulus_array_literal(
fn modulus_slice_literal(
&self,
bytes: Vec<u8>,
arr_elem_bits: IntegerBitSize,
Expand All @@ -1306,10 +1306,9 @@ impl<'interner> Monomorphizer<'interner> {
Expression::Literal(Literal::Integer((byte as u128).into(), int_type.clone(), location))
});

let typ = Type::Array(bytes_as_expr.len() as u32, Box::new(int_type));

let typ = Type::Slice(Box::new(int_type));
let arr_literal = ArrayLiteral { typ, contents: bytes_as_expr };
Expression::Literal(Literal::Array(arr_literal))
Expression::Literal(Literal::Slice(arr_literal))
}

fn queue_function(
Expand Down
11 changes: 11 additions & 0 deletions test_programs/execution_success/modulus/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ fn main(bn254_modulus_be_bytes: [u8; 32], bn254_modulus_be_bits: [u1; 254]) {
// NOTE: The constraints used in this circuit will only work when testing nargo with the plonk bn254 backend
assert(modulus_size == 254);

assert_reverse(
std::field::modulus_be_bytes(),
std::field::modulus_le_bytes()
);

let modulus_be_byte_array = std::field::modulus_be_bytes();
for i in 0..32 {
assert(modulus_be_byte_array[i] == bn254_modulus_be_bytes[i]);
Expand All @@ -21,3 +26,9 @@ fn main(bn254_modulus_be_bytes: [u8; 32], bn254_modulus_be_bits: [u1; 254]) {
assert(modulus_le_bits[i] == bn254_modulus_be_bits[253 - i]);
}
}

fn assert_reverse(forwards: [u8], backwards: [u8]) {
for i in 0..32 {
assert_eq(forwards[i], backwards[31 - i]);
}
}
Loading