Skip to content

Commit

Permalink
renaming compute_message_secret_hash as compute_message_secret
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Apr 23, 2024
1 parent 47da9aa commit eab7cd8
Show file tree
Hide file tree
Showing 41 changed files with 99 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ A message that is sent from L1 to L2.
| `sender` | `L1Actor` | The actor on L1 that is sending the message. |
| `recipient` | `L2Actor` | The actor on L2 that is to receive the message. |
| `content` | `field (~254 bits)` | The field element containing the content to be sent to L2. |
| `secretHash` | `field (~254 bits)` | The hash of a secret pre-image that must be known to consume the message on L2. Use the [`computeMessageSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. |
| `secretHash` | `field (~254 bits)` | The hash of a secret pre-image that must be known to consume the message on L2. Use the [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. |

## `L2ToL1Message`

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/developers/contracts/references/portals/inbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Sends a message from L1 to L2.
| -------------- | ------- | ----------- |
| Recipient | `L2Actor` | The recipient of the message. This **MUST** match the rollup version and an Aztec contract that is **attached** to the contract making this call. If the recipient is not attached to the caller, the message cannot be consumed by it. |
| Content | `field` (~254 bits) | The content of the message. This is the data that will be passed to the recipient. The content is limited to be a single field for rollup purposes. If the content is small enough it can just be passed along, otherwise it should be hashed and the hash passed along (you can use our [`Hash`](https://github.com/AztecProtocol/aztec-packages/blob/master/l1-contracts/src/core/libraries/Hash.sol) utilities with `sha256ToField` functions) |
| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeMessageSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. |
| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. |
| ReturnValue | `bytes32` | The message hash, used as an identifier |

#### Edge cases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ When sending messages, we need to specify quite a bit of information beyond just
| Name | Type | Description |
| ----------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Recipient | `L2Actor` | The message recipient. This **MUST** match the rollup version and an Aztec contract that is **attached** to the contract making this call. If the recipient is not attached to the caller, the message cannot be consumed by it. |
| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeMessageSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. |
| Secret Hash | `field` (~254 bits) | A hash of a secret that is used when consuming the message on L2. Keep this preimage a secret to make the consumption private. To consume the message the caller must know the pre-image (the value that was hashed) - so make sure your app keeps track of the pre-images! Use the [`computeSecretHash`](https://github.com/AztecProtocol/aztec-packages/blob/master/yarn-project/aztec.js/src/utils/secrets.ts) to compute it from a secret. |
| Content | `field` (~254 bits) | The content of the message. This is the data that will be passed to the recipient. The content is limited to be a single field. If the content is small enough it can just be passed along, otherwise it should be hashed and the hash passed along (you can use our [`Hash`](https://github.com/AztecProtocol/aztec-packages/blob/master/l1-contracts/src/core/libraries/Hash.sol) utilities with `sha256ToField` functions)

With all that information at hand, we can call the `sendL2Message` function on the Inbox. The function will return a `field` (inside `bytes32`) that is the hash of the message. This hash can be used as an identifier to spot when your message has been included in a rollup block.
Expand Down Expand Up @@ -56,7 +56,7 @@ In Solidity, you can use our `Hash.sha256ToField()` method:

#include_code deposit_public l1-contracts/test/portals/TokenPortal.sol solidity

The `secret_hash` uses the pederson hash which fits in a field element. You can use the utility method `computeMessageSecretHash()`in `@aztec/aztec.js` npm package to generate a secret and its corresponding hash.
The `secret_hash` uses the pederson hash which fits in a field element. You can use the utility method `computeSecretHash()`in `@aztec/aztec.js` npm package to generate a secret and its corresponding hash.

After the transaction has been mined, the message is consumed, a nullifier is emitted and the tokens have been minted on Aztec and are ready for claiming.

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/developers/tutorials/writing_dapp/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
ExtendedNote,
Fr,
Note,
computeMessageSecretHash,
computeSecretHash,
createPXEClient,
waitForPXE,
} from "@aztec/aztec.js";
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/context/avm_context.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::hash::{compute_message_secret_hash, compute_message_hash, compute_message_nullifier};
use crate::hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier};
use dep::protocol_types::{
address::{AztecAddress, EthAddress},
constants::{L1_TO_L2_MESSAGE_LENGTH, NESTED_CALL_L2_GAS_BUFFER}, header::Header
Expand Down Expand Up @@ -96,7 +96,7 @@ impl PublicContextInterface for AvmContext {
}

fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress, leaf_index: Field) {
let secret_hash = compute_message_secret_hash(secret);
let secret_hash = compute_secret_hash(secret);
let message_hash = compute_message_hash(
sender,
self.chain_id(),
Expand Down
6 changes: 3 additions & 3 deletions noir-projects/aztec-nr/aztec/src/hash.nr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use dep::protocol_types::{
address::{AztecAddress, EthAddress},
constants::{
GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT,
GENERATOR_INDEX__SECRET_HASH, GENERATOR_INDEX__MESSAGE_NULLIFIER, ARGS_HASH_CHUNK_COUNT,
GENERATOR_INDEX__FUNCTION_ARGS, ARGS_HASH_CHUNK_LENGTH
},
traits::Hash, hash::{pedersen_hash, poseidon2_hash, silo_nullifier, sha256_to_field}
};

pub fn compute_message_secret_hash(secret: Field) -> Field {
pedersen_hash([secret], GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET)
pub fn compute_secret_hash(secret: Field) -> Field {
pedersen_hash([secret], GENERATOR_INDEX__SECRET_HASH)
}

pub fn compute_message_hash(
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/messaging.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
hash::{compute_message_secret_hash, compute_message_hash, compute_message_nullifier},
hash::{compute_secret_hash, compute_message_hash, compute_message_nullifier},
oracle::get_l1_to_l2_membership_witness::get_l1_to_l2_membership_witness
};

Expand All @@ -15,7 +15,7 @@ pub fn process_l1_to_l2_message(
content: Field,
secret: Field
) -> Field {
let secret_hash = compute_message_secret_hash(secret);
let secret_hash = compute_secret_hash(secret);
let message_hash = compute_message_hash(
portal_contract_address,
chain_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ mod types;
contract TokenBlacklist {
// Libs
use dep::aztec::{
hash::poseidon2_hash,
hash::compute_secret_hash,
prelude::{AztecAddress, FunctionSelector, Map, NoteGetterOptions, PrivateSet, PublicMutable, SharedMutable}
};

use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}};

use crate::types::{
transparent_note::{TransparentNote, GENERATOR_INDEX__TRANSPARENT_NOTE}, token_note::TokenNote,
balances_map::BalancesMap, roles::UserFlags
};
use crate::types::{transparent_note::TransparentNote, token_note::TokenNote, balances_map::BalancesMap, roles::UserFlags};

// Changing an address' roles has a certain block delay before it goes into effect.
global CHANGE_ROLES_DELAY_BLOCKS = 5;
Expand Down Expand Up @@ -152,7 +149,7 @@ contract TokenBlacklist {
assert(!to_roles.is_blacklisted, "Blacklisted: Recipient");

let pending_shields = storage.pending_shields;
let secret_hash = poseidon2_hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]);
let secret_hash = compute_secret_hash(secret);
// Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash
// stored in field with index 1 (select(1, secret_hash)).
let mut options = NoteGetterOptions::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use dep::aztec::{
};

global TRANSPARENT_NOTE_LEN: Field = 2;
// Defined here as it's not a protocol constant. Copied over to private execution test.
global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543;

// Transparent note represents a note that is created in the clear (public execution), but can only be spent by those
// that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@ contract Token {
use dep::compressed_string::FieldCompressedString;

use dep::aztec::{
hash::poseidon2_hash,
hash::compute_secret_hash,
prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}
};

// docs:start:import_authwit
use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}};
// docs:end:import_authwit

use crate::types::{
transparent_note::{TransparentNote, GENERATOR_INDEX__TRANSPARENT_NOTE},
token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap
};
use crate::types::{transparent_note::TransparentNote, token_note::{TokenNote, TOKEN_NOTE_LEN}, balances_map::BalancesMap};
// docs:end::imports

// docs:start:storage_struct
Expand Down Expand Up @@ -244,7 +241,7 @@ contract Token {
#[aztec(private)]
fn redeem_shield(to: AztecAddress, amount: Field, secret: Field) {
let pending_shields = storage.pending_shields;
let secret_hash = poseidon2_hash([secret, GENERATOR_INDEX__TRANSPARENT_NOTE]);
let secret_hash = compute_secret_hash(secret);
// Get 1 note (set_limit(1)) which has amount stored in field with index 0 (select(0, amount)) and secret_hash
// stored in field with index 1 (select(1, secret_hash)).
let mut options = NoteGetterOptions::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use dep::aztec::{
};

global TRANSPARENT_NOTE_LEN: Field = 2;
// Defined here as it's not a protocol constant. Copied over to private execution test and test contract.
global GENERATOR_INDEX__TRANSPARENT_NOTE = 92543;

// Transparent note represents a note that is created in the clear (public execution), but can only be spent by those
// that know the preimage of the "secret_hash" (the secret). This is typically used when shielding a token balance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ global GENERATOR_INDEX__CONTRACT_LEAF = 16;
global GENERATOR_INDEX__CALL_CONTEXT = 17;
global GENERATOR_INDEX__CALL_STACK_ITEM = 18;
global GENERATOR_INDEX__CALL_STACK_ITEM_2 = 19;
global GENERATOR_INDEX__L1_TO_L2_MESSAGE_SECRET = 20;
global GENERATOR_INDEX__SECRET_HASH = 20;
global GENERATOR_INDEX__L2_TO_L1_MSG = 21;
global GENERATOR_INDEX__TX_CONTEXT = 22;
global GENERATOR_INDEX__PUBLIC_LEAF_INDEX = 23;
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec.js/src/fee/private_fee_payment_method.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type FunctionCall } from '@aztec/circuit-types';
import { FunctionData, type GasSettings } from '@aztec/circuits.js';
import { computeMessageSecretHash } from '@aztec/circuits.js/hash';
import { computeSecretHash } from '@aztec/circuits.js/hash';
import { FunctionSelector } from '@aztec/foundation/abi';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';
Expand Down Expand Up @@ -71,7 +71,7 @@ export class PrivateFeePaymentMethod implements FeePaymentMethod {
);
await this.wallet.createAuthWit(messageHash);

const secretHashForRebate = computeMessageSecretHash(this.rebateSecret);
const secretHashForRebate = computeSecretHash(this.rebateSecret);

return [
{
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export {
INITIAL_L2_BLOCK_NUM,
} from '@aztec/circuits.js';

export { computeMessageSecretHash } from '@aztec/circuits.js/hash';
export { computeSecretHash } from '@aztec/circuits.js/hash';

export {
computeAppNullifierSecretKey,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec/src/examples/token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getSingleKeyAccount } from '@aztec/accounts/single_key';
import { type AccountWallet, Fr, Note, computeMessageSecretHash, createPXEClient } from '@aztec/aztec.js';
import { type AccountWallet, Fr, Note, computeSecretHash, createPXEClient } from '@aztec/aztec.js';
import { ExtendedNote } from '@aztec/circuit-types';
import { createDebugLogger } from '@aztec/foundation/log';
import { TokenContract } from '@aztec/noir-contracts.js/Token';
Expand Down Expand Up @@ -45,7 +45,7 @@ async function main() {

// Create a secret and a corresponding hash that will be used to mint funds privately
const aliceSecret = Fr.random();
const aliceSecretHash = computeMessageSecretHash(aliceSecret);
const aliceSecretHash = computeSecretHash(aliceSecret);
const receipt = await tokenAlice.methods.mint_private(ALICE_MINT_BALANCE, aliceSecretHash).send().wait();

// Add the newly created "pending shield" note to PXE
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export enum GeneratorIndex {
CALL_CONTEXT = 17,
CALL_STACK_ITEM = 18,
CALL_STACK_ITEM_2 = 19,
L1_TO_L2_MESSAGE_SECRET = 20,
SECRET_HASH = 20,
L2_TO_L1_MSG = 21,
TX_CONTEXT = 22,
PUBLIC_LEAF_INDEX = 23,
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuits.js/src/hash/hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { makeAztecAddress, makeVerificationKey } from '../tests/factories.js';
import {
computeCommitmentNonce,
computeCommitmentsHash,
computeMessageSecretHash,
computeNullifierHash,
computePublicDataTreeLeafSlot,
computePublicDataTreeValue,
computeSecretHash,
computeUniqueCommitment,
computeVarArgsHash,
hashVK,
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('hash', () => {

it('compute secret message hash', () => {
const value = new Fr(8n);
const hash = computeMessageSecretHash(value);
const hash = computeSecretHash(value);
expect(hash).toMatchSnapshot();
});

Expand Down
7 changes: 4 additions & 3 deletions yarn-project/circuits.js/src/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ export function computeNullifierHash(input: SideEffectLinkedToNoteHash) {
}

/**
* Computes a hash of a secret as is done in the L1 to L2 message flow.
* Computes a hash of a secret.
* @dev This function is used to generate secrets for the L1 to L2 message flow and for the TransparentNote.
* @param secret - The secret to hash (could be generated however you want e.g. `Fr.random()`)
* @returns The hash
*/
export function computeMessageSecretHash(secret: Fr) {
return pedersenHash([secret], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET);
export function computeSecretHash(secret: Fr) {
return pedersenHash([secret], GeneratorIndex.SECRET_HASH);
}

export function computeL1ToL2MessageNullifier(
Expand Down
14 changes: 7 additions & 7 deletions yarn-project/end-to-end/src/composed/e2e_persistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
ExtendedNote,
Note,
type TxHash,
computeMessageSecretHash,
computeSecretHash,
waitForAccountSynch,
} from '@aztec/aztec.js';
import { type Salt } from '@aztec/aztec.js/account';
Expand Down Expand Up @@ -72,13 +72,13 @@ describe('Aztec persistence', () => {

const secret = Fr.random();

const mintTxReceipt = await contract.methods.mint_private(1000n, computeMessageSecretHash(secret)).send().wait();
const mintTxReceipt = await contract.methods.mint_private(1000n, computeSecretHash(secret)).send().wait();

await addPendingShieldNoteToPXE(
ownerWallet,
contractAddress,
1000n,
computeMessageSecretHash(secret),
computeSecretHash(secret),
mintTxReceipt.txHash,
);

Expand Down Expand Up @@ -130,12 +130,12 @@ describe('Aztec persistence', () => {
const balance = await contract.methods.balance_of_private(ownerWallet.getAddress()).simulate();

const secret = Fr.random();
const mintTxReceipt = await contract.methods.mint_private(1000n, computeMessageSecretHash(secret)).send().wait();
const mintTxReceipt = await contract.methods.mint_private(1000n, computeSecretHash(secret)).send().wait();
await addPendingShieldNoteToPXE(
ownerWallet,
contractAddress,
1000n,
computeMessageSecretHash(secret),
computeSecretHash(secret),
mintTxReceipt.txHash,
);

Expand Down Expand Up @@ -270,7 +270,7 @@ describe('Aztec persistence', () => {
secret = Fr.random();
mintAmount = 1000n;
const mintTxReceipt = await contract.methods
.mint_private(mintAmount, computeMessageSecretHash(secret))
.mint_private(mintAmount, computeSecretHash(secret))
.send()
.wait();
mintTxHash = mintTxReceipt.txHash;
Expand Down Expand Up @@ -311,7 +311,7 @@ describe('Aztec persistence', () => {
ownerWallet,
contractAddress,
mintAmount,
computeMessageSecretHash(secret),
computeSecretHash(secret),
mintTxHash,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
GrumpkinScalar,
Note,
type PXE,
computeMessageSecretHash,
computeSecretHash,
createDebugLogger,
createPXEClient,
waitForPXE,
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('e2e_sandbox_example', () => {

// Create a secret and a corresponding hash that will be used to mint funds privately
const aliceSecret = Fr.random();
const aliceSecretHash = computeMessageSecretHash(aliceSecret);
const aliceSecretHash = computeSecretHash(aliceSecret);

logger.info(`Minting tokens to Alice...`);
// Mint the initial supply privately "to secret hash"
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('e2e_sandbox_example', () => {
await tokenContractAlice.methods.set_minter(bob, true).send().wait();

const bobSecret = Fr.random();
const bobSecretHash = computeMessageSecretHash(bobSecret);
const bobSecretHash = computeSecretHash(bobSecret);
// Bob now has a secret 🥷

const mintQuantity = 10_000n;
Expand Down
Loading

0 comments on commit eab7cd8

Please sign in to comment.