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.

75 lines
2.0 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const wasm_tester = require("circom_tester").wasm;
  4. const buildEddsa = require("circomlibjs").buildEddsa;
  5. const buildBabyjub = require("circomlibjs").buildBabyjub;
  6. const Scalar = require("ffjavascript").Scalar;
  7. const assert = chai.assert;
  8. function print(circuit, w, s) {
  9. console.log(s + ": " + w[circuit.getSignalIdx(s)]);
  10. }
  11. function buffer2bits(buff) {
  12. const res = [];
  13. for (let i=0; i<buff.length; i++) {
  14. for (let j=0; j<8; j++) {
  15. if ((buff[i]>>j)&1) {
  16. res.push(1n);
  17. } else {
  18. res.push(0n);
  19. }
  20. }
  21. }
  22. return res;
  23. }
  24. describe("EdDSA test", function () {
  25. let circuit;
  26. let eddsa;
  27. let babyJub;
  28. let F;
  29. this.timeout(100000);
  30. before( async () => {
  31. eddsa = await buildEddsa();
  32. babyJub = await buildBabyjub();
  33. F = babyJub.F;
  34. circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsa_test.circom"));
  35. });
  36. it("Sign a single 10 bytes from 0 to 9", async () => {
  37. const msg = Buffer.from("00010203040506070809", "hex");
  38. // const prvKey = crypto.randomBytes(32);
  39. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  40. const pubKey = eddsa.prv2pub(prvKey);
  41. const pPubKey = babyJub.packPoint(pubKey);
  42. const signature = eddsa.signPedersen(prvKey, msg);
  43. const pSignature = eddsa.packSignature(signature);
  44. const uSignature = eddsa.unpackSignature(pSignature);
  45. assert(eddsa.verifyPedersen(msg, uSignature, pubKey));
  46. const msgBits = buffer2bits( msg);
  47. const r8Bits = buffer2bits( pSignature.slice(0, 32));
  48. const sBits = buffer2bits( pSignature.slice(32, 64));
  49. const aBits = buffer2bits( pPubKey);
  50. const w = await circuit.calculateWitness({A: aBits, R8: r8Bits, S: sBits, msg: msgBits}, true);
  51. await circuit.checkConstraints(w);
  52. });
  53. });