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.

74 lines
1.9 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("Aliascheck test", () => {
  23. let circuit;
  24. before( async() => {
  25. const cirDef = await compiler(path.join(__dirname, "circuits", "aliascheck_test.circom"));
  26. circuit = new snarkjs.Circuit(cirDef);
  27. console.log("NConstrains: " + circuit.nConstraints);
  28. });
  29. it("Satisfy the aliastest 0", async () => {
  30. const inp = getBits(bigInt.zero, 254);
  31. circuit.calculateWitness({in: inp});
  32. });
  33. it("Satisfy the aliastest 3", async () => {
  34. const inp = getBits(bigInt(3), 254);
  35. circuit.calculateWitness({in: inp});
  36. });
  37. it("Satisfy the aliastest q-1", async () => {
  38. const inp = getBits(q.sub(bigInt.one), 254);
  39. circuit.calculateWitness({in: inp});
  40. });
  41. it("Nhot not satisfy an input of q", async () => {
  42. const inp = getBits(q, 254);
  43. try {
  44. circuit.calculateWitness({in: inp});
  45. assert(false);
  46. } catch(err) {
  47. assert.equal(err.message, "Constraint doesn't match: 1 != 0");
  48. }
  49. });
  50. it("Nhot not satisfy all ones", async () => {
  51. const inp = getBits(bigInt(1).shl(254).sub(bigInt(1)), 254);
  52. try {
  53. circuit.calculateWitness({in: inp});
  54. assert(false);
  55. } catch(err) {
  56. assert.equal(err.message, "Constraint doesn't match: 1 != 0");
  57. }
  58. });
  59. });