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.

88 lines
2.4 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const compiler = require("circom");
  5. const assert = chai.assert;
  6. const bigInt = snarkjs.bigInt;
  7. function print(circuit, w, s) {
  8. console.log(s + ": " + w[circuit.getSignalIdx(s)]);
  9. }
  10. function getBits(v, n) {
  11. const res = [];
  12. for (let i=0; i<n; i++) {
  13. if (v.shr(i).isOdd()) {
  14. res.push(bigInt.one);
  15. } else {
  16. res.push(bigInt.zero);
  17. }
  18. }
  19. return res;
  20. }
  21. const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  22. describe("Sign test", () => {
  23. let circuit;
  24. before( async() => {
  25. const cirDef = await compiler(path.join(__dirname, "circuits", "sign_test.circom"));
  26. circuit = new snarkjs.Circuit(cirDef);
  27. console.log("NConstrains: " + circuit.nConstraints);
  28. });
  29. it("Sign of 0", async () => {
  30. const inp = getBits(bigInt.zero, 254);
  31. const w = circuit.calculateWitness({in: inp});
  32. assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(0)) );
  33. });
  34. it("Sign of 3", async () => {
  35. const inp = getBits(bigInt(3), 254);
  36. const w = circuit.calculateWitness({in: inp});
  37. assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(0)) );
  38. });
  39. it("Sign of q/2", async () => {
  40. const inp = getBits(q.shr(bigInt.one), 254);
  41. const w = circuit.calculateWitness({in: inp});
  42. assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(0)) );
  43. });
  44. it("Sign of q/2+1", async () => {
  45. const inp = getBits(q.shr(bigInt.one).add(bigInt.one), 254);
  46. const w = circuit.calculateWitness({in: inp});
  47. assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
  48. });
  49. it("Sign of q-1", async () => {
  50. const inp = getBits(q.sub(bigInt.one), 254);
  51. const w = circuit.calculateWitness({in: inp});
  52. assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
  53. });
  54. it("Sign of q", async () => {
  55. const inp = getBits(q, 254);
  56. const w = circuit.calculateWitness({in: inp});
  57. assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
  58. });
  59. it("Sign of all ones", async () => {
  60. const inp = getBits(bigInt(1).shl(254).sub(bigInt(1)), 254);
  61. const w = circuit.calculateWitness({in: inp});
  62. assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
  63. });
  64. });