diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6ea6acf2..cd77709c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -308,6 +308,10 @@ jobs: run: | sed -i 's/# *"pil2-components\/test\/std\/connection\/rs"/"pil2-components\/test\/std\/connection\/rs"/' ./Cargo.toml + - name: Modify Cargo.toml std diff_buses + run: | + sed -i 's/# *"pil2-components\/test\/std\/diff_buses\/rs"/"pil2-components\/test\/std\/diff_buses\/rs"/' ./Cargo.toml + - name: Modify Cargo.toml std direct_update run: | sed -i 's/# *"pil2-components\/test\/std\/direct_update\/rs"/"pil2-components\/test\/std\/direct_update\/rs"/' ./Cargo.toml @@ -368,6 +372,22 @@ jobs: run: | cargo run --bin proofman-cli pil-helpers --pilout ./pil2-components/test/std/connection/build/build.pilout --path ./pil2-components/test/std/connection/rs/src -o + - name: Create dir build diff_buses + run: | + mkdir -p ./pil2-components/test/std/diff_buses/build/ + + - name: Compile PIL diff_buses + run: | + node pil2-compiler/src/pil.js ./pil2-components/test/std/diff_buses/diff_buses.pil -I ./pil2-components/lib/std/pil -o ./pil2-components/test/std/diff_buses/build/build.pilout + + - name: Generate setup diff_buses + run: | + node pil2-proofman-js/src/main_setup.js -a ./pil2-components/test/std/diff_buses/build/build.pilout -b ./pil2-components/test/std/diff_buses/build -t pil2-stark/build/bctree + + - name: Generate PIL Helpers diff_buses + run: | + cargo run --bin proofman-cli pil-helpers --pilout ./pil2-components/test/std/diff_buses/build/build.pilout --path ./pil2-components/test/std/diff_buses/rs/src -o + - name: Create dir build direct_update run: | mkdir -p ./pil2-components/test/std/direct_update/build/ @@ -455,13 +475,25 @@ jobs: run: | node pil2-proofman-js/src/main_verify -k ./pil2-components/test/std/connection/build/provingKey -p ./pil2-components/test/std/connection/build + - name: Verify constraints diff_buses + run: | + cargo run --bin proofman-cli verify-constraints --witness-lib ./target/debug/libdiff_buses.so --proving-key ./pil2-components/test/std/diff_buses/build/provingKey + + - name: Generate proof diff_buses + run: | + cargo run --bin proofman-cli prove --witness-lib ./target/debug/libdiff_buses.so --proving-key ./pil2-components/test/std/diff_buses/build/provingKey --output-dir ./pil2-components/test/std/diff_buses/build + + - name: Verify proof diff_buses + run: | + node pil2-proofman-js/src/main_verify -k ./pil2-components/test/std/diff_buses/build/provingKey -p ./pil2-components/test/std/diff_buses/build + - name: Verify constraints direct_update run: | cargo run --bin proofman-cli verify-constraints --witness-lib ./target/debug/libdirect_update.so --proving-key ./pil2-components/test/std/direct_update/build/provingKey - name: Generate proof direct_update run: | - cargo run --bin proofman-cli prove --witness-lib ./target/debug/libdirect_update.so --proving-key ./pil2-components/test/std/direct_update/build/provingKey --output-dir ./pil2-components/test/std/connection/build + cargo run --bin proofman-cli prove --witness-lib ./target/debug/libdirect_update.so --proving-key ./pil2-components/test/std/direct_update/build/provingKey --output-dir ./pil2-components/test/std/direct_update/build - name: Verify proof direct_update run: | diff --git a/Cargo.toml b/Cargo.toml index afbcb225..18d7ff3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,12 +11,13 @@ members = [ "transcript", "util", "pil2-components/lib/std/rs", - #"pil2-components/test/std/direct_update/rs", - #"pil2-components/test/std/range_check/rs", - #"pil2-components/test/std/lookup/rs", - #"pil2-components/test/std/connection/rs", - #"pil2-components/test/std/permutation/rs", - #"pil2-components/test/simple/rs", + # "pil2-components/test/std/direct_update/rs", + # "pil2-components/test/std/diff_buses/rs", + # "pil2-components/test/std/range_check/rs", + # "pil2-components/test/std/lookup/rs", + # "pil2-components/test/std/connection/rs", + # "pil2-components/test/std/permutation/rs", + # "pil2-components/test/simple/rs", # whoever re-enables this, it has to work out of # the box with `cargo check --workspace` or CI will # break and dev experience will be bad since repo diff --git a/pil2-components/test/README.md b/pil2-components/test/README.md index 1fb8b3fd..01769075 100644 --- a/pil2-components/test/README.md +++ b/pil2-components/test/README.md @@ -50,6 +50,31 @@ mkdir -p ./pil2-components/test/std/connection/build/ \ --output-dir ./pil2-components/test/std/connection/build/proofs \ && node ../pil2-proofman-js/src/main_verify -k ./pil2-components/test/std/connection/build/provingKey -p ./pil2-components/test/std/connection/build/proofs +------------------------------------ +Diff Buses + +mkdir -p ./pil2-components/test/std/diff_buses/build/ \ +&& rm -rf pil2-components/test/diff_buses/build/proofs/ \ +&& node ../pil2-compiler/src/pil.js ./pil2-components/test/std/diff_buses/diff_buses.pil \ + -I ./pil2-components/lib/std/pil \ + -o ./pil2-components/test/std/diff_buses/build/diff_buses.pilout \ +&& node ../pil2-proofman-js/src/main_setup.js \ + -a ./pil2-components/test/std/diff_buses/build/diff_buses.pilout \ + -b ./pil2-components/test/std/diff_buses/build \ + -t ./pil2-stark/build/bctree \ +&& cargo run --bin proofman-cli pil-helpers \ + --pilout ./pil2-components/test/std/diff_buses/build/diff_buses.pilout \ + --path ./pil2-components/test/std/diff_buses/rs/src -o \ +&& cargo build \ +&& cargo run --bin proofman-cli verify-constraints \ + --witness-lib ./target/debug/libdiff_buses.so \ + --proving-key ./pil2-components/test/std/diff_buses/build/provingKey \ +&& cargo run --bin proofman-cli prove \ + --witness-lib ./target/debug/libdiff_buses.so \ + --proving-key ./pil2-components/test/std/diff_buses/build/provingKey \ + --output-dir ./pil2-components/test/std/diff_buses/build/proofs \ +&& node ../pil2-proofman-js/src/main_verify -k ./pil2-components/test/std/diff_buses/build/provingKey -p ./pil2-components/test/std/diff_buses/build/proofs + ------------------------------------ Direct Updates @@ -155,9 +180,6 @@ SPECIAL mkdir -p ./pil2-components/test/std/special/build/ \ && rm -rf pil2-components/test/special/build/proofs/ \ -&& node ../pil2-compiler/src/pil.js ./pil2-components/test/std/special/diff_buses.pil \ - -I ./pil2-components/lib/std/pil \ - -o ./pil2-components/test/std/special/build/diff_buses.pilout \ && node ../pil2-compiler/src/pil.js ./pil2-components/test/std/special/expr_optimizations.pil \ -I ./pil2-components/lib/std/pil \ -o ./pil2-components/test/std/special/build/expr_optimizations.pilout \ @@ -167,10 +189,6 @@ mkdir -p ./pil2-components/test/std/special/build/ \ && node ../pil2-compiler/src/pil.js ./pil2-components/test/std/special/table.pil \ -I ./pil2-components/lib/std/pil \ -o ./pil2-components/test/std/special/build/table.pilout \ -&& node ../pil2-proofman-js/src/main_setup.js \ - -a ./pil2-components/test/std/special/build/diff_buses.pilout \ - -b ./pil2-components/test/std/special/build \ - -t ./pil2-stark/build/bctree \ && node ../pil2-proofman-js/src/main_setup.js \ -a ./pil2-components/test/std/special/build/expr_optimizations.pilout \ -b ./pil2-components/test/std/special/build \ diff --git a/pil2-components/test/std/diff_buses/diff_buses.pil b/pil2-components/test/std/diff_buses/diff_buses.pil index ed4ff3b4..0e66edc1 100644 --- a/pil2-components/test/std/diff_buses/diff_buses.pil +++ b/pil2-components/test/std/diff_buses/diff_buses.pil @@ -29,5 +29,5 @@ airtemplate BothBuses(const int N = 2**4) { airgroup Buses { ProdBus(); SumBus(); - // BothBuses(); + BothBuses(); } \ No newline at end of file diff --git a/pil2-components/test/std/diff_buses/rs/src/both_buses.rs b/pil2-components/test/std/diff_buses/rs/src/both_buses.rs new file mode 100644 index 00000000..57639716 --- /dev/null +++ b/pil2-components/test/std/diff_buses/rs/src/both_buses.rs @@ -0,0 +1,74 @@ +use std::sync::Arc; + +use proofman::{WitnessComponent, WitnessManager}; +use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; + +use p3_field::PrimeField; +use rand::{distributions::Standard, prelude::Distribution, Rng}; + +use crate::{BothBusesTrace, BUSES_AIRGROUP_ID, BOTH_BUSES_AIR_IDS}; + +pub struct BothBuses { + _phantom: std::marker::PhantomData, +} + +impl BothBuses +where + Standard: Distribution, +{ + const MY_NAME: &'static str = "BothBuses"; + + pub fn new(wcm: Arc>) -> Arc { + let both_buses = Arc::new(Self { _phantom: std::marker::PhantomData }); + + wcm.register_component(both_buses.clone(), Some(BUSES_AIRGROUP_ID), Some(BOTH_BUSES_AIR_IDS)); + + both_buses + } + + pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { + let num_rows = pctx.global_info.airs[BUSES_AIRGROUP_ID][BOTH_BUSES_AIR_IDS[0]].num_rows; + let trace = BothBusesTrace::new(num_rows); + + let air_instance = + AirInstance::new(sctx.clone(), BUSES_AIRGROUP_ID, BOTH_BUSES_AIR_IDS[0], None, trace.buffer.unwrap()); + let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(BUSES_AIRGROUP_ID, BOTH_BUSES_AIR_IDS[0], 1); + if is_myne { + pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); + } + } +} + +impl WitnessComponent for BothBuses +where + Standard: Distribution, +{ + fn calculate_witness( + &self, + stage: u32, + air_instance_id: Option, + pctx: Arc>, + _ectx: Arc, + _sctx: Arc, + ) { + let mut rng = rand::thread_rng(); + log::debug!("{}: ยทยทยท Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "BothBuses", stage); + + if stage == 1 { + let air_instances_vec = &mut pctx.air_instance_repo.air_instances.write().unwrap(); + let air_instance = &mut air_instances_vec[air_instance_id.unwrap()]; + let buffer = &mut air_instance.trace; + let num_rows = pctx.pilout.get_air(BUSES_AIRGROUP_ID, BOTH_BUSES_AIR_IDS[0]).num_rows(); + + let mut trace = BothBusesTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + + for i in 0..num_rows { + trace[i].a = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + trace[i].b = trace[i].a; + + trace[i].c = F::from_canonical_usize(i); + trace[i].d = trace[i].c; + } + } + } +} diff --git a/pil2-components/test/std/diff_buses/rs/src/diff_buses_lib.rs b/pil2-components/test/std/diff_buses/rs/src/diff_buses_lib.rs index 02fb2dfa..383afeb5 100644 --- a/pil2-components/test/std/diff_buses/rs/src/diff_buses_lib.rs +++ b/pil2-components/test/std/diff_buses/rs/src/diff_buses_lib.rs @@ -8,12 +8,13 @@ use p3_field::PrimeField; use p3_goldilocks::Goldilocks; use rand::{distributions::Standard, prelude::Distribution}; -use crate::{Pilout, ProdBus, SumBus}; +use crate::{Pilout, ProdBus, BothBuses, SumBus}; pub struct DiffBusesWitness { pub wcm: Option>>, pub prod_bus: Option>>, pub sum_bus: Option>>, + pub both_buses: Option>>, pub std_lib: Option>>, } @@ -31,7 +32,7 @@ where Standard: Distribution, { pub fn new() -> Self { - DiffBusesWitness { wcm: None, prod_bus: None, sum_bus: None, std_lib: None } + DiffBusesWitness { wcm: None, prod_bus: None, sum_bus: None, both_buses: None, std_lib: None } } pub fn initialize(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { @@ -40,10 +41,12 @@ where let std_lib = Std::new(wcm.clone()); let prod_bus = ProdBus::new(wcm.clone()); let sum_bus = SumBus::new(wcm.clone()); + let both_buses = BothBuses::new(wcm.clone()); self.wcm = Some(wcm); self.prod_bus = Some(prod_bus); self.sum_bus = Some(sum_bus); + self.both_buses = Some(both_buses); self.std_lib = Some(std_lib); } } @@ -66,6 +69,7 @@ where // Execute those components that need to be executed self.prod_bus.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); self.sum_bus.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); + self.both_buses.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); } fn calculate_witness(&mut self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc) { diff --git a/pil2-components/test/std/diff_buses/rs/src/lib.rs b/pil2-components/test/std/diff_buses/rs/src/lib.rs index 8ad88349..36b2748a 100644 --- a/pil2-components/test/std/diff_buses/rs/src/lib.rs +++ b/pil2-components/test/std/diff_buses/rs/src/lib.rs @@ -1,9 +1,11 @@ mod prod_bus; mod sum_bus; +mod both_buses; mod diff_buses_lib; mod pil_helpers; pub use prod_bus::*; pub use sum_bus::*; +pub use both_buses::*; pub use diff_buses_lib::*; pub use pil_helpers::*;