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

aritmethic 32 + 64 bits #156

Open
wants to merge 36 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
b23f538
WIP arith
zkronos73 Sep 13, 2024
7b1c281
WIP updating pils and complete arith table
zkronos73 Sep 16, 2024
5cfffc9
Merge branch 'develop' into feature/update-pil-arith
zkronos73 Sep 16, 2024
1e2556b
wip aritmethic state machines
zkronos73 Sep 18, 2024
7149ae7
WIP arith witness comutation
zkronos73 Sep 18, 2024
ba17b71
Merge branch 'develop' into feature/update-pil-arith
zkronos73 Sep 27, 2024
d19018f
Merge branch 'develop' into feature/update-pil-arith
zkronos73 Oct 11, 2024
5eb8afb
update cargo files
zkronos73 Oct 14, 2024
5cbf947
WIP arith, fix errors after merge from develop
zkronos73 Oct 14, 2024
4f534bd
WIP arith: adding test of helpers
zkronos73 Oct 16, 2024
20f6f52
WIP arith
zkronos73 Oct 21, 2024
80af8ef
WIP arith
zkronos73 Oct 22, 2024
f0600c0
WIP arith tests
zkronos73 Oct 22, 2024
ce017b3
WIP, helpers test ok peding update pil
zkronos73 Oct 24, 2024
adf8e5b
WIP arith helpers pass bus tests
zkronos73 Oct 24, 2024
327846b
WIP update pil of arith_table, add method to calculate row of table
zkronos73 Oct 29, 2024
334a41a
WIP arith helper tests
zkronos73 Oct 30, 2024
54362c3
WIP arith - test ok
zkronos73 Nov 4, 2024
8766d06
arith, clean obsolete code
zkronos73 Nov 4, 2024
a50c886
add multiplicity on arith tables
zkronos73 Nov 4, 2024
41b08a0
merge branch 'develop' into feature/arith-pre-develop
zkronos73 Nov 4, 2024
4e53f1f
arith SM integration on develop
zkronos73 Nov 4, 2024
e9dc970
merge develop to feature/arith-pre-develop
zkronos73 Nov 5, 2024
7fbd91f
adding arith tables
zkronos73 Nov 6, 2024
23345b0
merge develop to arith-pre-develop
zkronos73 Nov 6, 2024
b0e6cf5
Merge branch 'develop' into feature/arith-pre-develop
zkronos73 Nov 6, 2024
50569a8
updated from develop, fix bug arith add debug traces
zkronos73 Nov 6, 2024
d4bb3f5
clean arith code and remove warnings
zkronos73 Nov 7, 2024
5eec9ee
fix bug and apply changes
zkronos73 Nov 7, 2024
6f0c19c
remove non used field
zkronos73 Nov 7, 2024
3001521
fix bug on arith pil
zkronos73 Nov 7, 2024
cead3bb
restore correct value on rom.rs
zkronos73 Nov 7, 2024
fb3c870
fix bug on arith, verify constraints ok
zkronos73 Nov 7, 2024
d256762
clean commented code
zkronos73 Nov 7, 2024
96d1050
merge changes from develop
zkronos73 Nov 7, 2024
d983ba0
WIP fix on arith table calculation
zkronos73 Nov 10, 2024
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
34 changes: 34 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,40 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "target/debug/proofman-cli",
//"cargo": {
"args": [
//"run",
//"--bin",
//"proofman-cli",
"verify-constraints",
"--witness-lib",
"../zisk/target/debug/libzisk_witness.so",
"--rom",
"../zisk/emulator/benches/data/my.elf",
"-i",
"../zisk/emulator/benches/data/input_two_segments.bin",
"--proving-key",
"../zisk/build/provingKey"
],
//"filter": {
// "name": "proofman_cli",
// "kind": "lib"
//}
//},
//"args": [],
"cwd": "${workspaceFolder}",
"environment": [
{ "name": "RUSTFLAGS", "value": "-L native=/home/zkronos73/devel/zisk2/pil2-proofman/pil2-stark/lib" }
],
"sourceLanguages": [
"rust"
]
},
{
"type": "lldb",
"request": "launch",
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

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

26 changes: 13 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ opt-level = 3
opt-level = 3

[workspace.dependencies]
proofman-common = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
proofman-macros = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
proofman-util = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
proofman = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
pil-std-lib = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
stark = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
#Local development
# proofman-common = { path = "../pil2-proofman/common" }
# proofman-macros = { path = "../pil2-proofman/macros" }
# proofman-util = { path = "../pil2-proofman/util" }
# proofman = { path = "../pil2-proofman/proofman" }
# pil-std-lib = { path = "../pil2-proofman/pil2-components/lib/std/rs" }
# stark = { path = "../pil2-proofman/provers/stark" }
# proofman-common = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
# proofman-macros = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
# proofman-util = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
# proofman = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
# pil-std-lib = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
# stark = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", rev = "0.0.10" }
# Local development
proofman-common = { path = "../pil2-proofman/common" }
proofman-macros = { path = "../pil2-proofman/macros" }
proofman-util = { path = "../pil2-proofman/util" }
proofman = { path = "../pil2-proofman/proofman" }
pil-std-lib = { path = "../pil2-proofman/pil2-components/lib/std/rs" }
stark = { path = "../pil2-proofman/provers/stark" }

p3-field = { git = "https://github.com/Plonky3/Plonky3.git", rev = "c3d754ef77b9fce585b46b972af751fe6e7a9803" }
log = "0.4"
Expand Down
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
28 changes: 14 additions & 14 deletions core/src/zisk_inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum ZiskOperationType {
PubOut,
}

pub const ZISK_OPERATION_TYPE_VARIANTS: usize = 6;
pub const ZISK_OPERATION_TYPE_VARIANTS: usize = 7;

/// ZisK instruction definition
///
Expand Down Expand Up @@ -167,19 +167,19 @@ impl ZiskInst {
}

pub fn get_flags(&self) -> u64 {
let flags: u64 = 1 |
(((self.a_src == SRC_IMM) as u64) << 1) |
(((self.a_src == SRC_MEM) as u64) << 2) |
(((self.a_src == SRC_STEP) as u64) << 3) |
(((self.b_src == SRC_IMM) as u64) << 4) |
(((self.b_src == SRC_MEM) as u64) << 5) |
((self.is_external_op as u64) << 6) |
((self.store_ra as u64) << 7) |
(((self.store == STORE_MEM) as u64) << 8) |
(((self.store == STORE_IND) as u64) << 9) |
((self.set_pc as u64) << 10) |
((self.m32 as u64) << 11) |
(((self.b_src == SRC_IND) as u64) << 12);
let flags: u64 = 1
| (((self.a_src == SRC_IMM) as u64) << 1)
| (((self.a_src == SRC_MEM) as u64) << 2)
| (((self.a_src == SRC_STEP) as u64) << 3)
| (((self.b_src == SRC_IMM) as u64) << 4)
| (((self.b_src == SRC_MEM) as u64) << 5)
| ((self.is_external_op as u64) << 6)
| ((self.store_ra as u64) << 7)
| (((self.store == STORE_MEM) as u64) << 8)
| (((self.store == STORE_IND) as u64) << 9)
| ((self.set_pc as u64) << 10)
| ((self.m32 as u64) << 11)
| (((self.b_src == SRC_IND) as u64) << 12);

flags
}
Expand Down
26 changes: 13 additions & 13 deletions core/src/zisk_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,19 @@ define_ops! {
(Or, "or", Binary, 77, 0x21, opc_or, op_or),
(Xor, "xor", Binary, 77, 0x22, opc_xor, op_xor),
(Mulu, "mulu", ArithAm32, 97, 0xb0, opc_mulu, op_mulu),
(Mul, "mul", ArithAm32, 97, 0xb1, opc_mul, op_mul),
(MulW, "mul_w", ArithAm32, 44, 0xb5, opc_mul_w, op_mul_w),
(Muluh, "muluh", ArithAm32, 97, 0xb8, opc_muluh, op_muluh),
(Mulh, "mulh", ArithAm32, 97, 0xb9, opc_mulh, op_mulh),
(Mulsuh, "mulsuh", ArithAm32, 97, 0xbb, opc_mulsuh, op_mulsuh),
(Divu, "divu", ArithAm32, 174, 0xc0, opc_divu, op_divu),
(Div, "div", ArithAm32, 174, 0xc1, opc_div, op_div),
(DivuW, "divu_w", ArithA32, 136, 0xc4, opc_divu_w, op_divu_w),
(DivW, "div_w", ArithA32, 136, 0xc5, opc_div_w, op_div_w),
(Remu, "remu", ArithAm32, 174, 0xc8, opc_remu, op_remu),
(Rem, "rem", ArithAm32, 174, 0xc9, opc_rem, op_rem),
(RemuW, "remu_w", ArithA32, 136, 0xcc, opc_remu_w, op_remu_w),
(RemW, "rem_w", ArithA32, 136, 0xcd, opc_rem_w, op_rem_w),
(Muluh, "muluh", ArithAm32, 97, 0xb1, opc_muluh, op_muluh),
(Mulsuh, "mulsuh", ArithAm32, 97, 0xb3, opc_mulsuh, op_mulsuh),
(Mul, "mul", ArithAm32, 97, 0xb4, opc_mul, op_mul),
(Mulh, "mulh", ArithAm32, 97, 0xb5, opc_mulh, op_mulh),
(MulW, "mul_w", ArithAm32, 44, 0xb6, opc_mul_w, op_mul_w),
(Divu, "divu", ArithAm32, 174, 0xb8, opc_divu, op_divu),
(Remu, "remu", ArithAm32, 174, 0xb9, opc_remu, op_remu),
(Div, "div", ArithAm32, 174, 0xba, opc_div, op_div),
(Rem, "rem", ArithAm32, 174, 0xbb, opc_rem, op_rem),
(DivuW, "divu_w", ArithA32, 136, 0xbc, opc_divu_w, op_divu_w),
(RemuW, "remu_w", ArithA32, 136, 0xbd, opc_remu_w, op_remu_w),
(DivW, "div_w", ArithA32, 136, 0xbe, opc_div_w, op_div_w),
(RemW, "rem_w", ArithA32, 136, 0xbf, opc_rem_w, op_rem_w),
(Minu, "minu", Binary, 77, 0x09, opc_minu, op_minu),
(Min, "min", Binary, 77, 0x0a, opc_min, op_min),
(MinuW, "minu_w", Binary, 77, 0x19, opc_minu_w, op_minu_w),
Expand Down
29 changes: 15 additions & 14 deletions emulator/src/emu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,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 @@ -520,9 +520,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 Expand Up @@ -699,11 +699,11 @@ impl<'a> Emu<'a> {

let mut current_box_id = 0;
let mut current_step_idx = loop {
if current_box_id == vec_traces.len() - 1 ||
vec_traces[current_box_id + 1].start_state.step >= emu_trace_start.step
if current_box_id == vec_traces.len() - 1
|| vec_traces[current_box_id + 1].start_state.step >= emu_trace_start.step
{
break emu_trace_start.step as usize -
vec_traces[current_box_id].start_state.step as usize;
break emu_trace_start.step as usize
- vec_traces[current_box_id].start_state.step as usize;
}
current_box_id += 1;
};
Expand Down Expand Up @@ -814,8 +814,8 @@ impl<'a> Emu<'a> {
let b = [inst_ctx.b & 0xFFFFFFFF, (inst_ctx.b >> 32) & 0xFFFFFFFF];
let c = [inst_ctx.c & 0xFFFFFFFF, (inst_ctx.c >> 32) & 0xFFFFFFFF];

let addr1 = (inst.b_offset_imm0 as i64 +
if inst.b_src == SRC_IND { inst_ctx.a as i64 } else { 0 }) as u64;
let addr1 = (inst.b_offset_imm0 as i64
+ if inst.b_src == SRC_IND { inst_ctx.a as i64 } else { 0 }) as u64;

let jmp_offset1 = if inst.jmp_offset1 >= 0 {
F::from_canonical_u64(inst.jmp_offset1 as u64)
Expand Down Expand Up @@ -893,8 +893,9 @@ impl<'a> Emu<'a> {
m32: F::from_bool(inst.m32),
addr1: F::from_canonical_u64(addr1),
__debug_operation_bus_enabled: F::from_bool(
inst.op_type == ZiskOperationType::Binary ||
inst.op_type == ZiskOperationType::BinaryE,
inst.op_type == ZiskOperationType::Arith
|| inst.op_type == ZiskOperationType::Binary
|| inst.op_type == ZiskOperationType::BinaryE,
),
}
}
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 @@ -92,11 +92,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
}
}
28 changes: 14 additions & 14 deletions emulator/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ impl Stats {
output += &format!(" COST_USUAL: {:02} sec\n", COST_USUAL);
output += &format!(" COST_STEP: {:02} sec\n", COST_STEP);

let total_mem_ops = self.mops.mread_na1 +
self.mops.mread_na2 +
self.mops.mread_a +
self.mops.mwrite_na1 +
self.mops.mwrite_na2 +
self.mops.mwrite_a;
let total_mem_align_steps = self.mops.mread_na1 +
self.mops.mread_na2 * 2 +
self.mops.mwrite_na1 * 2 +
self.mops.mwrite_na2 * 4;
let total_mem_ops = self.mops.mread_na1
+ self.mops.mread_na2
+ self.mops.mread_a
+ self.mops.mwrite_na1
+ self.mops.mwrite_na2
+ self.mops.mwrite_a;
let total_mem_align_steps = self.mops.mread_na1
+ self.mops.mread_na2 * 2
+ self.mops.mwrite_na1 * 2
+ self.mops.mwrite_na2 * 4;

let cost_mem = total_mem_ops as f64 * COST_MEM;
let cost_mem_align = self.mops.mread_na1 as f64 * COST_MEMA_R1 +
self.mops.mread_na2 as f64 * COST_MEMA_R2 +
self.mops.mwrite_na1 as f64 * COST_MEMA_W1 +
self.mops.mwrite_na2 as f64 * COST_MEMA_W2;
let cost_mem_align = self.mops.mread_na1 as f64 * COST_MEMA_R1
+ self.mops.mread_na2 as f64 * COST_MEMA_R2
+ self.mops.mwrite_na1 as f64 * COST_MEMA_W1
+ self.mops.mwrite_na2 as f64 * COST_MEMA_W2;

let mut total_opcodes: u64 = 0;
let mut opcode_steps: [u64; 256] = [0; 256];
Expand Down
Loading
Loading