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.

77 lines
2.1 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const assert = chai.assert;
  4. const Scalar = require("ffjavascript").Scalar;
  5. const F1Field = require("ffjavascript").F1Field;
  6. const utils = require("ffjavascript").utils;
  7. const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  8. const F = new F1Field(q);
  9. const tester = require("circom").tester;
  10. function print(circuit, w, s) {
  11. console.log(s + ": " + w[circuit.getSignalIdx(s)]);
  12. }
  13. function getBits(v, n) {
  14. const res = [];
  15. for (let i=0; i<n; i++) {
  16. if (Scalar.isOdd(Scalar.shr(v,i))) {
  17. res.push(F.one);
  18. } else {
  19. res.push(F.zero);
  20. }
  21. }
  22. return res;
  23. }
  24. describe("Aliascheck test", function () {
  25. this.timeout(100000);
  26. let cir;
  27. before( async() => {
  28. cir = await tester(path.join(__dirname, "circuits", "aliascheck_test.circom"));
  29. });
  30. it("Satisfy the aliastest 0", async () => {
  31. const inp = getBits(0, 254);
  32. await cir.calculateWitness({in: inp}, true);
  33. });
  34. it("Satisfy the aliastest 3", async () => {
  35. const inp = getBits(3, 254);
  36. await cir.calculateWitness({in: inp}, true);
  37. });
  38. it("Satisfy the aliastest q-1", async () => {
  39. const inp = getBits(F.minusone, 254);
  40. // console.log(JSON.stringify(utils.stringifyBigInts(inp)));
  41. await cir.calculateWitness({in: inp}, true);
  42. });
  43. it("Should not satisfy an input of q", async () => {
  44. const inp = getBits(q, 254);
  45. try {
  46. await cir.calculateWitness({in: inp}, true);
  47. assert(false);
  48. } catch(err) {
  49. assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
  50. }
  51. });
  52. it("Should not satisfy all ones", async () => {
  53. const inp = getBits(Scalar.sub(Scalar.shl(1, 254) , 1) , 254);
  54. try {
  55. await cir.calculateWitness({in: inp}, true);
  56. assert(false);
  57. } catch(err) {
  58. assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
  59. }
  60. });
  61. });