Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/ir/witgen-contract-instance' int…
Browse files Browse the repository at this point in the history
…o ek/feat/new-address-implementation/modify-encoder-to-allow-for-arbitrary-types
  • Loading branch information
sklppy88 committed Oct 17, 2024
1 parent 10a00c9 commit 2d638df
Show file tree
Hide file tree
Showing 35 changed files with 1,245 additions and 1,157 deletions.
12 changes: 10 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm/tests/execution.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2314,11 +2314,19 @@ TEST_F(AvmExecutionTests, opGetContractInstanceOpcodes)

// Generate Hint for call operation
// Note: opcode does not write 'address' into memory
// We store this random value since it's part of the return - we could return the rest as well but we don't need to.
auto returned_point = grumpkin::g1::affine_element::random_element();
PublicKeysHint public_keys_hints = {
returned_point,
grumpkin::g1::affine_element::random_element(),
grumpkin::g1::affine_element::random_element(),
grumpkin::g1::affine_element::random_element(),
};
auto execution_hints =
ExecutionHints().with_contract_instance_hints({ { address, { address, 1, 2, 3, 4, 5, 6 } } });
ExecutionHints().with_contract_instance_hints({ { address, { address, 1, 2, 3, 4, 5, public_keys_hints } } });

auto trace = Execution::gen_trace(instructions, returndata, calldata, public_inputs_vec, execution_hints);
EXPECT_EQ(returndata, std::vector<FF>({ 1, 2, 3, 4, 5, 6 })); // The first one represents true
EXPECT_EQ(returndata, std::vector<FF>({ 1, 2, 3, 4, 5, returned_point.x })); // The first one represents true

validate_trace(std::move(trace), public_inputs, calldata, returndata);
}
Expand Down
32 changes: 30 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/execution_hints.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#include "barretenberg/ecc/groups/affine_element.hpp"
#include "barretenberg/vm/avm/generated/flavor_settings.hpp"

