Skip to content
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
2 changes: 1 addition & 1 deletion cpp/barretenberg
Submodule barretenberg updated 128 files
24 changes: 15 additions & 9 deletions cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "private_circuit_public_inputs.hpp"
#include "tx_request.hpp"
#include "tx_context.hpp"
#include "function_data.hpp"
#include "function_leaf_preimage.hpp"
#include "rollup/base/base_rollup_inputs.hpp"
#include "rollup/base/base_rollup_public_inputs.hpp"
Expand All @@ -15,12 +16,14 @@
#include <aztec3/constants.hpp>

#include <aztec3/utils/types/native_types.hpp>
#include <aztec3/utils/array.hpp>
#include <barretenberg/stdlib/merkle_tree/membership.hpp>
#include <barretenberg/crypto/keccak/keccak.hpp>
#include <barretenberg/common/serialize.hpp>

namespace {

using aztec3::circuits::abis::FunctionData;
using aztec3::circuits::abis::FunctionLeafPreimage;
using aztec3::circuits::abis::TxContext;
using aztec3::circuits::abis::TxRequest;
Expand Down Expand Up @@ -232,27 +235,30 @@ WASM_EXPORT void abis__compute_function_tree_root(uint8_t const* function_leaves
* hash(function_signature_hash, args_hash, constructor_vk_hash)
* Return the serialized results in the `output` buffer.
*
* @param func_sig_hash_buf function signature field but as a buffer of bytes
* @param args_hash_buf constructor args hashed to a field but as a buffer of bytes
* @param function_data_buf function data struct but as a buffer of bytes
* @param args_buf constructor args (array of fields) but as a buffer of bytes
* @param constructor_vk_hash_buf constructor vk hashed to a field but as a buffer of bytes
* @param output buffer that will contain the output. The serialized constructor_vk_hash.
*/
WASM_EXPORT void abis__hash_constructor(uint8_t const* func_sig_hash_buf,
uint8_t const* args_hash_buf,
WASM_EXPORT void abis__hash_constructor(uint8_t const* function_data_buf,
uint8_t const* args_buf,
uint8_t const* constructor_vk_hash_buf,
uint8_t* output)
{
NT::fr func_sig_hash;
NT::fr args_hash;
FunctionData<NT> function_data;
std::array<NT::fr, aztec3::ARGS_LENGTH> args;
NT::fr constructor_vk_hash;

using serialize::read;
read(func_sig_hash_buf, func_sig_hash);
read(args_hash_buf, args_hash);
read(function_data_buf, function_data);
read(args_buf, args);
read(constructor_vk_hash_buf, constructor_vk_hash);

NT::fr function_data_hash = function_data.hash();
NT::fr args_hash = NT::compress(args, aztec3::CONSTRUCTOR_ARGS);

std::vector<NT::fr> inputs = {
func_sig_hash,
function_data_hash,
args_hash,
constructor_vk_hash,
};
Expand Down
11 changes: 4 additions & 7 deletions cpp/src/aztec3/circuits/abis/c_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@ WASM_EXPORT void abis__hash_tx_request(uint8_t const* tx_request_buf, uint8_t* o

WASM_EXPORT void abis__compute_function_selector(char const* func_sig_cstr, uint8_t* output);

WASM_EXPORT void abis__compute_function_leaf(uint8_t const* function_leaf_preimage_buf,
uint8_t* output);
WASM_EXPORT void abis__compute_function_leaf(uint8_t const* function_leaf_preimage_buf, uint8_t* output);

WASM_EXPORT void abis__compute_function_tree_root(uint8_t const* function_leaves_buf,
uint8_t num_leaves,
uint8_t* output);

WASM_EXPORT void abis__hash_vk(uint8_t const* vk_data_buf, uint8_t* output);

WASM_EXPORT void abis__hash_constructor(uint8_t const* func_sig_hash_buf,
uint8_t const* args_hash_buf,
WASM_EXPORT void abis__hash_constructor(uint8_t const* func_data_buf,
uint8_t const* args_buf,
uint8_t const* constructor_vk_hash_buf,
uint8_t* output);

Expand All @@ -28,7 +27,5 @@ WASM_EXPORT void abis__compute_contract_address(uint8_t const* deployer_address_
uint8_t const* constructor_hash_buf,
uint8_t* output);

WASM_EXPORT void abis__compute_contract_leaf(uint8_t const* contract_leaf_preimage_buf,
uint8_t* output);

WASM_EXPORT void abis__compute_contract_leaf(uint8_t const* contract_leaf_preimage_buf, uint8_t* output);
}
29 changes: 18 additions & 11 deletions cpp/src/aztec3/circuits/abis/c_bind.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,31 +193,38 @@ TEST(abi_tests, compute_function_tree_root)
TEST(abi_tests, hash_constructor)
{
// Randomize required values
NT::fr func_sig_hash = NT::fr::random_element();
NT::fr args_hash = NT::fr::random_element();
FunctionData<NT> func_data =
FunctionData<NT>{ .function_selector = 10, .is_private = true, .is_constructor = false };

std::array<NT::fr, aztec3::ARGS_LENGTH> args;
for (size_t i = 0; i < aztec3::ARGS_LENGTH; i++) {
args[i] = fr::random_element();
}
NT::fr constructor_vk_hash = NT::fr::random_element();

// Serialize values to buffers
std::array<uint8_t, sizeof(NT::fr)> func_sig_hash_buf = { 0 };
std::array<uint8_t, sizeof(NT::fr)> args_hash_buf = { 0 };
// Write the function data and args to a buffer
std::vector<uint8_t> func_data_buf;
write(func_data_buf, func_data);

std::vector<uint8_t> args_buf;
write(args_buf, args);

std::array<uint8_t, sizeof(NT::fr)> constructor_vk_hash_buf = { 0 };
NT::fr::serialize_to_buffer(func_sig_hash, func_sig_hash_buf.data());
NT::fr::serialize_to_buffer(args_hash, args_hash_buf.data());
NT::fr::serialize_to_buffer(constructor_vk_hash, constructor_vk_hash_buf.data());

// create an output buffer for cbind hash results
std::array<uint8_t, sizeof(NT::fr)> output = { 0 };

// Make the c_bind call to hash the constructor values
abis__hash_constructor(
func_sig_hash_buf.data(), args_hash_buf.data(), constructor_vk_hash_buf.data(), output.data());
abis__hash_constructor(func_data_buf.data(), args_buf.data(), constructor_vk_hash_buf.data(), output.data());

// Convert buffer to `fr` for comparison to in-test calculated hash
NT::fr got_hash = NT::fr::serialize_from_buffer(output.data());

// Calculate the expected hash in-test
NT::fr expected_hash =
NT::compress({ func_sig_hash, args_hash, constructor_vk_hash }, aztec3::GeneratorIndex::CONSTRUCTOR);
NT::fr expected_hash = NT::compress(
{ func_data.hash(), NT::compress(args, aztec3::GeneratorIndex::CONSTRUCTOR_ARGS), constructor_vk_hash },
aztec3::GeneratorIndex::CONSTRUCTOR);

// Confirm cbind output == expected hash
EXPECT_EQ(got_hash, expected_hash);
Expand Down
15 changes: 15 additions & 0 deletions cpp/src/aztec3/circuits/abis/private_kernel/new_contract_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ using std::is_same;
template <typename NCT> struct NewContractData {
typedef typename NCT::address address;
typedef typename NCT::fr fr;
typedef typename NCT::boolean boolean;

address contract_address;
address portal_contract_address;
Expand Down Expand Up @@ -45,6 +46,12 @@ template <typename NCT> struct NewContractData {
return new_contract_data;
};

boolean is_empty() const
{
return ((contract_address.to_field() == fr(0)) && (portal_contract_address.to_field() == fr(0)) &&
(function_tree_root == fr(0)));
}

void set_public()
{
static_assert(!(std::is_same<NativeTypes, NCT>::value));
Expand All @@ -64,6 +71,14 @@ template <typename NCT> struct NewContractData {

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

void conditional_select(const boolean& condition, const NewContractData<NCT>& other)
{
contract_address = address::conditional_assign(condition, other.contract_address, contract_address);
portal_contract_address =
address::conditional_assign(condition, other.portal_contract_address, portal_contract_address);
function_tree_root = fr::conditional_assign(condition, other.function_tree_root, function_tree_root);
}
};

template <typename NCT> void read(uint8_t const*& it, NewContractData<NCT>& new_contract_data)
Expand Down
5 changes: 3 additions & 2 deletions cpp/src/aztec3/circuits/abis/signed_tx_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ template <typename NCT> struct SignedTxRequest {
TxRequest<NCT> tx_request;
Signature signature;

template <typename Composer> SignedTxRequest<CircuitTypes<Composer>> to_circuit_type(Composer& /*composer*/) const
template <typename Composer> SignedTxRequest<CircuitTypes<Composer>> to_circuit_type(Composer& composer) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

// Capture the composer:
// auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(composer, e); };
// auto to_circuit_type = [&](auto& e) { return e.to_circuit_type(composer); };
auto to_circuit_type = [&](auto& e) { return e.to_circuit_type(composer); };

SignedTxRequest<CircuitTypes<Composer>> signed_tx_request;
signed_tx_request.tx_request = to_circuit_type(tx_request);
// TODO: to_ct(signature) is yielding an error.
// = {
// to_circuit_type(tx_request),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ template <typename Composer> class FunctionExecutionContext {
, private_circuit_public_inputs(OptionalPrivateCircuitPublicInputs<CT>::create())
{
private_circuit_public_inputs.call_context = oracle.get_call_context();
private_circuit_public_inputs.contract_deployment_data = oracle.get_contract_deployment_data();
}

void register_contract(Contract<NT>* contract)
Expand Down
12 changes: 12 additions & 0 deletions cpp/src/aztec3/circuits/apps/oracle_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <aztec3/circuits/abis/call_context.hpp>
#include <aztec3/circuits/abis/contract_deployment_data.hpp>
#include <aztec3/oracle/oracle.hpp>

#include <barretenberg/common/map.hpp>
Expand All @@ -12,6 +13,7 @@ namespace aztec3::circuits::apps {

using NT = aztec3::utils::types::NativeTypes;
using aztec3::circuits::abis::CallContext;
using aztec3::circuits::abis::ContractDeploymentData;
using aztec3::oracle::NativeOracle;
using aztec3::utils::types::CircuitTypes;

Expand Down Expand Up @@ -60,6 +62,15 @@ template <typename Composer> class OracleWrapperInterface {
return *call_context;
};

ContractDeploymentData<CT>& get_contract_deployment_data()
{
if (contract_deployment_data) {
return *contract_deployment_data;
}
contract_deployment_data = native_oracle.get_contract_deployment_data().to_circuit_type(composer);
return *contract_deployment_data;
};

address& get_msg_sender() { return get_call_context().msg_sender; };

address& get_this_contract_address() { return get_call_context().storage_contract_address; };
Expand Down Expand Up @@ -104,6 +115,7 @@ template <typename Composer> class OracleWrapperInterface {

private:
std::optional<CallContext<CT>> call_context;
std::optional<ContractDeploymentData<CT>> contract_deployment_data;
std::optional<fr> msg_sender_private_key;

void validate_msg_sender_private_key()
Expand Down
Loading