Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 21, 2024
1 parent e29d598 commit 5702055
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 48 deletions.
10 changes: 3 additions & 7 deletions yarn-project/accounts/src/defaults/account_entrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AuthWitnessProvider, EntrypointInterface, FeeOptions } from '@aztec/aztec.js/account';
import { FunctionCall, PackedArguments, TxExecutionRequest } from '@aztec/circuit-types';
import { AztecAddress, Fr, FunctionData, GeneratorIndex, TxContext } from '@aztec/circuits.js';
import { AztecAddress, FunctionData, GeneratorIndex, TxContext } from '@aztec/circuits.js';
import { FunctionAbi, encodeArguments } from '@aztec/foundation/abi';

import { DEFAULT_CHAIN_ID, DEFAULT_VERSION } from './constants.js';
Expand All @@ -25,12 +25,8 @@ export class DefaultAccountEntrypoint implements EntrypointInterface {
const abi = this.getEntrypointAbi();
const entrypointPackedArgs = PackedArguments.fromArgs(encodeArguments(abi, [appPayload, feePayload]));

const appAuthWitness = await this.auth.createAuthWitness(
Fr.fromBuffer(hashPayload(appPayload, GeneratorIndex.SIGNATURE_PAYLOAD)),
);
const feeAuthWitness = await this.auth.createAuthWitness(
Fr.fromBuffer(hashPayload(feePayload, GeneratorIndex.FEE_PAYLOAD)),
);
const appAuthWitness = await this.auth.createAuthWitness(hashPayload(appPayload, GeneratorIndex.SIGNATURE_PAYLOAD));
const feeAuthWitness = await this.auth.createAuthWitness(hashPayload(feePayload, GeneratorIndex.FEE_PAYLOAD));

const txRequest = TxExecutionRequest.from({
argsHash: entrypointPackedArgs.hash,
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec.js/src/utils/cheat_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export class AztecCheatCodes {
public computeSlotInMap(baseSlot: Fr | bigint, key: Fr | bigint | AztecAddress): Fr {
// Based on `at` function in
// aztec3-packages/aztec-nr/aztec/src/state_vars/map.nr
return Fr.fromBuffer(pedersenHash([new Fr(baseSlot), new Fr(key)].map(f => f.toBuffer())));
return pedersenHash([new Fr(baseSlot), new Fr(key)].map(f => f.toBuffer()));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/end-to-end/src/e2e_blacklist_token_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ describe('e2e_blacklist_token_contract', () => {
);

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${messageHash.toString('hex')}`,
`Unknown auth witness for message hash 0x${messageHash.toString()}`,
);
});

Expand All @@ -840,7 +840,7 @@ describe('e2e_blacklist_token_contract', () => {
);

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${expectedMessageHash.toString('hex')}`,
`Unknown auth witness for message hash 0x${expectedMessageHash.toString()}`,
);
expect(await asset.methods.balance_of_private(accounts[0].address).view()).toEqual(balance0);
});
Expand Down Expand Up @@ -1147,7 +1147,7 @@ describe('e2e_blacklist_token_contract', () => {
);

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${expectedMessageHash.toString('hex')}`,
`Unknown auth witness for message hash 0x${expectedMessageHash.toString()}`,
);
});

Expand Down Expand Up @@ -1386,7 +1386,7 @@ describe('e2e_blacklist_token_contract', () => {
const messageHash = computeAuthWitMessageHash(accounts[1].address, action.request());

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${messageHash.toString('hex')}`,
`Unknown auth witness for message hash 0x${messageHash.toString()}`,
);
});

Expand All @@ -1408,7 +1408,7 @@ describe('e2e_blacklist_token_contract', () => {
await wallets[2].addAuthWitness(witness);

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${expectedMessageHash.toString('hex')}`,
`Unknown auth witness for message hash 0x${expectedMessageHash.toString()}`,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('e2e_cross_chain_messaging', () => {
.withWallet(user1Wallet)
.methods.exit_to_l1_private(l2Token.address, ethAccount, withdrawAmount, EthAddress.ZERO, nonce)
.simulate(),
).rejects.toThrowError(`Unknown auth witness for message hash 0x${expectedBurnMessageHash.toString('hex')}`);
).rejects.toThrowError(`Unknown auth witness for message hash 0x${expectedBurnMessageHash.toString()}`);
}, 120_000);

it("Can't claim funds publicly if they were deposited privately", async () => {
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/end-to-end/src/e2e_lending_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe('e2e_lending_contract', () => {
.request(),
);

await wallet.createAuthWitness(Fr.fromBuffer(messageHash));
await wallet.createAuthWitness(messageHash);
await lendingSim.progressTime(TIME_JUMP);
lendingSim.depositPrivate(lendingAccount.address, lendingAccount.key(), depositAmount);

Expand Down Expand Up @@ -205,7 +205,7 @@ describe('e2e_lending_contract', () => {
.unshield(lendingAccount.address, lendingContract.address, depositAmount, nonce)
.request(),
);
await wallet.createAuthWitness(Fr.fromBuffer(messageHash));
await wallet.createAuthWitness(messageHash);

await lendingSim.progressTime(TIME_JUMP);
lendingSim.depositPrivate(lendingAccount.address, lendingAccount.address.toField(), depositAmount);
Expand Down Expand Up @@ -303,7 +303,7 @@ describe('e2e_lending_contract', () => {
lendingContract.address,
stableCoin.methods.burn(lendingAccount.address, repayAmount, nonce).request(),
);
await wallet.createAuthWitness(Fr.fromBuffer(messageHash));
await wallet.createAuthWitness(messageHash);

await lendingSim.progressTime(TIME_JUMP);
lendingSim.repayPrivate(lendingAccount.address, lendingAccount.key(), repayAmount);
Expand All @@ -329,7 +329,7 @@ describe('e2e_lending_contract', () => {
lendingContract.address,
stableCoin.methods.burn(lendingAccount.address, repayAmount, nonce).request(),
);
await wallet.createAuthWitness(Fr.fromBuffer(messageHash));
await wallet.createAuthWitness(messageHash);

await lendingSim.progressTime(TIME_JUMP);
lendingSim.repayPrivate(lendingAccount.address, lendingAccount.address.toField(), repayAmount);
Expand Down
10 changes: 5 additions & 5 deletions yarn-project/end-to-end/src/e2e_token_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ describe('e2e_token_contract', () => {
const messageHash = computeAuthWitMessageHash(accounts[1].address, action.request());

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${messageHash.toString('hex')}`,
`Unknown auth witness for message hash ${messageHash.toString()}`,
);
});

