1

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?

Ilia Sidorenko
  • 2,157
  • 3
  • 26
  • 30
  • Not sure if the right stack exchange site, happy to close. – Ilia Sidorenko Jan 28 '22 at 10:05
  • 1
    https://crypto.stackexchange.com/ might be more appropriate for ZKP-related questions. The official docs only illustrate the usage of functions with return values and don't mention passing args by reference, so you could try to define a new variable that receives the result of the increment and then use it inside log(). – Alex Kuzmin Jan 28 '22 at 10:50
  • I want to return two results out of `increment` function in the future, so passing by reference is more appropriate rather than returning the result. I don't think circom allows me to return tuples either. I've looked into making a template, but those seems to be unwieldy. Is it okay to repost the question verbatim to crypto.stackexchange.com? – Ilia Sidorenko Jan 29 '22 at 01:00

1 Answers1

1

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.

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