From d5256d29f3bc7d2094ba760c5a528dd61475bb40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bene=C5=A1?= Date: Fri, 12 Apr 2024 16:30:08 +0200 Subject: [PATCH] feat: poseidon separator (#5717) --- .../foundation/src/crypto/poseidon/index.ts | 5 +---- .../key-store/src/new_test_key_store.test.ts | 3 +-- yarn-project/key-store/src/new_test_key_store.ts | 16 +++++++--------- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/yarn-project/foundation/src/crypto/poseidon/index.ts b/yarn-project/foundation/src/crypto/poseidon/index.ts index c971299ec5f..b0c2a0c1ca2 100644 --- a/yarn-project/foundation/src/crypto/poseidon/index.ts +++ b/yarn-project/foundation/src/crypto/poseidon/index.ts @@ -6,18 +6,15 @@ import { type Fieldable, serializeToFields } from '../../serialize/serialize.js' /** * Create a poseidon hash (field) from an array of input fields. * @param input - The input fields to hash. - * @param index - The separator index to use for the hash. * @returns The poseidon hash. - * TODO(#5714): enable index once barretenberg API supports it */ -export function poseidon2Hash(input: Fieldable[], _index = 0): Fr { +export function poseidon2Hash(input: Fieldable[]): Fr { const inputFields = serializeToFields(input); return Fr.fromBuffer( Buffer.from( BarretenbergSync.getSingleton() .poseidon2Hash( inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion - // index, // TODO: enable once the barretenberg API supports it ) .toBuffer(), ), diff --git a/yarn-project/key-store/src/new_test_key_store.test.ts b/yarn-project/key-store/src/new_test_key_store.test.ts index 4732e0c6ca0..45a1ca06f1d 100644 --- a/yarn-project/key-store/src/new_test_key_store.test.ts +++ b/yarn-project/key-store/src/new_test_key_store.test.ts @@ -15,10 +15,9 @@ describe('NewTestKeyStore', () => { const accountAddress = await keyStore.addAccount(sk, partialAddress); expect(accountAddress.toString()).toMatchInlineSnapshot( - `"0x2e34847ad9019320ac89a6ec9b42fec90f94ef4162fdfdd7f5b7668e32d82655"`, + `"0x0ba7834252d19c4f09d29303c269f303f40ae3d2043f921ed0bf8c0709926d4e"`, ); - // TODO(#5714): The keys are currently the same here because separator is currently ignored in poseidon const masterNullifierPublicKey = await keyStore.getMasterNullifierPublicKey(accountAddress); expect(masterNullifierPublicKey.toString()).toMatchInlineSnapshot( `"0x2ef5d15dd65d29546680ab72846fb071f41cb9f2a0212215e6c560e29df4ff650ce764818364b376be92dc2f49577fe440e64a16012584f7c4ee94f7edbc323a"`, diff --git a/yarn-project/key-store/src/new_test_key_store.ts b/yarn-project/key-store/src/new_test_key_store.ts index 5a48036d760..7ba118d58d7 100644 --- a/yarn-project/key-store/src/new_test_key_store.ts +++ b/yarn-project/key-store/src/new_test_key_store.ts @@ -46,20 +46,18 @@ export class NewTestKeyStore implements NewKeyStore { const masterTaggingPublicKey = this.curve.mul(this.curve.generator(), masterTaggingSecretKey); // We hash the public keys to get the public keys hash - const publicKeysHash = poseidon2Hash( - [ - masterNullifierPublicKey, - masterIncomingViewingPublicKey, - masterOutgoingViewingPublicKey, - masterTaggingPublicKey, - ], + const publicKeysHash = poseidon2Hash([ + masterNullifierPublicKey, + masterIncomingViewingPublicKey, + masterOutgoingViewingPublicKey, + masterTaggingPublicKey, GeneratorIndex.PUBLIC_KEYS_HASH, - ); + ]); // We hash the partial address and the public keys hash to get the account address // TODO(#5726): Should GeneratorIndex.CONTRACT_ADDRESS be removed given that we introduced CONTRACT_ADDRESS_V1? // TODO(#5726): Move the following line to AztecAddress class? - const accountAddressFr = poseidon2Hash([partialAddress, publicKeysHash], GeneratorIndex.CONTRACT_ADDRESS_V1); + const accountAddressFr = poseidon2Hash([partialAddress, publicKeysHash, GeneratorIndex.CONTRACT_ADDRESS_V1]); const accountAddress = AztecAddress.fromField(accountAddressFr); // We store the keys in the database