Skip to content

Commit

Permalink
WIP witness computation (#49)
Browse files Browse the repository at this point in the history
Witness Computation Lib added
  • Loading branch information
xavi-pinsach authored Aug 7, 2024
1 parent 27466d7 commit 3419c00
Show file tree
Hide file tree
Showing 98 changed files with 3,097 additions and 960 deletions.
458 changes: 346 additions & 112 deletions Cargo.lock

Large diffs are not rendered by default.

28 changes: 26 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@
members = [
"cli",
"core",
"riscv",
"emulator",
"pil",
"riscv",
"state-machines/arith",
"state-machines/arith-32",
"state-machines/arith-64",
"state-machines/arith-3264",
"state-machines/binary",
"state-machines/binary-32",
"state-machines/binary-64",
"state-machines/binary-3264",
"state-machines/common",
"state-machines/freq-ops",
"state-machines/main",
"state-machines/mem",
"state-machines/mem-aligned",
"state-machines/mem-unaligned",
"witness-computation",
"ziskos/entrypoint",
"ziskos/entrypoint",
]

resolver = "2"
Expand All @@ -17,3 +30,14 @@ opt-level = 3

[profile.bench]
opt-level = 3

[workspace.dependencies]
proofman-common = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch ="witness" }
proofman = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch ="witness" }
proofman-util = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch ="witness" }
witness-helpers = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch ="witness" }
#Local development
# proofman-common = { path = "../pil2-proofman/common" }
# proofman = { path = "../pil2-proofman/proofman" }
# proofman-util = { path = "../pil2-proofman/util" }
# witness-helpers = { path = "../pil2-proofman/witness-helpers" }
13 changes: 6 additions & 7 deletions emulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ path = "src/lib.rs"
name = "ziskemu"
path = "src/bin/ziskemu.rs"

[dependencies]
zisk-core = { path = "../core" }
clap = { version = "4.5.9", features = ["derive", "env"] }
riscv = { path="../riscv" }
sysinfo = "0.31.2"

