Skip to content

Commit

Permalink
fix: adding missing pieces for PKC in noir (#2782)
Browse files Browse the repository at this point in the history
Please provide a paragraph or two giving a summary of the change,
including relevant motivation and context.

# Checklist:
Remove the checklist to signal you've completed it. Enable auto-merge if
the PR is ready to merge.
- [ ] If the pull request requires a cryptography review (e.g.
cryptographic algorithm implementations) I have added the 'crypto' tag.
- [ ] I have reviewed my diff in github, line by line and removed
unexpected formatting changes, testing logs, or commented-out code.
- [ ] Every change is related to the PR description.
- [ ] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to relevant issues (if any exist).

---------

Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
sirasistant and kevaundray authored Oct 11, 2023
1 parent 523d96a commit 9cb2063
Show file tree
Hide file tree
Showing 42 changed files with 778 additions and 139 deletions.
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/c_bind.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ TEST(abi_tests, hash_constructor)

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

// Confirm cbind output == expected hash
EXPECT_EQ(got_hash, expected_hash);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ template <typename NCT> struct FunctionLeafPreimage {
std::vector<fr> const inputs = {
selector.value, fr(is_internal), fr(is_private), vk_hash, acir_hash,
};
return NCT::compress(inputs, GeneratorIndex::FUNCTION_LEAF);
return NCT::hash(inputs, GeneratorIndex::FUNCTION_LEAF);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ template <typename NCT> struct NewContractData {
fr(function_tree_root),
};

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

void conditional_select(const boolean& condition, const NewContractData<NCT>& other)
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/public_data_read.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ template <typename NCT> struct PublicDataRead {
value,
};

return NCT::compress(inputs, GeneratorIndex::PUBLIC_DATA_READ);
return NCT::hash(inputs, GeneratorIndex::PUBLIC_DATA_READ);
}

void set_public()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ template <typename NCT> struct PublicDataUpdateRequest {
new_value,
};

return NCT::compress(inputs, GeneratorIndex::PUBLIC_DATA_UPDATE_REQUEST);
return NCT::hash(inputs, GeneratorIndex::PUBLIC_DATA_UPDATE_REQUEST);
}

void set_public()
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/tx_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ template <typename NCT> struct TxContext {
version,
};

return NCT::compress(inputs, GeneratorIndex::TX_CONTEXT);
return NCT::hash(inputs, GeneratorIndex::TX_CONTEXT);
}
};

Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/abis/tx_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ template <typename NCT> struct TxRequest {
inputs.push_back(args_hash);
inputs.push_back(tx_context.hash());

return NCT::compress(inputs, GeneratorIndex::TX_REQUEST);
return NCT::hash(inputs, GeneratorIndex::TX_REQUEST);
}
};

Expand Down
6 changes: 3 additions & 3 deletions circuits/cpp/src/aztec3/circuits/hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ template <typename NCT> typename NCT::fr compute_constructor_hash(FunctionData<N
constructor_vk_hash,
};

return NCT::compress(inputs, aztec3::GeneratorIndex::CONSTRUCTOR);
return NCT::hash(inputs, aztec3::GeneratorIndex::CONSTRUCTOR);
}

template <typename NCT> typename NCT::fr compute_partial_address(typename NCT::fr const& contract_address_salt,
Expand Down Expand Up @@ -122,7 +122,7 @@ typename NCT::fr silo_nullifier(typename NCT::address const& contract_address, t
};

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1475): use hash here (everywhere?)
return NCT::compress(inputs, aztec3::GeneratorIndex::OUTER_NULLIFIER);
return NCT::hash(inputs, aztec3::GeneratorIndex::OUTER_NULLIFIER);
}


Expand All @@ -140,7 +140,7 @@ template <typename NCT> typename NCT::fr compute_block_hash(typename NCT::fr con
contract_tree_root, l1_to_l2_data_tree_root, public_data_tree_root,
};

