Zokrates / ZoKrates

A toolbox for zkSNARKs on Ethereum
https://zokrates.github.io
GNU Lesser General Public License v3.0
1.8k stars 360 forks source link

Return statement in for-loop causes compiler panic #1350

Open gasilano opened 11 months ago

gasilano commented 11 months ago

Description

When a return statement is used in a for-loop, the compiler would panic during the witness generation phase. It would be better if the compiler can throw an error message instead of panicking.

A minimal example is shown below:

def main() -> u32 {
    u32 mut res = 0;
    for u32 i in 0..2 {
        res = res + i;
        return res;
    }
    return res;
}

Environment

How to reproduce

Save the above code as for_return.zok and run the following command:

zokrates compile -i for_return.zok
zokrates setup
zokrates compute-witness

The compiler would throw an error message on the last step:

Compiling for_return.zok

Compiled code written to 'out'
Number of constraints: 2
Performing setup...
Verification key written to 'verification.key'
Proving key written to 'proving.key'
Setup completed
Computing witness...
The compiler unexpectedly panicked
panicked at 'Found an unsatisfied constraint without an attached error.', zokrates_interpreter/src/lib.rs:390:26
This is unexpected, please submit a full bug report at https://github.com/Zokrates/ZoKrates/issues
dark64 commented 7 months ago

Hey, it seems like our semantic checker missed this one – using return statements in the for-loop block shouldn't be allowed. That's causing some funky undefined behavior in the other steps.