Skip to content

Instantly share code, notes, and snippets.

@themighty1
Created May 5, 2022 11:21
Show Gist options
  • Save themighty1/0254065bb7c9259751b648cfae99a015 to your computer and use it in GitHub Desktop.
Save themighty1/0254065bb7c9259751b648cfae99a015 to your computer and use it in GitHub Desktop.
snarkyjs Poseidon test
import {
Poseidon,
Field,
Circuit,
circuitMain,
public_,
isReady,
CircuitValue,
arrayProp,
} from 'snarkyjs';
/* Exercise 0:
Public input: a hash value h
Prove:
I know a value x such that hash(x) = h
*/
// count is how many Field elements to hash. 1 element's size is 32 bytes
const count = 1024;
class TwoFields extends CircuitValue {
@arrayProp(Field, count) fields: Field[];
constructor(fields: Field[]) {
super();
this.fields = fields;
}
}
class Main extends Circuit {
@circuitMain
static main(preimage: TwoFields, @public_ hash: Field) {
Poseidon.hash(preimage.fields).assertEquals(hash);
}
}
await isReady;
console.log('generating keypair...');
const kp = Main.generateKeypair();
const preimage = new TwoFields(Array(count).fill(Field.one));
const hash = Poseidon.hash(preimage.fields);
console.log('prove...');
console.time('prove');
const pi = Main.prove([preimage], [hash], kp);
console.timeEnd('prove');
console.log('proof', pi);
console.log('verify...');
let ok = Main.verify([hash], kp.verificationKey(), pi);
console.log('ok?', ok);
@themighty1
Copy link
Author

themighty1 commented May 5, 2022

// use nodejs version >= 16.5
// nvm use 16.5

git clone https://github.com/o1-labs/snarkyjs
cd snarkyjs
// copy poseidon_test.ts into src/examples/
npm install
npm run build
./run src/examples/poseidon_test.ts
// adjust count on line 20 to hash more/less data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment