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

chainid and version as globals in contracts #910

Merged
merged 8 commits into from
Jun 27, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "aztec3/utils/types/convert.hpp"
#include "aztec3/utils/types/native_types.hpp"

#include "barretenberg/common/serialize.hpp"
#include <barretenberg/barretenberg.hpp>

namespace aztec3::circuits::abis {
Expand Down Expand Up @@ -52,6 +53,9 @@ template <typename NCT> class PrivateCircuitPublicInputs {

ContractDeploymentData<NCT> contract_deployment_data{};

fr chain_id = 0;
fr version = 0;

boolean operator==(PrivateCircuitPublicInputs<NCT> const& other) const
{
return call_context == other.call_context && args_hash == other.args_hash &&
Expand All @@ -66,7 +70,8 @@ template <typename NCT> class PrivateCircuitPublicInputs {
historic_nullifier_tree_root == other.historic_nullifier_tree_root &&
historic_contract_tree_root == other.historic_contract_tree_root &&
historic_l1_to_l2_messages_tree_root == other.historic_l1_to_l2_messages_tree_root &&
contract_deployment_data == other.contract_deployment_data;
contract_deployment_data == other.contract_deployment_data && chain_id == other.chain_id &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add these in different lines to be coherent and easily spottable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is when formatted, notice the first line also have two values, both call_context and args_hash.

version == other.version;
};

template <typename Composer>
Expand Down Expand Up @@ -105,6 +110,9 @@ template <typename NCT> class PrivateCircuitPublicInputs {
to_ct(historic_l1_to_l2_messages_tree_root),

to_circuit_type(contract_deployment_data),

to_ct(chain_id),
to_ct(version),
};

return pis;
Expand Down Expand Up @@ -143,6 +151,9 @@ template <typename NCT> class PrivateCircuitPublicInputs {
to_nt(historic_l1_to_l2_messages_tree_root),

to_native_type(contract_deployment_data),

to_nt(chain_id),
to_nt(version),
};

return pis;
Expand Down Expand Up @@ -181,6 +192,9 @@ template <typename NCT> class PrivateCircuitPublicInputs {

inputs.push_back(contract_deployment_data.hash());

inputs.push_back(chain_id);
inputs.push_back(version);

return NCT::compress(inputs, GeneratorIndex::PRIVATE_CIRCUIT_PUBLIC_INPUTS);
}

Expand Down Expand Up @@ -214,6 +228,8 @@ template <typename NCT> void read(uint8_t const*& it, PrivateCircuitPublicInputs
read(it, pis.historic_contract_tree_root);
read(it, pis.historic_l1_to_l2_messages_tree_root);
read(it, pis.contract_deployment_data);
read(it, pis.chain_id);
read(it, pis.version);
};

template <typename NCT>
Expand Down Expand Up @@ -242,6 +258,8 @@ void write(std::vector<uint8_t>& buf, PrivateCircuitPublicInputs<NCT> const& pri
write(buf, pis.historic_l1_to_l2_messages_tree_root);

write(buf, pis.contract_deployment_data);
write(buf, pis.chain_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we are here we could probably alter this to use the MSGPACK_FIELDS() macro

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it needs changes quite a few places to use MSGPACK_FIELDS() consistently (in other part os the code as well), so might be better to put that off to a separate cleanup PR.

write(buf, pis.version);
};

template <typename NCT>
Expand All @@ -266,7 +284,9 @@ std::ostream& operator<<(std::ostream& os, PrivateCircuitPublicInputs<NCT> const
<< "historic_nullifier_tree_root: " << pis.historic_nullifier_tree_root << "\n"
<< "historic_contract_tree_root: " << pis.historic_contract_tree_root << "\n"
<< "historic_l1_to_l2_messages_tree_root: " << pis.historic_l1_to_l2_messages_tree_root << "\n"
<< "contract_deployment_data: " << pis.contract_deployment_data << "\n";
<< "contract_deployment_data: " << pis.contract_deployment_data << "\n"
<< "chain_id: " << pis.chain_id << "\n"
<< "version: " << pis.version << "\n";
}

// It's been extremely useful for all members here to be std::optional. It allows test app circuits to be very
Expand Down Expand Up @@ -305,6 +325,9 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {

std::optional<ContractDeploymentData<NCT>> contract_deployment_data;

opt_fr chain_id;
opt_fr version;

OptionalPrivateCircuitPublicInputs<NCT>() = default;

OptionalPrivateCircuitPublicInputs<NCT>(std::optional<CallContext<NCT>> const& call_context,
Expand Down Expand Up @@ -332,7 +355,10 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {
opt_fr const& historic_contract_tree_root,
opt_fr const& historic_l1_to_l2_messages_tree_root,

std::optional<ContractDeploymentData<NCT>> const& contract_deployment_data)
std::optional<ContractDeploymentData<NCT>> const& contract_deployment_data,

opt_fr const& chain_id,
opt_fr const& version)
: call_context(call_context)
, args_hash(args_hash)
, return_values(return_values)
Expand All @@ -350,7 +376,9 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {
, historic_nullifier_tree_root(historic_nullifier_tree_root)
, historic_contract_tree_root(historic_contract_tree_root)
, historic_l1_to_l2_messages_tree_root(historic_l1_to_l2_messages_tree_root)
, contract_deployment_data(contract_deployment_data){};
, contract_deployment_data(contract_deployment_data)
, chain_id(chain_id)
, version(version){};

bool operator==(OptionalPrivateCircuitPublicInputs<NCT> const&) const = default;

Expand Down Expand Up @@ -385,6 +413,9 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {

new_inputs.contract_deployment_data = std::nullopt;

new_inputs.chain_id = std::nullopt;
new_inputs.version = std::nullopt;

return new_inputs;
};

Expand Down Expand Up @@ -439,6 +470,9 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {

make_unused_element_zero(composer, contract_deployment_data);

make_unused_element_zero(composer, chain_id);
make_unused_element_zero(composer, version);

all_elements_populated = true;
}

Expand Down Expand Up @@ -476,6 +510,9 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {
(*historic_l1_to_l2_messages_tree_root).set_public();

(*contract_deployment_data).set_public();

(*chain_id).set_public();
(*version).set_public();
}

template <typename Composer>
Expand Down Expand Up @@ -516,6 +553,9 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {
to_ct(historic_l1_to_l2_messages_tree_root),

to_circuit_type(contract_deployment_data),

to_ct(chain_id),
to_ct(version),
};

return pis;
Expand All @@ -530,34 +570,35 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {
};
// auto to_native_type = [&]<typename T>(T& e) { return e.to_native_type(); };

OptionalPrivateCircuitPublicInputs<NativeTypes> pis = {
to_native_type(call_context),
OptionalPrivateCircuitPublicInputs<NativeTypes> pis = { to_native_type(call_context),

to_nt(args_hash),
to_nt(return_values),
to_nt(args_hash),
to_nt(return_values),

to_nt(read_requests),
to_nt(read_requests),

to_nt(new_commitments),
to_nt(new_nullifiers),
to_nt(new_commitments),
to_nt(new_nullifiers),

to_nt(private_call_stack),
to_nt(public_call_stack),
to_nt(new_l2_to_l1_msgs),
to_nt(private_call_stack),
to_nt(public_call_stack),
to_nt(new_l2_to_l1_msgs),

to_nt(encrypted_logs_hash),
to_nt(unencrypted_logs_hash),
to_nt(encrypted_logs_hash),
to_nt(unencrypted_logs_hash),

to_nt(encrypted_log_preimages_length),
to_nt(unencrypted_log_preimages_length),
to_nt(encrypted_log_preimages_length),
to_nt(unencrypted_log_preimages_length),

to_nt(historic_private_data_tree_root),
to_nt(historic_nullifier_tree_root),
to_nt(historic_contract_tree_root),
to_nt(historic_l1_to_l2_messages_tree_root),
to_nt(historic_private_data_tree_root),
to_nt(historic_nullifier_tree_root),
to_nt(historic_contract_tree_root),
to_nt(historic_l1_to_l2_messages_tree_root),

to_native_type(contract_deployment_data),
};
to_native_type(contract_deployment_data),

to_nt(chain_id),
to_nt(version) };

return pis;
};
Expand Down Expand Up @@ -600,6 +641,9 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {

inputs.push_back((*contract_deployment_data).hash());

inputs.push_back(*chain_id);
inputs.push_back(*version);

return NCT::compress(inputs, GeneratorIndex::PRIVATE_CIRCUIT_PUBLIC_INPUTS);
}

Expand Down Expand Up @@ -635,6 +679,9 @@ template <typename NCT> class OptionalPrivateCircuitPublicInputs {
.historic_l1_to_l2_messages_tree_root = historic_l1_to_l2_messages_tree_root.value(),

.contract_deployment_data = contract_deployment_data.value(),

.chain_id = chain_id.value(),
.version = version.value(),
};
}

Expand Down Expand Up @@ -730,6 +777,8 @@ void read(uint8_t const*& it, OptionalPrivateCircuitPublicInputs<NCT>& private_c
read(it, pis.historic_contract_tree_root);
read(it, pis.historic_l1_to_l2_messages_tree_root);
read(it, pis.contract_deployment_data);
read(it, pis.chain_id);
read(it, pis.version);
};

template <typename NCT>
Expand Down Expand Up @@ -757,6 +806,8 @@ void write(std::vector<uint8_t>& buf, OptionalPrivateCircuitPublicInputs<NCT> co
write(buf, pis.historic_contract_tree_root);
write(buf, pis.historic_l1_to_l2_messages_tree_root);
write(buf, pis.contract_deployment_data);
write(buf, pis.chain_id);
write(buf, pis.version);
};

template <typename NCT>
Expand All @@ -781,7 +832,9 @@ std::ostream& operator<<(std::ostream& os, OptionalPrivateCircuitPublicInputs<NC
<< "historic_nullifier_tree_root: " << pis.historic_nullifier_tree_root << "\n"
<< "historic_contract_tree_root: " << pis.historic_contract_tree_root << "\n"
<< "historic_l1_to_l2_messages_tree_root: " << pis.historic_l1_to_l2_messages_tree_root << "\n"
<< "contract_deployment_data: " << pis.contract_deployment_data << "\n";
<< "contract_deployment_data: " << pis.contract_deployment_data << "\n"
<< "chain_id: " << pis.chain_id << "\n"
<< "version: " << pis.version << "\n";
}

} // namespace aztec3::circuits::abis
5 changes: 5 additions & 0 deletions yarn-project/acir-simulator/src/acvm/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export function extractPublicInputs(partialWitness: ACVMWitness, acir: Buffer):
EthAddress.fromField(witnessReader.readField()),
);

const chainId = witnessReader.readField();
const version = witnessReader.readField();

return new PrivateCircuitPublicInputs(
callContext,
argsHash,
Expand All @@ -174,5 +177,7 @@ export function extractPublicInputs(partialWitness: ACVMWitness, acir: Buffer):
contractTreeRoot,
l1Tol2TreeRoot,
contractDeploymentData,
chainId,
version,
);
}
2 changes: 2 additions & 0 deletions yarn-project/acir-simulator/src/acvm/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export function toACVMPublicInputs(publicInputs: PrivateCircuitPublicInputs): AC
toACVMField(publicInputs.historicL1ToL2MessagesTreeRoot),

...toACVMContractDeploymentData(publicInputs.contractDeploymentData),
toACVMField(publicInputs.chainId),
toACVMField(publicInputs.version),
];
}

