using circom to count 0s in an array of input signal

19 views Asked by At

I tried to count the number of 0s in an array of input signal using circom.

template cnt(N) {
  signal input x[N];
  signal output y;
  var s[N];
  var sum = 0;
  for (var i = 0; i < N; i++) {
    s[i] = x[i] == 0 ? 1 : 0;
    sum += s[i];
  }
  y <== sum;
}

component main = cnt(5);

The compiler said Non quadratic constraints are not allowed! at y <== sum; But when I changed the above code from var to signal, the code compiled. I don't understand why changing a declaration from var to signal allows my code to compile. Can anyone explain the difference here?

template cnt(N) {
  signal input x[N];
  signal output y;
  signal s[N];
  var sum = 0;
  for (var i = 0; i < N; i++) {
    s[i] <-- x[i] == 0 ? 1 : 0;
    sum += s[i];
  }
  y <== sum;
}

component main = cnt(5);

Moreover, even the above code compiles but the number of constraints is 0. Does having zero constraints means there is nothing to prove? So should I use the following code, which has 10 non-linear constraints?

pragma circom 2.1.8;

include "comparators.circom";

template cnt(N) {
  signal input x[N];
  signal output y;
  var sum = 0;
  for (var i = 0; i < N; i++) {
    var v = IsZero()(x[i]);
    sum += v;
  }
  y <== sum;
}

component main = cnt(5);
0

There are 0 answers