DEV Community

Building your first ZKP program on Solana

Zeref on January 08, 2024

👋 Introduction Zero-knowledge proofs are a kind of security tool in cryptography. They let one person (the prover) show another person (...
Collapse
 
ppoliani profile image
Pavlos Polianidis • Edited

@zeref101 quick question. I've got a circuit with the following public inputs

  signal output out1[5];
  signal output address;
Enter fullscreen mode Exit fullscreen mode

When I create the proof.json it ends up having the following values:


[
"50",
"50",
"30",
"10",
"45",
"12737771764621907568098507625979260347390739857011548125897812973048106564980"
]

Enter fullscreen mode Exit fullscreen mode

out1 is basically an array with 5 bytes e.g. values between 0 and 255. The address output though is a poseidon hash. As I understand it's a big value in the Circom curve's Field set.

How do I pass this into the on-chain program? Do I call it this way?

const publicSignalsBuffer = publicSignals.map((i) => to32ByteBuffer(BigInt(i)))

Update

I can't get this example verity the ZKP successfully. It feels like the JS code is missing some transformation on the proof_a similar to what the rust code does here github.com/Lightprotocol/groth16-s...

For anyone interested, I have written a Rust crate and bundled it into wasm to use it directly in JS. Check it out here github.com/Apocentre/solana-zk-exa...

Collapse
 
ppoliani profile image
Pavlos Polianidis

This is an awesome article. Thanks for this tutorial. May I ask you if you can share the code for to32ByteBuffer?

Collapse
 
ppoliani profile image
Pavlos Polianidis • Edited

Ended up doing something like this:

const to32ByteBuffer = (num) => {
  const result = new Uint8Array(32);
  let i = 0;

  while (num > 0n) {
    result[i] = Number(num % 256n);
    num = num / 256n;
    i += 1;
  }

    return result;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
zeref101 profile image
Zeref

Thanks, Pavlos! Good catch on the missing code. I've updated the post. I'm not sure about your solution, but if it works, then great! 🙌