-
-
Save themighty1/0254065bb7c9259751b648cfae99a015 to your computer and use it in GitHub Desktop.
snarkyjs Poseidon test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// 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