Expand All @@ -635,7 +635,7 @@ describe('e2e_token_contract', () => {
await wallets[2].addAuthWitness(witness);

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${expectedMessageHash.toString('hex')}`,
`Unknown auth witness for message hash ${expectedMessageHash.toString()}`,
);
expect(await asset.methods.balance_of_private(accounts[0].address).view()).toEqual(balance0);
});
Expand Down Expand Up @@ -874,7 +874,7 @@ describe('e2e_token_contract', () => {
await wallets[2].addAuthWitness(witness);

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${expectedMessageHash.toString('hex')}`,
`Unknown auth witness for message hash ${expectedMessageHash.toString()}`,
);
});
});
Expand Down Expand Up @@ -1062,7 +1062,7 @@ describe('e2e_token_contract', () => {
const messageHash = computeAuthWitMessageHash(accounts[1].address, action.request());

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${messageHash.toString('hex')}`,
`Unknown auth witness for message hash ${messageHash.toString()}`,
);
});

Expand All @@ -1081,7 +1081,7 @@ describe('e2e_token_contract', () => {
await wallets[2].addAuthWitness(witness);

await expect(action.simulate()).rejects.toThrowError(
`Unknown auth witness for message hash 0x${expectedMessageHash.toString('hex')}`,
`Unknown auth witness for message hash ${expectedMessageHash.toString()}`,
);
});
});
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/end-to-end/src/shared/uniswap_l1_l2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export const uniswapL1L2TestSuite = (
.unshield(ownerAddress, uniswapL2Contract.address, wethAmountToBridge, nonceForWETHUnshieldApproval)
.request(),
);
await ownerWallet.createAuthWitness(Fr.fromBuffer(unshieldToUniswapMessageHash));
await ownerWallet.createAuthWitness(unshieldToUniswapMessageHash);

// 4. Swap on L1 - sends L2 to L1 message to withdraw WETH to L1 and another message to swap assets.
logger('Withdrawing weth to L1 and sending message to swap to dai');
Expand Down Expand Up @@ -475,7 +475,7 @@ export const uniswapL1L2TestSuite = (
ownerEthAddress,
)
.simulate(),
).rejects.toThrowError(`Unknown auth witness for message hash 0x${expectedMessageHash.toString('hex')}`);
).rejects.toThrowError(`Unknown auth witness for message hash ${expectedMessageHash.toString()}`);
});

it("can't swap if user passes a token different to what the bridge tracks", async () => {
Expand All @@ -494,7 +494,7 @@ export const uniswapL1L2TestSuite = (
.unshield(ownerAddress, uniswapL2Contract.address, wethAmountToBridge, nonceForWETHUnshieldApproval)
.request(),
);
await ownerWallet.createAuthWitness(Fr.fromBuffer(unshieldToUniswapMessageHash));
await ownerWallet.createAuthWitness(unshieldToUniswapMessageHash);

// 3. Swap but send the wrong token address
logger('Swap but send the wrong token address');
Expand Down Expand Up @@ -644,7 +644,7 @@ export const uniswapL1L2TestSuite = (
.unshield(ownerAddress, uniswapL2Contract.address, wethAmountToBridge, nonceForWETHUnshieldApproval)
.request(),
);
await ownerWallet.createAuthWitness(Fr.fromBuffer(unshieldToUniswapMessageHash));
await ownerWallet.createAuthWitness(unshieldToUniswapMessageHash);
const wethL2BalanceBeforeSwap = await wethCrossChainHarness.getL2PrivateBalanceOf(ownerAddress);

// Swap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class LendingAccount {
* @returns Key in public space
*/
public key() {
return Fr.fromBuffer(pedersenHash([this.address, this.secret].map(f => f.toBuffer())));
return pedersenHash([this.address, this.secret].map(f => f.toBuffer()));
}
}

Expand Down
16 changes: 10 additions & 6 deletions yarn-project/foundation/src/crypto/poseidon/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { BarretenbergSync, Fr } from '@aztec/bb.js';
import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';

import { Fr } from '../../fields/fields.js';

/**
* Create a poseidon hash (field) from an array of input fields.
* Left pads any inputs less than 32 bytes.
*/
export function poseidonHash(input: Buffer[]): Buffer {
return Buffer.from(
BarretenbergSync.getSingleton()
.poseidonHash(input.map(i => new Fr(i)))
.toBuffer(),
export function poseidonHash(input: Buffer[]): Fr {
return Fr.fromBuffer(
Buffer.from(
BarretenbergSync.getSingleton()
.poseidonHash(input.map(i => new FrBarretenberg(i)))
.toBuffer(),
),
);
}
4 changes: 2 additions & 2 deletions yarn-project/merkle-tree/src/pedersen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export class Pedersen implements Hasher {
* purposes.
*/
public hash(lhs: Uint8Array, rhs: Uint8Array): Buffer {
return pedersenHash([Buffer.from(lhs), Buffer.from(rhs)]);
return pedersenHash([Buffer.from(lhs), Buffer.from(rhs)]).toBuffer();
}

/*
* @deprecated Don't call pedersen directly in production code. Instead, create suitably-named functions for specific
* purposes.
*/
public hashInputs(inputs: Buffer[]): Buffer {
return pedersenHash(inputs);
return pedersenHash(inputs).toBuffer();
}
}
2 changes: 1 addition & 1 deletion yarn-project/pxe/src/note_processor/note_processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Note Processor', () => {
const firstBlockDataStartIndex = (firstBlockNum - 1) * numCommitmentsPerBlock;
const firstBlockDataEndIndex = firstBlockNum * numCommitmentsPerBlock;

const computeMockNoteHash = (note: Note) => Fr.fromBuffer(pedersenHash(note.items.map(i => i.toBuffer())));
const computeMockNoteHash = (note: Note) => pedersenHash(note.items.map(i => i.toBuffer()));

// ownedData: [tx1, tx2, ...], the numbers in each tx represents the indices of the note hashes the account owns.
const createEncryptedLogsAndOwnedL1NotePayloads = (ownedData: number[][], ownedNotes: L1NotePayload[]) => {
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/avm/avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('AVM simulator', () => {
describe.each([
['avm_poseidon_hash', poseidonHash],
['avm_pedersen_hash', pedersenHash],
])('Hashes with field returned in noir contracts', (name: string, hashFunction: (data: Buffer[]) => Buffer) => {
])('Hashes with field returned in noir contracts', (name: string, hashFunction: (data: Buffer[]) => Fr) => {
it(`Should execute contract function that performs ${name} hash`, async () => {
const calldata = [new Fr(1), new Fr(2), new Fr(3)];
const hash = hashFunction(calldata.map(f => f.toBuffer()));
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/simulator/src/avm/opcodes/hashing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('Hashing Opcodes', () => {
await new Poseidon2(indirect, dstOffset, hashOffset, args.length).execute(context);

const result = context.machineState.memory.get(dstOffset);
expect(result).toEqual(new Field(toBigIntBE(expectedHash)));
expect(result).toEqual(new Field(expectedHash));
});

it('Should hash correctly - indirect', async () => {
Expand All @@ -63,7 +63,7 @@ describe('Hashing Opcodes', () => {
await new Poseidon2(indirect, dstOffset, hashOffset, args.length).execute(context);

const result = context.machineState.memory.get(dstOffset);
expect(result).toEqual(new Field(toBigIntBE(expectedHash)));
expect(result).toEqual(new Field(expectedHash));
});
});

Expand Down Expand Up @@ -225,7 +225,7 @@ describe('Hashing Opcodes', () => {
await new Pedersen(indirect, dstOffset, hashOffset, args.length).execute(context);

const result = context.machineState.memory.get(dstOffset);
expect(result).toEqual(new Field(toBigIntBE(expectedHash)));
expect(result).toEqual(new Field(expectedHash));
});

it('Should hash correctly - indirect', async () => {
Expand All @@ -244,7 +244,7 @@ describe('Hashing Opcodes', () => {
await new Pedersen(indirect, dstOffset, hashOffset, args.length).execute(context);

const result = context.machineState.memory.get(dstOffset);
expect(result).toEqual(new Field(toBigIntBE(expectedHash)));
expect(result).toEqual(new Field(expectedHash));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('Private Execution test suite', () => {
return trees[name];
};

const hashFields = (data: Fr[]) => Fr.fromBuffer(pedersenHash(data.map(f => f.toBuffer())));
const hashFields = (data: Fr[]) => pedersenHash(data.map(f => f.toBuffer()));

beforeAll(() => {
logger = createDebugLogger('aztec:test:private_execution');
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/client/simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Simulator', () => {
const ownerNullifierSecretKey = GrumpkinScalar.random();
const ownerNullifierPublicKey = Point.random();

const hashFields = (data: Fr[]) => Fr.fromBuffer(pedersenHash(data.map(f => f.toBuffer())));
const hashFields = (data: Fr[]) => pedersenHash(data.map(f => f.toBuffer()));

beforeEach(() => {
oracle = mock<DBOracle>();
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/simulator/src/public/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ describe('ACIR public execution simulator', () => {

const expectedNoteHash = pedersenHash([amount.toBuffer(), secretHash.toBuffer()]);
const storageSlot = new Fr(5); // for pending_shields
const expectedInnerNoteHash = pedersenHash([storageSlot.toBuffer(), expectedNoteHash]);
const expectedInnerNoteHash = pedersenHash([storageSlot, expectedNoteHash].map(f => f.toBuffer()));
expect(result.newCommitments[0].value.toBuffer()).toEqual(expectedInnerNoteHash);
});

Expand Down Expand Up @@ -365,7 +365,7 @@ describe('ACIR public execution simulator', () => {

const expectedNewMessage = new L2ToL1Message(
portalContractAddress,
Fr.fromBuffer(pedersenHash(params.map(a => a.toBuffer()))),
pedersenHash(params.map(a => a.toBuffer())),
);

expect(result.newL2ToL1Messages[0]).toEqual(expectedNewMessage);
Expand Down Expand Up @@ -478,7 +478,7 @@ describe('ACIR public execution simulator', () => {

let root = messageKey ?? preimage.hash();
for (const sibling of siblingPathBuffers) {
root = Fr.fromBuffer(pedersenHash([root.toBuffer(), sibling]));
root = pedersenHash([root.toBuffer(), sibling]);
}
commitmentsDb.getL1ToL2Message.mockImplementation(() => {
return Promise.resolve(new MessageLoadOracleInputs(preimage, 0n, siblingPath));
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export function computeSlotForMapping(
toField: () => Fr;
},
) {
return Fr.fromBuffer(pedersenHash([mappingSlot, key.toField()].map(field => field.toBuffer())));
return pedersenHash([mappingSlot, key.toField()].map(field => field.toBuffer()));
}

0 comments on commit 5702055

Please sign in to comment.