[build-dependencies]
vergen = { version = "8", default-features = false, features = [
"build",
Expand All @@ -22,13 +28,6 @@ vergen = { version = "8", default-features = false, features = [
criterion = { version = "0.5.1", features = ["html_reports"] }
pprof = { version = "0.13.0", features = ["flamegraph", "criterion"] }


[dependencies]
clap = { version = "4.5.9", features = ["derive", "env"] }
zisk-core = { path = "../core" }
riscv = { path = "../riscv" }
cpu-freq = "0.0.2"

[[bench]]
name = "benchmark"
harness = false
7 changes: 6 additions & 1 deletion emulator/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ fn bench_process_rom(c: &mut Criterion) {
};

b.iter(|| {
let _ = ZiskEmulator::process_rom(&mut rom, &input, &options, None);
let _ = ZiskEmulator::process_rom(
&mut rom,
&input,
&options,
None::<Box<dyn Fn(Vec<EmuTrace>)>>,
);
});
});

Expand Down
4 changes: 2 additions & 2 deletions emulator/src/bin/ziskemu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use std::{fmt::Write, process};
use ziskemu::{EmuOptions, Emulator, ZiskEmulator};
use ziskemu::{EmuOptions, EmuTrace, Emulator, ZiskEmulator};

fn main() {
// Create a emulator options instance based on arguments or default values
Expand All @@ -13,7 +13,7 @@ fn main() {

// Call emulate, with these options
let emulator = ZiskEmulator;
let result = emulator.emulate(&options, None);
let result = emulator.emulate(&options, None::<Box<dyn Fn(Vec<EmuTrace>)>>);

match result {
Ok(result) => {
Expand Down
10 changes: 7 additions & 3 deletions emulator/src/emu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<'a> Emu<'a> {

/// Performs one single step of the emulation
#[inline(always)]
pub fn step(&mut self, options: &EmuOptions, callback: &Option<Box<dyn Fn(Vec<EmuTrace>)>>) {
pub fn step(&mut self, options: &EmuOptions, callback: &Option<impl Fn(Vec<EmuTrace>)>) {
// Check if we are tracing steps to improve the execution
let tracing_steps = options.trace_steps.is_some();

Expand Down Expand Up @@ -213,7 +213,11 @@ impl<'a> Emu<'a> {
if self.ctx.end ||
((self.ctx.step - self.ctx.last_callback_step) == self.ctx.callback_steps)
{
let callback = callback.as_ref().unwrap();
if callback.is_none() {
panic!("Emu::step() found empty callback");
}
let callback = callback.as_ref().expect("Emu::step() found empty callback");

let emu_trace = mem::take(&mut self.ctx.emu_trace);
(callback)(emu_trace);
self.ctx.last_callback_step += self.ctx.callback_steps;
Expand Down Expand Up @@ -315,7 +319,7 @@ impl<'a> Emu<'a> {
&mut self,
inputs: Vec<u8>,
options: &EmuOptions,
callback: Option<Box<dyn Fn(Vec<EmuTrace>)>>,
callback: Option<impl Fn(Vec<EmuTrace>)>,
) {
// Context, where the state of the execution is stored and modified at every execution step
self.ctx = self.create_emu_context(inputs);
Expand Down
1 change: 1 addition & 0 deletions emulator/src/emu_trace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::MemTrace;

#[derive(Debug, Clone)]
pub struct EmuTrace {
pub opcode: u8,
pub a: u64,
Expand Down
24 changes: 14 additions & 10 deletions emulator/src/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use std::{
path::{Path, PathBuf},
time::Instant,
};
use sysinfo::System;
use zisk_core::{Riscv2zisk, ZiskInst, ZiskRom, ROM_ADDR, ROM_ADDR_MAX, ROM_ENTRY};

pub trait Emulator<ET> {
pub trait Emulator {
fn emulate(
&self,
options: &EmuOptions,
callback: Option<Box<dyn Fn(Vec<ET>)>>,
callback: Option<impl Fn(Vec<EmuTrace>)>,
) -> Result<Vec<u8>, ZiskEmulatorErr>;
}

Expand All @@ -29,7 +30,7 @@ impl ZiskEmulator {
let files = Self::list_files(&directory).unwrap();
for file in files {
if file.contains("dut") && file.ends_with(".elf") {
Self::process_elf_file(file, inputs, options, None)?;
Self::process_elf_file(file, inputs, options, None::<Box<dyn Fn(Vec<EmuTrace>)>>)?;
}
}

Expand All @@ -40,7 +41,7 @@ impl ZiskEmulator {
elf_filename: String,
inputs: &[u8],
options: &EmuOptions,
callback: Option<Box<dyn Fn(Vec<EmuTrace>)>>,
callback: Option<impl Fn(Vec<EmuTrace>)>,
) -> Result<Vec<u8>, ZiskEmulatorErr> {
if options.verbose {
println!("process_elf_file() elf_file={}", elf_filename);
Expand All @@ -63,7 +64,7 @@ impl ZiskEmulator {
rom_filename: String,
inputs: &[u8],
options: &EmuOptions,
callback: Option<Box<dyn Fn(Vec<EmuTrace>)>>,
callback: Option<impl Fn(Vec<EmuTrace>)>,
) -> Result<Vec<u8>, ZiskEmulatorErr> {
if options.verbose {
println!("process_rom_file() rom_file={}", rom_filename);
Expand All @@ -78,7 +79,7 @@ impl ZiskEmulator {
rom: &mut ZiskRom,
inputs: &[u8],
options: &EmuOptions,
callback: Option<Box<dyn Fn(Vec<EmuTrace>)>>,
callback: Option<impl Fn(Vec<EmuTrace>)>,
) -> Result<Vec<u8>, ZiskEmulatorErr> {
if options.verbose {
println!("process_rom() rom size={} inputs size={}", rom.insts.len(), inputs.len());
Expand Down Expand Up @@ -165,8 +166,11 @@ impl ZiskEmulator {
let secs = duration.as_secs_f64();
let steps = emu.number_of_steps();
let tp = steps as f64 / secs / 1_000_000.0;
let cpus = cpu_freq::get();
let cpu_frequency: f64 = cpus[0].max.unwrap() as f64;

let system = System::new_all();
let cpu = &system.cpus()[0];
let cpu_frequency = cpu.frequency() as f64;

let clocks_per_step = cpu_frequency / tp;
println!(
"process_rom() steps={} duration={:.4} tp={:.4} Msteps/s freq={:.4} {:.4} clocks/step",
Expand Down Expand Up @@ -220,11 +224,11 @@ impl ZiskEmulator {
}
}

impl Emulator<EmuTrace> for ZiskEmulator {
impl Emulator for ZiskEmulator {
fn emulate(
&self,
options: &EmuOptions,
callback: Option<Box<dyn Fn(Vec<EmuTrace>)>>,
callback: Option<impl Fn(Vec<EmuTrace>)>,
) -> Result<Vec<u8>, ZiskEmulatorErr> {
// Log this call
if options.verbose {
Expand Down
1 change: 1 addition & 0 deletions emulator/src/mem_trace.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Debug, Clone, Copy)]
pub struct MemTrace {
pub is_write: bool,
pub address: u64,
Expand Down
8 changes: 8 additions & 0 deletions pil/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "zisk-pil"
version = "0.1.0"
edition = "2021"

[dependencies]
proofman = { workspace = true }
proofman-common = { workspace = true }
5 changes: 5 additions & 0 deletions pil/pil/zisk.pil
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "../../state-machines/main/pil/main.pil"

airgroup Main {
Main(N: 2**21, RC: 1);
}
Binary file added pil/pil/zisk.pilout
Binary file not shown.
3 changes: 3 additions & 0 deletions pil/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod pil_helpers;

pub use pil_helpers::*;
7 changes: 7 additions & 0 deletions pil/src/pil_helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// WARNING: This file has been autogenerated from the PILOUT file.
// Manual modifications are not recommended and may be overwritten.
mod pilout;
mod traces;

pub use pilout::*;
pub use traces::*;
27 changes: 27 additions & 0 deletions pil/src/pil_helpers/pilout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// WARNING: This file has been autogenerated from the PILOUT file.
// Manual modifications are not recommended and may be overwritten.
use proofman_common::WitnessPilout;

pub const PILOUT_HASH: &[u8] = b"Zisk-hash";

//SUBPROOFS CONSTANTS

pub const MAIN_SUBPROOF_ID: &[usize] = &[0];

//AIR CONSTANTS

pub const MAIN_AIR_IDS: &[usize] = &[0];

pub struct Pilout;

impl Pilout {
pub fn pilout() -> WitnessPilout {
let mut pilout = WitnessPilout::new("Zisk", 2, PILOUT_HASH.to_vec());

let air_group = pilout.add_air_group(Some("Main"));

air_group.add_air(Some("Main"), 2097152);

pilout
}
}
7 changes: 7 additions & 0 deletions pil/src/pil_helpers/traces.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// WARNING: This file has been autogenerated from the PILOUT file.
// Manual modifications are not recommended and may be overwritten.
use proofman::trace;

trace!(Main0Trace<F> {
a: [F; 1], b: [F; 1], c: [F; 1], last_c: [F; 1], flag: F, pc: F, a_src_imm: F, a_src_mem: F, a_offset_imm0: F, sp: F, a_src_sp: F, a_use_sp_imm1: F, a_src_step: F, b_src_imm: F, b_src_mem: F, b_offset_imm0: F, b_use_sp_imm1: F, b_src_ind: F, ind_width: F, is_external_op: F, op: F, store_ra: F, store_mem: F, store_ind: F, store_offset: F, set_pc: F, store_use_sp: F, set_sp: F, inc_sp: F, jmp_offset1: F, jmp_offset2: F,
});
21 changes: 21 additions & 0 deletions state-machines/arith-32/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "sm-arith-32"
version = "0.1.0"
edition = "2021"

[dependencies]
zisk-core = { path = "../../core" }
sm-common = { path = "../common" }

# proofman-common = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch ="develop" }
# proofman = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch ="develop" }
# witness-helpers = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch ="develop" }
#Local development
proofman-common = { workspace = true }
proofman = { workspace = true }
witness-helpers = { workspace = true }

log = { version = "0.4", default-features = false }
p3-goldilocks = { git = "https://github.com/Plonky3/Plonky3.git", rev = "c3d754ef77b9fce585b46b972af751fe6e7a9803" }
p3-field = { git = "https://github.com/Plonky3/Plonky3.git", rev = "c3d754ef77b9fce585b46b972af751fe6e7a9803" }
rayon = "1.10.0"
File renamed without changes.
Loading

0 comments on commit 3419c00

Please sign in to comment.