Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: pedersenHash(...) TS func returns Fr #4704

Merged
merged 5 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 9 additions & 13 deletions yarn-project/circuits.js/src/contract/contract_address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ export function computePartialAddress(
? instance.saltedInitializationHash
: computeSaltedInitializationHash(instance);

return Fr.fromBuffer(
pedersenHash(
[instance.contractClassId, saltedInitializationHash].map(x => x.toBuffer()),
GeneratorIndex.PARTIAL_ADDRESS,
),
return pedersenHash(
[instance.contractClassId, saltedInitializationHash].map(x => x.toBuffer()),
GeneratorIndex.PARTIAL_ADDRESS,
);
}

Expand All @@ -58,11 +56,9 @@ export function computePartialAddress(
export function computeSaltedInitializationHash(
instance: Pick<ContractInstance, 'initializationHash' | 'salt' | 'portalContractAddress'>,
): Fr {
return Fr.fromBuffer(
pedersenHash(
[instance.salt, instance.initializationHash, instance.portalContractAddress].map(x => x.toBuffer()),
GeneratorIndex.PARTIAL_ADDRESS,
),
return pedersenHash(
[instance.salt, instance.initializationHash, instance.portalContractAddress].map(x => x.toBuffer()),
GeneratorIndex.PARTIAL_ADDRESS,
);
}

Expand All @@ -79,7 +75,7 @@ export function computeContractAddressFromPartial(
[publicKeyHash.toBuffer(), args.partialAddress.toBuffer()],
GeneratorIndex.CONTRACT_ADDRESS,
);
return new AztecAddress(result);
return AztecAddress.fromField(result);
}

/**
Expand All @@ -91,7 +87,7 @@ export function computePublicKeysHash(publicKey: PublicKey | undefined): Fr {
if (!publicKey) {
return Fr.ZERO;
}
return Fr.fromBuffer(pedersenHash([publicKey.x.toBuffer(), publicKey.y.toBuffer()], GeneratorIndex.PARTIAL_ADDRESS));
return pedersenHash([publicKey.x.toBuffer(), publicKey.y.toBuffer()], GeneratorIndex.PARTIAL_ADDRESS);
}

/**
Expand All @@ -114,5 +110,5 @@ export function computeInitializationHash(initFn: FunctionAbi, args: any[]): Fr
*/
export function computeInitializationHashFromEncodedArgs(initFn: FunctionSelector, encodedArgs: Fr[]): Fr {
const argsHash = computeVarArgsHash(encodedArgs);
return Fr.fromBuffer(pedersenHash([initFn.toBuffer(), argsHash.toBuffer()], GeneratorIndex.CONSTRUCTOR));
return pedersenHash([initFn.toBuffer(), argsHash.toBuffer()], GeneratorIndex.CONSTRUCTOR);
}
8 changes: 3 additions & 5 deletions yarn-project/circuits.js/src/contract/contract_class_id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ export function computeContractClassIdWithPreimage(
'publicBytecodeCommitment' in contractClass
? contractClass.publicBytecodeCommitment
: computePublicBytecodeCommitment(contractClass.packedBytecode);
const id = Fr.fromBuffer(
pedersenHash(
[artifactHash.toBuffer(), privateFunctionsRoot.toBuffer(), publicBytecodeCommitment.toBuffer()],
GeneratorIndex.CONTRACT_LEAF, // TODO(@spalladino): Review all generator indices in this file
),
const id = pedersenHash(
[artifactHash.toBuffer(), privateFunctionsRoot.toBuffer(), publicBytecodeCommitment.toBuffer()],
GeneratorIndex.CONTRACT_LEAF, // TODO(@spalladino): Review all generator indices in this file
);
return { id, artifactHash, privateFunctionsRoot, publicBytecodeCommitment };
}
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/circuits.js/src/contract/private_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export function computePrivateFunctionLeaf(fn: PrivateFunction): Buffer {
return pedersenHash(
[fn.selector, fn.vkHash].map(x => x.toBuffer()),
GeneratorIndex.FUNCTION_LEAF,
);
).toBuffer();
}

function getPrivateFunctionTreeCalculator(): MerkleTreeCalculator {
if (!privateFunctionTreeCalculator) {
const functionTreeZeroLeaf = pedersenHash(new Array(PRIVATE_FUNCTION_SIZE).fill(Buffer.alloc(32)));
const functionTreeZeroLeaf = pedersenHash(new Array(PRIVATE_FUNCTION_SIZE).fill(Buffer.alloc(32))).toBuffer();
privateFunctionTreeCalculator = new MerkleTreeCalculator(FUNCTION_TREE_HEIGHT, functionTreeZeroLeaf);
}
return privateFunctionTreeCalculator;
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 @@ -105,13 +105,13 @@ describe('hash', () => {
it('Computes an empty nullifier hash ', () => {
const emptyNull = SideEffectLinkedToNoteHash.empty();

const emptyHash = Fr.fromBuffer(computeNullifierHash(emptyNull)).toString();
const emptyHash = computeNullifierHash(emptyNull).toString();
expect(emptyHash).toMatchSnapshot();
});

it('Computes an empty sideeffect hash ', () => {
const emptySideEffect = SideEffect.empty();
const emptyHash = Fr.fromBuffer(computeCommitmentsHash(emptySideEffect)).toString();
const emptyHash = computeCommitmentsHash(emptySideEffect).toString();
expect(emptyHash).toMatchSnapshot();
});

Expand Down
41 changes: 16 additions & 25 deletions yarn-project/circuits.js/src/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ let functionTreeRootCalculator: MerkleTreeCalculator | undefined;
*/
function getFunctionTreeRootCalculator() {
if (!functionTreeRootCalculator) {
const functionTreeZeroLeaf = pedersenHash(new Array(5).fill(Buffer.alloc(32)));
const functionTreeZeroLeaf = pedersenHash(new Array(5).fill(Buffer.alloc(32))).toBuffer();
functionTreeRootCalculator = new MerkleTreeCalculator(FUNCTION_TREE_HEIGHT, functionTreeZeroLeaf);
}
return functionTreeRootCalculator;
Expand Down Expand Up @@ -102,8 +102,9 @@ export function computeFunctionTreeRoot(fnLeaves: Fr[]) {
* @returns The constructor hash.
*/
export function hashConstructor(functionData: FunctionData, argsHash: Fr, constructorVKHash: Buffer): Fr {
return Fr.fromBuffer(
pedersenHash([functionData.hash().toBuffer(), argsHash.toBuffer(), constructorVKHash], GeneratorIndex.CONSTRUCTOR),
return pedersenHash(
[functionData.hash().toBuffer(), argsHash.toBuffer(), constructorVKHash],
GeneratorIndex.CONSTRUCTOR,
);
}

Expand All @@ -114,9 +115,7 @@ export function hashConstructor(functionData: FunctionData, argsHash: Fr, constr
* @returns A commitment nonce.
*/
export function computeCommitmentNonce(nullifierZero: Fr, commitmentIndex: number): Fr {
return Fr.fromBuffer(
pedersenHash([nullifierZero.toBuffer(), numToUInt32BE(commitmentIndex, 32)], GeneratorIndex.COMMITMENT_NONCE),
);
return pedersenHash([nullifierZero.toBuffer(), numToUInt32BE(commitmentIndex, 32)], GeneratorIndex.COMMITMENT_NONCE);
}

/**
Expand All @@ -127,9 +126,7 @@ export function computeCommitmentNonce(nullifierZero: Fr, commitmentIndex: numbe
* @returns A siloed commitment.
*/
export function siloCommitment(contract: AztecAddress, innerCommitment: Fr): Fr {
return Fr.fromBuffer(
pedersenHash([contract.toBuffer(), innerCommitment.toBuffer()], GeneratorIndex.SILOED_COMMITMENT),
);
return pedersenHash([contract.toBuffer(), innerCommitment.toBuffer()], GeneratorIndex.SILOED_COMMITMENT);
}

/**
Expand All @@ -139,7 +136,7 @@ export function siloCommitment(contract: AztecAddress, innerCommitment: Fr): Fr
* @returns A unique commitment.
*/
export function computeUniqueCommitment(nonce: Fr, siloedCommitment: Fr): Fr {
return Fr.fromBuffer(pedersenHash([nonce.toBuffer(), siloedCommitment.toBuffer()], GeneratorIndex.UNIQUE_COMMITMENT));
return pedersenHash([nonce.toBuffer(), siloedCommitment.toBuffer()], GeneratorIndex.UNIQUE_COMMITMENT);
}

/**
Expand All @@ -150,7 +147,7 @@ export function computeUniqueCommitment(nonce: Fr, siloedCommitment: Fr): Fr {
* @returns A siloed nullifier.
*/
export function siloNullifier(contract: AztecAddress, innerNullifier: Fr): Fr {
return Fr.fromBuffer(pedersenHash([contract.toBuffer(), innerNullifier.toBuffer()], GeneratorIndex.OUTER_NULLIFIER));
return pedersenHash([contract.toBuffer(), innerNullifier.toBuffer()], GeneratorIndex.OUTER_NULLIFIER);
}

/**
Expand All @@ -171,9 +168,7 @@ export function computePublicDataTreeValue(value: Fr): Fr {
*/
export function computePublicDataTreeLeafSlot(contractAddress: AztecAddress, storageSlot: Fr): Fr {
return Fr.fromBuffer(
pedersenHash([contractAddress.toBuffer(), storageSlot.toBuffer()], GeneratorIndex.PUBLIC_LEAF_INDEX),
);
return pedersenHash([contractAddress.toBuffer(), storageSlot.toBuffer()], GeneratorIndex.PUBLIC_LEAF_INDEX);
}

/**
Expand All @@ -193,23 +188,19 @@ export function computeVarArgsHash(args: Fr[]) {
if (c.length < ARGS_HASH_CHUNK_LENGTH) {
c = padArrayEnd(c, Fr.ZERO, ARGS_HASH_CHUNK_LENGTH);
}
return Fr.fromBuffer(
pedersenHash(
c.map(a => a.toBuffer()),
GeneratorIndex.FUNCTION_ARGS,
),
return pedersenHash(
c.map(a => a.toBuffer()),
GeneratorIndex.FUNCTION_ARGS,
);
});

if (chunksHashes.length < ARGS_HASH_CHUNK_COUNT) {
chunksHashes = padArrayEnd(chunksHashes, Fr.ZERO, ARGS_HASH_CHUNK_COUNT);
}

return Fr.fromBuffer(
pedersenHash(
chunksHashes.map(a => a.toBuffer()),
GeneratorIndex.FUNCTION_ARGS,
),
return pedersenHash(
chunksHashes.map(a => a.toBuffer()),
GeneratorIndex.FUNCTION_ARGS,
);
}

Expand All @@ -230,5 +221,5 @@ export function computeNullifierHash(input: SideEffectLinkedToNoteHash) {
* @returns the hash
*/
export function computeMessageSecretHash(secretMessage: Fr) {
return Fr.fromBuffer(pedersenHash([secretMessage.toBuffer()], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET));
return pedersenHash([secretMessage.toBuffer()], GeneratorIndex.L1_TO_L2_MESSAGE_SECRET);
}
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function deriveSecretKey(secretKey: GrumpkinPrivateKey, index: Fr): GrumpkinPriv
// TODO: Temporary hack. Should replace it with a secure way to derive the secret key.
// Match the way keys are derived in noir-protocol-circuits/src/crates/private_kernel_lib/src/common.nr
const hash = pedersenHash([secretKey.high, secretKey.low, index].map(v => v.toBuffer()));
return new GrumpkinScalar(hash);
return new GrumpkinScalar(hash.toBuffer());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class MerkleTreeCalculator {
constructor(
private height: number,
zeroLeaf = Buffer.alloc(32),
hasher = (left: Buffer, right: Buffer) => pedersenHash([left, right]),
hasher = (left: Buffer, right: Buffer) => pedersenHash([left, right]).toBuffer(),
) {
this.hasher = hasher;
this.zeroHashes = Array.from({ length: height }).reduce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@ export class ContractDeploymentData {
}

hash(): Fr {
return Fr.fromBuffer(
pedersenHash(
this.toFields().map(f => f.toBuffer()),
GeneratorIndex.CONTRACT_DEPLOYMENT_DATA,
),
return pedersenHash(
this.toFields().map(f => f.toBuffer()),
GeneratorIndex.CONTRACT_DEPLOYMENT_DATA,
);
}
}
8 changes: 3 additions & 5 deletions yarn-project/circuits.js/src/structs/function_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ export class FunctionData {
}

hash(): Fr {
return Fr.fromBuffer(
pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.FUNCTION_DATA,
),
return pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.FUNCTION_DATA,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ export class FunctionLeafPreimage {
}

hash(): Fr {
return Fr.fromBuffer(
pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.FUNCTION_LEAF,
),
return pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.FUNCTION_LEAF,
);
}
}
8 changes: 3 additions & 5 deletions yarn-project/circuits.js/src/structs/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ export class Header {
}

hash(): Fr {
return Fr.fromBuffer(
pedersenHash(
this.toFields().map(f => f.toBuffer()),
GeneratorIndex.BLOCK_HASH,
),
return pedersenHash(
this.toFields().map(f => f.toBuffer()),
GeneratorIndex.BLOCK_HASH,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ export class NewContractData {
if (this.isEmpty()) {
return new Fr(0);
}
return Fr.fromBuffer(
pedersenHash(
NewContractData.getFields(this).map(f => f.toBuffer()),
GeneratorIndex.CONTRACT_LEAF,
),
return pedersenHash(
NewContractData.getFields(this).map(f => f.toBuffer()),
GeneratorIndex.CONTRACT_LEAF,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,9 @@ export class PrivateCallStackItem {
* @returns Hash.
*/
public hash(): Fr {
return Fr.fromBuffer(
pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.CALL_STACK_ITEM,
),
return pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.CALL_STACK_ITEM,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,9 @@ export class PrivateCircuitPublicInputs {
}

hash(): Fr {
return Fr.fromBuffer(
pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.PRIVATE_CIRCUIT_PUBLIC_INPUTS,
),
return pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.PRIVATE_CIRCUIT_PUBLIC_INPUTS,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ export class PublicCallStackItem {
this.publicInputs.argsHash = argsHash;
}

return Fr.fromBuffer(
pedersenHash(
[this.contractAddress, this.functionData.hash(), this.publicInputs.hash()].map(f => f.toBuffer()),
GeneratorIndex.CALL_STACK_ITEM,
),
return pedersenHash(
[this.contractAddress, this.functionData.hash(), this.publicInputs.hash()].map(f => f.toBuffer()),
GeneratorIndex.CALL_STACK_ITEM,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,9 @@ export class PublicCircuitPublicInputs {
}

hash(): Fr {
return Fr.fromBuffer(
pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.PUBLIC_CIRCUIT_PUBLIC_INPUTS,
),
return pedersenHash(
this.toFields().map(field => field.toBuffer()),
GeneratorIndex.PUBLIC_CIRCUIT_PUBLIC_INPUTS,
);
}
}
Loading
Loading