Search code examples
circuit-sdkzk-snarkcircom

Out signal based on two other our signals


I am trying to write a circuit to prove that a number is within a specified range. I am using the circomlib library: comparators.circuit file. My code is this :

template RangeProof(n) {
    assert(n <= 252);
    signal input in; // number to be proved 
    signal input range[2]; //  [lower bound, upper bound]
    signal output out;

    component low = LessEqThan(n);
    component high = GreaterEqThan(n);
    low.in[0] <== in;
    low.in[1] <== range[0];
    low.out === 1;
    high.in[0] <== in;
    high.in[1]<==range[1];
    high.out === 1;
    out <== (low.out + high.out) == 2 ? 1: 0; //this is the line in question
}

I want to return 1 if true and 0 if false. But that would depend on whether the other two out signals. How can I do this?


Solution

  • In your code, you can simply do out <== low.out * high.out.

    It achieves exactly what you want:

    low.out 0 * high.out 0 = out 0
    low.out 1 * high.out 0 = out 0
    low.out 0 * high.out 1 = out 0
    low.out 1 * high.out 1 = out 1
    

    In general though, if you want to assign to a signal based on an 'if statement' which branches into 2 branches, you need to use a Multiplexer. See Mux1 template from circomlib https://github.com/iden3/circomlib/blob/master/circuits/mux1.circom#L33