Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Aug 30, 2023
1 parent 343a97a commit 0c10c00
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 113 deletions.
4 changes: 2 additions & 2 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

namespace {

using aztec3::circuits::compute_complete_contract_address;
using aztec3::circuits::compute_complete_address;
using aztec3::circuits::compute_constructor_hash;
using aztec3::circuits::compute_partial_address;
using aztec3::circuits::abis::CallStackItem;
Expand Down Expand Up @@ -291,7 +291,7 @@ WASM_EXPORT void abis__hash_constructor(uint8_t const* function_data_buf,
/**
* @brief Compute a complete address.
*/
CBIND(abis__compute_complete_contract_address, aztec3::circuits::compute_complete_contract_address<NT>);
CBIND(abis__compute_complete_address, aztec3::circuits::compute_complete_address<NT>);

/**
* @brief Compute a contract address from deployer public key and partial address.
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/c_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ WASM_EXPORT void abis__hash_constructor(uint8_t const* func_data_buf,
uint8_t const* constructor_vk_hash_buf,
uint8_t* output);

CBIND_DECL(abis__compute_complete_contract_address);
CBIND_DECL(abis__compute_complete_address);

WASM_EXPORT void abis__compute_partial_address(uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
Expand Down
28 changes: 0 additions & 28 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,34 +72,6 @@ TEST(abi_tests, compute_partial_address)
EXPECT_EQ(actual, expected);
}

TEST(abi_tests, compute_complete_contract_address)
{
Point<NT> const point = { .x = 1, .y = 3 };
auto const contract_address_salt = NT::fr(5);
auto const function_tree_root = NT::fr(6);
auto const constructor_hash = NT::fr(7);
CompleteAddress<NT> const expected =
compute_complete_contract_address(point, contract_address_salt, function_tree_root, constructor_hash);

std::array<uint8_t, sizeof(NT::fr)> output = { 0 };
std::vector<uint8_t> contract_address_salt_buf;
std::vector<uint8_t> function_tree_root_buf;
std::vector<uint8_t> constructor_hash_buf;
std::vector<uint8_t> point_buf;
write(contract_address_salt_buf, contract_address_salt);
write(function_tree_root_buf, function_tree_root);
write(constructor_hash_buf, constructor_hash);
serialize::write(point_buf, point);
abis__compute_complete_contract_address(point_buf.data(),
contract_address_salt_buf.data(),
function_tree_root_buf.data(),
constructor_hash_buf.data(),
output.data());

// Convert buffer to `fr` for comparison to in-test calculated hash
NT::fr const actual = NT::fr::serialize_from_buffer(output.data());
EXPECT_EQ(actual, expected);
}
TEST(abi_tests, hash_tx_request)
{
// Construct TxRequest with some randomized fields
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/complete_address.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ template <typename NCT> struct CompleteAddress {
using fr = typename NCT::fr;
using boolean = typename NCT::boolean;

fr address;
typename NCT::address address;
Point<NCT> public_key;
fr partial_address;

Expand Down
18 changes: 11 additions & 7 deletions circuits/cpp/src/aztec3/circuits/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,22 @@ typename NCT::address compute_contract_address_from_partial(Point<NCT> const& po
return { NCT::hash(inputs, aztec3::GeneratorIndex::CONTRACT_ADDRESS) };
}

template <typename NCT>
CompleteAddress<NCT> compute_complete_contract_address(Point<NCT> const& point,
typename NCT::fr const& contract_address_salt,
typename NCT::fr const& function_tree_root,
typename NCT::fr const& constructor_hash)
template <typename NCT> typename aztec3::circuits::abis::CompleteAddress<NCT> compute_complete_address(
Point<NCT> const& point,
typename NCT::fr const& contract_address_salt,
typename NCT::fr const& function_tree_root,
typename NCT::fr const& constructor_hash)
{
using fr = typename NCT::fr;

const fr partial_address =
compute_partial_address<NCT>(contract_address_salt, function_tree_root, constructor_hash);
const fr contract_address = compute_contract_address_from_partial(point, partial_address);
const CompleteAddress complete_address = { contract_address, point, partial_address };

typename aztec3::circuits::abis::CompleteAddress<NCT> complete_address;
complete_address.address = compute_contract_address_from_partial(point, partial_address);
complete_address.public_key = point;
complete_address.partial_address = partial_address;

return complete_address;
}

Expand Down
8 changes: 4 additions & 4 deletions circuits/cpp/src/aztec3/circuits/kernel/private/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,10 @@ void common_contract_logic(DummyBuilder& builder,
auto constructor_hash =
compute_constructor_hash(function_data, private_call_public_inputs.args_hash, private_call_vk_hash);

auto const new_contract_address = compute_complete_contract_address(contract_dep_data.deployer_public_key,
contract_dep_data.contract_address_salt,
contract_dep_data.function_tree_root,
constructor_hash)
auto const new_contract_address = compute_complete_address(contract_dep_data.deployer_public_key,
contract_dep_data.contract_address_salt,
contract_dep_data.function_tree_root,
constructor_hash)
.address;

// Add new contract data if its a contract deployment function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ using plonk::stdlib::array_push;
using plonk::stdlib::is_array_empty;
using plonk::stdlib::push_array_to_array;

using aztec3::circuits::compute_complete_contract_address;
using aztec3::circuits::compute_complete_address;
using aztec3::circuits::compute_constructor_hash;
using aztec3::circuits::silo_commitment;
using aztec3::circuits::silo_nullifier;
Expand Down Expand Up @@ -113,11 +113,11 @@ void update_end_values(PrivateKernelInputsInner<CT> const& private_inputs, Kerne
"constructor_vk_hash does not match private call vk hash");

// compute the contract address (only valid if this is a contract deployment)
auto contract_address = compute_complete_contract_address<CT>(contract_deployment_data.deployer_public_key,
contract_deployment_data.contract_address_salt,
contract_deployment_data.function_tree_root,
constructor_hash)
.address;
auto const contract_address = compute_complete_address<CT>(contract_deployment_data.deployer_public_key,
contract_deployment_data.contract_address_salt,
contract_deployment_data.function_tree_root,
constructor_hash)
.address;

// must imply == derived address
is_contract_deployment.must_imply(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ std::pair<PrivateCallData<NT>, ContractDeploymentData<NT>> create_private_call_d
auto constructor_hash = compute_constructor_hash<NT>(function_data, args_hash, private_circuit_vk_hash);

// Derive contract address so that it can be used inside the constructor itself
const auto complete_contract_address = compute_complete_contract_address<NT>(
const auto complete_contract_address = compute_complete_address<NT>(
msg_sender_pub_key, contract_address_salt, contract_deployment_data.function_tree_root, constructor_hash);
// update the contract address in the call context now that it is known
call_context.storage_contract_address = complete_contract_address.address;
Expand Down Expand Up @@ -529,8 +529,10 @@ bool validate_deployed_contract_address(PrivateKernelInputsInit<NT> const& priva
auto expected_constructor_hash = compute_constructor_hash(
private_inputs.private_call.call_stack_item.function_data, tx_request.args_hash, private_circuit_vk_hash);

NT::fr const expected_contract_address = compute_complete_contract_address(
cdd.deployer_public_key, cdd.contract_address_salt, cdd.function_tree_root, expected_constructor_hash);
NT::fr const expected_contract_address =
compute_complete_address(
cdd.deployer_public_key, cdd.contract_address_salt, cdd.function_tree_root, expected_constructor_hash)
.address;

return (public_inputs.end.new_contracts[0].contract_address.to_field() == expected_contract_address);
}
Expand Down
11 changes: 3 additions & 8 deletions yarn-project/aztec-rpc/src/contract_tree/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
CONTRACT_TREE_HEIGHT,
CircuitsWasm,
CompleteAddress,
EthAddress,
FUNCTION_TREE_HEIGHT,
Fr,
Expand All @@ -17,10 +16,9 @@ import {
isConstructor,
} from '@aztec/circuits.js';
import {
computeCompleteContractAddress,
computeCompleteAddress,
computeContractLeaf,
computeFunctionTreeRoot,
computePartialAddress,
computeVarArgsHash,
hashConstructor,
} from '@aztec/circuits.js/abis';
Expand Down Expand Up @@ -95,11 +93,8 @@ export class ContractTree {
const vkHash = hashVKStr(constructorAbi.verificationKey, wasm);
const argsHash = await computeVarArgsHash(wasm, args);
const constructorHash = hashConstructor(wasm, functionData, argsHash, vkHash);
// TODO(benesjan) https://github.com/AztecProtocol/aztec-packages/issues/1873: create computeCompleteAddress
// function --> The following is wasteful as it computes partial address twice
const partialAddress = computePartialAddress(wasm, contractAddressSalt, root, constructorHash);
const address = computeCompleteContractAddress(wasm, from, contractAddressSalt, root, constructorHash);
const completeAddress = await CompleteAddress.create(address, from, partialAddress);

const completeAddress = computeCompleteAddress(wasm, from, contractAddressSalt, root, constructorHash);

const contractDao: ContractDao = {
...abi,
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/circuits.js/src/abis/abis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { makeAztecAddress, makeEthAddress, makePoint, makeTxRequest, makeVerific
import { CircuitsWasm } from '../wasm/circuits_wasm.js';
import {
computeCommitmentNonce,
computeCompleteContractAddress,
computeCompleteAddress,
computeContractLeaf,
computeFunctionLeaf,
computeFunctionSelector,
Expand Down Expand Up @@ -62,12 +62,12 @@ describe('abis wasm bindings', () => {
expect(res).toMatchSnapshot();
});

it('computes a contract address', () => {
it('computes a complete address', () => {
const deployerPubKey = makePoint();
const contractAddrSalt = new Fr(2n);
const treeRoot = new Fr(3n);
const constructorHash = new Fr(4n);
const res = computeCompleteContractAddress(wasm, deployerPubKey, contractAddrSalt, treeRoot, constructorHash);
const res = computeCompleteAddress(wasm, deployerPubKey, contractAddrSalt, treeRoot, constructorHash);
expect(res).toMatchSnapshot();
});

Expand Down
15 changes: 5 additions & 10 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
abisComputeBlockHash,
abisComputeBlockHashWithGlobals,
abisComputeCommitmentNonce,
abisComputeCompleteAddress,
abisComputeGlobalsHash,
abisComputePublicDataTreeIndex,
abisComputePublicDataTreeValue,
Expand Down Expand Up @@ -183,29 +184,23 @@ export function hashConstructor(
}

/**
* Computes a complete contract address.
* Computes a complete address.
* @param wasm - A module providing low-level wasm access.
* @param deployerPubKey - The pubkey of the contract deployer.
* @param contractAddrSalt - The salt used as one of the inputs of the contract address computation.
* @param fnTreeRoot - The function tree root of the contract being deployed.
* @param constructorHash - The hash of the constructor.
* @returns The complete contract address.
* @returns The complete address.
*/
export function computeCompleteContractAddress(
export function computeCompleteAddress(
wasm: IWasmModule,
deployerPubKey: PublicKey,
contractAddrSalt: Fr,
fnTreeRoot: Fr,
constructorHash: Fr,
): CompleteAddress {
wasm.call('pedersen__init');
const result = inputBuffersToOutputBuffer(
wasm,
'abis__compute_complete_contract_address',
[deployerPubKey.toBuffer(), contractAddrSalt.toBuffer(), fnTreeRoot.toBuffer(), constructorHash.toBuffer()],
32,
);
return CompleteAddress.fromBuffer(result);
return abisComputeCompleteAddress(wasm, deployerPubKey, contractAddrSalt, fnTreeRoot, constructorHash);
}

/**
Expand Down
Loading

0 comments on commit 0c10c00

Please sign in to comment.