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 14, 2024
1 parent 1fc95a9 commit 1104ba0
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 42 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 @@ -2404,14 +2404,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
16 changes: 4 additions & 12 deletions noir-projects/aztec-nr/aztec/src/deploy.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,10 @@ pub fn deploy_contract(context: &mut PrivateContext, target: AztecAddress) {
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,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 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
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
16 changes: 3 additions & 13 deletions yarn-project/circuits.js/src/structs/avm/avm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,7 @@ export class AvmContractInstanceHint {
* @returns A new AvmHint instance.
*/
static from(fields: FieldsOf<AvmContractInstanceHint>): AvmContractInstanceHint {
const contractInstanceHintFields = AvmContractInstanceHint.getFields(fields);

return new AvmContractInstanceHint(
contractInstanceHintFields[0],
contractInstanceHintFields[1],
contractInstanceHintFields[2],
contractInstanceHintFields[3],
contractInstanceHintFields[4],
contractInstanceHintFields[5],
PublicKeys.fromFields(contractInstanceHintFields.slice(6, 18)),
);
return new AvmContractInstanceHint(...AvmContractInstanceHint.getFields(fields));
}

/**
Expand All @@ -243,8 +233,8 @@ export class AvmContractInstanceHint {
fields.deployer,
fields.contractClassId,
fields.initializationHash,
...fields.publicKeys.toFields(),
] as [Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr, Fr];
fields.publicKeys,
] as const;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/pxe/src/pxe_http/pxe_http_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
UnencryptedL2Log,
UniqueNote,
} from '@aztec/circuit-types';
import { FunctionSelector, PrivateCallStackItem } from '@aztec/circuits.js';
import { FunctionSelector, PrivateCallStackItem, 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 @@ -57,6 +57,7 @@ export function createPXERpcServer(pxeService: PXE): JsonRpcServer {
GrumpkinScalar,
Note,
ExtendedNote,
PublicKeys,
UniqueNote,
SiblingPath,
AuthWitness,
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/avm/opcodes/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class GetContractInstance extends Instruction {

memory.setSlice(dstOffset, data);

memory.assert({ reads: 1, writes: 6, addressing });
memory.assert({ reads: 1, writes: 17, addressing });
context.machineState.incrementPc();
}
}

0 comments on commit 1104ba0

Please sign in to comment.