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 e6aee29 commit d6953fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion yarn-project/foundation/src/fields/fields.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BarretenbergSync } from '@aztec/bb.js';

import { inspect } from 'util';

import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
import { randomBytes } from '../crypto/random/index.js';
import { BufferReader } from '../serialize/buffer_reader.js';
import { TypeRegistry } from '../serialize/type_registry.js';
import { BarretenbergSync } from '@aztec/bb.js';

const ZERO_BUFFER = Buffer.alloc(32);

Expand Down
17 changes: 17 additions & 0 deletions yarn-project/foundation/src/fields/point.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Fr } from './fields.js';
import { Point } from './point.js';

describe('Point', () => {
it('converts to and from x and sign of y coordinate', () => {
const p = new Point(
new Fr(0x30426e64aee30e998c13c8ceecda3a77807dbead52bc2f3bf0eae851b4b710c1n),
new Fr(0x113156a068f603023240c96b4da5474667db3b8711c521c748212a15bc034ea6n),
false,
);

const [x, sign] = p.toXAndSign();
const p2 = Point.fromXAndSign(x, sign);

expect(p.equals(p2)).toBeTruthy();
});
});
17 changes: 14 additions & 3 deletions yarn-project/foundation/src/fields/point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class Point {
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(A);

// Calculate the square root of ySquared
const y = ySquared.sqrt();
Expand All @@ -101,14 +101,25 @@ export class Point {
throw new Error('The given x-coordinate is not on the Grumpkin curve');
}

const yPositiveBigInt = y.toBigInt() > (Fr.MODULUS - 1n) / 2n ? Fr.MODULUS - y.toBigInt() : y.toBigInt();
const yNegativeBigInt = Fr.MODULUS - yPositiveBigInt;

// Choose the positive or negative root based on isPositive
// const finalY = sign ? y : y.neg();
const finalY = y;
const finalY = sign ? new Fr(yPositiveBigInt) : new Fr(yNegativeBigInt);

// Create and return the new Point
return new this(x, finalY, false);
}

/**
* Returns the x coordinate and the sign of the y coordinate.
* @dev The y sign can be determined by checking if the y coordinate is greater than half of the modulus.
* @returns The x coordinate and the sign of the y coordinate.
*/
toXAndSign(): [Fr, boolean] {
return [this.x, this.y.toBigInt() <= (Fr.MODULUS - 1n) / 2n];
}

/**
* Returns the contents of the point as BigInts.
* @returns The point as BigInts
Expand Down

0 comments on commit d6953fb

Please sign in to comment.