Skip to content

Commit

Permalink
fix: fix incorrect return type being applied to stdlib functions `mod…
Browse files Browse the repository at this point in the history
…ulus_be_bytes()`, `modulus_be_bits()`, etc. (#5278)

# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

We're currently returning an array literal for these methods in the
stdlib whereas the Noir code states that it should return a slice. This
results in issues when passing these "slices" into other functions such
that it causes the inlining pass to panic.

I've fixed this by replacing the array literal with a slice literal.

## Additional Context



## Documentation\*

Check one:
- [ ] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [ ] I have tested the changes locally.
- [ ] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Jun 20, 2024
1 parent f761b7c commit 91a9b72
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
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]);
}
}

0 comments on commit 91a9b72

Please sign in to comment.