Skip to content

Commit

Permalink
fix: fields fromstring not working as intended (#7365)
Browse files Browse the repository at this point in the history
Please read [contributing guidelines](CONTRIBUTING.md) and remove this
line.
  • Loading branch information
sklppy88 authored Jul 9, 2024
1 parent 894ac3b commit 633eb6b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
12 changes: 12 additions & 0 deletions yarn-project/foundation/src/fields/fields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions yarn-project/foundation/src/fields/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,13 @@ function random<T extends BaseField>(f: DerivedField<T>): T {
*/
function fromHexString<T extends BaseField>(buf: string, f: DerivedField<T>) {
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);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 633eb6b

Please sign in to comment.