return NCT::compress(inputs, aztec3::GeneratorIndex::BLOCK_HASH);
return NCT::hash(inputs, aztec3::GeneratorIndex::BLOCK_HASH);
}

template <typename NCT>
Expand Down
2 changes: 1 addition & 1 deletion circuits/cpp/src/aztec3/circuits/rollup/base/.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ TEST_F(base_rollup_tests, native_contract_leaf_inserted_in_non_empty_snapshot_tr
inputs.new_contracts_subtree_sibling_path = sibling_path;

// create expected end contract tree snapshot
auto expected_contract_leaf = crypto::pedersen_commitment::compress_native(
auto expected_contract_leaf = crypto::pedersen_commitment::lookup::compress_native(
{ new_contract.contract_address, new_contract.portal_contract_address, new_contract.function_tree_root },
GeneratorIndex::CONTRACT_LEAF);

Expand Down
142 changes: 141 additions & 1 deletion yarn-project/aztec.js/src/abis/ecdsa_account_contract.json

Large diffs are not rendered by default.

142 changes: 141 additions & 1 deletion yarn-project/aztec.js/src/abis/schnorr_account_contract.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions yarn-project/end-to-end/src/e2e_deploy_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('e2e_deploy_contract', () => {
const contractAddress = receiptAfterMined.contractAddress!;
expect(await isContractDeployed(pxe, contractAddress)).toBe(true);
expect(await isContractDeployed(pxe, AztecAddress.random())).toBe(false);
}, 30_000);
}, 60_000);

