Skip to content

Commit

Permalink
refactor: computeContractAddress as computeCompleteAddress (#1876)
Browse files Browse the repository at this point in the history
Fixes #1873
benesjan authored Sep 22, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 2fe8f5e commit 4d95b44
Showing 21 changed files with 274 additions and 280 deletions.
72 changes: 5 additions & 67 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@
#include "rollup/root/root_rollup_public_inputs.hpp"

#include "aztec3/circuits/abis/combined_accumulated_data.hpp"
#include "aztec3/circuits/abis/complete_address.hpp"
#include "aztec3/circuits/abis/final_accumulated_data.hpp"
#include "aztec3/circuits/abis/new_contract_data.hpp"
#include "aztec3/circuits/abis/packers.hpp"
@@ -33,9 +34,8 @@
namespace {

using aztec3::circuits::compute_constructor_hash;
using aztec3::circuits::compute_contract_address;
using aztec3::circuits::compute_partial_address;
using aztec3::circuits::abis::CallStackItem;
using aztec3::circuits::abis::CompleteAddress;
using aztec3::circuits::abis::ConstantsPacker;
using aztec3::circuits::abis::FunctionData;
using aztec3::circuits::abis::FunctionLeafPreimage;
@@ -289,39 +289,9 @@ WASM_EXPORT void abis__hash_constructor(uint8_t const* function_data_buf,
}

/**
* @brief Compute a contract address
* This is a WASM-export that can be called from Typescript.
*
* @details Computes a contract address by hashing the deployers public key along with the previously computed partial
* address Return the serialized results in the `output` buffer.
*
* @param point_data_buf point data struct as a buffer of bytes
* @param contract_address_salt_buf salt value for the contract address
* @param function_tree_root_buf root value of the contract's function tree
* @param constructor_hash_buf the hash of the contract constructor's verification key
* @param output buffer that will contain the output. The serialized contract address.
* @brief Compute a complete address.
*/
WASM_EXPORT void abis__compute_contract_address(uint8_t const* point_data_buf,
uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
uint8_t const* constructor_hash_buf,
uint8_t* output)
{
Point<NT> deployer_public_key;
NT::fr contract_address_salt;
NT::fr function_tree_root;
NT::fr constructor_hash;

serialize::read(point_data_buf, deployer_public_key);
read(contract_address_salt_buf, contract_address_salt);
read(function_tree_root_buf, function_tree_root);
read(constructor_hash_buf, constructor_hash);

NT::fr const contract_address =
compute_contract_address(deployer_public_key, contract_address_salt, function_tree_root, constructor_hash);

NT::fr::serialize_to_buffer(contract_address, output);
}
CBIND(abis__compute_complete_address, aztec3::circuits::abis::CompleteAddress<NT>::compute);

/**
* @brief Compute a contract address from deployer public key and partial address.
@@ -350,38 +320,6 @@ WASM_EXPORT void abis__compute_contract_address_from_partial(uint8_t const* poin
NT::fr::serialize_to_buffer(contract_address, output);
}

/**
* @brief Compute a partial address
* This is a WASM-export that can be called from Typescript.
*
* @details Computes a partial address by hashing the salt, function tree root and constructor hash
* Return the serialized results in the `output` buffer.
*
* @param contract_address_salt_buf salt value for the contract address
* @param function_tree_root_buf root value of the contract's function tree
* @param constructor_hash_buf the hash of the contract constructor's verification key
* @param output buffer that will contain the output. The serialized contract address.
* See the link bellow for more details:
* https://github.com/AztecProtocol/aztec-packages/blob/master/docs/docs/concepts/foundation/accounts/keys.md#addresses-partial-addresses-and-public-keys
*/
WASM_EXPORT void abis__compute_partial_address(uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
uint8_t const* constructor_hash_buf,
uint8_t* output)
{
NT::fr contract_address_salt;
NT::fr function_tree_root;
NT::fr constructor_hash;

read(contract_address_salt_buf, contract_address_salt);
read(function_tree_root_buf, function_tree_root);
read(constructor_hash_buf, constructor_hash);
NT::fr const partial_address =
compute_partial_address<NT>(contract_address_salt, function_tree_root, constructor_hash);

NT::fr::serialize_to_buffer(partial_address, output);
}

/**
* @brief Hash args for a function call.
*
@@ -400,7 +338,7 @@ WASM_EXPORT void abis__compute_var_args_hash(uint8_t const* args_buf, uint8_t* o
* @brief Generates a function tree leaf from its preimage.
* This is a WASM-export that can be called from Typescript.
*
* @details given a `uint8_t const*` buffer representing a function leaf's prieimage,
* @details given a `uint8_t const*` buffer representing a function leaf's preimage,
* construct a NewContractData instance, hash, and return the serialized results
* in the `output` buffer.
*
11 changes: 1 addition & 10 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.h
Original file line number Diff line number Diff line change
@@ -21,16 +21,7 @@ WASM_EXPORT void abis__hash_constructor(uint8_t const* func_data_buf,
uint8_t const* constructor_vk_hash_buf,
uint8_t* output);

WASM_EXPORT void abis__compute_contract_address(uint8_t const* point_data_buf,
uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
uint8_t const* constructor_hash_buf,
uint8_t* output);

WASM_EXPORT void abis__compute_partial_address(uint8_t const* contract_address_salt_buf,
uint8_t const* function_tree_root_buf,
uint8_t const* constructor_hash_buf,
uint8_t* output);
CBIND_DECL(abis__compute_complete_address);

CBIND_DECL(abis__compute_commitment_nonce);
CBIND_DECL(abis__compute_unique_commitment);
51 changes: 1 addition & 50 deletions circuits/cpp/src/aztec3/circuits/abis/c_bind.test.cpp
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
#include "function_leaf_preimage.hpp"
#include "tx_request.hpp"

#include "aztec3/circuits/abis/complete_address.hpp"
#include "aztec3/circuits/abis/new_contract_data.hpp"
#include "aztec3/circuits/abis/tx_request.hpp"
#include "aztec3/circuits/hash.hpp"
@@ -50,56 +51,6 @@ template <size_t NUM_BYTES> std::string bytes_to_hex_str(std::array<uint8_t, NUM

namespace aztec3::circuits::abis {

TEST(abi_tests, compute_partial_address)
{
auto const contract_address_salt = NT::fr(3);
auto const function_tree_root = NT::fr(4);
auto const constructor_hash = NT::fr(5);
NT::fr const expected = compute_partial_address<NT>(contract_address_salt, function_tree_root, constructor_hash);

std::array<uint8_t, sizeof(NT::fr)> output = { 0 };
std::vector<uint8_t> salt_buf;
std::vector<uint8_t> function_tree_root_buf;
std::vector<uint8_t> constructor_hash_buf;
write(salt_buf, contract_address_salt);
write(function_tree_root_buf, function_tree_root);
write(constructor_hash_buf, constructor_hash);
abis__compute_partial_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, compute_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);
NT::fr const expected =
compute_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_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
90 changes: 90 additions & 0 deletions circuits/cpp/src/aztec3/circuits/abis/complete_address.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#pragma once
#include "aztec3/circuits/abis/coordinate.hpp"
#include "aztec3/circuits/abis/point.hpp"
#include "aztec3/circuits/hash.hpp"
#include "aztec3/utils/types/circuit_types.hpp"
#include "aztec3/utils/types/convert.hpp"
#include "aztec3/utils/types/native_types.hpp"

#include <barretenberg/barretenberg.hpp>

namespace aztec3::circuits::abis {

using aztec3::circuits::compute_partial_address;
using aztec3::utils::types::CircuitTypes;
using aztec3::utils::types::NativeTypes;

template <typename NCT> struct CompleteAddress {
using fr = typename NCT::fr;
using boolean = typename NCT::boolean;

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

// for serialization, update with new fields
MSGPACK_FIELDS(address, public_key, partial_address);
bool operator==(CompleteAddress<NCT> const&) const = default;

template <typename Builder> CompleteAddress<CircuitTypes<Builder>> to_circuit_type(Builder& builder) const
{
static_assert((std::is_same<NativeTypes, NCT>::value));

auto to_ct = [&](auto& e) { return aztec3::utils::types::to_ct(builder, e); };

CompleteAddress<CircuitTypes<Builder>> complete_address = { to_ct(address),
to_ct(public_key),
to_ct(partial_address) };

return complete_address;
};

template <typename Builder> CompleteAddress<NativeTypes> to_native_type() const
{
static_assert((std::is_same<CircuitTypes<Builder>, NCT>::value));

auto to_nt = [&](auto& e) { return aztec3::utils::types::to_nt<Builder>(e); };

CompleteAddress<NativeTypes> complete_address = { to_nt(address), to_nt(public_key), to_nt(partial_address) };

return complete_address;
};

void set_public()
{
static_assert(!(std::is_same<NativeTypes, NCT>::value));

address.set_public();
public_key.set_public();
partial_address.set_public();
}

void assert_is_zero()
{
static_assert(!(std::is_same<NativeTypes, NCT>::value));

address.assert_is_zero();
public_key.assert_is_zero();
partial_address.assert_is_zero();
}

static CompleteAddress<NCT> compute(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);

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;
}
};

} // namespace aztec3::circuits::abis
13 changes: 0 additions & 13 deletions circuits/cpp/src/aztec3/circuits/hash.hpp
Original file line number Diff line number Diff line change
@@ -72,19 +72,6 @@ typename NCT::address compute_contract_address_from_partial(Point<NCT> const& po
return { NCT::hash(inputs, aztec3::GeneratorIndex::CONTRACT_ADDRESS) };
}

template <typename NCT> typename NCT::address compute_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)
{
using fr = typename NCT::fr;

const fr partial_address =
compute_partial_address<NCT>(contract_address_salt, function_tree_root, constructor_hash);

return compute_contract_address_from_partial(point, partial_address);
}

template <typename NCT> typename NCT::fr compute_commitment_nonce(typename NCT::fr const& first_nullifier,
typename NCT::fr const& commitment_index)
{
10 changes: 6 additions & 4 deletions circuits/cpp/src/aztec3/circuits/kernel/private/common.cpp
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

#include "init.hpp"

#include "aztec3/circuits/abis/complete_address.hpp"
#include "aztec3/circuits/abis/contract_deployment_data.hpp"
#include "aztec3/circuits/abis/function_data.hpp"
#include "aztec3/circuits/abis/kernel_circuit_public_inputs.hpp"
@@ -288,10 +289,11 @@ 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_contract_address<NT>(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 = abis::CompleteAddress<NT>::compute(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
NewContractData<NT> const native_new_contract_data{ new_contract_address,
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "init.hpp"

#include "aztec3/circuits/abis/complete_address.hpp"
#include "aztec3/circuits/abis/kernel_circuit_public_inputs.hpp"
#include "aztec3/circuits/abis/new_contract_data.hpp"
#include "aztec3/circuits/abis/private_kernel/private_kernel_inputs_inner.hpp"
@@ -21,7 +22,6 @@ using plonk::stdlib::is_array_empty;
using plonk::stdlib::push_array_to_array;

using aztec3::circuits::compute_constructor_hash;
using aztec3::circuits::compute_contract_address;
using aztec3::circuits::silo_commitment;
using aztec3::circuits::silo_nullifier;

@@ -113,10 +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_contract_address<CT>(contract_deployment_data.deployer_public_key,
contract_deployment_data.contract_address_salt,
contract_deployment_data.function_tree_root,
constructor_hash);
auto const contract_address = abis::CompleteAddress<CT>::compute(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(
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
#include "aztec3/circuits/abis/call_stack_item.hpp"
#include "aztec3/circuits/abis/combined_accumulated_data.hpp"
#include "aztec3/circuits/abis/combined_constant_data.hpp"
#include "aztec3/circuits/abis/complete_address.hpp"
#include "aztec3/circuits/abis/contract_deployment_data.hpp"
#include "aztec3/circuits/abis/function_data.hpp"
#include "aztec3/circuits/abis/historic_block_data.hpp"
@@ -225,8 +226,11 @@ 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
contract_address = compute_contract_address<NT>(
msg_sender_pub_key, contract_address_salt, contract_deployment_data.function_tree_root, constructor_hash);
contract_address = abis::CompleteAddress<NT>::compute(msg_sender_pub_key,
contract_address_salt,
contract_deployment_data.function_tree_root,
constructor_hash)
.address;
// update the contract address in the call context now that it is known
call_context.storage_contract_address = contract_address;
} else {
@@ -529,8 +533,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_contract_address(
cdd.deployer_public_key, cdd.contract_address_salt, cdd.function_tree_root, expected_constructor_hash);
NT::fr const expected_contract_address =
abis::CompleteAddress<NT>::compute(
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);
}
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,
@@ -17,10 +16,9 @@ import {
isConstructor,
} from '@aztec/circuits.js';
import {
computeContractAddress,
computeCompleteAddress,
computeContractLeaf,
computeFunctionTreeRoot,
computePartialAddress,
computeVarArgsHash,
hashConstructor,
} from '@aztec/circuits.js/abis';
@@ -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 = computeContractAddress(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,
90 changes: 52 additions & 38 deletions yarn-project/circuits.js/src/abis/__snapshots__/abis.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,43 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`abis wasm bindings computes a contract address 1`] = `
AztecAddress {
"buffer": {
"data": [
17,
160,
201,
96,
127,
185,
91,
146,
4,
147,
169,
211,
33,
226,
156,
174,
240,
174,
157,
149,
81,
249,
149,
226,
160,
18,
239,
108,
218,
140,
217,
137,
],
"type": "Buffer",
exports[`abis wasm bindings computes a complete address 1`] = `
CompleteAddress {
"address": AztecAddress {
"buffer": {
"data": [
17,
160,
201,
96,
127,
185,
91,
146,
4,
147,
169,
211,
33,
226,
156,
174,
240,
174,
157,
149,
81,
249,
149,
226,
160,
18,
239,
108,
218,
140,
217,
137,
],
"type": "Buffer",
},
},
"partialAddress": Fr {
"value": 14273840083696241216436261500085649155281267590119694711623393161813305965081n,
},
"publicKey": Point {
"kind": "point",
"x": Fr {
"value": 1n,
},
"y": Fr {
"value": 2n,
},
},
}
`;
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
@@ -5,7 +5,7 @@ import { makeAztecAddress, makeEthAddress, makePoint, makeTxRequest, makeVerific
import { CircuitsWasm } from '../wasm/circuits_wasm.js';
import {
computeCommitmentNonce,
computeContractAddress,
computeCompleteAddress,
computeContractLeaf,
computeFunctionLeaf,
computeFunctionSelector,
@@ -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 = computeContractAddress(wasm, deployerPubKey, contractAddrSalt, treeRoot, constructorHash);
const res = computeCompleteAddress(wasm, deployerPubKey, contractAddrSalt, treeRoot, constructorHash);
expect(res).toMatchSnapshot();
});

42 changes: 7 additions & 35 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import {
abisComputeBlockHash,
abisComputeBlockHashWithGlobals,
abisComputeCommitmentNonce,
abisComputeCompleteAddress,
abisComputeGlobalsHash,
abisComputePublicDataTreeIndex,
abisComputePublicDataTreeValue,
@@ -17,6 +18,7 @@ import {
} from '../cbind/circuits.gen.js';
import {
AztecAddress,
CompleteAddress,
FUNCTION_SELECTOR_NUM_BYTES,
Fr,
FunctionData,
@@ -182,53 +184,23 @@ export function hashConstructor(
}

/**
* Computes a 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 contract address.
* @returns The complete address.
*/
export function computeContractAddress(
export function computeCompleteAddress(
wasm: IWasmModule,
deployerPubKey: PublicKey,
contractAddrSalt: Fr,
fnTreeRoot: Fr,
constructorHash: Fr,
): AztecAddress {
wasm.call('pedersen__init');
const result = inputBuffersToOutputBuffer(
wasm,
'abis__compute_contract_address',
[deployerPubKey.toBuffer(), contractAddrSalt.toBuffer(), fnTreeRoot.toBuffer(), constructorHash.toBuffer()],
32,
);
return new AztecAddress(result);
}

/**
* Computes a partial address. Consists of all contract address components except the deployer public key.
* @param wasm - A module providing low-level wasm access.
* @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 partially constructed contract address.
*/
export function computePartialAddress(
wasm: IWasmModule,
contractAddrSalt: Fr,
fnTreeRoot: Fr,
constructorHash: Fr,
): Fr {
): CompleteAddress {
wasm.call('pedersen__init');
const result = inputBuffersToOutputBuffer(
wasm,
'abis__compute_partial_address',
[contractAddrSalt.toBuffer(), fnTreeRoot.toBuffer(), constructorHash.toBuffer()],
32,
);
return Fr.fromBuffer(result);
return abisComputeCompleteAddress(wasm, deployerPubKey, contractAddrSalt, fnTreeRoot, constructorHash);
}

/**
109 changes: 81 additions & 28 deletions yarn-project/circuits.js/src/cbind/circuits.gen.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import {
CircuitError,
CombinedAccumulatedData,
CombinedConstantData,
CompleteAddress,
ConstantRollupData,
ContractDeploymentData,
ContractStorageRead,
@@ -64,6 +65,70 @@ import {
toBuffer,
} from './types.js';

interface MsgpackPoint {
x: Buffer;
y: Buffer;
}

export function toPoint(o: MsgpackPoint): Point {
if (o.x === undefined) {
throw new Error('Expected x in Point deserialization');
}
if (o.y === undefined) {
throw new Error('Expected y in Point deserialization');
}
return new Point(Fr.fromBuffer(o.x), Fr.fromBuffer(o.y));
}

export function fromPoint(o: Point): MsgpackPoint {
if (o.x === undefined) {
throw new Error('Expected x in Point serialization');
}
if (o.y === undefined) {
throw new Error('Expected y in Point serialization');
}
return {
x: toBuffer(o.x),
y: toBuffer(o.y),
};
}

interface MsgpackCompleteAddress {
address: Buffer;
public_key: MsgpackPoint;
partial_address: Buffer;
}

export function toCompleteAddress(o: MsgpackCompleteAddress): CompleteAddress {
if (o.address === undefined) {
throw new Error('Expected address in CompleteAddress deserialization');
}
if (o.public_key === undefined) {
throw new Error('Expected public_key in CompleteAddress deserialization');
}
if (o.partial_address === undefined) {
throw new Error('Expected partial_address in CompleteAddress deserialization');
}
return new CompleteAddress(Address.fromBuffer(o.address), toPoint(o.public_key), Fr.fromBuffer(o.partial_address));
}

export function fromCompleteAddress(o: CompleteAddress): MsgpackCompleteAddress {
if (o.address === undefined) {
throw new Error('Expected address in CompleteAddress serialization');
}
if (o.publicKey === undefined) {
throw new Error('Expected publicKey in CompleteAddress serialization');
}
if (o.partialAddress === undefined) {
throw new Error('Expected partialAddress in CompleteAddress serialization');
}
return {
address: toBuffer(o.address),
public_key: fromPoint(o.publicKey),
partial_address: toBuffer(o.partialAddress),
};
}

interface MsgpackGlobalVariables {
chain_id: Buffer;
version: Buffer;
@@ -702,34 +767,6 @@ export function fromHistoricBlockData(o: HistoricBlockData): MsgpackHistoricBloc
};
}

interface MsgpackPoint {
x: Buffer;
y: Buffer;
}

export function toPoint(o: MsgpackPoint): Point {
if (o.x === undefined) {
throw new Error('Expected x in Point deserialization');
}
if (o.y === undefined) {
throw new Error('Expected y in Point deserialization');
}
return new Point(Fr.fromBuffer(o.x), Fr.fromBuffer(o.y));
}

export function fromPoint(o: Point): MsgpackPoint {
if (o.x === undefined) {
throw new Error('Expected x in Point serialization');
}
if (o.y === undefined) {
throw new Error('Expected y in Point serialization');
}
return {
x: toBuffer(o.x),
y: toBuffer(o.y),
};
}

interface MsgpackContractDeploymentData {
deployer_public_key: MsgpackPoint;
constructor_vk_hash: Buffer;
@@ -3088,6 +3125,22 @@ export function fromRootRollupPublicInputs(o: RootRollupPublicInputs): MsgpackRo
};
}

export function abisComputeCompleteAddress(
wasm: IWasmModule,
arg0: Point,
arg1: Fr,
arg2: Fr,
arg3: Fr,
): CompleteAddress {
return toCompleteAddress(
callCbind(wasm, 'abis__compute_complete_address', [
fromPoint(arg0),
toBuffer(arg1),
toBuffer(arg2),
toBuffer(arg3),
]),
);
}
export function abisComputeCommitmentNonce(wasm: IWasmModule, arg0: Fr, arg1: Fr): Fr {
return Fr.fromBuffer(callCbind(wasm, 'abis__compute_commitment_nonce', [toBuffer(arg0), toBuffer(arg1)]));
}
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/cbind/types.ts
Original file line number Diff line number Diff line change
@@ -95,6 +95,7 @@ export {
ContractDeploymentData,
TxContext,
CombinedConstantData,
CompleteAddress,
KernelCircuitPublicInputs,
KernelCircuitPublicInputsFinal,
Proof,
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {
computeContractAddress,
computeCompleteAddress,
computeFunctionTreeRoot,
computePartialAddress,
computeVarArgsHash,
hashConstructor,
} from '@aztec/circuits.js/abis';
import { ContractAbi, FunctionSelector, encodeArguments } from '@aztec/foundation/abi';

import { CircuitsWasm, CompleteAddress, DeploymentInfo, Fr, FunctionData, PublicKey } from '../index.js';
import { CircuitsWasm, DeploymentInfo, Fr, FunctionData, PublicKey } from '../index.js';
import { generateFunctionLeaves, hashVKStr, isConstructor } from './contract_tree/contract_tree.js';

/**
@@ -46,19 +45,14 @@ export async function getContractDeploymentInfo(
const argsHash = await computeVarArgsHash(wasm, flatArgs);
const constructorHash = hashConstructor(wasm, functionData, argsHash, constructorVkHash.toBuffer());

// 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, functionTreeRoot, constructorHash);
const contractAddress = computeContractAddress(
const completeAddress = computeCompleteAddress(
wasm,
publicKey,
contractAddressSalt,
functionTreeRoot,
constructorHash,
);

const completeAddress = await CompleteAddress.create(contractAddress, publicKey, partialAddress);

return {
completeAddress,
constructorHash: constructorVkHash,
Original file line number Diff line number Diff line change
@@ -3,9 +3,7 @@ import { BufferReader } from '@aztec/foundation/serialize';

import { computeContractAddressFromPartial } from '../abis/abis.js';
import { Grumpkin } from '../barretenberg/index.js';
import { CircuitsWasm, Fr, GrumpkinPrivateKey, Point } from '../index.js';
import { PartialAddress } from './partial_address.js';
import { PublicKey } from './public_key.js';
import { CircuitsWasm, Fr, GrumpkinPrivateKey, PartialAddress, Point, PublicKey } from '../index.js';

/**
* A complete address is a combination of an Aztec address, a public key and a partial address.
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/structs/index.ts
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ export * from './aggregation_object.js';
export * from './membership_witness.js';
export * from './read_request_membership_witness.js';
export * from './public_call_request.js';
export * from './complete_address.js';
export * from '@aztec/foundation/eth-address';

export * from '@aztec/foundation/fields';
1 change: 0 additions & 1 deletion yarn-project/circuits.js/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './contract_function_dao.js';
export * from './complete_address.js';
export * from './deployment_info.js';
export * from './partial_address.js';
export * from './grumpkin_private_key.js';
4 changes: 2 additions & 2 deletions yarn-project/end-to-end/src/e2e_sandbox_example.test.ts
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ import {
computeMessageSecretHash,
createAztecRpcClient,
createDebugLogger,
getSchnorrAccount,
getSandboxAccountsWallets,
getSchnorrAccount,
waitForSandbox,
} from '@aztec/aztec.js';
import { GrumpkinScalar } from '@aztec/circuits.js';
@@ -47,7 +47,7 @@ describe('e2e_sandbox_example', () => {
logger(`Loaded alice's account at ${alice.toShortString()}`);
logger(`Loaded bob's account at ${bob.toShortString()}`);
// docs:end:load_accounts

// docs:start:Deployment
////////////// DEPLOY OUR TOKEN CONTRACT //////////////

1 change: 1 addition & 0 deletions yarn-project/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -23,3 +23,4 @@ export * from './interfaces/index.js';
export * from './sibling_path.js';
export * from './auth_witness.js';
export * from '@aztec/circuits.js/types';
export { CompleteAddress } from '@aztec/circuits.js';

0 comments on commit 4d95b44

Please sign in to comment.