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

expose hash to group #887

Merged
merged 7 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion src/bindings
37 changes: 35 additions & 2 deletions src/lib/hash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { HashInput, ProvableExtended, Struct } from './circuit_value.js';
import { Poseidon as Poseidon_, Field } from '../snarky.js';
import {
HashInput,
provable,
ProvableExtended,
Struct,
} from './circuit_value.js';
import { Poseidon as Poseidon_, Field, Bool, Circuit } from '../snarky.js';
import { inCheckedComputation } from './proof_system.js';
import { createHashHelpers } from './hash-generic.js';

Expand Down Expand Up @@ -43,6 +48,34 @@ const Poseidon = {
return Poseidon_.hash(input, isChecked);
},

hashToGroup(input: Field[]) {
let isChecked = !input.every((x) => x.isConstant());
// y = sqrt(y^2)
let { x, y } = Poseidon_.hashToGroup(input, isChecked);

let { x0, x1 } = Circuit.witness(provable({ x0: Field, x1: Field }), () => {
// the even root of y^2 will become x0, so the APIs are uniform
let isEven = y.toBigInt() % 2n === 0n;

// r is the second root of sqrt(y^2)
let r = y.mul(-1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we factor that computation, albeit not costly with the let y_ = y.mul(-1) below?

// we just change the order so the even root is x0 and the odd x1
return isEven ? { x0: y, x1: r } : { x0: r, x1: y };
});

// we check that either x0 or x1 match the original root y
y.equals(x0).or(y.equals(x1)).assertTrue();

// and then we check that either x0 or x1 is the expected second root of y^2
let y_ = y.mul(-1);
y_.equals(x0).or(y_.equals(x1)).assertTrue();
Trivo25 marked this conversation as resolved.
Show resolved Hide resolved

return {
x,
y: { x0, x1 },
};
Trivo25 marked this conversation as resolved.
Show resolved Hide resolved
},

update(state: [Field, Field, Field], input: Field[]) {
let isChecked = !(
state.every((x) => x.isConstant()) && input.every((x) => x.isConstant())
Expand Down
7 changes: 7 additions & 0 deletions src/snarky.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,13 @@ declare const Poseidon: {
input: Field[],
isChecked: boolean
): [Field, Field, Field];
hashToGroup(
input: Field[],
isChecked: boolean
): {
x: Field;
y: Field;
};
prefixes: Record<
| 'event'
| 'events'
Expand Down