Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: o1-labs/o1js-bindings
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a4dd716fdea62b81ad5957c73f8800e1a2cc57bd
Choose a base ref
...
head repository: o1-labs/o1js-bindings
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 90969fdeb288be9f9a2f856f6d390b2106848358
Choose a head ref
  • 3 commits
  • 2 files changed
  • 1 contributor

Commits on Apr 9, 2024

  1. Copy the full SHA
    be034f2 View commit details

Commits on Apr 16, 2024

  1. Merge pull request #260 from o1-labs/feature/no-shifted-scale

    Efficient scalar mul and other Scalar improvements
    mitschabaude authored Apr 16, 2024
    Copy the full SHA
    ce6bf1d View commit details
  2. Copy the full SHA
    90969fd View commit details
Showing with 10 additions and 14 deletions.
  1. +7 −12 crypto/poseidon.ts
  2. +3 −2 crypto/poseidon.unit-test.ts
19 changes: 7 additions & 12 deletions crypto/poseidon.ts
Original file line number Diff line number Diff line change
@@ -27,18 +27,13 @@ function makeHashToGroup(hash: (i: bigint[]) => bigint) {
let digest = hash(input);
let g = fieldToGroup(digest);
if (g === undefined) return undefined;
// we split the y coordinate into two elements, x0 = -sqrt(y^2) and x1 = sqrt(y^2)
// then put the even root into x0, and the odd one into x1 so APIs equal even tho the underlying algorithms to calculate the sqrt differ
// we do the same in-snark - so both APIs are deterministic
let isEven = g.y % 2n === 0n;
let gy_neg = Fp.negate(g.y);
return {
x: g.x,
y: {
x0: isEven ? g.y : gy_neg,
x1: isEven ? gy_neg : g.y,
},
};

// the y coordinate is calculated using a square root, so it has two possible values
// to make the output deterministic, we negate y if it is odd
// we do the same in-snark, so both APIs match
let isOdd = (g.y & 1n) === 1n;
let y = isOdd ? Fp.negate(g.y) : g.y;
return { x: g.x, y };
};
}

5 changes: 3 additions & 2 deletions crypto/poseidon.unit-test.ts
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import { test, Random } from '../../lib/testing/property.js';
import { Test } from '../../snarky.js';
import { FieldConst } from '../../lib/provable/core/fieldvar.js';
import { MlArray } from '../../lib/ml/base.js';
import { Fp } from './finite-field.js';

function checkTestVectors(
testVectors: { input: string[]; output: string }[],
@@ -34,11 +35,11 @@ test(Random.array(Random.field, Random.nat(20)), (xs) => {
);

expect(g1).toBeDefined();

expect(g1.x).toEqual(FieldConst.toBigint(g2x));

let g2y_ = FieldConst.toBigint(g2y);
expect(g1.y.x0 === g2y_ || g1.y.x1 === g2y_).toEqual(true);
expect(g1.y === g2y_ || Fp.negate(g1.y) === g2y_).toEqual(true);
expect(Fp.isEven(g1.y)).toEqual(true);
});

console.log('poseidon hashToGroup implementations match! 🎉');