You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.2 KiB

5 years ago
5 years ago
5 years ago
  1. <html>
  2. <header>
  3. </header>
  4. <script src="websnark.js"></script>
  5. <script>
  6. var witness;
  7. var provingKey;
  8. function onLoad() {
  9. fetch("proving_key.bin").then( (response) => {
  10. return response.arrayBuffer();
  11. }).then( (b) => {
  12. provingKey = b;
  13. });
  14. fetch("witness.bin").then( (response) => {
  15. return response.arrayBuffer();
  16. }).then( (b) => {
  17. witness = b;
  18. });
  19. }
  20. function calcProof() {
  21. const start = new Date().getTime();
  22. document.getElementById("time").innerHTML = "processing....";
  23. document.getElementById("proof").innerHTML = "";
  24. window.genZKSnarkProof(witness, provingKey).then((p)=> {
  25. const end = new Date().getTime();
  26. const time = end - start;
  27. document.getElementById("time").innerHTML = `Time to compute: ${time}ms`;
  28. document.getElementById("proof").innerHTML = JSON.stringify(p, null, 1);
  29. });
  30. }
  31. function test() {
  32. const groth16 = window.groth16;
  33. const nSignals = 1;
  34. const pkey32 = new Uint32Array(provingKey);
  35. const pPointsA = pkey32[5];
  36. const points = provingKey.slice(pPointsA, pPointsA + nSignals*64);
  37. const signals = witness.slice(0, nSignals*32);
  38. const pr1 = groth16.alloc(96);
  39. const pPoints = groth16.alloc(points.byteLength);
  40. groth16.putBin(pPoints, points);
  41. const pSignals = groth16.alloc(signals.byteLength);
  42. groth16.putBin(pSignals, signals);
  43. groth16.instance.exports.g1_zero(pr1);
  44. groth16.instance.exports.g1_multiexp(pSignals, pPoints, nSignals, 1, pr1);
  45. groth16.instance.exports.g1_affine(pr1, pr1);
  46. groth16.instance.exports.g1_fromMontgomery(pr1, pr1);
  47. const r1 = groth16.bin2g1(groth16.getBin(pr1, 96));
  48. groth16.instance.exports.g1_zero(pr1);
  49. groth16.instance.exports.g1_multiexp2(pSignals, pPoints, nSignals, 1, pr1);
  50. groth16.instance.exports.g1_affine(pr1, pr1);
  51. groth16.instance.exports.g1_fromMontgomery(pr1, pr1);
  52. const r2 = groth16.bin2g1(groth16.getBin(pr1, 96));
  53. console.log(r1);
  54. console.log(r2);
  55. }
  56. </script>
  57. <body onLoad="onLoad()">
  58. <h1>iden3</h1>
  59. <h2>Zero knowledge proof generator</h2>
  60. <button onClick="calcProof()">Test</button>
  61. <div id="time"></div>
  62. <pre id="proof"></pre>
  63. </body>
  64. </html>