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 aztec-packages #5528

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion .aztec-sync-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e44ef7042c87d3c78a14413ad7d54e4ed642ad89
deb69180f3a3670c7a512d4c401c3ad8b5da0e17
4 changes: 2 additions & 2 deletions acvm-repo/blackbox_solver/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct BigIntSolver {
}

impl BigIntSolver {
pub(crate) fn get_bigint(
pub fn get_bigint(
&self,
id: u32,
func: BlackBoxFunc,
Expand All @@ -32,7 +32,7 @@ impl BigIntSolver {
.cloned()
}

pub(crate) fn get_modulus(
pub fn get_modulus(
&self,
id: u32,
func: BlackBoxFunc,
Expand Down
8 changes: 8 additions & 0 deletions acvm-repo/brillig_vm/src/black_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ impl BrilligBigintSolver {
rhs: u32,
func: BlackBoxFunc,
) -> Result<u32, BlackBoxResolutionError> {
let modulus_lhs = self.bigint_solver.get_modulus(lhs, func)?;
let modulus_rhs = self.bigint_solver.get_modulus(rhs, func)?;
if modulus_lhs != modulus_rhs {
return Err(BlackBoxResolutionError::Failed(
func,
"moduli should be identical in BigInt operation".to_string(),
));
}
let id = self.create_bigint_id();
self.bigint_solver.bigint_op(lhs, rhs, id, func)?;
Ok(id)
Expand Down
69 changes: 38 additions & 31 deletions aztec_macros/src/transforms/contract_interface.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use acvm::acir::AcirField;

use noirc_errors::Location;
use noirc_frontend::ast::{Ident, NoirFunction, UnresolvedTypeData};
use noirc_frontend::{
graph::CrateId,
macros_api::{FileId, HirContext, HirExpression, HirLiteral, HirStatement},
macros_api::{FieldElement, FileId, HirContext, HirExpression, HirLiteral, HirStatement},
parse_program,
parser::SortedModule,
Type,
};

use tiny_keccak::{Hasher, Keccak};

use crate::utils::{
constants::SELECTOR_PLACEHOLDER,
errors::AztecMacroError,
hir_utils::{collect_crate_structs, get_contract_module_data, signature_of_type},
};
Expand Down Expand Up @@ -64,11 +67,6 @@ pub fn stub_function(aztec_visibility: &str, func: &NoirFunction, is_static_call
.join(", ");
let fn_return_type: noirc_frontend::ast::UnresolvedType = func.return_type();

let fn_selector = format!(
"dep::aztec::protocol_types::abis::function_selector::FunctionSelector::from_signature(\"{}\")",
SELECTOR_PLACEHOLDER
);

let parameters = func.parameters();
let is_void = if matches!(fn_return_type.typ, UnresolvedTypeData::Unit) { "Void" } else { "" };
let is_static = if is_static_call { "Static" } else { "" };
Expand Down Expand Up @@ -160,7 +158,7 @@ pub fn stub_function(aztec_visibility: &str, func: &NoirFunction, is_static_call

let fn_body = format!(
"{}
let selector = {};
let selector = dep::aztec::protocol_types::abis::function_selector::FunctionSelector::from_field(0);
dep::aztec::context::{}{}{}CallInterface {{
target_contract: self.target_contract,
selector,
Expand All @@ -172,7 +170,6 @@ pub fn stub_function(aztec_visibility: &str, func: &NoirFunction, is_static_call
{}
}}",
args,
fn_selector,
aztec_visibility,
is_static,
is_void,
Expand Down Expand Up @@ -291,27 +288,34 @@ pub fn generate_contract_interface(
Ok(())
}

fn compute_fn_signature(fn_name: &str, parameters: &[Type]) -> String {
format!(
fn compute_fn_signature_hash(fn_name: &str, parameters: &[Type]) -> u32 {
let signature = format!(
"{}({})",
fn_name,
parameters.iter().map(signature_of_type).collect::<Vec<_>>().join(",")
)
);
let mut keccak = Keccak::v256();
let mut result = [0u8; 32];
keccak.update(signature.as_bytes());
keccak.finalize(&mut result);
// Take the first 4 bytes of the hash and convert them to an integer
// If you change the following value you have to change NUM_BYTES_PER_NOTE_TYPE_ID in l1_note_payload.ts as well
let num_bytes_per_note_type_id = 4;
u32::from_be_bytes(result[0..num_bytes_per_note_type_id].try_into().unwrap())
}

// Updates the function signatures in the contract interface with the actual ones, replacing the placeholder.
// This is done by locating the contract interface struct, its functions (stubs) and assuming the last statement of each
// is the constructor for a <visibility>CallInterface. This constructor has a selector field that holds a
// FunctionSelector::from_signature function that receives the signature as a string literal.
// This is done by locating the contract interface struct, its functions (stubs) and assuming the second to last statement of each
// is a let statement initializing the selector with a FunctionSelector::from_field call.
pub fn update_fn_signatures_in_contract_interface(
crate_id: &CrateId,
context: &mut HirContext,
) -> Result<(), (AztecMacroError, FileId)> {
if let Some((name, _, file_id)) = get_contract_module_data(context, crate_id) {
if let Some((struct_name, _, file_id)) = get_contract_module_data(context, crate_id) {
let maybe_interface_struct =
collect_crate_structs(crate_id, context).iter().find_map(|struct_id| {
let r#struct = context.def_interner.get_struct(*struct_id);
if r#struct.borrow().name.0.contents == name {
if r#struct.borrow().name.0.contents == struct_name {
Some(r#struct)
} else {
None
Expand All @@ -329,7 +333,7 @@ pub fn update_fn_signatures_in_contract_interface(
continue;
}

let fn_signature = compute_fn_signature(
let fn_signature_hash = compute_fn_signature_hash(
name,
&fn_parameters
.iter()
Expand Down Expand Up @@ -381,14 +385,12 @@ pub fn update_fn_signatures_in_contract_interface(
context.def_interner.expression(&current_fn_signature_expression_id);

match current_fn_signature_expression {
HirExpression::Literal(HirLiteral::Str(signature)) => {
if signature != SELECTOR_PLACEHOLDER {
HirExpression::Literal(HirLiteral::Integer(value, _)) => {
if !value.is_zero() {
Err((
AztecMacroError::CouldNotGenerateContractInterface {
secondary_message: Some(format!(
"Function signature argument must be a placeholder: {}",
SELECTOR_PLACEHOLDER
)),
secondary_message: Some(
"Function signature argument must be a placeholder with value 0".to_string()),
},
file_id,
))
Expand All @@ -397,20 +399,25 @@ pub fn update_fn_signatures_in_contract_interface(
}
}
_ => Err((
AztecMacroError::CouldNotAssignStorageSlots {
AztecMacroError::CouldNotGenerateContractInterface {
secondary_message: Some(
"Function signature argument must be a literal string".to_string(),
"Function signature argument must be a literal field element"
.to_string(),
),
},
file_id,
)),
}?;

context
.def_interner
.update_expression(current_fn_signature_expression_id, |expr| {
*expr = HirExpression::Literal(HirLiteral::Str(fn_signature))
});
context.def_interner.update_expression(
current_fn_signature_expression_id,
|expr| {
*expr = HirExpression::Literal(HirLiteral::Integer(
FieldElement::from(fn_signature_hash as u128),
false,
))
},
);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion aztec_macros/src/transforms/note_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ pub fn generate_note_interface_impl(module: &mut SortedModule) -> Result<(), Azt
})
.collect::<Result<Vec<_>, _>>()?;
let [note_serialized_len, note_bytes_len]: [_; 2] =
note_interface_generics.try_into().unwrap();
note_interface_generics.try_into().expect(
"NoteInterface must be generic over 2 types, NOTE_FIELDS_LEN and NOTE_BYTES_LEN",
);

// Automatically inject the header field if it's not present
let (header_field_name, _) = if let Some(existing_header) =
Expand Down
1 change: 0 additions & 1 deletion aztec_macros/src/utils/constants.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub const FUNCTION_TREE_HEIGHT: u32 = 5;
pub const MAX_CONTRACT_PRIVATE_FUNCTIONS: usize = 2_usize.pow(FUNCTION_TREE_HEIGHT);
pub const SELECTOR_PLACEHOLDER: &str = "SELECTOR_PLACEHOLDER";
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use acvm::{
use crate::brillig::brillig_ir::{
brillig_variable::{BrilligVariable, BrilligVector, SingleAddrVariable},
debug_show::DebugToString,
BrilligBinaryOp, BrilligContext,
BrilligContext,
};

/// Transforms SSA's black box function calls into the corresponding brillig instructions
Expand Down Expand Up @@ -239,11 +239,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
BlackBoxFunc::RecursiveAggregation => {}
BlackBoxFunc::BigIntAdd => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
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 @@ -257,11 +256,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
}
BlackBoxFunc::BigIntSub => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
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 @@ -275,11 +273,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
}
BlackBoxFunc::BigIntMul => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
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 @@ -293,11 +290,10 @@ pub(crate) fn convert_black_box_call<F: AcirField + DebugToString>(
}
BlackBoxFunc::BigIntDiv => {
if let (
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(modulus_id)],
[BrilligVariable::SingleAddr(lhs), BrilligVariable::SingleAddr(_lhs_modulus), BrilligVariable::SingleAddr(rhs), BrilligVariable::SingleAddr(_rhs_modulus)],
[BrilligVariable::SingleAddr(output), BrilligVariable::SingleAddr(_modulus_id)],
) = (function_arguments, function_results)
{
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 Down Expand Up @@ -416,27 +412,3 @@ fn convert_array_or_vector<F: AcirField + DebugToString>(
),
}
}

fn prepare_bigint_output<F: AcirField + DebugToString>(
brillig_context: &mut BrilligContext<F>,
lhs_modulus: &SingleAddrVariable,
rhs_modulus: &SingleAddrVariable,
modulus_id: &SingleAddrVariable,
) {
// Check moduli
let condition = brillig_context.allocate_register();
let condition_adr = SingleAddrVariable { address: condition, bit_size: 1 };
brillig_context.binary_instruction(
*lhs_modulus,
*rhs_modulus,
condition_adr,
BrilligBinaryOp::Equals,
);
brillig_context.codegen_constrain(
condition_adr,
Some("moduli should be identical in BigInt operation".to_string()),
);
brillig_context.deallocate_register(condition);

brillig_context.mov_instruction(modulus_id.address, lhs_modulus.address);
}
Loading