Skip to content

Commit

Permalink
fix: buggy e2e key registry test setup (#6496)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed May 17, 2024
1 parent 2b37729 commit 52d85d1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
12 changes: 11 additions & 1 deletion yarn-project/circuits.js/src/types/public_keys.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { poseidon2Hash } from '@aztec/foundation/crypto';
import { type Fr, Point } from '@aztec/foundation/fields';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
import { BufferReader, FieldReader, serializeToBuffer } from '@aztec/foundation/serialize';

import { GeneratorIndex } from '../constants.gen.js';
import { type PublicKey } from './public_key.js';
Expand Down Expand Up @@ -107,4 +107,14 @@ export class PublicKeys {
...this.masterTaggingPublicKey.toFields(),
];
}

static fromFields(fields: Fr[] | FieldReader): PublicKeys {
const reader = FieldReader.asReader(fields);
return new PublicKeys(
reader.readObject(Point),
reader.readObject(Point),
reader.readObject(Point),
reader.readObject(Point),
);
}
}
20 changes: 12 additions & 8 deletions yarn-project/end-to-end/src/e2e_key_registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,25 @@ describe('Key Registry', () => {

describe('failure cases', () => {
it('throws when address preimage check fails', async () => {
const publicKeysBuf = account.publicKeys.toBuffer();
// We randomly invalidate some of the keys by overwriting random byte
const byteIndex = Math.floor(Math.random() * publicKeysBuf.length);
publicKeysBuf[byteIndex] = (publicKeysBuf[byteIndex] + 2) % 256;
// First we get invalid keys by replacing any of the 8 fields of public keys with a random value
let invalidPublicKeys: PublicKeys;
{
// We call toBuffer and fromBuffer first to ensure that we get a deep copy
const publicKeysFields = PublicKeys.fromBuffer(account.publicKeys.toBuffer()).toFields();
const randomIndex = Math.floor(Math.random() * publicKeysFields.length);
publicKeysFields[randomIndex] = Fr.random();

const publicKeys = PublicKeys.fromBuffer(publicKeysBuf);
invalidPublicKeys = PublicKeys.fromFields(publicKeysFields);
}

await expect(
keyRegistry
.withWallet(wallets[0])
.methods.register(
account,
account.partialAddress,
// TODO(#6337): Directly dump account.publicKeys here
publicKeys.toNoirStruct(),
// TODO(#6337): Make calling `toNoirStruct()` unnecessary
invalidPublicKeys.toNoirStruct(),
)
.send()
.wait(),
Expand Down Expand Up @@ -120,7 +124,7 @@ describe('Key Registry', () => {
.methods.register(
account,
account.partialAddress,
// TODO(#6337): Directly dump account.publicKeys here
// TODO(#6337): Make calling `toNoirStruct()` unnecessary
account.publicKeys.toNoirStruct(),
)
.send()
Expand Down

0 comments on commit 52d85d1

Please sign in to comment.