Skip to content

Commit

Permalink
feat: Enable pkc in noir (#3194)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant authored Nov 2, 2023
1 parent ffafcef commit 1ef892b
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 50 deletions.
11 changes: 7 additions & 4 deletions circuits/cpp/src/aztec3/circuits/kernel/private/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ void common_contract_logic(DummyBuilder& builder,
const auto& storage_contract_address = private_call_public_inputs.call_context.storage_contract_address;
const auto& portal_contract_address = private_call.portal_contract_address;

const auto private_call_vk_hash = stdlib::recursion::verification_key<CT::bn254>::hash_native(private_call.vk);
// TODO(#3062) VKs are mocked out for now
// const auto private_call_vk_hash = stdlib::recursion::verification_key<CT::bn254>::hash_native(private_call.vk);
const auto private_call_vk_hash = 0;

const auto is_contract_deployment = public_inputs.constants.tx_context.is_contract_deployment_tx;

Expand All @@ -367,9 +369,10 @@ void common_contract_logic(DummyBuilder& builder,
native_new_contract_data,
format(PRIVATE_KERNEL_CIRCUIT_ERROR_MESSAGE_BEGINNING, "too many contracts created in one tx"));

builder.do_assert(contract_dep_data.constructor_vk_hash == private_call_vk_hash,
"constructor_vk_hash doesn't match private_call_vk_hash",
CircuitErrorCode::PRIVATE_KERNEL__INVALID_CONSTRUCTOR_VK_HASH);
// TODO(#3062) VKs are mocked out for now
// builder.do_assert(contract_dep_data.constructor_vk_hash == private_call_vk_hash,
// "constructor_vk_hash doesn't match private_call_vk_hash",
// CircuitErrorCode::PRIVATE_KERNEL__INVALID_CONSTRUCTOR_VK_HASH);

// must imply == derived address
builder.do_assert(storage_contract_address == new_contract_address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,22 @@ TEST_F(native_private_kernel_init_tests, contract_deployment_call_stack_item_has
CircuitErrorCode::PRIVATE_KERNEL__PRIVATE_CALL_STACK_ITEM_HASH_MISMATCH);
}

TEST_F(native_private_kernel_init_tests, contract_deployment_incorrect_constructor_vk_hash_fails)
{
auto private_inputs = do_private_call_get_kernel_inputs_init(true, constructor, standard_test_args());

// Pollute the constructor vk hash in the tx_request.
private_inputs.tx_request.tx_context.contract_deployment_data.constructor_vk_hash = NT::fr::random_element();

DummyBuilder builder =
DummyBuilder("private_kernel_tests__contract_deployment_incorrect_constructor_vk_hash_fails");
native_private_kernel_circuit_initial(builder, private_inputs);

EXPECT_EQ(builder.failed(), true);
EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::PRIVATE_KERNEL__INVALID_CONSTRUCTOR_VK_HASH);
EXPECT_EQ(builder.get_first_failure().message, "constructor_vk_hash doesn't match private_call_vk_hash");
}
// TODO(#3062) VKs are mocked out for now
// TEST_F(native_private_kernel_init_tests, contract_deployment_incorrect_constructor_vk_hash_fails)
// {
// auto private_inputs = do_private_call_get_kernel_inputs_init(true, constructor, standard_test_args());

// // Pollute the constructor vk hash in the tx_request.
// private_inputs.tx_request.tx_context.contract_deployment_data.constructor_vk_hash = NT::fr::random_element();

// DummyBuilder builder =
// DummyBuilder("private_kernel_tests__contract_deployment_incorrect_constructor_vk_hash_fails");
// native_private_kernel_circuit_initial(builder, private_inputs);

// EXPECT_EQ(builder.failed(), true);
// EXPECT_EQ(builder.get_first_failure().code, CircuitErrorCode::PRIVATE_KERNEL__INVALID_CONSTRUCTOR_VK_HASH);
// EXPECT_EQ(builder.get_first_failure().message, "constructor_vk_hash doesn't match private_call_vk_hash");
// }

TEST_F(native_private_kernel_init_tests, contract_deployment_incorrect_contract_address_fails)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ std::pair<PrivateCallData<NT>, ContractDeploymentData<NT>> create_private_call_d
// for private calls - to generate the function leaf, etc
auto const private_circuit_vk = is_circuit ? utils::get_verification_key_from_file() : utils::fake_vk();

const NT::fr private_circuit_vk_hash =
stdlib::recursion::verification_key<CT::bn254>::hash_native(private_circuit_vk);
// TODO(#3062) VKs are mocked out for now
// const NT::fr private_circuit_vk_hash =
// stdlib::recursion::verification_key<CT::bn254>::hash_native(private_circuit_vk);
const NT::fr private_circuit_vk_hash = 0;

ContractDeploymentData<NT> contract_deployment_data{};
NT::fr contract_tree_root = 0; // TODO(david) set properly for constructor?
Expand Down Expand Up @@ -529,8 +531,10 @@ bool validate_deployed_contract_address(PrivateKernelInputsInit<NT> const& priva
auto tx_request = private_inputs.tx_request;
auto cdd = private_inputs.tx_request.tx_context.contract_deployment_data;

auto private_circuit_vk_hash =
stdlib::recursion::verification_key<CT::bn254>::hash_native(private_inputs.private_call.vk);
// TODO(#3062) VKs are mocked out for now
// auto private_circuit_vk_hash =
// stdlib::recursion::verification_key<CT::bn254>::hash_native(private_inputs.private_call.vk);
auto private_circuit_vk_hash = 0;

auto expected_constructor_hash = compute_constructor_hash(
private_inputs.private_call.call_stack_item.function_data, tx_request.args_hash, private_circuit_vk_hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export function generateFunctionLeaves(functions: ContractFunctionDao[], wasm: C
const isInternal = f.isInternal;
const isPrivate = f.functionType === FunctionType.SECRET;
// All non-unconstrained functions have vks
const vkHash = hashVKStr(f.verificationKey!, wasm);
// TODO we'd need to have a defined length of the VK for this to be computed in noir
// const vkHash = hashVKStr(f.verificationKey!, wasm);
const vkHash = Buffer.alloc(32, 0);
// TODO
// FIXME: https://github.com/AztecProtocol/aztec3-packages/issues/262
// const acirHash = keccak(Buffer.from(f.bytecode, 'hex'));
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_deploy_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,5 @@ describe('e2e_deploy_contract', () => {
minTxsPerBlock: 1,
});
}
});
}, 60_000);
});
8 changes: 4 additions & 4 deletions yarn-project/end-to-end/src/guides/up_quick_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
set -eux

