Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arith SM #119

Draft
wants to merge 20 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/build
/proofs
*.pilout
/tmp
/tmp
*.log
57 changes: 29 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions book/getting_started/proof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
## Steps to verify constraints or generate proof

compile pils:
```
node ../pil2-compiler/src/pil.js pil/fork_0/pil/zisk.pil -I lib/std/pil -o pil/fork_0/pil/zisk.pilout
```

generate "structs" for different airs:
`(cd ../pil2-proofman; cargo run --bin proofman-cli pil-helpers --pilout ../zisk/pil/fork_0/pil/zisk.pilout --path ../zisk/pil/fork_0/src/ -o)`

prepare "fast tools" (only first time):
`(cd ../zkevm-prover && git switch develop_rust_lib && git submodule init && git submodule update && make -j bctree && make starks_lib -j)`

setup for pil, this step is necessary **only when pil change**:
`node ../pil2-proofman-js/src/main_setup.js -a pil/fork_0/pil/zisk.pilout -b build -t ../zkevm-prover/build/bctree`

this step should be done once and is optional. Edit file pil2-proofman/provers/starks-lib-c/Cargo.toml to remove "no_lib_link" from line 12:
`nano ../pil2-proofman/provers/starks-lib-c/Cargo.toml`

compile witness computation library (libzisk_witness.so). If you haven't nightly mode as default, must add +nightly when do build.
`cargo build --release`

In the following steps to verify constraints or generate prove, select one of these inputs:
- input.bin: large number of sha
- input_one_segment.bin: only one sha
- input_two_segments.bin: 512 shas

To **verify constraints** use:
`(cd ../pil2-proofman; cargo run --release --bin proofman-cli verify-constraints --witness-lib ../zisk/target/release/libzisk_witness.so --rom ../zisk/emulator/benches/data/my.elf -i ../zisk/emulator/benches/data/input.bin --proving-key ../zisk/build/provingKey)`

To **generate proof** use:
`(cd ../pil2-proofman; cargo run --release --bin proofman-cli verify-constraints --witness-lib ../zisk/target/release/libzisk_witness.so --rom ../zisk/emulator/benches/data/my.elf -i ../zisk/emulator/benches/data/input.bin --proving-key ../zisk/build/provingKey)`

## Steps to compile a verifiable rust program

### Setup
Install qemu:
`sudo apt-get install qemu-system`
Add tokens to access repos:
```
export GITHUB_ACCESS_TOKEN=....
export ZISK_TOKEN=....
```
### Create new hello_world project
Create project with toolchain:
```bash
cargo-zisk sdk new hello_world
cd hello_world
```

Compile and execute in **riskv mode**:
`cargo-zisk run --release`

Compile and execute in **zisk mode**:
`cargo-zisk run --release --sim`

Execute with ziskemu:
`ziskemu -i build/input.bin -x -e target/riscv64ima-polygon-ziskos-elf/release/fibonacci`

### Update toolchain
```
ziskup
```
If ziskup fails, could update ziskemu manually.

### Update ziskemu manually
```bash
cd zisk
git pull
cargo install --path emulator
cp ~/.cargo/bin/ziskemu ~/.zisk/bin/
```

