Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fields fromstring not working as intended #7365

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't arbitary string resolve to 123 and not 291?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always resolved as hex currently, regardless of whether it's prefixed or not. Do we want to change the base behavior ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base issue was that any string was getting truncated if it was an odd length.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm can we make it clear then that fromString expects a hex string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, added the comment as you specified in scrum, and refactored the check because the check didn't actually work for a hexstring like 12xx34xx56.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sklppy88 can this problem get revised? I am hitting this issue in a deep object, so i cant easily convert all my strings to hex format

Copy link
Contributor Author

@sklppy88 sklppy88 Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @olehmisar: I haven't had the green light to change the current behavior, but will your problem be alleviated if I add a param for you to specify the encoding ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See here, would this fix your problem ?

Copy link
Contributor

@olehmisar olehmisar Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sklppy88 i am have to do something like this:

encodeArguments(abi, args.map((arg) => {
  if (Number.isFinite(Number(arg))) {
    return new Fr(BigInt(arg));
  }
  return arg;
}));

Now imagine that args contains a nested object or array. i will have to iterate over each object deeply (prob using recursion) to convert each number into a hex string manually.

Your example won't help as it will just change:

- return new Fr(BigInt(arg));
+ return new Fr(arg, "dec");

But if you fixed the original problem, i would be able to just write this code:

encodeArguments(abi, args)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please explain the reasoning behind not wanting to change it to autodetection of decimal vs hex? For me it's such an obvious decision. What am i missing?

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
Loading