diff --git a/yarn-project/circuits.js/src/contract/contract_address.ts b/yarn-project/circuits.js/src/contract/contract_address.ts index e9636e6de392..933cbe6207d1 100644 --- a/yarn-project/circuits.js/src/contract/contract_address.ts +++ b/yarn-project/circuits.js/src/contract/contract_address.ts @@ -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, ); } @@ -58,11 +56,9 @@ export function computePartialAddress( export function computeSaltedInitializationHash( instance: Pick, ): 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, ); } @@ -79,7 +75,7 @@ export function computeContractAddressFromPartial( [publicKeyHash.toBuffer(), args.partialAddress.toBuffer()], GeneratorIndex.CONTRACT_ADDRESS, ); - return new AztecAddress(result); + return AztecAddress.fromField(result); } /** @@ -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); } /** @@ -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); } diff --git a/yarn-project/circuits.js/src/contract/contract_class_id.ts b/yarn-project/circuits.js/src/contract/contract_class_id.ts index 2165225a37cf..d2e218f3fe8f 100644 --- a/yarn-project/circuits.js/src/contract/contract_class_id.ts +++ b/yarn-project/circuits.js/src/contract/contract_class_id.ts @@ -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 }; } diff --git a/yarn-project/circuits.js/src/contract/private_function.ts b/yarn-project/circuits.js/src/contract/private_function.ts index a1abcf166eea..0e7bde1cdc9b 100644 --- a/yarn-project/circuits.js/src/contract/private_function.ts +++ b/yarn-project/circuits.js/src/contract/private_function.ts @@ -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; diff --git a/yarn-project/circuits.js/src/hash/hash.test.ts b/yarn-project/circuits.js/src/hash/hash.test.ts index fd85f2634732..1839ba3e4758 100644 --- a/yarn-project/circuits.js/src/hash/hash.test.ts +++ b/yarn-project/circuits.js/src/hash/hash.test.ts @@ -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(); }); diff --git a/yarn-project/circuits.js/src/hash/hash.ts b/yarn-project/circuits.js/src/hash/hash.ts index f3f8964c3c8e..c9407b2b1e92 100644 --- a/yarn-project/circuits.js/src/hash/hash.ts +++ b/yarn-project/circuits.js/src/hash/hash.ts @@ -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; @@ -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, ); } @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -193,11 +188,9 @@ 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, ); }); @@ -205,11 +198,9 @@ export function computeVarArgsHash(args: Fr[]) { 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, ); } @@ -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); } diff --git a/yarn-project/circuits.js/src/keys/index.ts b/yarn-project/circuits.js/src/keys/index.ts index 07e8b0b2a65a..dee7623db74b 100644 --- a/yarn-project/circuits.js/src/keys/index.ts +++ b/yarn-project/circuits.js/src/keys/index.ts @@ -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()); } /** diff --git a/yarn-project/circuits.js/src/merkle/merkle_tree_calculator.ts b/yarn-project/circuits.js/src/merkle/merkle_tree_calculator.ts index 388d08a5f2d2..c8d4870039c0 100644 --- a/yarn-project/circuits.js/src/merkle/merkle_tree_calculator.ts +++ b/yarn-project/circuits.js/src/merkle/merkle_tree_calculator.ts @@ -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( diff --git a/yarn-project/circuits.js/src/structs/contract_deployment_data.ts b/yarn-project/circuits.js/src/structs/contract_deployment_data.ts index 55f13f19ecdf..e7fdb5d7e71f 100644 --- a/yarn-project/circuits.js/src/structs/contract_deployment_data.ts +++ b/yarn-project/circuits.js/src/structs/contract_deployment_data.ts @@ -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, ); } } diff --git a/yarn-project/circuits.js/src/structs/function_data.ts b/yarn-project/circuits.js/src/structs/function_data.ts index 3cf82fc57841..310d03507aa0 100644 --- a/yarn-project/circuits.js/src/structs/function_data.ts +++ b/yarn-project/circuits.js/src/structs/function_data.ts @@ -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, ); } } diff --git a/yarn-project/circuits.js/src/structs/function_leaf_preimage.ts b/yarn-project/circuits.js/src/structs/function_leaf_preimage.ts index 53f2b1e71caf..6a5b0f22cc8e 100644 --- a/yarn-project/circuits.js/src/structs/function_leaf_preimage.ts +++ b/yarn-project/circuits.js/src/structs/function_leaf_preimage.ts @@ -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, ); } } diff --git a/yarn-project/circuits.js/src/structs/header.ts b/yarn-project/circuits.js/src/structs/header.ts index 1e28525a99a1..0194ec5f97ec 100644 --- a/yarn-project/circuits.js/src/structs/header.ts +++ b/yarn-project/circuits.js/src/structs/header.ts @@ -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, ); } } diff --git a/yarn-project/circuits.js/src/structs/kernel/new_contract_data.ts b/yarn-project/circuits.js/src/structs/kernel/new_contract_data.ts index 94203ddfad27..82921fc545f1 100644 --- a/yarn-project/circuits.js/src/structs/kernel/new_contract_data.ts +++ b/yarn-project/circuits.js/src/structs/kernel/new_contract_data.ts @@ -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, ); } diff --git a/yarn-project/circuits.js/src/structs/private_call_stack_item.ts b/yarn-project/circuits.js/src/structs/private_call_stack_item.ts index ec56ca3bc970..5da5b0208417 100644 --- a/yarn-project/circuits.js/src/structs/private_call_stack_item.ts +++ b/yarn-project/circuits.js/src/structs/private_call_stack_item.ts @@ -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, ); } diff --git a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts index 99b263181d74..cdf55d91df2e 100644 --- a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts +++ b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts @@ -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, ); } } diff --git a/yarn-project/circuits.js/src/structs/public_call_stack_item.ts b/yarn-project/circuits.js/src/structs/public_call_stack_item.ts index 0f1e43b7db3b..5f9f98d2caea 100644 --- a/yarn-project/circuits.js/src/structs/public_call_stack_item.ts +++ b/yarn-project/circuits.js/src/structs/public_call_stack_item.ts @@ -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, ); } diff --git a/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.ts b/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.ts index 2b0e111f9dfa..4bf4d0140f69 100644 --- a/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.ts +++ b/yarn-project/circuits.js/src/structs/public_circuit_public_inputs.ts @@ -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, ); } } diff --git a/yarn-project/circuits.js/src/structs/tx_context.ts b/yarn-project/circuits.js/src/structs/tx_context.ts index dd5107105b49..e66e0596ed8e 100644 --- a/yarn-project/circuits.js/src/structs/tx_context.ts +++ b/yarn-project/circuits.js/src/structs/tx_context.ts @@ -120,11 +120,9 @@ export class TxContext { } hash(): Fr { - return Fr.fromBuffer( - pedersenHash( - this.toFields().map(f => f.toBuffer()), - GeneratorIndex.TX_CONTEXT, - ), + return pedersenHash( + this.toFields().map(f => f.toBuffer()), + GeneratorIndex.TX_CONTEXT, ); } } diff --git a/yarn-project/circuits.js/src/structs/tx_request.ts b/yarn-project/circuits.js/src/structs/tx_request.ts index 855da757002a..f2d169fc827e 100644 --- a/yarn-project/circuits.js/src/structs/tx_request.ts +++ b/yarn-project/circuits.js/src/structs/tx_request.ts @@ -71,11 +71,9 @@ export class TxRequest { } hash() { - return Fr.fromBuffer( - pedersenHash( - this.toFields().map(field => field.toBuffer()), - GeneratorIndex.TX_REQUEST, - ), + return pedersenHash( + this.toFields().map(field => field.toBuffer()), + GeneratorIndex.TX_REQUEST, ); } diff --git a/yarn-project/foundation/src/crypto/pedersen/pedersen.wasm.ts b/yarn-project/foundation/src/crypto/pedersen/pedersen.wasm.ts index 6d38d88a1101..dbe457a2971c 100644 --- a/yarn-project/foundation/src/crypto/pedersen/pedersen.wasm.ts +++ b/yarn-project/foundation/src/crypto/pedersen/pedersen.wasm.ts @@ -1,4 +1,6 @@ -import { BarretenbergSync, Fr } from '@aztec/bb.js'; +import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js'; + +import { Fr } from '../../fields/fields.js'; /** * Create a pedersen commitment (point) from an array of input fields. @@ -9,7 +11,7 @@ export function pedersenCommit(input: Buffer[]) { throw new Error('All Pedersen Commit input buffers must be <= 32 bytes.'); } input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i)); - const point = BarretenbergSync.getSingleton().pedersenCommit(input.map(i => new Fr(i))); + const point = BarretenbergSync.getSingleton().pedersenCommit(input.map(i => new FrBarretenberg(i))); // toBuffer returns Uint8Arrays (browser/worker-boundary friendly). // TODO: rename toTypedArray()? return [Buffer.from(point.x.toBuffer()), Buffer.from(point.y.toBuffer())]; @@ -19,18 +21,20 @@ export function pedersenCommit(input: Buffer[]) { * Create a pedersen hash (field) from an array of input fields. * Left pads any inputs less than 32 bytes. */ -export function pedersenHash(input: Buffer[], index = 0): Buffer { +export function pedersenHash(input: Buffer[], index = 0): Fr { if (!input.every(i => i.length <= 32)) { throw new Error('All Pedersen Hash input buffers must be <= 32 bytes.'); } input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i)); - return Buffer.from( - BarretenbergSync.getSingleton() - .pedersenHash( - input.map(i => new Fr(i)), - index, - ) - .toBuffer(), + return Fr.fromBuffer( + Buffer.from( + BarretenbergSync.getSingleton() + .pedersenHash( + input.map(i => new FrBarretenberg(i)), + index, + ) + .toBuffer(), + ), ); }