circuitcircomzkpzk-snark

How to pass function argument by reference in Circom?


How to pass function argument by reference in the circom circuit language?

I'm trying to do the following:

pragma circom 2.0.0;


function increment(foo) {
    foo++;
}

template MyTemplate() {
    signal input a;
    signal output b;

    var foo;
    foo = 0;

    increment(foo);
    log(foo);


    // ...
}

component main = MyTemplate();

I expect log(pos) to output 1, but I'm getting 0. Is there a certain way I need to pass pos into increment so that it can modify the variable by reference?


Solution

  • I decided to use the C preprocessor to generate circom code, so now I have:

    main.circom: 
        cpp -P maintpl.circom > main.circom
    

    in my Makefile

    and

    #define increment(foo) foo++
    

    in my circom code.