if-statementcircuitcircomzkpzk-snark

How to write a constraint that depends on a condition in Circom?


I have code of the following form in Circom circuit compiler

template DecodeUint(len) {
    signal input x;
    signal input pos;
    signal output value;
    signal output nextpos;

    component getV = GetV(len);

    if (x <= 23) {
        value <== x;
        pos ==> nextpos;
    }
    else if(x == 24) {
        value <== 255;
        pos + 1 ==> nextpos;
    }
}

I'm getting this error:

error[T2005]: Typing error found
   ┌─ "/Users/ilia/compiling/main-circom/main.circom":93:13
   │
93 │     else if(x == 24) {
   │             ^^^^^^^ There are constraints depending on the value of the condition and it can be unknown during the constraint generation phase

How do I rewrite the conditions in a form that can be generated into a circuit? Is there something like Quin Selector but for if conditions?


Solution

  • I used LessThan and IsEqual from circomlib which return either 1 or 0 depending on if it's true of false, then multiplied the output of those conditions by the return value and finally used CalculateTotal to sum the multiplied conditions together to get the "result" of all the if branches in an arithmetic way.