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.

79 lines
2.2 KiB

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