Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-kothari committed May 3, 2023
1 parent e0e1198 commit 930ba00
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 72 deletions.
13 changes: 12 additions & 1 deletion circuits/cpp/src/aztec3/circuits/abis/c_bind.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "c_bind.h"

#include "call_stack_item.hpp"
#include "function_data.hpp"
#include "function_leaf_preimage.hpp"
#include "kernel_circuit_public_inputs.hpp"
Expand All @@ -18,6 +19,7 @@
#include "aztec3/circuits/abis/function_data.hpp"
#include "aztec3/circuits/abis/function_leaf_preimage.hpp"
#include "aztec3/circuits/abis/new_contract_data.hpp"
#include "aztec3/circuits/abis/types.hpp"
#include <aztec3/circuits/hash.hpp>
#include <aztec3/constants.hpp>
#include <aztec3/utils/array.hpp>
Expand All @@ -31,12 +33,14 @@ namespace {

using aztec3::circuits::compute_constructor_hash;
using aztec3::circuits::compute_contract_address;
using aztec3::circuits::abis::CallStackItem;
using aztec3::circuits::abis::FunctionData;
using aztec3::circuits::abis::FunctionLeafPreimage;
using aztec3::circuits::abis::NewContractData;
using aztec3::circuits::abis::TxContext;
using aztec3::circuits::abis::TxRequest;
using NT = aztec3::utils::types::NativeTypes;
using aztec3::circuits::abis::PublicTypes;

// Cbind helper functions

Expand Down Expand Up @@ -125,7 +129,7 @@ WASM_EXPORT void abis__hash_tx_request(uint8_t const* tx_request_buf, uint8_t* o
{
TxRequest<NT> tx_request;
read(tx_request_buf, tx_request);
// TODO consider using write() and read() instead of
// TODO(dbanks12) consider using write() and read() instead of
// serialize to/from everywhere here and in test
NT::fr::serialize_to_buffer(tx_request.hash(), output);
}
Expand Down Expand Up @@ -342,6 +346,13 @@ WASM_EXPORT void abis__compute_contract_leaf(uint8_t const* contract_leaf_preima
NT::fr::serialize_to_buffer(to_write, output);
}

WASM_EXPORT void abis__compute_call_stack_item_hash(uint8_t const* call_stack_item_buf, uint8_t* output)
{
CallStackItem<NT, PublicTypes> call_stack_item;
read(call_stack_item_buf, call_stack_item);
NT::fr::serialize_to_buffer(call_stack_item.hash(), output);
}

/* Typescript test helpers that call as_string_output() to stress serialization.
* Each of these take an object buffer, and a string size pointer.
* They return a string pointer (to be bbfree'd) and write to the string size pointer. */
Expand Down
5 changes: 4 additions & 1 deletion circuits/cpp/src/aztec3/circuits/abis/c_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ WASM_EXPORT void abis__compute_contract_address(uint8_t const* deployer_address_
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_call_stack_item_hash(uint8_t const* call_stack_item_buf, uint8_t* output);

}
6 changes: 5 additions & 1 deletion circuits/cpp/src/aztec3/circuits/kernel/public/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ void validate_this_public_call_hash(DummyComposer& composer,

composer.do_assert(
popped_public_call_hash == calculated_this_public_call_hash,
"calculated public_call_hash does not match provided public_call_hash at the top of the call stack",
format("calculated public_call_hash (",
calculated_this_public_call_hash,
") does not match provided public_call_hash (",
popped_public_call_hash,
") at the top of the call stack"),
CircuitErrorCode::PUBLIC_KERNEL__CALCULATED_PRIVATE_CALL_HASH_AND_PROVIDED_PRIVATE_CALL_HASH_MISMATCH);
};
} // namespace aztec3::circuits::kernel::public_kernel
8 changes: 5 additions & 3 deletions circuits/cpp/src/aztec3/circuits/kernel/public/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ void common_validate_call_stack(DummyComposer& composer, KernelInput const& publ
// Note: this assumes it's computationally infeasible to have `0` as a valid call_stack_item_hash.
// Assumes `hash == 0` means "this stack item is empty".
const auto calculated_hash = hash == 0 ? 0 : preimage.hash();
composer.do_assert(hash == calculated_hash,
format("public_call_stack[", i, "] = ", hash, "; does not reconcile"),
CircuitErrorCode::PUBLIC_KERNEL__PUBLIC_CALL_STACK_MISMATCH);
composer.do_assert(
hash == calculated_hash,
format(
"public_call_stack[", i, "] = ", hash, "; does not reconcile with calculatedHash = ", calculated_hash),
CircuitErrorCode::PUBLIC_KERNEL__PUBLIC_CALL_STACK_MISMATCH);

// here we validate the msg sender for each call on the stack
// we need to consider regular vs delegate calls
Expand Down
7 changes: 7 additions & 0 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 {
TxRequest,
NewContractData,
FunctionLeafPreimage,
PublicCallStackItem,
} from '../index.js';
import { serializeToBuffer, serializeBufferArrayToVector } from '../utils/serialize.js';
import { AsyncWasmWrapper, WasmWrapper } from '@aztec/foundation/wasm';
Expand Down Expand Up @@ -146,3 +147,9 @@ export function computeContractLeaf(wasm: WasmWrapper, cd: NewContractData) {
const value = wasmSyncCall(wasm, 'abis__compute_contract_leaf', cd, 32);
return Fr.fromBuffer(value);
}

export function computeCallStackItemHash(wasm: WasmWrapper, callStackItem: PublicCallStackItem) {
wasm.call('pedersen__init');
const value = wasmSyncCall(wasm, 'abis__compute_call_stack_item_hash', callStackItem, 32);
return Fr.fromBuffer(value);
}
9 changes: 5 additions & 4 deletions yarn-project/circuits.js/src/kernel/public_kernel.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { simulatePublicKernelCircuit, simulatePublicKernelCircuitNoPreviousKernel } from '../index.js';
import { Fr, simulatePublicKernelCircuit, simulatePublicKernelCircuitNoPreviousKernel } from '../index.js';
import { makePublicKernelInputsNoKernelInput, makePublicKernelInputsWithEmptyOutput } from '../tests/factories.js';

describe('kernel/public_kernel', () => {
it('simulates public kernel circuit with previous public kernel', async function () {
const input = makePublicKernelInputsWithEmptyOutput();
const input = await makePublicKernelInputsWithEmptyOutput();
input.previousKernel.publicInputs.isPrivateKernel = false;
const result = await simulatePublicKernelCircuit(input);
expect(result).toBeDefined();
});

it('simulates public kernel circuit with previous private kernel', async function () {
const input = makePublicKernelInputsWithEmptyOutput();
const input = await makePublicKernelInputsWithEmptyOutput();
input.previousKernel.publicInputs.isPrivateKernel = true;
input.previousKernel.publicInputs.end.publicCallCount = Fr.ZERO;
const result = await simulatePublicKernelCircuit(input);
expect(result).toBeDefined();
});

it('simulates public kernel circuit with no previous kernel', async function () {
const input = makePublicKernelInputsNoKernelInput();
const input = await makePublicKernelInputsNoKernelInput();
const result = await simulatePublicKernelCircuitNoPreviousKernel(input);
expect(result).toBeDefined();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,11 +803,11 @@ public_call:
call_stack_item:
contract_address: 0x1001
function_data: function_selector: 4098
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x1011
storage_contract_address: 0x1012
storage_contract_address: 0x1001
portal_contract_address: 0x1313131313131313131313131313131313131313
is_delegate_call: 0
is_static_call: 0
Expand Down Expand Up @@ -838,7 +838,7 @@ current_value: 0x1514
storage_slot: 0x1514
current_value: 0x1515
]
public_call_stack: [ 0x1611 0x1612 0x1613 0x1614 ]
public_call_stack: [ 0x1f7ec2163f0e78b25257ca693b8606d5e6c940c36e2e77cd0aa1afbbf167e95a 0xbfa6e6e8892f68b806bca1fd20cf704dd5c449440b54ef2957a3d267342bda6 0x29618099103194faea4e2c1c2a77bbb165bfe89961c16369b797cfd38671a2ba 0x1403602a993517c978fd5ea6ea17c02280692bb4b84e38e1a92f8c6d1d7b858b ]
l1_msg_stack: [ 0x1711 0x1712 ]
historic_public_data_tree_root: 0x1811
prover_address: 0x1812
Expand All @@ -847,11 +847,11 @@ prover_address: 0x1812
public_call_stack_preimages:
[ contract_address: 0x1301
function_data: function_selector: 1302
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x1311
storage_contract_address: 0x1312
public_inputs: call_context: msg_sender: 0x1001
storage_contract_address: 0x1301
portal_contract_address: 0x1313131313131313131313131313131313131313
is_delegate_call: 0
is_static_call: 0
Expand Down Expand Up @@ -889,11 +889,11 @@ prover_address: 0x1b12
contract_address: 0x1302
function_data: function_selector: 1303
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x1312
storage_contract_address: 0x1313
public_inputs: call_context: msg_sender: 0x1001
storage_contract_address: 0x1302
portal_contract_address: 0x1414141414141414141414141414141414141414
is_delegate_call: 0
is_static_call: 0
Expand Down Expand Up @@ -931,11 +931,11 @@ prover_address: 0x1b13
contract_address: 0x1303
function_data: function_selector: 1304
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x1313
storage_contract_address: 0x1314
public_inputs: call_context: msg_sender: 0x1001
storage_contract_address: 0x1303
portal_contract_address: 0x1515151515151515151515151515151515151515
is_delegate_call: 0
is_static_call: 0
Expand Down Expand Up @@ -973,11 +973,11 @@ prover_address: 0x1b14
contract_address: 0x1304
function_data: function_selector: 1305
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x1314
storage_contract_address: 0x1315
public_inputs: call_context: msg_sender: 0x1001
storage_contract_address: 0x1304
portal_contract_address: 0x1616161616161616161616161616161616161616
is_delegate_call: 0
is_static_call: 0
Expand Down Expand Up @@ -1050,11 +1050,11 @@ public_call:
call_stack_item:
contract_address: 0x101
function_data: function_selector: 258
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x111
storage_contract_address: 0x112
storage_contract_address: 0x101
portal_contract_address: 0x1313131313131313131313131313131313131313
is_delegate_call: 0
is_static_call: 0
Expand Down Expand Up @@ -1085,7 +1085,7 @@ current_value: 0x614
storage_slot: 0x614
current_value: 0x615
]
public_call_stack: [ 0x711 0x712 0x713 0x714 ]
public_call_stack: [ 0x2d514733a86bef4a33e2d599e01f3a73cbb7c17c316dfc5009838ce546587685 0x80562236b9b336749abfe2e71c4d2688ef96a2e2c09accce5e34fbb61c5416a 0x2569d412538dd4259fd18e60c166f3adbd0ab124dca7175a8448a7afdc1e7c40 0x1acab4b3623e71d9b08ecc27a170d06e38d1fca67ee03b3efe92365ebfb250f4 ]
l1_msg_stack: [ 0x811 0x812 ]
historic_public_data_tree_root: 0x911
prover_address: 0x912
Expand All @@ -1094,11 +1094,11 @@ prover_address: 0x912
public_call_stack_preimages:
[ contract_address: 0x401
function_data: function_selector: 402
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x411
storage_contract_address: 0x412
public_inputs: call_context: msg_sender: 0x101
storage_contract_address: 0x401
portal_contract_address: 0x1313131313131313131313131313131313131313
is_delegate_call: 0
is_static_call: 0
Expand Down Expand Up @@ -1136,11 +1136,11 @@ prover_address: 0xc12
contract_address: 0x402
function_data: function_selector: 403
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x412
storage_contract_address: 0x413
public_inputs: call_context: msg_sender: 0x101
storage_contract_address: 0x402
portal_contract_address: 0x1414141414141414141414141414141414141414
is_delegate_call: 0
is_static_call: 0
Expand Down Expand Up @@ -1178,11 +1178,11 @@ prover_address: 0xc13
contract_address: 0x403
function_data: function_selector: 404
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x413
storage_contract_address: 0x414
public_inputs: call_context: msg_sender: 0x101
storage_contract_address: 0x403
portal_contract_address: 0x1515151515151515151515151515151515151515
is_delegate_call: 0
is_static_call: 0
Expand Down Expand Up @@ -1220,11 +1220,11 @@ prover_address: 0xc14
contract_address: 0x404
function_data: function_selector: 405
is_private: 1
is_constructor: 1
is_private: 0
is_constructor: 0
public_inputs: call_context: msg_sender: 0x414
storage_contract_address: 0x415
public_inputs: call_context: msg_sender: 0x101
storage_contract_address: 0x404
portal_contract_address: 0x1616161616161616161616161616161616161616
is_delegate_call: 0
is_static_call: 0
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuits.js/src/structs/kernel/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ describe('structs/kernel', () => {
});

it(`serializes and prints public_kernel_inputs`, async () => {
const kernelInputs = makePublicKernelInputs();
const kernelInputs = await makePublicKernelInputs();
await expectSerializeToMatchSnapshot(
kernelInputs.toBuffer(),
'abis__test_roundtrip_serialize_public_kernel_inputs',
);
});

it(`serializes and prints public_kernel_inputs_no_previous_kernel`, async () => {
const kernelInputs = makePublicKernelInputsNoKernelInput();
const kernelInputs = await makePublicKernelInputsNoKernelInput();
await expectSerializeToMatchSnapshot(
kernelInputs.toBuffer(),
'abis__test_roundtrip_serialize_public_kernel_inputs_no_previous_kernel',
Expand Down
Loading

0 comments on commit 930ba00

Please sign in to comment.