Expand Down
12 changes: 7 additions & 5 deletions yarn-project/acir-simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Private Execution test suite', () => {

const historicRoots = PrivateHistoricTreeRoots.empty();
const contractDeploymentData = ContractDeploymentData.empty();
const txContext = new TxContext(false, false, false, contractDeploymentData, Fr.ZERO, Fr.ZERO);
const txContext = new TxContext(false, false, false, contractDeploymentData, new Fr(69), new Fr(420));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const buildTxExecutionRequest = (args: {
abi: FunctionAbi;
Expand Down Expand Up @@ -303,9 +303,11 @@ describe('Private Execution test suite', () => {
});

describe('nested calls', () => {
const privateIncrement = txContext.chainId.value + txContext.version.value;
it('child function should be callable', async () => {
const initialValue = 100n;
const abi = ChildAbi.functions.find(f => f.name === 'value')!;
const txRequest = buildTxExecutionRequest({ args: [100n], abi });
const txRequest = buildTxExecutionRequest({ args: [initialValue], abi });
const result = await acirSimulator.run(
txRequest,
abi,
Expand All @@ -315,7 +317,7 @@ describe('Private Execution test suite', () => {
await Grumpkin.new(),
);

expect(result.callStackItem.publicInputs.returnValues[0]).toEqual(new Fr(142n));
expect(result.callStackItem.publicInputs.returnValues[0]).toEqual(new Fr(initialValue + privateIncrement));
});

it('parent should call child', async () => {
Expand All @@ -342,11 +344,11 @@ describe('Private Execution test suite', () => {
await Grumpkin.new(),
);

expect(result.callStackItem.publicInputs.returnValues[0]).toEqual(new Fr(42n));
expect(result.callStackItem.publicInputs.returnValues[0]).toEqual(new Fr(privateIncrement));
expect(oracle.getFunctionABI.mock.calls[0]).toEqual([childAddress, childSelector]);
expect(oracle.getPortalContractAddress.mock.calls[0]).toEqual([childAddress]);
expect(result.nestedExecutions).toHaveLength(1);
expect(result.nestedExecutions[0].callStackItem.publicInputs.returnValues[0]).toEqual(new Fr(42n));
expect(result.nestedExecutions[0].callStackItem.publicInputs.returnValues[0]).toEqual(new Fr(privateIncrement));
});
});

Expand Down
3 changes: 3 additions & 0 deletions yarn-project/acir-simulator/src/client/private_execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ export class PrivateFunctionExecution {
contractDeploymentData.contractAddressSalt,
contractDeploymentData.portalContractAddress,

this.context.txContext.chainId,
this.context.txContext.version,

...this.args.slice(0, argsSize),
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ function_tree_root: 0x3
contract_address_salt: 0x4
portal_contract_address: 0x505050505050505050505050505050505050505

chain_id: 0x1100
version: 0x1200
"
`;
Loading