Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
70 changes: 70 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
rc::Rc,
};

use acvm::{AcirField, FieldElement};
use chumsky::Parser;
use noirc_errors::Location;

Expand Down Expand Up @@ -37,6 +38,11 @@ pub(super) fn call_builtin(
"trait_constraint_eq" => trait_constraint_eq(interner, arguments, location),
"trait_constraint_hash" => trait_constraint_hash(interner, arguments, location),
"quoted_as_trait_constraint" => quoted_as_trait_constraint(interner, arguments, location),
"modulus_num_bits" => modulus_num_bits(interner, arguments, location),
"modulus_le_bits" => modulus_le_bits(interner, arguments, location),
"modulus_be_bits" => modulus_be_bits(interner, arguments, location),
"modulus_be_bytes" => modulus_be_bytes(interner, arguments, location),
"modulus_le_bytes" => modulus_le_bytes(interner, arguments, location),
Comment thread
TomAFrench marked this conversation as resolved.
Outdated
_ => {
let item = format!("Comptime evaluation for builtin function {name}");
Err(InterpreterError::Unimplemented { item, location })
Expand Down Expand Up @@ -386,3 +392,67 @@ fn trait_constraint_eq(

Ok(Value::Bool(constraint_a == constraint_b))
}

fn modulus_num_bits(
_interner: &mut NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
check_argument_count(0, &arguments, location)?;
let bits = FieldElement::max_num_bits().into();
Ok(Value::U64(bits))
}

fn modulus_be_bits(
_interner: &mut NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
check_argument_count(0, &arguments, location)?;

let bits = FieldElement::modulus().to_radix_be(2);
let bits_vector = bits.into_iter().map(|bit| Value::U1(bit != 0)).collect();

let int_type = Type::Integer(crate::ast::Signedness::Unsigned, IntegerBitSize::One);
let typ = Type::Slice(Box::new(int_type));
Ok(Value::Slice(bits_vector, typ))
}

fn modulus_le_bits(
interner: &mut NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let Value::Slice(bits, typ) = modulus_be_bits(interner, arguments, location)? else {
unreachable!("modulus_be_bits must return slice")
};
let reversed_bits = bits.into_iter().rev().collect();
Ok(Value::Slice(reversed_bits, typ))
}

fn modulus_be_bytes(
_interner: &mut NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
check_argument_count(0, &arguments, location)?;

let bytes = FieldElement::modulus().to_bytes_be();
let bytes_vector = bytes.into_iter().map(Value::U8).collect();

let int_type = Type::Integer(crate::ast::Signedness::Unsigned, IntegerBitSize::Eight);
let typ = Type::Slice(Box::new(int_type));
Ok(Value::Slice(bytes_vector, typ))
}

fn modulus_le_bytes(
interner: &mut NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let Value::Slice(bytes, typ) = modulus_be_bytes(interner, arguments, location)? else {
unreachable!("modulus_be_bytes must return slice")
};
let reversed_bytes = bytes.into_iter().rev().collect();
Ok(Value::Slice(reversed_bytes, typ))
}
9 changes: 9 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum Value {
I16(i16),
I32(i32),
I64(i64),
U1(bool),
U8(u8),
U16(u16),
U32(u32),
Expand Down Expand Up @@ -62,6 +63,7 @@ impl Value {
Value::I16(_) => Type::Integer(Signedness::Signed, IntegerBitSize::Sixteen),
Value::I32(_) => Type::Integer(Signedness::Signed, IntegerBitSize::ThirtyTwo),
Value::I64(_) => Type::Integer(Signedness::Signed, IntegerBitSize::SixtyFour),
Value::U1(_) => Type::Integer(Signedness::Unsigned, IntegerBitSize::One),
Value::U8(_) => Type::Integer(Signedness::Unsigned, IntegerBitSize::Eight),
Value::U16(_) => Type::Integer(Signedness::Unsigned, IntegerBitSize::Sixteen),
Value::U32(_) => Type::Integer(Signedness::Unsigned, IntegerBitSize::ThirtyTwo),
Expand Down Expand Up @@ -124,6 +126,9 @@ impl Value {
let value = (value as u128).into();
ExpressionKind::Literal(Literal::Integer(value, negative))
}
Value::U1(value) => {
ExpressionKind::Literal(Literal::Integer((value as u128).into(), false))
}
Value::U8(value) => {
ExpressionKind::Literal(Literal::Integer((value as u128).into(), false))
}
Expand Down Expand Up @@ -249,6 +254,9 @@ impl Value {
let value = (value as u128).into();
HirExpression::Literal(HirLiteral::Integer(value, negative))
}
Value::U1(value) => {
HirExpression::Literal(HirLiteral::Integer((value as u128).into(), false))
}
Value::U8(value) => {
HirExpression::Literal(HirLiteral::Integer((value as u128).into(), false))
}
Expand Down Expand Up @@ -385,6 +393,7 @@ impl Display for Value {
Value::I16(value) => write!(f, "{value}"),
Value::I32(value) => write!(f, "{value}"),
Value::I64(value) => write!(f, "{value}"),
Value::U1(value) => write!(f, "{value}"),
Value::U8(value) => write!(f, "{value}"),
Value::U16(value) => write!(f, "{value}"),
Value::U32(value) => write!(f, "{value}"),
Expand Down
17 changes: 15 additions & 2 deletions noir_stdlib/src/compat.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
global BN254_MODULUS_BE_BYTES: [u8] = &[
comptime global BN254_MODULUS_BE_BYTES: [u8] = &[
48, 100, 78, 114, 225, 49, 160, 41, 184, 80, 69, 182, 129, 129, 88, 93, 40, 51, 232, 72, 121, 185, 112, 145, 67, 225, 245, 147, 240, 0, 0, 1
];

pub fn is_bn254() -> bool {
crate::field::modulus_be_bytes() == BN254_MODULUS_BE_BYTES
comptime {
// We can't use the `Eq` trait here due to limitations on calling non-comptime functions
// defined within the same crate.
let mut eq = true;

let modulus_be_bytes = crate::field::modulus_be_bytes();
// We can't do `BN254_MODULUS_BE_BYTES.len()` due to limitations on calling non-comptime functions.
assert_eq(crate::field::modulus_num_bits(), 254);
for i in 0..32 {
eq &= modulus_be_bytes[i] == BN254_MODULUS_BE_BYTES[i];
}

eq
}
}
12 changes: 7 additions & 5 deletions noir_stdlib/src/field/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,21 @@ impl Field {
}

#[builtin(modulus_num_bits)]
pub fn modulus_num_bits() -> u64 {}
pub comptime fn modulus_num_bits() -> u64 {}

#[builtin(modulus_be_bits)]
pub fn modulus_be_bits() -> [u1] {}
pub comptime fn modulus_be_bits() -> [u1] {}

#[builtin(modulus_le_bits)]
pub fn modulus_le_bits() -> [u1] {}
pub comptime fn modulus_le_bits() -> [u1] {}

#[builtin(modulus_be_bytes)]
pub fn modulus_be_bytes() -> [u8] {}
pub comptime fn modulus_be_bytes() -> [u8] {}

#[builtin(modulus_le_bytes)]
pub fn modulus_le_bytes() -> [u8] {}
pub comptime fn modulus_le_bytes() -> [u8] {}


// Convert a 32 byte array to a field element by modding
pub fn bytes32_to_field(bytes32: [u8; 32]) -> Field {
// Convert it to a field element
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "comptime_slice_equality"
type = "bin"
authors = [""]
compiler_version = ">=0.31.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
comptime {
assert_eq(&[1], &[1]);
}
}