/**
* Verify that we can produce multiple rollups.
Expand All @@ -68,7 +68,7 @@ describe('e2e_deploy_contract', () => {
const receipt = await tx.getReceipt();
expect(receipt.status).toBe(TxStatus.MINED);
}
}, 30_000);
}, 60_000);

/**
* Verify that we can deploy multiple contracts and interact with all of them.
Expand All @@ -84,7 +84,7 @@ describe('e2e_deploy_contract', () => {
logger(`Sending TX to contract ${index + 1}...`);
await contract.methods.getPublicKey(accounts[0].address).send().wait();
}
}, 30_000);
}, 60_000);

/**
* Milestone 1.2.
Expand All @@ -110,7 +110,7 @@ describe('e2e_deploy_contract', () => {
/A settled tx with equal hash/,
);
}
}, 30_000);
}, 60_000);

it('should deploy a contract connected to a portal contract', async () => {
const deployer = new ContractDeployer(TestContractAbi, wallet);
Expand All @@ -127,5 +127,5 @@ describe('e2e_deploy_contract', () => {
expect((await pxe.getExtendedContractData(contractAddress))?.contractData.portalContractAddress.toString()).toEqual(
portalContract.toString(),
);
});
}, 60_000);
});
2 changes: 1 addition & 1 deletion yarn-project/noir-contracts/src/scripts/copy_output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function writeToProject(abi: any) {
...abi,
functions: abi.functions.map((f: any) => omit(f, projectContract.exclude)),
// If we maintain debug symbols they will get commited to git.
debug: undefined,
// debug: undefined,
};
const targetFilename = pathJoin(projectContract.target, `${snakeCase(abi.name)}_contract.json`);
writeFileSync(targetFilename, JSON.stringify(toWrite, null, 2) + '\n');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use dep::private_kernel_lib::utils::arrays;
use dep::std::unsafe;

fn main(input: PrivateKernelInputsInit) -> distinct pub KernelCircuitPublicInputs {
let empty_public_inputs : KernelCircuitPublicInputs = unsafe::zeroed();
empty_public_inputs
input.native_private_kernel_circuit_initial()
}

// Initialization struct for private inputs to the private kernel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ struct CallContext{

impl CallContext {
fn hash(self) -> Field {
let _hash_index = constants_gen::GENERATOR_INDEX__CALL_CONTEXT;
0
dep::std::hash::pedersen_with_separator([
self.msg_sender.to_field(),
self.storage_contract_address.to_field(),
self.portal_contract_address.to_field(),
self.function_selector.to_field(),
self.is_delegate_call as Field,
self.is_static_call as Field,
self.is_contract_deployment as Field,
], constants_gen::GENERATOR_INDEX__CALL_CONTEXT)[0]
}

fn assert_is_zero(self) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::abis::function_data::FunctionData;
use dep::aztec::constants_gen::{MAX_READ_REQUESTS_PER_CALL};
use crate::abis::private_circuit_public_inputs::PrivateCircuitPublicInputs;
use crate::abis::public_circuit_public_inputs::PublicCircuitPublicInputs;
use dep::aztec::constants_gen;

// TODO(Noir-bug): These should be type aliases

Expand Down Expand Up @@ -46,7 +47,11 @@ struct CallStackItem<AppCircuitPublicInputs> {
// public_inputs and contract_address
impl PublicCallStackItem {
fn hash(self) -> Field {
0
dep::std::hash::pedersen_with_separator([
self.inner.contract_address.to_field(),
self.inner.function_data.hash(),
self.inner.public_inputs.hash(),
], constants_gen::GENERATOR_INDEX__CALL_STACK_ITEM)[0]
}

fn public_inputs(self) -> PublicCircuitPublicInputs {
Expand All @@ -63,7 +68,11 @@ impl PublicCallStackItem {
}
impl PrivateCallStackItem {
fn hash(self) -> Field {
0
dep::std::hash::pedersen_with_separator([
self.inner.contract_address.to_field(),
self.inner.function_data.hash(),
self.inner.public_inputs.hash(),
], constants_gen::GENERATOR_INDEX__CALL_STACK_ITEM)[0]
}

fn public_inputs(self) -> PrivateCircuitPublicInputs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl CompleteAddress{
assert(self.partial_address == 0);
}

fn compute(point : Point, contract_address_salt : Field, function_tree_root : Field, constructor_hash : Field) -> CompleteAddress {
pub fn compute(point : Point, contract_address_salt : Field, function_tree_root : Field, constructor_hash : Field) -> CompleteAddress {
let partial_address = compute_partial_address(contract_address_salt, function_tree_root, constructor_hash);

CompleteAddress{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::abis::function_selector::FunctionSelector;
use dep::aztec::constants_gen;

struct FunctionData {
// First four bytes of the abi encoding
Expand All @@ -15,6 +16,11 @@ impl FunctionData {
// TODO(hash): Since, function data can fit into a Field,
// This method will simply return a bit packed Field instead of hashing
fn hash(self) -> Field {
0
dep::std::hash::pedersen_with_separator([
self.selector.to_field(),
self.is_internal as Field,
self.is_private as Field,
self.is_constructor as Field,
], constants_gen::GENERATOR_INDEX__FUNCTION_DATA)[0]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ struct FunctionLeafPreimage {

impl FunctionLeafPreimage {
fn hash(self) -> Field {
let _hash_index = constants_gen::GENERATOR_INDEX__FUNCTION_LEAF;
0
dep::std::hash::pedersen_with_separator([
self.selector.to_field(),
self.is_internal as Field,
self.is_private as Field,
self.vk_hash,
self.acir_hash
], constants_gen::GENERATOR_INDEX__FUNCTION_LEAF)[0]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ impl NewContractData {
if self.is_empty() {
0 // We want to return 0 here since the contract_address is zero
} else {
let _hash_index = constants_gen::GENERATOR_INDEX__CONTRACT_LEAF;

// hash all contents and put in compress method
0
dep::std::hash::pedersen_with_separator([
self.contract_address.to_field(),
self.portal_contract_address.to_field(),
self.function_tree_root,
], constants_gen::GENERATOR_INDEX__CONTRACT_LEAF)[0]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use dep::aztec::{
use crate::hash::NUM_FIELDS_PER_SHA256;
use crate::abis::historical_block_data::HistoricalBlockData;
use crate::contrakt::deployment_data::ContractDeploymentData;
use crate::utils::bounded_vec::BoundedVec;

struct PrivateCircuitPublicInputs{
call_context : CallContext,
Expand Down Expand Up @@ -48,8 +49,28 @@ struct PrivateCircuitPublicInputs{
}

impl PrivateCircuitPublicInputs{
fn hash(self) -> Field {
let _hash_index = constants_gen::GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS;
0
// TODO : Maybe reuse aztec-nr?
fn hash(self) -> Field {
let mut fields: BoundedVec<Field, constants_gen::PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH> = BoundedVec::new(0);
fields.push(self.call_context.hash());
fields.push(self.args_hash);
fields.push_array(self.return_values);
fields.push_array(self.read_requests);
fields.push_array(self.new_commitments);
fields.push_array(self.new_nullifiers);
fields.push_array(self.nullified_commitments);
fields.push_array(self.private_call_stack);
fields.push_array(self.public_call_stack);
fields.push_array(self.new_l2_to_l1_msgs);
fields.push_array(self.encrypted_logs_hash);
fields.push_array(self.unencrypted_logs_hash);
fields.push(self.encrypted_log_preimages_length);
fields.push(self.unencrypted_log_preimages_length);
fields.push_array(self.historical_block_data.to_array());
fields.push(self.contract_deployment_data.hash());
fields.push(self.chain_id);
fields.push(self.version);

dep::std::hash::pedersen_with_separator(fields.storage, constants_gen::GENERATOR_INDEX__PRIVATE_CIRCUIT_PUBLIC_INPUTS)[0]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use dep::aztec::constants_gen::{
RETURN_VALUES_LENGTH,
};
use crate::abis::call_context::CallContext;
use crate::utils::bounded_vec::BoundedVec;

struct PublicCircuitPublicInputs{
call_context : CallContext,
Expand Down Expand Up @@ -42,8 +43,28 @@ struct PublicCircuitPublicInputs{


impl PublicCircuitPublicInputs{
fn hash(self) -> Field {
let _hash_index = constants_gen::GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS;
0
// TODO : Maybe reuse aztec-nr?
// TODO : Implement this with BoundedVec
fn hash(self) -> Field {
let mut inputs: BoundedVec<Field, constants_gen::PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH> = BoundedVec::new(0);
inputs.push(self.call_context.hash());
inputs.push(self.args_hash);
inputs.push_array(self.return_values);
for i in 0..MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL {
inputs.push(self.contract_storage_update_requests[i].hash());
}
for i in 0..MAX_PUBLIC_DATA_READS_PER_CALL {
inputs.push(self.contract_storage_reads[i].hash());
}
inputs.push_array(self.public_call_stack);
inputs.push_array(self.new_commitments);
inputs.push_array(self.new_nullifiers);
inputs.push_array(self.new_l2_to_l1_msgs);
inputs.push_array(self.unencrypted_logs_hash);
inputs.push(self.unencrypted_log_preimages_length);
inputs.push_array(self.historical_block_data.to_array());
inputs.push(self.prover_address.to_field());

dep::std::hash::pedersen_with_separator(inputs.storage, constants_gen::GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS)[0]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ struct PublicDataRead {

impl PublicDataRead {
fn hash(self) -> Field {
let _hash_index = constants_gen::GENERATOR_INDEX__PUBLIC_DATA_READ;
0
dep::std::hash::pedersen_with_separator([
self.leaf_index,
self.value,
], constants_gen::GENERATOR_INDEX__PUBLIC_DATA_READ)[0]
}

// TODO(David): explain again why we don't check other fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ struct PublicDataUpdateRequest {

impl PublicDataUpdateRequest {
fn hash(self) -> Field {
let _hash_index = constants_gen::GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST;
0
dep::std::hash::pedersen_with_separator([
self.leaf_index,
self.old_value,
self.new_value
], constants_gen::GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST)[0]
}

// TODO(David): explain why we don't check other fields
Expand Down
Loading

0 comments on commit 9cb2063

Please sign in to comment.