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.

85 lines
1.8 KiB

6 years ago
6 years ago
  1. /*
  2. Copyright 2018 0KIMS association.
  3. This file is part of circom (Zero Knowledge Circuit Compiler).
  4. circom is a free software: you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. circom is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with circom. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. include "bitify.circom";
  16. include "binsum.circom";
  17. template IsZero() {
  18. signal input in;
  19. signal output out;
  20. signal inv;
  21. inv <-- in!=0 ? 1/in : 0;
  22. out <== -in*inv +1;
  23. in*out === 0;
  24. }
  25. template IsEqual() {
  26. signal input in[2];
  27. signal output out;
  28. component isz = IsZero();
  29. in[1] - in[0] ==> isz.in;
  30. isz.out ==> out;
  31. }
  32. template ForceEqualIfEnabled() {
  33. signal input enabled;
  34. signal input in[2];
  35. component isz = IsZero();
  36. in[1] - in[0] ==> isz.in;
  37. (1 - isz.out)*enabled === 0;
  38. }
  39. // N is the number of bits the input have.
  40. // The MSF is the sign bit.
  41. template LessThan(n) {
  42. signal input in[2];
  43. signal output out;
  44. component num2Bits0;
  45. component num2Bits1;
  46. component adder;
  47. adder = BinSum(n, 2);
  48. num2Bits0 = Num2Bits(n);
  49. num2Bits1 = Num2BitsNeg(n);
  50. in[0] ==> num2Bits0.in;
  51. in[1] ==> num2Bits1.in;
  52. var i;
  53. for (i=0;i<n;i++) {
  54. num2Bits0.out[i] ==> adder.in[0][i];
  55. num2Bits1.out[i] ==> adder.in[1][i];
  56. }
  57. adder.out[n-1] ==> out;
  58. }