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 11, 2024
1 parent f28bbe2 commit 2b7ba33
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 25 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 @@ -2319,11 +2319,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 @@ -2394,14 +2394,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
4 changes: 1 addition & 3 deletions noir-projects/aztec-nr/aztec/src/deploy.nr
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ pub fn deploy_contract(context: &mut PrivateContext, target: AztecAddress) {
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)"
)
FunctionSelector::from_signature("deploy(Field,(Field),Field,(Field,Field,Field,Field,Field,Field,Field,Field,Field,Field,Field,Field),bool)")
},
serialized_args
);
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
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
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 2b7ba33

Please sign in to comment.