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

feat: Sync from noir #7257

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .noir-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6936a52bb4de2170b6e4c41afd9cf4327d4fbeb1
7b77bbfc19c51829814149e623257a3424d8e8c2
2 changes: 0 additions & 2 deletions noir/noir-repo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 66 additions & 15 deletions noir/noir-repo/acvm-repo/brillig_vm/src/black_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) fn evaluate_black_box<F: AcirField, Solver: BlackBoxFunctionSolver<F>
op: &BlackBoxOp,
solver: &Solver,
memory: &mut Memory<F>,
bigint_solver: &mut BigIntSolver,
bigint_solver: &mut BrilligBigintSolver,
) -> Result<(), BlackBoxResolutionError> {
match op {
BlackBoxOp::AES128Encrypt { inputs, iv, key, outputs } => {
Expand Down Expand Up @@ -270,38 +270,44 @@ pub(crate) fn evaluate_black_box<F: AcirField, Solver: BlackBoxFunctionSolver<F>
BlackBoxOp::BigIntAdd { lhs, rhs, output } => {
let lhs = memory.read(*lhs).try_into().unwrap();
let rhs = memory.read(*rhs).try_into().unwrap();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_op(lhs, rhs, output, BlackBoxFunc::BigIntAdd)?;

let new_id = bigint_solver.bigint_op(lhs, rhs, BlackBoxFunc::BigIntAdd)?;
memory.write(*output, new_id.into());
Ok(())
}
BlackBoxOp::BigIntSub { lhs, rhs, output } => {
let lhs = memory.read(*lhs).try_into().unwrap();
let rhs = memory.read(*rhs).try_into().unwrap();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_op(lhs, rhs, output, BlackBoxFunc::BigIntSub)?;

let new_id = bigint_solver.bigint_op(lhs, rhs, BlackBoxFunc::BigIntSub)?;
memory.write(*output, new_id.into());
Ok(())
}
BlackBoxOp::BigIntMul { lhs, rhs, output } => {
let lhs = memory.read(*lhs).try_into().unwrap();
let rhs = memory.read(*rhs).try_into().unwrap();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_op(lhs, rhs, output, BlackBoxFunc::BigIntMul)?;

let new_id = bigint_solver.bigint_op(lhs, rhs, BlackBoxFunc::BigIntMul)?;
memory.write(*output, new_id.into());
Ok(())
}
BlackBoxOp::BigIntDiv { lhs, rhs, output } => {
let lhs = memory.read(*lhs).try_into().unwrap();
let rhs = memory.read(*rhs).try_into().unwrap();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_op(lhs, rhs, output, BlackBoxFunc::BigIntDiv)?;

let new_id = bigint_solver.bigint_op(lhs, rhs, BlackBoxFunc::BigIntDiv)?;
memory.write(*output, new_id.into());
Ok(())
}
BlackBoxOp::BigIntFromLeBytes { inputs, modulus, output } => {
let input = read_heap_vector(memory, inputs);
let input: Vec<u8> = input.iter().map(|x| x.try_into().unwrap()).collect();
let modulus = read_heap_vector(memory, modulus);
let modulus: Vec<u8> = modulus.iter().map(|x| x.try_into().unwrap()).collect();
let output = memory.read(*output).try_into().unwrap();
bigint_solver.bigint_from_bytes(&input, &modulus, output)?;

let new_id = bigint_solver.bigint_from_bytes(&input, &modulus)?;
memory.write(*output, new_id.into());

Ok(())
}
BlackBoxOp::BigIntToLeBytes { input, output } => {
Expand Down Expand Up @@ -381,6 +387,46 @@ pub(crate) fn evaluate_black_box<F: AcirField, Solver: BlackBoxFunctionSolver<F>
}
}

/// Wrapper over the generic bigint solver to automatically assign bigint ids in brillig
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub(crate) struct BrilligBigintSolver {
bigint_solver: BigIntSolver,
last_id: u32,
}

impl BrilligBigintSolver {
pub(crate) fn create_bigint_id(&mut self) -> u32 {
let output = self.last_id;
self.last_id += 1;
output
}

pub(crate) fn bigint_from_bytes(
&mut self,
inputs: &[u8],
modulus: &[u8],
) -> Result<u32, BlackBoxResolutionError> {
let id = self.create_bigint_id();
self.bigint_solver.bigint_from_bytes(inputs, modulus, id)?;
Ok(id)
}

pub(crate) fn bigint_to_bytes(&self, input: u32) -> Result<Vec<u8>, BlackBoxResolutionError> {
self.bigint_solver.bigint_to_bytes(input)
}

pub(crate) fn bigint_op(
&mut self,
lhs: u32,
rhs: u32,
func: BlackBoxFunc,
) -> Result<u32, BlackBoxResolutionError> {
let id = self.create_bigint_id();
self.bigint_solver.bigint_op(lhs, rhs, id, func)?;
Ok(id)
}
}

fn black_box_function_from_op(op: &BlackBoxOp) -> BlackBoxFunc {
match op {
BlackBoxOp::AES128Encrypt { .. } => BlackBoxFunc::AES128Encrypt,
Expand Down Expand Up @@ -414,10 +460,10 @@ mod test {
brillig::{BlackBoxOp, MemoryAddress},
FieldElement,
};
use acvm_blackbox_solver::{BigIntSolver, StubbedBlackBoxSolver};
use acvm_blackbox_solver::StubbedBlackBoxSolver;

use crate::{
black_box::{evaluate_black_box, to_u8_vec, to_value_vec},
black_box::{evaluate_black_box, to_u8_vec, to_value_vec, BrilligBigintSolver},
HeapArray, HeapVector, Memory,
};

Expand All @@ -439,8 +485,13 @@ mod test {
output: HeapArray { pointer: 2.into(), size: 32 },
};

evaluate_black_box(&op, &StubbedBlackBoxSolver, &mut memory, &mut BigIntSolver::default())
.unwrap();
evaluate_black_box(
&op,
&StubbedBlackBoxSolver,
&mut memory,
&mut BrilligBigintSolver::default(),
)
.unwrap();

let result = memory.read_slice(MemoryAddress(result_pointer), 32);

Expand Down
6 changes: 3 additions & 3 deletions noir/noir-repo/acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use acir::brillig::{
HeapVector, MemoryAddress, Opcode, ValueOrArray,
};
use acir::AcirField;
use acvm_blackbox_solver::{BigIntSolver, BlackBoxFunctionSolver};
use acvm_blackbox_solver::BlackBoxFunctionSolver;
use arithmetic::{evaluate_binary_field_op, evaluate_binary_int_op, BrilligArithmeticError};
use black_box::evaluate_black_box;
use black_box::{evaluate_black_box, BrilligBigintSolver};
use num_bigint::BigUint;

// Re-export `brillig`.
Expand Down Expand Up @@ -88,7 +88,7 @@ pub struct VM<'a, F, B: BlackBoxFunctionSolver<F>> {
/// The solver for blackbox functions
black_box_solver: &'a B,
// The solver for big integers
bigint_solver: BigIntSolver,
bigint_solver: BrilligBigintSolver,
}

impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
Expand Down
1 change: 0 additions & 1 deletion noir/noir-repo/compiler/fm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ codespan-reporting.workspace = true
serde.workspace = true

[dev-dependencies]
tempfile.workspace = true
iter-extended.workspace = true
47 changes: 18 additions & 29 deletions noir/noir-repo/compiler/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,17 @@ mod path_normalization {
#[cfg(test)]
mod tests {
use super::*;
use tempfile::{tempdir, TempDir};

// Returns the absolute path to the file
fn create_dummy_file(dir: &TempDir, file_name: &Path) -> PathBuf {
let file_path = dir.path().join(file_name);
let _file = std::fs::File::create(&file_path).unwrap();
file_path
fn add_file(fm: &mut FileManager, file_name: &Path) -> FileId {
fm.add_file_with_source(file_name, "fn foo() {}".to_string()).unwrap()
}

#[test]
fn path_resolve_file_module_other_ext() {
let dir = tempdir().unwrap();
let file_name = Path::new("foo.nr");
create_dummy_file(&dir, file_name);
let dir = PathBuf::new();
let mut fm = FileManager::new(&dir);

let mut fm = FileManager::new(dir.path());

let file_id = fm.add_file_with_source(file_name, "fn foo() {}".to_string()).unwrap();
let file_id = add_file(&mut fm, &dir.join("foo.nr"));

assert!(fm.path(file_id).unwrap().ends_with("foo.nr"));
}
Expand All @@ -213,23 +206,19 @@ mod tests {
/// they should both resolve to ../foo.nr
#[test]
fn path_resolve_modules_with_different_paths_as_same_file() {
let dir = tempdir().unwrap();
let sub_dir = TempDir::new_in(&dir).unwrap();
let sub_sub_dir = TempDir::new_in(&sub_dir).unwrap();

let mut fm = FileManager::new(dir.path());

// Create a lib.nr file at the root.
let file_name = Path::new("lib.nr");
create_dummy_file(&dir, file_name);

// Create another path with `./` and `../` inside it
let second_file_name = PathBuf::from(sub_sub_dir.path()).join("./../../lib.nr");

// Add both files to the file manager
let file_id = fm.add_file_with_source(file_name, "fn foo() {}".to_string()).unwrap();
let second_file_id =
fm.add_file_with_source(&second_file_name, "fn foo() {}".to_string()).unwrap();
let dir = PathBuf::new();
let mut fm = FileManager::new(&dir);

// Create a lib.nr file at the root and add it to the file manager.
let file_id = add_file(&mut fm, &dir.join("lib.nr"));

// Create another path with `./` and `../` inside it, and add it to the file manager
let sub_dir = dir.join("sub_dir");
let sub_sub_dir = sub_dir.join("sub_sub_dir");
let second_file_id = add_file(
&mut fm,
PathBuf::from(sub_sub_dir.as_path()).join("./../../lib.nr").as_path(),
);

assert_eq!(file_id, second_file_id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(
brillig_context,
lhs_modulus,
rhs_modulus,
output,
modulus_id,
);
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntAdd {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -267,13 +261,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(
brillig_context,
lhs_modulus,
rhs_modulus,
output,
modulus_id,
);
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntSub {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -291,13 +279,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(
brillig_context,
lhs_modulus,
rhs_modulus,
output,
modulus_id,
);
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntMul {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -315,13 +297,7 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
) = (function_arguments, function_results)
{
prepare_bigint_output(
brillig_context,
lhs_modulus,
rhs_modulus,
output,
modulus_id,
);
prepare_bigint_output(brillig_context, lhs_modulus, rhs_modulus, modulus_id);
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntDiv {
lhs: lhs.address,
rhs: rhs.address,
Expand All @@ -341,8 +317,6 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
{
let inputs_vector = convert_array_or_vector(brillig_context, inputs, bb_func);
let modulus_vector = convert_array_or_vector(brillig_context, modulus, bb_func);
let output_id = brillig_context.get_new_bigint_id();
brillig_context.const_instruction(*output, F::from(output_id as u128));
brillig_context.black_box_op_instruction(BlackBoxOp::BigIntFromLeBytes {
inputs: inputs_vector.to_heap_vector(),
modulus: modulus_vector.to_heap_vector(),
Expand Down Expand Up @@ -447,7 +421,6 @@ fn prepare_bigint_output<F: AcirField + DebugToString>(
brillig_context: &mut BrilligContext<F>,
lhs_modulus: &SingleAddrVariable,
rhs_modulus: &SingleAddrVariable,
output: &SingleAddrVariable,
modulus_id: &SingleAddrVariable,
) {
// Check moduli
Expand All @@ -464,8 +437,6 @@ fn prepare_bigint_output<F: AcirField + DebugToString>(
Some("moduli should be identical in BigInt operation".to_string()),
);
brillig_context.deallocate_register(condition);
// Set output id
let output_id = brillig_context.get_new_bigint_id();
brillig_context.const_instruction(*output, F::from(output_id as u128));

brillig_context.mov_instruction(modulus_id.address, lhs_modulus.address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ pub(crate) struct BrilligContext<F> {
next_section: usize,
/// IR printer
debug_show: DebugShow,
/// Counter for generating bigint ids in unconstrained functions
bigint_new_id: u32,
}

impl<F: AcirField + DebugToString> BrilligContext<F> {
Expand All @@ -105,15 +103,9 @@ impl<F: AcirField + DebugToString> BrilligContext<F> {
section_label: 0,
next_section: 1,
debug_show: DebugShow::new(enable_debug_trace),
bigint_new_id: 0,
}
}

pub(crate) fn get_new_bigint_id(&mut self) -> u32 {
let result = self.bigint_new_id;
self.bigint_new_id += 1;
result
}
/// Adds a brillig instruction to the brillig byte code
fn push_opcode(&mut self, opcode: BrilligOpcode<F>) {
self.obj.push_opcode(opcode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ impl<F: AcirField + DebugToString> BrilligContext<F> {
section_label: 0,
next_section: 1,
debug_show: DebugShow::new(false),
bigint_new_id: 0,
};

context.codegen_entry_point(&arguments, &return_parameters);
Expand Down
1 change: 0 additions & 1 deletion noir/noir-repo/compiler/noirc_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ lalrpop-util = { version = "0.20.2", features = ["lexer"] }
base64.workspace = true
strum = "0.24"
strum_macros = "0.24"
tempfile.workspace = true

[build-dependencies]
lalrpop = "0.20.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ impl<'context> Elaborator<'context> {
Interpreter::new(self.interner, &mut self.comptime_scopes, self.crate_id);

let location = Location::new(span, self.file);
let arguments = vec![(Value::TypeDefinition(struct_id), location)];
let arguments = vec![(Value::StructDefinition(struct_id), location)];

let value = interpreter
.call_function(function, arguments, TypeBindings::new(), location)
Expand Down
Loading
Loading