2

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?

Ilia Sidorenko
  • 2,157
  • 3
  • 26
  • 30

2 Answers2

4

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.

Ilia Sidorenko
  • 2,157
  • 3
  • 26
  • 30
-1

In Circom, constraints need to be determined during the constraint generation phase which means that the values in the conditions cannot be unknown. In your code, the condition x == 24 is causing an error because the value of x can be unknown during the constraint generation phase.

Here is an example of circuit that uses if else pattern:

template Example () {
    signal input in[100][2];
    signal input match;

    signal output num_match;
    signal output out[100][2];


    // Count the number of elements in 'in' whose first entry matches 'match'
    var n_match = 0;
    
    for (var i = 0; i < 100; i++) {
        if (in[i][0] == match) {
            n_match++;
        }
    }

    var outArray[100][2];

    // Copy the tuples with matching first entry to 'out' and 0-pad the remaining entries
    for (var i = 0; i < 100; i++) {
        if (in[i][0] == match) {
            outArray[i][0] = in[i][0];
            outArray[i][1] = 0;
        } else {
            outArray[i][0] = 0;
            outArray[i][1] = 0;
        }
    }

    num_match <-- n_match;
    out <-- outArray;
}
Pavel Fedotov
  • 748
  • 1
  • 7
  • 29