```bash
ziskemu -i build/input.bin -x -e target/riscv64ima-polygon-ziskos-elf/debug/fibonacci
```
8 changes: 4 additions & 4 deletions cli/src/commands/install_toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ impl InstallToolchainCmd {
if let Ok(entry) = entry {
let entry_path = entry.path();
let entry_name = entry_path.file_name().unwrap();
if entry_path.is_dir() &&
entry_name != "bin" &&
entry_name != "circuits" &&
entry_name != "toolchains"
if entry_path.is_dir()
&& entry_name != "bin"
&& entry_name != "circuits"
&& entry_name != "toolchains"
{
if let Err(err) = fs::remove_dir_all(&entry_path) {
println!("Failed to remove directory {:?}: {}", entry_path, err);
Expand Down
6 changes: 3 additions & 3 deletions core/src/elf2rom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub fn elf2rom(elf_file: String) -> Result<ZiskRom, Box<dyn Error>> {
add_zisk_code(&mut rom, addr, &data);
}

if (section_header.sh_flags & SHF_WRITE as u64) != 0 &&
addr >= RAM_ADDR &&
addr + data.len() as u64 <= RAM_ADDR + RAM_SIZE
if (section_header.sh_flags & SHF_WRITE as u64) != 0
&& addr >= RAM_ADDR
&& addr + data.len() as u64 <= RAM_ADDR + RAM_SIZE
{
add_zisk_init_data(&mut rom, addr, &data);
} else {
Expand Down
26 changes: 13 additions & 13 deletions core/src/zisk_rom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,19 +374,19 @@ impl ZiskRom {
// (i.store_use_sp as u64) << 15;

// #[cfg(not(feature = "sp"))]
let rom_flags: u64 = ((i.a_src == SRC_IMM) as u64) |
((i.a_src == SRC_MEM) as u64) << 1 |
((i.b_src == SRC_IMM) as u64) << 2 |
((i.b_src == SRC_MEM) as u64) << 3 |
(i.store_ra as u64) << 4 |
((i.store == STORE_MEM) as u64) << 5 |
((i.store == STORE_IND) as u64) << 6 |
(i.set_pc as u64) << 7 |
(i.m32 as u64) << 8 |
(i.end as u64) << 9 |
(i.is_external_op as u64) << 10 |
((i.a_src == SRC_STEP) as u64) << 13 |
((i.b_src == SRC_IND) as u64) << 14;
let rom_flags: u64 = ((i.a_src == SRC_IMM) as u64)
| ((i.a_src == SRC_MEM) as u64) << 1
| ((i.b_src == SRC_IMM) as u64) << 2
| ((i.b_src == SRC_MEM) as u64) << 3
| (i.store_ra as u64) << 4
| ((i.store == STORE_MEM) as u64) << 5
| ((i.store == STORE_IND) as u64) << 6
| (i.set_pc as u64) << 7
| (i.m32 as u64) << 8
| (i.end as u64) << 9
| (i.is_external_op as u64) << 10
| ((i.a_src == SRC_STEP) as u64) << 13
| ((i.b_src == SRC_IND) as u64) << 14;

rom_flags
}
Expand Down
12 changes: 6 additions & 6 deletions emulator/src/emu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,9 @@ impl<'a> Emu<'a> {
}

// Log emulation step, if requested
if options.print_step.is_some() &&
(options.print_step.unwrap() != 0) &&
((self.ctx.inst_ctx.step % options.print_step.unwrap()) == 0)
if options.print_step.is_some()
&& (options.print_step.unwrap() != 0)
&& ((self.ctx.inst_ctx.step % options.print_step.unwrap()) == 0)
{
println!("step={}", self.ctx.inst_ctx.step);
}
Expand Down Expand Up @@ -515,9 +515,9 @@ impl<'a> Emu<'a> {
// Increment step counter
self.ctx.inst_ctx.step += 1;

if self.ctx.inst_ctx.end ||
((self.ctx.inst_ctx.step - self.ctx.last_callback_step) ==
self.ctx.callback_steps)
if self.ctx.inst_ctx.end
|| ((self.ctx.inst_ctx.step - self.ctx.last_callback_step)
== self.ctx.callback_steps)
{
// In run() we have checked the callback consistency with ctx.do_callback
let callback = callback.as_ref().unwrap();
Expand Down
12 changes: 6 additions & 6 deletions emulator/src/emu_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ impl fmt::Display for EmuOptions {

impl EmuOptions {
pub fn is_fast(&self) -> bool {
self.trace_steps.is_none() &&
self.print_step.is_none() &&
self.trace.is_none() &&
!self.log_step &&
!self.verbose &&
!self.tracerv
self.trace_steps.is_none()
&& self.print_step.is_none()
&& self.trace.is_none()
&& !self.log_step
&& !self.verbose
&& !self.tracerv
}
}
Loading