Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Dec 9, 2024
1 parent 621cbaf commit 22bdfbe
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions yarn-project/foundation/src/fields/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,31 @@ export class Fr extends BaseField {
return fromBufferReduce(buffer, Fr);
}

/**
* Creates a Fr instance from a string.
* @param buf - the string to create a Fr from.
* @returns the Fr instance
* @remarks if the string only consists of numbers, we assume we are parsing a bigint,
* otherwise we require the hex string to be prepended with "0x", to ensure there is no misunderstanding
* as to what is being parsed.
*/
static fromString(buf: string) {
if (buf.match(/^\d+$/) !== null) {
return new Fr(BigInt(buf));
}
if (buf.match(/^0x/i) !== null) {
return fromHexString(buf, Fr);
}

throw new Error('Tried to create a Fr from an invalid string');
}

/**
* Creates a Fr instance from a hex string.
* @param buf - a hex encoded string.
* @returns the Fr instance
*/
static fromString(buf: string) {
static fromHexString(buf: string) {
return fromHexString(buf, Fr);
}

Expand Down Expand Up @@ -368,12 +387,31 @@ export class Fq extends BaseField {
return fromBufferReduce(buffer, Fq);
}

/**
* Creates a Fq instance from a string.
* @param buf - the string to create a Fq from.
* @returns the Fq instance
* @remarks if the string only consists of numbers, we assume we are parsing a bigint,
* otherwise we require the hex string to be prepended with "0x", to ensure there is no misunderstanding
* as to what is being parsed.
*/
static fromString(buf: string) {
if (buf.match(/^\d+$/) !== null) {
return new Fq(BigInt(buf));
}
if (buf.match(/^0x/i) !== null) {
return fromHexString(buf, Fq);
}

throw new Error('Tried to create a F1 from an invalid string');
}

/**
* Creates a Fq instance from a hex string.
* @param buf - a hex encoded string.
* @returns the Fq instance
*/
static fromString(buf: string) {
static fromHexString(buf: string) {
return fromHexString(buf, Fq);
}

Expand Down

0 comments on commit 22bdfbe

Please sign in to comment.