iden3 / circom

zkSnark circuit compiler
GNU General Public License v3.0
1.28k stars 244 forks source link

Definition and usage of new signal inside different if blocks fails in 2.1.5 #180

Closed Divide-By-0 closed 11 months ago

Divide-By-0 commented 1 year ago

If I have code like,

signal input x;
if(a){
  signal y <== x;
}
if(b){
  signal input offset;
  signal z;
  if(a) {
    signal z <== y + offset;
  } else {
    signal z <== x + offset;
  }
}

Then the compiler fails with something like this:

   |      signal z <== y + offset;
   |      ^^^^^^^^^^^^^^
   │                    ^ Using unknown symbol

Expected behavior would be to automatically recognize at compile time that that signal has been defined previously in an also defined if block. A pattern like above would be hard to represent without repeated code, unless we have this bugfix!

alrubio commented 11 months ago

Hi, thanks for your comment. Although we understand the point of your comment, it would be unconventional and counterintuitive to allow using a signal outside its syntactic scope (using some form dynamic scoping as, by instantiation, the conditional is removed), since we are applying static scoping in the rest of the language. We have clarified this point in the documentation https://github.com/iden3/circom/blob/master/mkdocs/docs/circom-language/scoping.md. Signals are only visible in the block they are defined and they can be defined in an inner block if it is below if statements with known conditions at compile time. Best, Albert