From 633eb6b73e5e5c782e871f826435fcb9850271eb Mon Sep 17 00:00:00 2001 From: esau <152162806+sklppy88@users.noreply.github.com> Date: Tue, 9 Jul 2024 15:49:54 +0200 Subject: [PATCH] fix: fields fromstring not working as intended (#7365) Please read [contributing guidelines](CONTRIBUTING.md) and remove this line. --- .../src/main.nr | 4 ++-- .../foundation/src/fields/fields.test.ts | 12 ++++++++++++ yarn-project/foundation/src/fields/fields.ts | 17 ++++++++++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/noir-projects/noir-contracts/contracts/schnorr_hardcoded_account_contract/src/main.nr b/noir-projects/noir-contracts/contracts/schnorr_hardcoded_account_contract/src/main.nr index 8d776eab233..870f45806ac 100644 --- a/noir-projects/noir-contracts/contracts/schnorr_hardcoded_account_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/schnorr_hardcoded_account_contract/src/main.nr @@ -9,8 +9,8 @@ contract SchnorrHardcodedAccount { auth_witness::get_auth_witness }; - global public_key_x: Field = 0x0ede3d33c920df8fdf43f3e39ed38b0882c25b056620ef52fd016fe811aa2443; - global public_key_y: Field = 0x29155934ffaa105323695b5f91faadd84acc21f4a8bda2fad760f992d692bc7f; + global public_key_x: Field = 0x16b93f4afae55cab8507baeb8e7ab4de80f5ab1e9e1f5149bf8cd0d375451d90; + global public_key_y: Field = 0x208d44b36eb6e73b254921134d002da1a90b41131024e3b1d721259182106205; // Note: If you globally change the entrypoint signature don't forget to update default_entrypoint.ts #[aztec(private)] diff --git a/yarn-project/foundation/src/fields/fields.test.ts b/yarn-project/foundation/src/fields/fields.test.ts index ff94978f0d1..b11d4db941c 100644 --- a/yarn-project/foundation/src/fields/fields.test.ts +++ b/yarn-project/foundation/src/fields/fields.test.ts @@ -31,6 +31,18 @@ describe('GrumpkinScalar Serialization', () => { // Check if the deserialized instance is equal to the original expect(deserialized).toEqual(original); + + // Note odd number of digits + const arbitraryString = '123'; + const arbitraryHexString = '0x123'; + const expectedBigInt = 291n; + + expect(GrumpkinScalar.fromString(arbitraryString).toBigInt()).toEqual(expectedBigInt); + expect(GrumpkinScalar.fromString(arbitraryHexString).toBigInt()).toEqual(expectedBigInt); + + const incorrectlyFormattedString = '12xx34xx45'; + + expect(() => GrumpkinScalar.fromString(incorrectlyFormattedString).toBigInt()).toThrow(); }); // Test case for GrumpkinScalar.toBuffer diff --git a/yarn-project/foundation/src/fields/fields.ts b/yarn-project/foundation/src/fields/fields.ts index a3d65d3f333..8220b48efc4 100644 --- a/yarn-project/foundation/src/fields/fields.ts +++ b/yarn-project/foundation/src/fields/fields.ts @@ -170,12 +170,13 @@ function random(f: DerivedField): T { */ function fromHexString(buf: string, f: DerivedField) { const withoutPrefix = buf.replace(/^0x/i, ''); - const buffer = Buffer.from(withoutPrefix, 'hex'); - - if (buffer.length === 0 && withoutPrefix.length > 0) { + const checked = withoutPrefix.match(/^[0-9A-F]+$/i)?.[0]; + if (checked === undefined) { throw new Error(`Invalid hex-encoded string: "${buf}"`); } + const buffer = Buffer.from(checked.length % 2 === 1 ? '0' + checked : checked, 'hex'); + return new f(buffer); } @@ -227,6 +228,11 @@ export class Fr extends BaseField { return fromBufferReduce(buffer, Fr); } + /** + * Creates a Fr instance from a hex string. + * @param buf - a hex encoded string. + * @returns the Fr instance + */ static fromString(buf: string) { return fromHexString(buf, Fr); } @@ -336,6 +342,11 @@ export class Fq extends BaseField { return fromBufferReduce(buffer, Fq); } + /** + * Creates a Fq instance from a hex string. + * @param buf - a hex encoded string. + * @returns the Fq instance + */ static fromString(buf: string) { return fromHexString(buf, Fq); }