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.

80 lines
2.2 KiB

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