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.

140 lines
2.7 KiB

5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 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. /*
  40. // N is the number of bits the input have.
  41. // The MSF is the sign bit.
  42. template LessThan(n) {
  43. signal input in[2];
  44. signal output out;
  45. component num2Bits0;
  46. component num2Bits1;
  47. component adder;
  48. adder = BinSum(n, 2);
  49. num2Bits0 = Num2Bits(n);
  50. num2Bits1 = Num2BitsNeg(n);
  51. in[0] ==> num2Bits0.in;
  52. in[1] ==> num2Bits1.in;
  53. var i;
  54. for (i=0;i<n;i++) {
  55. num2Bits0.out[i] ==> adder.in[0][i];
  56. num2Bits1.out[i] ==> adder.in[1][i];
  57. }
  58. adder.out[n-1] ==> out;
  59. }
  60. */
  61. template LessThan(n) {
  62. assert(n <= 252);
  63. signal input in[2];
  64. signal output out;
  65. component n2b = Num2Bits(n+1);
  66. n2b.in <== in[0]+ (1<<n) - in[1];
  67. out <== 1-n2b.out[n];
  68. }
  69. // N is the number of bits the input have.
  70. // The MSF is the sign bit.
  71. template LessEqThan(n) {
  72. signal input in[2];
  73. signal output out;
  74. component lt = LessThan(n);
  75. lt.in[0] <== in[0];
  76. lt.in[1] <== in[1]+1;
  77. lt.out ==> out;
  78. }
  79. // N is the number of bits the input have.
  80. // The MSF is the sign bit.
  81. template GreaterThan(n) {
  82. signal input in[2];
  83. signal output out;
  84. component lt = LessThan(n);
  85. lt.in[0] <== in[1];
  86. lt.in[1] <== in[0];
  87. lt.out ==> out;
  88. }
  89. // N is the number of bits the input have.
  90. // The MSF is the sign bit.
  91. template GreaterEqThan(n) {
  92. signal input in[2];
  93. signal output out;
  94. component lt = LessThan(n);
  95. lt.in[0] <== in[1];
  96. lt.in[1] <== in[0]+1;
  97. lt.out ==> out;
  98. }