Skip to content

Commit

Permalink
Integrating pil2-proofman latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerTaule committed Dec 16, 2024
1 parent e160cbb commit 2098c65
Show file tree
Hide file tree
Showing 35 changed files with 254 additions and 432 deletions.
136 changes: 50 additions & 86 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ opt-level = 3
proofman-common = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "feature/minor_clean" }
proofman-macros = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "feature/minor_clean" }
proofman-util = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "feature/minor_clean" }
proofman = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "feature/minor_clean" }
pil-std-lib = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "feature/minor_clean" }
stark = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "feature/minor_clean" }
witness = { git = "https://github.com/0xPolygonHermez/pil2-proofman.git", branch = "feature/minor_clean" }
# 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" }
# witness = { path = "../pil2-proofman/witness" }

p3-field = { git = "https://github.com/Plonky3/Plonky3.git", rev = "c3d754ef77b9fce585b46b972af751fe6e7a9803" }
log = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion book/getting_started/quickstart_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Run this whenever the `.pilout` file changes:
### Generate Setup Data
Run this whenever the `.pilout` file changes:

```bash
```bash[]
node --max-old-space-size=65536 ../pil2-proofman-js/src/main_setup.js -a pil/zisk.pilout -b build -t ../pil2-proofman/pil2-stark/build/bctree
```

Expand Down
2 changes: 1 addition & 1 deletion executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ zisk-common = { path = "../common" }

proofman-common = { workspace = true }
proofman-util = { workspace = true }
proofman = { workspace = true }
witness = { workspace = true }
p3-field = { workspace=true }
pil-std-lib = { workspace = true }
log = { workspace = true }
Expand Down
216 changes: 111 additions & 105 deletions executor/src/executor.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
use p3_field::PrimeField;
use proofman::WitnessManager;
use proofman_common::ProofCtx;
use proofman_util::{timer_start_debug, timer_stop_and_log_debug};
use witness::WitnessComponent;

use rayon::prelude::*;

use sm_common::{CheckPoint, ComponentProvider, InstanceExpanderCtx, InstanceType, Plan};
use sm_main::{MainInstance, MainSM};

use std::{
fs,
path::{Path, PathBuf},
sync::Arc,
};
use std::{fs, path::PathBuf, sync::Arc};
use zisk_core::ZiskRom;
use zisk_pil::{MainTrace, MAIN_AIR_IDS, ZISK_AIRGROUP_ID};
use ziskemu::{EmuOptions, EmuTrace, ZiskEmulator};

use crate::MetricsProxy;

pub struct ZiskExecutor<F: PrimeField> {
/// Witness Manager
pub wcm: Arc<WitnessManager<F>>,
pub public_inputs_path: PathBuf,

/// ZisK ROM, a binary file that contains the ZisK program to be executed
pub zisk_rom: Arc<ZiskRom>,
Expand All @@ -36,108 +31,16 @@ pub struct ZiskExecutor<F: PrimeField> {
impl<F: PrimeField> ZiskExecutor<F> {
const NUM_THREADS: usize = 8;

pub fn new(wcm: Arc<WitnessManager<F>>, zisk_rom: Arc<ZiskRom>) -> Self {
let main_sm = MainSM::new(wcm.clone());
pub fn new(public_inputs_path: PathBuf, zisk_rom: Arc<ZiskRom>) -> Self {
let main_sm = MainSM::new();

Self { wcm, zisk_rom, main_sm, secondary_sm: Vec::new() }
Self { public_inputs_path, zisk_rom, main_sm, secondary_sm: Vec::new() }
}

pub fn register_sm(&mut self, sm: Arc<dyn ComponentProvider<F>>) {
self.secondary_sm.push(sm);
}

pub fn execute(&self, public_inputs_path: &Path, pctx: Arc<ProofCtx<F>>) {
// Call emulate with these options
let public_inputs = {
// Read inputs data from the provided inputs path
let path = PathBuf::from(public_inputs_path.display().to_string());
fs::read(path).expect("Could not read inputs file")
};

// PHASE 1. MINIMAL TRACES. Process the ROM super fast to collect the Minimal Traces
// ---------------------------------------------------------------------------------
let min_traces = self.compute_minimal_traces(public_inputs, Self::NUM_THREADS);
let min_traces = Arc::new(min_traces);

// =================================================================================
// PATH A Main SM instances
// =================================================================================

// PATH A PHASE 2. Count & Reduce the Minimal Traces to get the Plans
// ---------------------------------------------------------------------------------
let mut main_planning = self.create_main_plans(&min_traces);
let mut main_layouts = self.create_main_instances(&mut main_planning);

// PATH A PHASE 3. Expand the Minimal Traces to fill the Main Traces
// ---------------------------------------------------------------------------------
let main_task = {
let main_sm = self.main_sm.clone();
let zisk_rom = self.zisk_rom.clone();
let minimal_traces = min_traces.clone();

std::thread::spawn(move || {
main_layouts.par_iter_mut().for_each(|main_instance| {
main_sm.prove_main(&zisk_rom, &minimal_traces, main_instance);
});
main_layouts
})
};

// =================================================================================
// PATH B Secondary SM instances
// =================================================================================

// PATH B PHASE 2. Count & Reduce the Minimal Traces to get the Plans
// ---------------------------------------------------------------------------------
// Compute counters for each minimal trace
let mut plans = self.compute_plans(min_traces.clone());

// Create the buffer ta the distribution context
let mut sec_instances = Vec::new();
for (i, plans_by_sm) in plans.iter_mut().enumerate() {
for plan in plans_by_sm.drain(..) {
let (is_mine, global_idx) =
pctx.dctx_add_instance(plan.airgroup_id, plan.air_id, 1);

if is_mine || plan.instance_type == InstanceType::Table {
let iectx = InstanceExpanderCtx::new(global_idx, plan);

let instance = self.secondary_sm[i].get_instance(iectx);
sec_instances.push((global_idx, instance));
}
}
}

// PATH B PHASE 3. Expand the Minimal Traces to fill the Secondary SM Traces
// ---------------------------------------------------------------------------------
sec_instances.par_iter_mut().for_each(|(global_idx, sec_instance)| {
if sec_instance.instance_type() == InstanceType::Instance {
let _ = sec_instance.collect_inputs(&self.zisk_rom, &min_traces);
if let Some(air_instance) = sec_instance.compute_witness() {
let pctx = self.wcm.get_pctx();
pctx.air_instance_repo.add_air_instance(air_instance, Some(*global_idx));
}
}
});

sec_instances.par_iter_mut().for_each(|(global_idx, sec_instance)| {
if sec_instance.instance_type() == InstanceType::Table {
if let Some(air_instance) = sec_instance.compute_witness() {
let pctx = self.wcm.get_pctx();
pctx.air_instance_repo.add_air_instance(air_instance, Some(*global_idx));
}
}
});

// Drop memory
std::thread::spawn(move || {
drop(min_traces);
});

// Wait for the main task to finish
main_task.join().unwrap();
}

fn compute_plans(&self, min_traces: Arc<Vec<EmuTrace>>) -> Vec<Vec<Plan>> {
timer_start_debug!(PROCESS_OBSERVER);
let mut metrics_slices = min_traces
Expand Down Expand Up @@ -214,9 +117,12 @@ impl<F: PrimeField> ZiskExecutor<F> {
.collect()
}

fn create_main_instances(&self, main_planning: &mut Vec<Plan>) -> Vec<MainInstance<F>> {
fn create_main_instances(
&self,
pctx: Arc<ProofCtx<F>>,
main_planning: &mut Vec<Plan>,
) -> Vec<MainInstance<F>> {
let mut main_instances = Vec::new();
let pctx = self.wcm.get_pctx();

for plan in main_planning.drain(..) {
if let (true, global_idx) = pctx.dctx_add_instance(plan.airgroup_id, plan.air_id, 1) {
Expand All @@ -228,3 +134,103 @@ impl<F: PrimeField> ZiskExecutor<F> {
main_instances
}
}

impl<F: PrimeField> WitnessComponent<F> for ZiskExecutor<F> {
fn execute(&self, pctx: Arc<ProofCtx<F>>) {
// Call emulate with these options
let public_inputs = {
// Read inputs data from the provided inputs path
let path = PathBuf::from(self.public_inputs_path.display().to_string());
fs::read(path).expect("Could not read inputs file")
};

// PHASE 1. MINIMAL TRACES. Process the ROM super fast to collect the Minimal Traces
// ---------------------------------------------------------------------------------
let min_traces = self.compute_minimal_traces(public_inputs, Self::NUM_THREADS);
let min_traces = Arc::new(min_traces);

// =================================================================================
// PATH A Main SM instances
// =================================================================================

// PATH A PHASE 2. Count & Reduce the Minimal Traces to get the Plans
// ---------------------------------------------------------------------------------
let mut main_planning = self.create_main_plans(&min_traces);
let mut main_layouts = self.create_main_instances(pctx.clone(), &mut main_planning);

// PATH A PHASE 3. Expand the Minimal Traces to fill the Main Traces
// ---------------------------------------------------------------------------------
let pctx_clone = pctx.clone();
let main_task = {
let main_sm = self.main_sm.clone();
let zisk_rom = self.zisk_rom.clone();
let minimal_traces = min_traces.clone();
std::thread::spawn(move || {
main_layouts.par_iter_mut().for_each(|main_instance| {
main_sm.prove_main(
pctx_clone.clone(),
&zisk_rom,
&minimal_traces,
main_instance,
);
});
main_layouts
})
};

// =================================================================================
// PATH B Secondary SM instances
// =================================================================================

// PATH B PHASE 2. Count & Reduce the Minimal Traces to get the Plans
// ---------------------------------------------------------------------------------
// Compute counters for each minimal trace
let mut plans = self.compute_plans(min_traces.clone());

// Create the buffer ta the distribution context
let mut sec_instances = Vec::new();
let pctx_clone = pctx.clone();
for (i, plans_by_sm) in plans.iter_mut().enumerate() {
for plan in plans_by_sm.drain(..) {
let (is_mine, global_idx) =
pctx_clone.clone().dctx_add_instance(plan.airgroup_id, plan.air_id, 1);

if is_mine || plan.instance_type == InstanceType::Table {
let iectx = InstanceExpanderCtx::new(global_idx, plan);

let instance = self.secondary_sm[i].get_instance(iectx);
sec_instances.push((global_idx, instance));
}
}
}

// PATH B PHASE 3. Expand the Minimal Traces to fill the Secondary SM Traces
// ---------------------------------------------------------------------------------
sec_instances.par_iter_mut().for_each(|(global_idx, sec_instance)| {
if sec_instance.instance_type() == InstanceType::Instance {
let _ = sec_instance.collect_inputs(&self.zisk_rom, &min_traces);
if let Some(air_instance) = sec_instance.compute_witness(&pctx) {
pctx.clone()
.air_instance_repo
.add_air_instance(air_instance, Some(*global_idx));
}
}
});

sec_instances.par_iter_mut().for_each(|(global_idx, sec_instance)| {
if sec_instance.instance_type() == InstanceType::Table {
if let Some(air_instance) = sec_instance.compute_witness(&pctx) {
pctx.air_instance_repo.add_air_instance(air_instance, Some(*global_idx));
}
}
});

// Drop memory
std::thread::spawn(move || {
drop(min_traces);
});

// Wait for the main task to finish
main_task.join().unwrap();
}
}
3 changes: 1 addition & 2 deletions pil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ edition = "2021"
[dependencies]
proofman-common = { workspace = true }
proofman-macros = { workspace = true }
proofman = { workspace = true }

[features]
default = []
no_lib_link = ["proofman-common/no_lib_link", "proofman/no_lib_link"]
no_lib_link = ["proofman-common/no_lib_link"]
1 change: 0 additions & 1 deletion rom-merkle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
[dependencies]
sm-rom = { path = "../state-machines/rom" }
log = { workspace = true }
stark = { workspace = true }
proofman-common = { workspace = true }
zisk-core = { path = "../core" }

Expand Down
1 change: 0 additions & 1 deletion rom-merkle/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::{Arg, Command};
use colored::Colorize;
// use proofman_common::{GlobalInfo, ProofType, SetupCtx};
// use stark::StarkBufferAllocator;
use std::path::Path;
use sysinfo::System;
// use zisk_core::Riscv2zisk;
Expand Down
3 changes: 1 addition & 2 deletions state-machines/arith/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ p3-field = { workspace=true }
proofman-common = { workspace = true }
proofman-macros = { workspace = true }
proofman-util = { workspace = true }
proofman = { workspace = true }
pil-std-lib = { workspace = true }

log = { workspace = true }
Expand All @@ -25,4 +24,4 @@ num-bigint = { workspace = true }
[features]
default = []
generate_code_arith_range_table = []
no_lib_link = ["proofman-common/no_lib_link", "proofman/no_lib_link"]
no_lib_link = ["proofman-common/no_lib_link"]
Loading

0 comments on commit 2098c65

Please sign in to comment.