From 7ffccf7f060aee30b08ef7fda75d8695f047abd8 Mon Sep 17 00:00:00 2001 From: Michael J Klein Date: Mon, 1 Jul 2024 10:28:50 -0400 Subject: [PATCH 1/2] feat!: rename struct-specific TypeDefinition -> StructDefinition (#5356) # Description ## Problem\* `TypeDefinition` is currently struct-specific ## Summary\* ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- compiler/noirc_frontend/src/elaborator/mod.rs | 2 +- .../src/hir/comptime/interpreter/builtin.rs | 38 +++++++++---------- .../noirc_frontend/src/hir/comptime/value.rs | 10 ++--- compiler/noirc_frontend/src/hir_def/types.rs | 4 +- compiler/noirc_frontend/src/lexer/token.rs | 6 +-- .../noirc_frontend/src/parser/parser/types.rs | 10 ++--- noir_stdlib/src/meta/type_def.nr | 14 +++---- .../type_definition_annotation/src/main.nr | 2 +- .../comptime_type_definition/src/main.nr | 2 +- .../derive_impl/src/main.nr | 2 +- 10 files changed, 45 insertions(+), 45 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index ae8237706cc..e0671d6f7ff 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -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) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 1c0c4e6f274..8523e13aeea 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -19,9 +19,9 @@ pub(super) fn call_builtin( "array_len" => array_len(&arguments), "as_slice" => as_slice(arguments), "slice_push_back" => slice_push_back(arguments), - "type_def_as_type" => type_def_as_type(interner, arguments), - "type_def_generics" => type_def_generics(interner, arguments), - "type_def_fields" => type_def_fields(interner, arguments), + "struct_def_as_type" => struct_def_as_type(interner, arguments), + "struct_def_generics" => struct_def_generics(interner, arguments), + "struct_def_fields" => struct_def_fields(interner, arguments), _ => { let item = format!("Comptime evaluation for builtin function {name}"); Err(InterpreterError::Unimplemented { item, location }) @@ -63,19 +63,19 @@ fn slice_push_back(mut arguments: Vec<(Value, Location)>) -> IResult { } /// fn as_type(self) -> Quoted -fn type_def_as_type( +fn struct_def_as_type( interner: &NodeInterner, mut arguments: Vec<(Value, Location)>, ) -> IResult { assert_eq!(arguments.len(), 1, "ICE: `generics` should only receive a single argument"); - let (type_def, span) = match arguments.pop() { - Some((Value::TypeDefinition(id), location)) => (id, location.span), + let (struct_def, span) = match arguments.pop() { + Some((Value::StructDefinition(id), location)) => (id, location.span), other => { - unreachable!("ICE: `as_type` expected a `TypeDefinition` argument, found {other:?}") + unreachable!("ICE: `as_type` expected a `StructDefinition` argument, found {other:?}") } }; - let struct_def = interner.get_struct(type_def); + let struct_def = interner.get_struct(struct_def); let struct_def = struct_def.borrow(); let make_token = |name| SpannedToken::new(Token::Ident(name), span); @@ -92,19 +92,19 @@ fn type_def_as_type( } /// fn generics(self) -> [Quoted] -fn type_def_generics( +fn struct_def_generics( interner: &NodeInterner, mut arguments: Vec<(Value, Location)>, ) -> IResult { assert_eq!(arguments.len(), 1, "ICE: `generics` should only receive a single argument"); - let (type_def, span) = match arguments.pop() { - Some((Value::TypeDefinition(id), location)) => (id, location.span), + let (struct_def, span) = match arguments.pop() { + Some((Value::StructDefinition(id), location)) => (id, location.span), other => { - unreachable!("ICE: `as_type` expected a `TypeDefinition` argument, found {other:?}") + unreachable!("ICE: `as_type` expected a `StructDefinition` argument, found {other:?}") } }; - let struct_def = interner.get_struct(type_def); + let struct_def = interner.get_struct(struct_def); let generics = struct_def .borrow() @@ -121,20 +121,20 @@ fn type_def_generics( } /// fn fields(self) -> [(Quoted, Quoted)] -/// Returns (name, type) pairs of each field of this TypeDefinition -fn type_def_fields( +/// Returns (name, type) pairs of each field of this StructDefinition +fn struct_def_fields( interner: &mut NodeInterner, mut arguments: Vec<(Value, Location)>, ) -> IResult { assert_eq!(arguments.len(), 1, "ICE: `generics` should only receive a single argument"); - let (type_def, span) = match arguments.pop() { - Some((Value::TypeDefinition(id), location)) => (id, location.span), + let (struct_def, span) = match arguments.pop() { + Some((Value::StructDefinition(id), location)) => (id, location.span), other => { - unreachable!("ICE: `as_type` expected a `TypeDefinition` argument, found {other:?}") + unreachable!("ICE: `as_type` expected a `StructDefinition` argument, found {other:?}") } }; - let struct_def = interner.get_struct(type_def); + let struct_def = interner.get_struct(struct_def); let struct_def = struct_def.borrow(); let make_token = |name| SpannedToken::new(Token::Ident(name), span); diff --git a/compiler/noirc_frontend/src/hir/comptime/value.rs b/compiler/noirc_frontend/src/hir/comptime/value.rs index c956cdb5796..adb13c4bfbc 100644 --- a/compiler/noirc_frontend/src/hir/comptime/value.rs +++ b/compiler/noirc_frontend/src/hir/comptime/value.rs @@ -44,7 +44,7 @@ pub enum Value { Array(Vector, Type), Slice(Vector, Type), Code(Rc), - TypeDefinition(StructId), + StructDefinition(StructId), } impl Value { @@ -74,7 +74,7 @@ impl Value { Value::Array(_, typ) => return Cow::Borrowed(typ), Value::Slice(_, typ) => return Cow::Borrowed(typ), Value::Code(_) => Type::Quoted(QuotedType::Quoted), - Value::TypeDefinition(_) => Type::Quoted(QuotedType::TypeDefinition), + Value::StructDefinition(_) => Type::Quoted(QuotedType::StructDefinition), Value::Pointer(element) => { let element = element.borrow().get_type().into_owned(); Type::MutableReference(Box::new(element)) @@ -192,7 +192,7 @@ impl Value { } }; } - Value::Pointer(_) | Value::TypeDefinition(_) => { + Value::Pointer(_) | Value::StructDefinition(_) => { return Err(InterpreterError::CannotInlineMacro { value: self, location }) } }; @@ -298,7 +298,7 @@ impl Value { HirExpression::Literal(HirLiteral::Slice(HirArrayLiteral::Standard(elements))) } Value::Code(block) => HirExpression::Unquote(unwrap_rc(block)), - Value::Pointer(_) | Value::TypeDefinition(_) => { + Value::Pointer(_) | Value::StructDefinition(_) => { return Err(InterpreterError::CannotInlineMacro { value: self, location }) } }; @@ -398,7 +398,7 @@ impl Display for Value { } write!(f, " }}") } - Value::TypeDefinition(_) => write!(f, "(type definition)"), + Value::StructDefinition(_) => write!(f, "(struct definition)"), } } } diff --git a/compiler/noirc_frontend/src/hir_def/types.rs b/compiler/noirc_frontend/src/hir_def/types.rs index 0a7797c2bfb..b529ca17887 100644 --- a/compiler/noirc_frontend/src/hir_def/types.rs +++ b/compiler/noirc_frontend/src/hir_def/types.rs @@ -214,7 +214,7 @@ pub enum QuotedType { Quoted, TopLevelItem, Type, - TypeDefinition, + StructDefinition, } /// A list of TypeVariableIds to bind to a type. Storing the @@ -1176,7 +1176,7 @@ impl std::fmt::Display for QuotedType { QuotedType::Quoted => write!(f, "Quoted"), QuotedType::TopLevelItem => write!(f, "TopLevelItem"), QuotedType::Type => write!(f, "Type"), - QuotedType::TypeDefinition => write!(f, "TypeDefinition"), + QuotedType::StructDefinition => write!(f, "StructDefinition"), } } } diff --git a/compiler/noirc_frontend/src/lexer/token.rs b/compiler/noirc_frontend/src/lexer/token.rs index f98343ba52f..41de13fb17e 100644 --- a/compiler/noirc_frontend/src/lexer/token.rs +++ b/compiler/noirc_frontend/src/lexer/token.rs @@ -913,7 +913,7 @@ pub enum Keyword { Trait, Type, TypeType, - TypeDefinition, + StructDefinition, Unchecked, Unconstrained, Use, @@ -961,7 +961,7 @@ impl fmt::Display for Keyword { Keyword::Trait => write!(f, "trait"), Keyword::Type => write!(f, "type"), Keyword::TypeType => write!(f, "Type"), - Keyword::TypeDefinition => write!(f, "TypeDefinition"), + Keyword::StructDefinition => write!(f, "StructDefinition"), Keyword::Unchecked => write!(f, "unchecked"), Keyword::Unconstrained => write!(f, "unconstrained"), Keyword::Use => write!(f, "use"), @@ -1012,7 +1012,7 @@ impl Keyword { "trait" => Keyword::Trait, "type" => Keyword::Type, "Type" => Keyword::TypeType, - "TypeDefinition" => Keyword::TypeDefinition, + "StructDefinition" => Keyword::StructDefinition, "unchecked" => Keyword::Unchecked, "unconstrained" => Keyword::Unconstrained, "use" => Keyword::Use, diff --git a/compiler/noirc_frontend/src/parser/parser/types.rs b/compiler/noirc_frontend/src/parser/parser/types.rs index 493ebd1fb2f..32929312d54 100644 --- a/compiler/noirc_frontend/src/parser/parser/types.rs +++ b/compiler/noirc_frontend/src/parser/parser/types.rs @@ -25,7 +25,7 @@ pub(super) fn parse_type_inner<'a>( bool_type(), string_type(), expr_type(), - type_definition_type(), + struct_definition_type(), top_level_item_type(), type_of_quoted_types(), quoted_type(), @@ -80,10 +80,10 @@ pub(super) fn expr_type() -> impl NoirParser { .map_with_span(|_, span| UnresolvedTypeData::Quoted(QuotedType::Expr).with_span(span)) } -/// This is the type `TypeDefinition` - the type of a quoted type definition -pub(super) fn type_definition_type() -> impl NoirParser { - keyword(Keyword::TypeDefinition).map_with_span(|_, span| { - UnresolvedTypeData::Quoted(QuotedType::TypeDefinition).with_span(span) +/// This is the type `StructDefinition` - the type of a quoted struct definition +pub(super) fn struct_definition_type() -> impl NoirParser { + keyword(Keyword::StructDefinition).map_with_span(|_, span| { + UnresolvedTypeData::Quoted(QuotedType::StructDefinition).with_span(span) }) } diff --git a/noir_stdlib/src/meta/type_def.nr b/noir_stdlib/src/meta/type_def.nr index b9354485921..c01aab4b141 100644 --- a/noir_stdlib/src/meta/type_def.nr +++ b/noir_stdlib/src/meta/type_def.nr @@ -1,16 +1,16 @@ -impl TypeDefinition { - /// Return a syntactic version of this type definition as a type. +impl StructDefinition { + /// Return a syntactic version of this struct definition as a type. /// For example, `as_type(quote { type Foo { ... } })` would return `Foo` - #[builtin(type_def_as_type)] + #[builtin(struct_def_as_type)] fn as_type(self) -> Quoted {} - /// Return each generic on this type. The names of these generics are unchanged + /// Return each generic on this struct. The names of these generics are unchanged /// so users may need to keep name collisions in mind if this is used directly in a macro. - #[builtin(type_def_generics)] + #[builtin(struct_def_generics)] fn generics(self) -> [Quoted] {} - /// Returns (name, type) pairs of each field in this type. Each type is as-is + /// Returns (name, type) pairs of each field in this struct. Each type is as-is /// with any generic arguments unchanged. - #[builtin(type_def_fields)] + #[builtin(struct_def_fields)] fn fields(self) -> [(Quoted, Quoted)] {} } diff --git a/test_programs/compile_failure/type_definition_annotation/src/main.nr b/test_programs/compile_failure/type_definition_annotation/src/main.nr index 91f9c3a52f4..d4fef84442d 100644 --- a/test_programs/compile_failure/type_definition_annotation/src/main.nr +++ b/test_programs/compile_failure/type_definition_annotation/src/main.nr @@ -1,7 +1,7 @@ #[fail_assert] struct Foo { x: Field } -comptime fn fail_assert(_typ: TypeDefinition) { +comptime fn fail_assert(_typ: StructDefinition) { assert(false); } diff --git a/test_programs/compile_success_empty/comptime_type_definition/src/main.nr b/test_programs/compile_success_empty/comptime_type_definition/src/main.nr index 025f6a0b0bf..cdfc9bd6b75 100644 --- a/test_programs/compile_success_empty/comptime_type_definition/src/main.nr +++ b/test_programs/compile_success_empty/comptime_type_definition/src/main.nr @@ -6,7 +6,7 @@ struct MyType { field2: (B, C), } -comptime fn my_comptime_fn(typ: TypeDefinition) { +comptime fn my_comptime_fn(typ: StructDefinition) { let _ = typ.as_type(); assert_eq(typ.generics().len(), 3); assert_eq(typ.fields().len(), 2); diff --git a/test_programs/compile_success_empty/derive_impl/src/main.nr b/test_programs/compile_success_empty/derive_impl/src/main.nr index 9636e4c7383..5463a61d969 100644 --- a/test_programs/compile_success_empty/derive_impl/src/main.nr +++ b/test_programs/compile_success_empty/derive_impl/src/main.nr @@ -1,4 +1,4 @@ -comptime fn derive_default(typ: TypeDefinition) -> Quoted { +comptime fn derive_default(typ: StructDefinition) -> Quoted { let generics: [Quoted] = typ.generics(); assert_eq( generics.len(), 0, "derive_default: Deriving Default on generic types is currently unimplemented" From a8928ddcffcae15babf7aa5aff0e462e4549552e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Rodr=C3=ADguez?= Date: Mon, 1 Jul 2024 16:54:14 +0200 Subject: [PATCH 2/2] fix: Runtime brillig bigint id assignment (#5369) # Description ## Problem\* Resolves https://github.com/noir-lang/noir/issues/5368 ## Summary\* Switches BigInd id assignment to be done in runtime, to fix assignment within control flow. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- acvm-repo/brillig_vm/src/black_box.rs | 81 +++++++++++++++---- acvm-repo/brillig_vm/src/lib.rs | 6 +- .../brillig/brillig_gen/brillig_black_box.rs | 39 ++------- .../noirc_evaluator/src/brillig/brillig_ir.rs | 8 -- .../src/brillig/brillig_ir/entry_point.rs | 1 - 5 files changed, 74 insertions(+), 61 deletions(-) diff --git a/acvm-repo/brillig_vm/src/black_box.rs b/acvm-repo/brillig_vm/src/black_box.rs index 36d045efabf..53599f79bc7 100644 --- a/acvm-repo/brillig_vm/src/black_box.rs +++ b/acvm-repo/brillig_vm/src/black_box.rs @@ -42,7 +42,7 @@ pub(crate) fn evaluate_black_box op: &BlackBoxOp, solver: &Solver, memory: &mut Memory, - bigint_solver: &mut BigIntSolver, + bigint_solver: &mut BrilligBigintSolver, ) -> Result<(), BlackBoxResolutionError> { match op { BlackBoxOp::AES128Encrypt { inputs, iv, key, outputs } => { @@ -270,29 +270,33 @@ pub(crate) fn evaluate_black_box 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 } => { @@ -300,8 +304,10 @@ pub(crate) fn evaluate_black_box let input: Vec = input.iter().map(|x| x.try_into().unwrap()).collect(); let modulus = read_heap_vector(memory, modulus); let modulus: Vec = 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 } => { @@ -381,6 +387,46 @@ pub(crate) fn evaluate_black_box } } +/// 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 { + 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, BlackBoxResolutionError> { + self.bigint_solver.bigint_to_bytes(input) + } + + pub(crate) fn bigint_op( + &mut self, + lhs: u32, + rhs: u32, + func: BlackBoxFunc, + ) -> Result { + 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, @@ -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, }; @@ -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); diff --git a/acvm-repo/brillig_vm/src/lib.rs b/acvm-repo/brillig_vm/src/lib.rs index 01f45bf653c..4d2dd2b8333 100644 --- a/acvm-repo/brillig_vm/src/lib.rs +++ b/acvm-repo/brillig_vm/src/lib.rs @@ -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`. @@ -88,7 +88,7 @@ pub struct VM<'a, F, B: BlackBoxFunctionSolver> { /// 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> VM<'a, F, B> { diff --git a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs index 367cdbe4973..aa9cb8cd7a3 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_black_box.rs @@ -243,13 +243,7 @@ pub(crate) fn convert_black_box_call( [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, @@ -267,13 +261,7 @@ pub(crate) fn convert_black_box_call( [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, @@ -291,13 +279,7 @@ pub(crate) fn convert_black_box_call( [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, @@ -315,13 +297,7 @@ pub(crate) fn convert_black_box_call( [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, @@ -341,8 +317,6 @@ pub(crate) fn convert_black_box_call( { 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(), @@ -447,7 +421,6 @@ fn prepare_bigint_output( brillig_context: &mut BrilligContext, lhs_modulus: &SingleAddrVariable, rhs_modulus: &SingleAddrVariable, - output: &SingleAddrVariable, modulus_id: &SingleAddrVariable, ) { // Check moduli @@ -464,8 +437,6 @@ fn prepare_bigint_output( 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); } diff --git a/compiler/noirc_evaluator/src/brillig/brillig_ir.rs b/compiler/noirc_evaluator/src/brillig/brillig_ir.rs index 9785e073be9..80367d07635 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_ir.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_ir.rs @@ -91,8 +91,6 @@ pub(crate) struct BrilligContext { next_section: usize, /// IR printer debug_show: DebugShow, - /// Counter for generating bigint ids in unconstrained functions - bigint_new_id: u32, } impl BrilligContext { @@ -105,15 +103,9 @@ impl BrilligContext { 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) { self.obj.push_opcode(opcode); diff --git a/compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs b/compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs index dc06c2fa0d7..d10e31533dc 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_ir/entry_point.rs @@ -23,7 +23,6 @@ impl BrilligContext { section_label: 0, next_section: 1, debug_show: DebugShow::new(false), - bigint_new_id: 0, }; context.codegen_entry_point(&arguments, &return_parameters);