简体   繁体   中英

How to run a loop with unknown number of iterations in Circom?

I have the following circuit in Circom cicuit compiler :

pragma circom 2.0.0;

template MAIN() {

    signal len;
    len <== 32;

    for (k = 0; k < maplen; k++) { 
        // do something
    }

}

component main = MAIN();

I'm getting an error:

error[T2005]: Typing error found
    ┌─ "/Users/ilia/compiling/main-circom/circuits/main.circom":118:17
    │
118 │     for (k = 0; k < len; k++) {
    │                 ^^^^^^^ There are constraints depending on the value of the condition and it can be unknown during the constraint generation phase

How do I write this loop in a way which makes it possible to iterate len times where len is a signal?

You need to decide how many maximum iterations your loop can have (an iteration budget) and then discard all the iterations you don't need using LessThan component. You can also select the needed result of a iteration using QuinSelector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM