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.

67 lines
1.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const tester = require("circom").tester;
  4. const Fr = require("ffjavascript").bn128.Fr;
  5. const eddsa = require("../src/eddsa.js");
  6. const babyJub = require("../src/babyjub.js");
  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(Fr.one);
  17. } else {
  18. res.push(Fr.zero);
  19. }
  20. }
  21. }
  22. return res;
  23. }
  24. describe("EdDSA test", function () {
  25. let circuit;
  26. this.timeout(100000);
  27. before( async () => {
  28. circuit = await tester(path.join(__dirname, "circuits", "eddsa_test.circom"));
  29. });
  30. it("Sign a single 10 bytes from 0 to 9", async () => {
  31. const msg = Buffer.from("00010203040506070809", "hex");
  32. // const prvKey = crypto.randomBytes(32);
  33. const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
  34. const pubKey = eddsa.prv2pub(prvKey);
  35. const pPubKey = babyJub.packPoint(pubKey);
  36. const signature = eddsa.sign(prvKey, msg);
  37. const pSignature = eddsa.packSignature(signature);
  38. const uSignature = eddsa.unpackSignature(pSignature);
  39. assert(eddsa.verify(msg, uSignature, pubKey));
  40. const msgBits = buffer2bits(msg);
  41. const r8Bits = buffer2bits(pSignature.slice(0, 32));
  42. const sBits = buffer2bits(pSignature.slice(32, 64));
  43. const aBits = buffer2bits(pPubKey);
  44. const w = await circuit.calculateWitness({A: aBits, R8: r8Bits, S: sBits, msg: msgBits}, true);
  45. await circuit.checkConstraints(w);
  46. });
  47. });