# docs:start:declare-accounts
ALICE="0x0f394e8bd156e15153376a711e3054821c2a1c1047dcfb3745d636a57fb42ab1"
BOB="0x2b67f90f0044596190644ddafea4152de47bd4781559493860fa7358e19d090a"
ALICE="0x16efad912187aa8ef0dcc6ef4f3743ab327b06465d4d229943f2fe3f88b06ad9"
BOB="0x17f5e66bfe7dafc76434268bcb3968a8bc207b476aeed78d1e4a2f02aad45842"
ALICE_PRIVATE_KEY="0x2153536ff6628eee01cf4024889ff977a18d9fa61d0e414422f7681cf085c281"
# docs:end:declare-accounts

Expand All @@ -15,9 +15,9 @@ aztec-cli deploy \
--salt 0 \
--args $ALICE

aztec-cli check-deploy --contract-address 0x2f45f498b7912c779dde8e3594622e36d7908088b09e99ab91caaafb40d1f9ef
aztec-cli check-deploy --contract-address 0x0ed3aaa22d69559ee368b32fbafb24b49b103c0a07bd834fd519c8157553ec1f

CONTRACT="0x2f45f498b7912c779dde8e3594622e36d7908088b09e99ab91caaafb40d1f9ef"
CONTRACT="0x0ed3aaa22d69559ee368b32fbafb24b49b103c0a07bd834fd519c8157553ec1f"
# docs:end:deploy

