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.

139 lines
2.7 KiB

6 years ago
5 years ago
5 years ago
5 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. signal input in[2];
  63. signal output out;
  64. component n2b = Num2Bits(n*2+1);
  65. n2b.in <== in[0]+ (1<<n) - in[1];
  66. out <== 1-n2b.out[n];
  67. }
  68. // N is the number of bits the input have.
  69. // The MSF is the sign bit.
  70. template LessEqThan(n) {
  71. signal input in[2];
  72. signal output out;
  73. component lt = LessThan(n);
  74. lt.in[0] <== in[0];
  75. lt.in[1] <== in[1]+1;
  76. lt.out ==> out;
  77. }
  78. // N is the number of bits the input have.
  79. // The MSF is the sign bit.
  80. template GreaterThan(n) {
  81. signal input in[2];
  82. signal output out;
  83. component lt = LessThan(n);
  84. lt.in[0] <== in[1];
  85. lt.in[1] <== in[0];
  86. lt.out ==> out;
  87. }
  88. // N is the number of bits the input have.
  89. // The MSF is the sign bit.
  90. template GreaterEqThan(n) {
  91. signal input in[2];
  92. signal output out;
  93. component lt = LessThan(n);
  94. lt.in[0] <== in[1];
  95. lt.in[1] <== in[0]+1;
  96. lt.out ==> out;
  97. }