Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jul 12, 2024
1 parent 4ccc9c0 commit bdc53f8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
7 changes: 4 additions & 3 deletions yarn-project/foundation/src/fields/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,16 +282,17 @@ export class Fr extends BaseField {

/**
* Computes the square root of the field element.
* @returns The square root of the field element if it exists (undefined if not).
* @returns The square root of the field element (null if it does not exist).
*/
sqrt(): Fr | undefined {
sqrt(): Fr | null {
const wasm = BarretenbergSync.getSingleton().getWasm();
wasm.writeMemory(0, this.toBuffer());
wasm.call('grumpkin_fr_sqrt', 0, Fr.SIZE_IN_BYTES);
const isSqrtBuf = Buffer.from(wasm.getMemorySlice(Fr.SIZE_IN_BYTES, Fr.SIZE_IN_BYTES + 1));
const isSqrt = isSqrtBuf[0] === 1;
if (!isSqrt) {
return undefined;
// Field element is not a quadratic residue mod p so it has no square root.
return null;
}

const rootBuf = Buffer.from(wasm.getMemorySlice(Fr.SIZE_IN_BYTES + 1, Fr.SIZE_IN_BYTES * 2 + 1))
Expand Down
5 changes: 1 addition & 4 deletions yarn-project/foundation/src/fields/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ export class Point {
* @returns The point as an array of 2 fields
*/
static fromXAndSign(x: Fr, sign: boolean) {
// Define the constant A for the Grumpkin curve equation (y^2 = x^3 - A)
const A = new Fr(17);

// Calculate y^2 = x^3 - 17
const ySquared = x.square().mul(x).sub(A);
const ySquared = x.square().mul(x).sub(new Fr(17));

// Calculate the square root of ySquared
const y = ySquared.sqrt();
Expand Down

0 comments on commit bdc53f8

Please sign in to comment.