# docs:start:mint-private
Expand Down
1 change: 1 addition & 0 deletions yarn-project/pxe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@aztec/foundation": "workspace:^",
"@aztec/key-store": "workspace:^",
"@aztec/noir-compiler": "workspace:^",
"@aztec/noir-protocol-circuits": "workspace:^",
"@aztec/types": "workspace:^",
"koa": "^2.14.2",
"koa-router": "^12.0.0",
Expand Down
24 changes: 4 additions & 20 deletions yarn-project/pxe/src/kernel_prover/proof_creator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
CircuitError,
CircuitsWasm,
KernelCircuitPublicInputs,
KernelCircuitPublicInputsFinal,
Expand All @@ -9,14 +8,12 @@ import {
PrivateKernelInputsOrdering,
Proof,
makeEmptyProof,
privateKernelSimInit,
privateKernelSimInner,
privateKernelSimOrdering,
} from '@aztec/circuits.js';
import { siloCommitment } from '@aztec/circuits.js/abis';
import { Fr } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
import { elapsed } from '@aztec/foundation/timer';
import { executeInit, executeInner, executeOrdering } from '@aztec/noir-protocol-circuits';
import { CircuitSimulationStats } from '@aztec/types/stats';

/**
Expand Down Expand Up @@ -109,11 +106,7 @@ export class KernelProofCreator implements ProofCreator {
}

public async createProofInit(privateInputs: PrivateKernelInputsInit): Promise<ProofOutput> {
const wasm = await CircuitsWasm.get();
const [duration, result] = await elapsed(() => privateKernelSimInit(wasm, privateInputs));
if (result instanceof CircuitError) {
throw new CircuitError(result.code, result.message);
}
const [duration, result] = await elapsed(() => executeInit(privateInputs));
this.log(`Simulated private kernel init`, {
eventName: 'circuit-simulation',
circuitName: 'private-kernel-init',
Expand All @@ -131,11 +124,7 @@ export class KernelProofCreator implements ProofCreator {
}

public async createProofInner(privateInputs: PrivateKernelInputsInner): Promise<ProofOutput> {
const wasm = await CircuitsWasm.get();
const [duration, result] = await elapsed(() => privateKernelSimInner(wasm, privateInputs));
if (result instanceof CircuitError) {
throw new CircuitError(result.code, result.message);
}
const [duration, result] = await elapsed(() => executeInner(privateInputs));
this.log(`Simulated private kernel inner`, {
eventName: 'circuit-simulation',
circuitName: 'private-kernel-inner',
Expand All @@ -153,12 +142,7 @@ export class KernelProofCreator implements ProofCreator {
}

public async createProofOrdering(privateInputs: PrivateKernelInputsOrdering): Promise<ProofOutputFinal> {
const wasm = await CircuitsWasm.get();
this.log('Executing private kernel simulation ordering...');
const [duration, result] = await elapsed(() => privateKernelSimOrdering(wasm, privateInputs));
if (result instanceof CircuitError) {
throw new CircuitError(result.code, result.message);
}
const [duration, result] = await elapsed(() => executeOrdering(privateInputs));
this.log(`Simulated private kernel ordering`, {
eventName: 'circuit-simulation',
circuitName: 'private-kernel-ordering',
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/pxe/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
{
"path": "../noir-compiler"
},
{
"path": "../noir-protocol-circuits"
},
{
"path": "../types"
}
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ __metadata:
languageName: unknown
linkType: soft

"@aztec/noir-protocol-circuits@workspace:noir-protocol-circuits":
"@aztec/noir-protocol-circuits@workspace:^, @aztec/noir-protocol-circuits@workspace:noir-protocol-circuits":
version: 0.0.0-use.local
resolution: "@aztec/noir-protocol-circuits@workspace:noir-protocol-circuits"
dependencies:
Expand Down Expand Up @@ -757,6 +757,7 @@ __metadata:
"@aztec/foundation": "workspace:^"
"@aztec/key-store": "workspace:^"
"@aztec/noir-compiler": "workspace:^"
"@aztec/noir-protocol-circuits": "workspace:^"
"@aztec/types": "workspace:^"
"@jest/globals": ^29.5.0
"@rushstack/eslint-patch": ^1.1.4
Expand Down

0 comments on commit 1ef892b

Please sign in to comment.