namespace bb::avm_trace {

using FF = AvmFlavorSettings::FF;
using AffinePoint = grumpkin::g1::affine_element;

struct ExternalCallHint {
FF success;
Expand All @@ -26,16 +28,42 @@ inline void read(uint8_t const*& it, ExternalCallHint& hint)
read(it, hint.end_side_effect_counter);
}

struct PublicKeysHint {
AffinePoint nullifier_key;
/** Incoming viewing public key */
AffinePoint incoming_viewing_key;
/** Outgoing viewing public key */
AffinePoint outgoing_viewing_key;
/** Tagging viewing public key */
AffinePoint tagging_key;

std::vector<FF> to_fields() const
{
return { nullifier_key.x, nullifier_key.y, incoming_viewing_key.x, incoming_viewing_key.y,
outgoing_viewing_key.x, outgoing_viewing_key.y, tagging_key.x, tagging_key.y };
}
};

struct ContractInstanceHint {
FF address;
FF instance_found_in_address;
FF salt;
FF deployer_addr;
FF contract_class_id;
FF initialisation_hash;
FF public_key_hash;
PublicKeysHint public_keys;
};

inline void read(uint8_t const*& it, PublicKeysHint& hint)
{
using serialize::read;
// CAREFUL: We assume we never receive a point at infinity here
// TS does not serialize the infinity flag when converting to buffer
read(it, hint.nullifier_key);
read(it, hint.incoming_viewing_key);
read(it, hint.outgoing_viewing_key);
read(it, hint.tagging_key);
}
// Add support for deserialization of ContractInstanceHint.
inline void read(uint8_t const*& it, ContractInstanceHint& hint)
{
Expand All @@ -46,7 +74,7 @@ inline void read(uint8_t const*& it, ContractInstanceHint& hint)
read(it, hint.deployer_addr);
read(it, hint.contract_class_id);
read(it, hint.initialisation_hash);
read(it, hint.public_key_hash);
read(it, hint.public_keys);
}

struct ExecutionHints {
Expand Down
6 changes: 3 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm/avm/trace/trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2403,14 +2403,14 @@ void AvmTraceBuilder::op_get_contract_instance(uint8_t indirect, uint32_t addres

// Read the contract instance
ContractInstanceHint contract_instance = execution_hints.contract_instance_hints.at(read_address.val);

std::vector<FF> public_key_fields = contract_instance.public_keys.to_fields();
// NOTE: we don't write the first entry (the contract instance's address/key) to memory
std::vector<FF> contract_instance_vec = { contract_instance.instance_found_in_address,
contract_instance.salt,
contract_instance.deployer_addr,
contract_instance.contract_class_id,
contract_instance.initialisation_hash,
contract_instance.public_key_hash };
contract_instance.initialisation_hash };
contract_instance_vec.insert(contract_instance_vec.end(), public_key_fields.begin(), public_key_fields.end());
write_slice_to_memory(resolved_dst_offset, AvmMemoryTag::FF, contract_instance_vec);

debug("contract_instance cnt: ", side_effect_counter);
Expand Down
20 changes: 6 additions & 14 deletions noir-projects/aztec-nr/aztec/src/deploy.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,24 @@ pub fn deploy_contract(context: &mut PrivateContext, target: AztecAddress) {
// Adapted from noir-contracts/contracts/contract_instance_deployer_contract/src/interface/ContractInstanceDeployer.nr
// That file was autogenerated running the following command from noir-projects/noir-contracts:
// ../../yarn-project/node_modules/.bin/aztec-cli codegen target/contract_instance_deployer_contract-ContractInstanceDeployer.json --nr -o ./contracts/contract_instance_deployer_contract/src/interface
let mut serialized_args = [0; 5];
let mut serialized_args = [0; 16];
serialized_args[0] = instance.salt;
serialized_args[1] = instance.contract_class_id.to_field();
serialized_args[2] = instance.initialization_hash;

let serialized_public_keys = instance.public_keys.serialize();
serialized_args[3] = serialized_public_keys[0];
serialized_args[4] = serialized_public_keys[1];
serialized_args[5] = serialized_public_keys[2];
serialized_args[6] = serialized_public_keys[3];
serialized_args[7] = serialized_public_keys[4];
serialized_args[8] = serialized_public_keys[5];
serialized_args[9] = serialized_public_keys[6];
serialized_args[10] = serialized_public_keys[7];
serialized_args[11] = serialized_public_keys[8];
serialized_args[12] = serialized_public_keys[9];
serialized_args[13] = serialized_public_keys[10];
serialized_args[14] = serialized_public_keys[11];

for i in 0..12 {
serialized_args[i + 3] = serialized_public_keys[i];
}

serialized_args[15] = universal_deploy as Field;

let _call_result = context.call_private_function(
DEPLOYER_CONTRACT_ADDRESS,
comptime {
FunctionSelector::from_signature(
"deploy(Field,(Field),Field,(Field,Field,Field,Field,Field,Field,Field,Field,Field,Field,Field,Field),bool)"
"deploy(Field,(Field),Field,(((Field,Field,bool)),((Field,Field,bool)),((Field,Field,bool)),((Field,Field,bool))),bool)"
)
},
serialized_args
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,18 @@ contract AvmTest {
assert(fields[2] == 0x456);
assert(fields[3] == 0x789);
assert(fields[4] == 0x101112);
assert(fields[5] == 0x161718);
assert(fields[5] == 0x131415);
assert(fields[6] == 0x161718);
assert(fields[7] == 0x00);
assert(fields[8] == 0x192021);
assert(fields[9] == 0x222324);
assert(fields[10] == 0x00);
assert(fields[11] == 0x252627);
assert(fields[12] == 0x282930);
assert(fields[13] == 0x00);
assert(fields[14] == 0x313233);
assert(fields[15] == 0x343536);
assert(fields[16] == 0x00);
}

#[public]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::macros::aztec;
#[aztec]
contract ContractInstanceDeployer {
use dep::aztec::protocol_types::{
address::{AztecAddress, PublicKeysHash, PublicKeys, PartialAddress},
address::{AztecAddress, PublicKeysHash, PartialAddress}, public_keys::PublicKeys,
contract_class_id::ContractClassId, constants::DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE,
abis::log_hash::LogHash, traits::Serialize
};
Expand All @@ -14,7 +14,6 @@ contract ContractInstanceDeployer {
use std::meta::derive;

#[event]
#[derive(Serialize)]
struct ContractInstanceDeployed {
DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE: Field,
address: AztecAddress,
Expand All @@ -26,6 +25,56 @@ contract ContractInstanceDeployer {
deployer: AztecAddress,
}

// We need to impl this separately because ts deserializes a point as two fields only.
// We had issues that:
// Notice how the 'is_infinite' field is deserialized as the next point.
// {
// masterNullifierPublicKey: Point {
// x: Fr<0x0000000000000000000000000000000000000000000000000000000000000012>,
// y: Fr<0x0000000000000000000000000000000000000000000000000000000000000034>,
// isInfinite: false,
// kind: 'point'
// },
// masterIncomingViewingPublicKey: Point {
// x: Fr<0x0000000000000000000000000000000000000000000000000000000000000000>,
// y: Fr<0x0000000000000000000000000000000000000000000000000000000000000056>,
// isInfinite: false,
// kind: 'point'
// },
// masterOutgoingViewingPublicKey: Point {
// x: Fr<0x0000000000000000000000000000000000000000000000000000000000000078>,
// y: Fr<0x0000000000000000000000000000000000000000000000000000000000000000>,
// isInfinite: false,
// kind: 'point'
// },
// masterTaggingPublicKey: Point {
// x: Fr<0x0000000000000000000000000000000000000000000000000000000000000910>,
// y: Fr<0x0000000000000000000000000000000000000000000000000000000000001112>,
// isInfinite: false,
// kind: 'point'
// }
impl Serialize<15> for ContractInstanceDeployed {
fn serialize(self) -> [Field; 15] {
[
self.DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE,
self.address.to_field(),
self.version.to_field(),
self.salt,
self.contract_class_id.to_field(),
self.initialization_hash,
self.public_keys.npk_m.serialize()[0],
self.public_keys.npk_m.serialize()[1],
self.public_keys.ivpk_m.serialize()[0],
self.public_keys.ivpk_m.serialize()[1],
self.public_keys.ovpk_m.serialize()[0],
self.public_keys.ovpk_m.serialize()[1],
self.public_keys.tpk_m.serialize()[0],
self.public_keys.tpk_m.serialize()[1],
self.deployer.to_field()
]
}
}

#[private]
#['private]
fn deploy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::{
address::{
aztec_address::AztecAddress, partial_address::PartialAddress, public_keys_hash::PublicKeysHash,
public_keys::PublicKeys
},
contract_class_id::ContractClassId, constants::CONTRACT_INSTANCE_LENGTH,
address::{aztec_address::AztecAddress, partial_address::PartialAddress, public_keys_hash::PublicKeysHash},
public_keys::PublicKeys, contract_class_id::ContractClassId, constants::CONTRACT_INSTANCE_LENGTH,
traits::{Deserialize, Hash, Serialize}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ impl ToPoint for TpkM {
}
}

impl Serialize<POINT_LENGTH> for TpkM {
fn serialize(self) -> [Field; POINT_LENGTH] {
self.inner.serialize()
}
}

impl Empty for PublicKeys {
fn empty() -> Self {
PublicKeys {
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/aztec-node/src/aztec-node/http_rpc_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
TxReceipt,
UnencryptedL2BlockL2Logs,
} from '@aztec/circuit-types';
import { FunctionSelector, Header } from '@aztec/circuits.js';
import { FunctionSelector, Header, PublicKeys } from '@aztec/circuits.js';
import { NoteSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Buffer32 } from '@aztec/foundation/buffer';
Expand Down Expand Up @@ -44,6 +44,7 @@ export function createAztecNodeRpcServer(node: AztecNode) {
TxHash,
Buffer32,
PublicDataWitness,
PublicKeys,
SiblingPath,
},
{
Expand Down
1 change: 0 additions & 1 deletion yarn-project/aztec.js/src/contract/contract.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PublicKeys } from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';

import { type Wallet } from '../account/index.js';
import { ContractBase } from './contract_base.js';
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/contract/deploy_method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type FunctionCall, type TxExecutionRequest } from '@aztec/circuit-types
import {
AztecAddress,
type ContractInstanceWithAddress,
PublicKeys,
type PublicKeys,
computePartialAddress,
getContractClassFromArtifact,
getContractInstanceFromDeployParams,
Expand Down
1 change: 0 additions & 1 deletion yarn-project/aztec.js/src/deployment/contract_deployer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type AztecAddress, PublicKeys } from '@aztec/circuits.js';
import { type ContractArtifact } from '@aztec/foundation/abi';
import { Fr } from '@aztec/foundation/fields';

import { type Wallet } from '../account/wallet.js';
import { Contract } from '../contract/contract.js';
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/aztec.js/src/rpc_clients/pxe_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
GrumpkinScalar,
Point,
PrivateCallStackItem,
PublicKeys,
} from '@aztec/circuits.js';
import { NoteSelector } from '@aztec/foundation/abi';
import { Buffer32 } from '@aztec/foundation/buffer';
Expand Down Expand Up @@ -64,6 +65,7 @@ export const createPXEClient = (url: string, fetch = makeFetch([1, 2, 3], false)
LogId,
Note,
Point,
PublicKeys,
TxExecutionRequest,
TxHash,
Buffer32,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FunctionSelector, Header } from '@aztec/circuits.js';
import { FunctionSelector, Header, PublicKeys } from '@aztec/circuits.js';
import { EventSelector, NoteSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { Buffer32 } from '@aztec/foundation/buffer';
Expand Down Expand Up @@ -42,6 +42,7 @@ export function createAztecNodeClient(url: string, fetch = defaultFetch): AztecN
L2Block,
LogId,
PublicDataWitness,
PublicKeys,
SiblingPath,
TxEffect,
TxHash,
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0000000085864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631173b1e288f0f29f945ffa7b4ec2b69393e32b78501d0f193288e4a886a9f6e18000000000000000000000000000000000000000000000000000000000000000113554cdde23fee1efdb34bb3f685929e9098b8328083970984754948007e11ea0798434d6f2adf997c4fe3d14cb8468aa3cbf7a70d8c499c3c775fc8feff67960f68e30db11bc73392acfc02600ad1177cbe0d17cbbb5d3086a592b736f414a8000000000000000000000000507a47e77fc808e31716c47865eb372b2a487b7505331ff473c938df8b7a6dba01cf45bd6b029d4aeeff7e76953b31ef2510f7b80000000000000000000000000000000000000000000000000000000000000000
0000000085864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631011870b273ea9661b2893efeb641df4136b3f67b24fc79aed1d5bd779d35e3cd00000000000000000000000000000000000000000000000000000000000000011f99b84f796dd16265d803ef0f80c9cc4988c0797d1f9a895115d3c2c15d016723ced3716a04d81b58822bc3e1843626aa2884888b1a2d2250e79fb7d41a365e1ab0c6a467b58a91aab18f3ec7f996410a1855d75d08d73ed8796a2465a64ac8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading

0 comments on commit 2d638df

Please sign in to comment.