scipr-lab / libsnark

C++ library for zkSNARKs
Other
1.81k stars 579 forks source link

Fix tinyram prelude, tinyram read from aux input. #90

Open tkerber opened 6 years ago

tkerber commented 6 years ago

When trying to get a simple example (attached) to function with the demo_ram_ppzksnark executable, I encountered a problem that the read opcode wasn't functioning as specified.

Two mistakes I noticed there were: a) the flag on read was being incorrectly set when read_not1 was != 1, while it should be when read_not1 is != 0 (this matches the r1cs constraints one read_not1, and further correctly sets the flag for tape 0 and not for tape 1). b) Fixing this resulted in values being consumed from tape 1 regardless of opcode, as the tape reading did not expicitly check for a read opcode.

Unrelated during my debugging of this, I found that the prelude generation of tinyram increments by log(w)/8. This is clearly a mistake, as w is already a size in bits.


The example I was using which did not work before: command: ``` $ libsnark/demo_ram_ppzksnark --assembly simple.s --processed_assembly simple.proc --architecture_params simple.params --computation_bounds simple.bounds --primary_input simple.input --auxiliary_input simple.input ``` `simple.s`: ```asm ; TinyRAM V=1.00 W=32 K=8 ; Simple test program. Read from the input tape, from the aux tape, and check ; if they're equal. ; begin prelude store.w 0, r0 mov r0, 2147483648 read r1, 0 cjmp 7 add r0, r0, 4 store.w r0, r1 jmp 2 store.w 2147483648, r0 ; end prelude load.w r0, 2147483648 ; The first word of the primary input tape should be here. (memory 0x8000 0000, this is 1<<(w-1)) read r1, 1 xor r0, r0, r1 ; r0 is 0 iff r0 = r1 answer r0 ```
`simple.proc` ``` store.w 1 0 0 0 mov 1 0 0 2147483648 read 1 0 0 1 cjmp 1 0 0 7 add 1 0 0 4 store.w 0 1 0 0 jmp 1 0 0 2 store.w 1 0 0 2147483648 load.w 1 0 0 2147483648 read 1 1 0 1 xor 0 0 0 1 answer 0 0 0 0 ```
`simple.params`: ``` 32 8 ``` `simple.bounds`: ``` 32 32 32 ``` `simple.input`: ``` 42 ```

P.S. Is there a public assember for TinyRAM? I had to write the processed assembly by hand, and while it'd be easy enough to write one, I'd prefer to avoid duplicating effort.