From b413660b54b02fc9876ba53d7cfb5a1aff6cd20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Masip?= Date: Wed, 18 Dec 2024 17:57:11 +0000 Subject: [PATCH] Adding the direct_update and diff_buses tests --- .github/workflows/ci.yaml | 64 +++++++++ Cargo.toml | 12 +- .../std/rs/src/range_check/std_range_check.rs | 3 +- pil2-components/test/README.md | 64 +++++++-- .../{special => diff_buses}/diff_buses.pil | 15 +- .../test/std/diff_buses/rs/Cargo.toml | 25 ++++ .../test/std/diff_buses/rs/src/both_buses.rs | 74 ++++++++++ .../std/diff_buses/rs/src/diff_buses_lib.rs | 93 ++++++++++++ .../test/std/diff_buses/rs/src/lib.rs | 11 ++ .../test/std/diff_buses/rs/src/prod_bus.rs | 72 ++++++++++ .../test/std/diff_buses/rs/src/sum_bus.rs | 63 +++++++++ .../test/std/direct_update/direct_update.pil | 91 ++++++++++++ .../test/std/direct_update/rs/Cargo.toml | 25 ++++ .../direct_update/rs/src/direct_update_lib.rs | 104 ++++++++++++++ .../rs/src/direct_update_prod_global.rs | 126 +++++++++++++++++ .../rs/src/direct_update_prod_local.rs | 133 ++++++++++++++++++ .../rs/src/direct_update_sum_global.rs | 126 +++++++++++++++++ .../rs/src/direct_update_sum_local.rs | 133 ++++++++++++++++++ .../test/std/direct_update/rs/src/lib.rs | 13 ++ .../test/std/special/direct_update.pil | 73 ---------- 20 files changed, 1218 insertions(+), 102 deletions(-) rename pil2-components/test/std/{special => diff_buses}/diff_buses.pil (66%) create mode 100644 pil2-components/test/std/diff_buses/rs/Cargo.toml create mode 100644 pil2-components/test/std/diff_buses/rs/src/both_buses.rs create mode 100644 pil2-components/test/std/diff_buses/rs/src/diff_buses_lib.rs create mode 100644 pil2-components/test/std/diff_buses/rs/src/lib.rs create mode 100644 pil2-components/test/std/diff_buses/rs/src/prod_bus.rs create mode 100644 pil2-components/test/std/diff_buses/rs/src/sum_bus.rs create mode 100644 pil2-components/test/std/direct_update/direct_update.pil create mode 100644 pil2-components/test/std/direct_update/rs/Cargo.toml create mode 100644 pil2-components/test/std/direct_update/rs/src/direct_update_lib.rs create mode 100644 pil2-components/test/std/direct_update/rs/src/direct_update_prod_global.rs create mode 100644 pil2-components/test/std/direct_update/rs/src/direct_update_prod_local.rs create mode 100644 pil2-components/test/std/direct_update/rs/src/direct_update_sum_global.rs create mode 100644 pil2-components/test/std/direct_update/rs/src/direct_update_sum_local.rs create mode 100644 pil2-components/test/std/direct_update/rs/src/lib.rs delete mode 100644 pil2-components/test/std/special/direct_update.pil diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6ff32625..cd77709c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -308,6 +308,14 @@ 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 + - name: Modify Cargo.toml std range-check run: | sed -i 's/# *"pil2-components\/test\/std\/range_check\/rs"/"pil2-components\/test\/std\/range_check\/rs"/' ./Cargo.toml @@ -364,6 +372,38 @@ 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/ + + - name: Compile PIL direct_update + run: | + node pil2-compiler/src/pil.js ./pil2-components/test/std/direct_update/direct_update.pil -I ./pil2-components/lib/std/pil -o ./pil2-components/test/std/direct_update/build/build.pilout + + - name: Generate setup direct_update + run: | + node pil2-proofman-js/src/main_setup.js -a ./pil2-components/test/std/direct_update/build/build.pilout -b ./pil2-components/test/std/direct_update/build -t pil2-stark/build/bctree + + - name: Generate PIL Helpers direct_update + run: | + cargo run --bin proofman-cli pil-helpers --pilout ./pil2-components/test/std/direct_update/build/build.pilout --path ./pil2-components/test/std/direct_update/rs/src -o + - name: Create dir build range-check run: | mkdir -p ./pil2-components/test/std/range_check/build/ @@ -435,6 +475,30 @@ 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/direct_update/build + + - name: Verify proof direct_update + run: | + node pil2-proofman-js/src/main_verify -k ./pil2-components/test/std/direct_update/build/provingKey -p ./pil2-components/test/std/direct_update/build + - name: Verify constraints range-check run: | cargo run --bin proofman-cli verify-constraints --witness-lib ./target/debug/librange_check.so --proving-key ./pil2-components/test/std/range_check/build/provingKey diff --git a/Cargo.toml b/Cargo.toml index 3c798dc6..18d7ff3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,11 +11,13 @@ members = [ "transcript", "util", "pil2-components/lib/std/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/lib/std/rs/src/range_check/std_range_check.rs b/pil2-components/lib/std/rs/src/range_check/std_range_check.rs index d44e7abe..37277f78 100644 --- a/pil2-components/lib/std/rs/src/range_check/std_range_check.rs +++ b/pil2-components/lib/std/rs/src/range_check/std_range_check.rs @@ -12,7 +12,8 @@ use proofman_common::{ExecutionCtx, ProofCtx, SetupCtx, StdMode, ModeName}; use proofman_hints::{get_hint_field_constant, get_hint_ids_by_name, HintFieldOptions, HintFieldValue}; use crate::{ - extract_field_element_as_usize, get_global_hint_field_constant_as, get_hint_field_constant_as_field, AirComponent, Range, SpecifiedRanges, U16Air, U8Air + extract_field_element_as_usize, get_global_hint_field_constant_as, get_hint_field_constant_as_field, AirComponent, + Range, SpecifiedRanges, U16Air, U8Air, }; #[derive(Debug, Eq, Hash, PartialEq, Clone)] diff --git a/pil2-components/test/README.md b/pil2-components/test/README.md index 1f69ce42..01769075 100644 --- a/pil2-components/test/README.md +++ b/pil2-components/test/README.md @@ -50,6 +50,56 @@ 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 + +mkdir -p ./pil2-components/test/std/direct_update/build/ \ +&& rm -rf pil2-components/test/direct_update/build/proofs/ \ +&& node ../pil2-compiler/src/pil.js ./pil2-components/test/std/direct_update/direct_update.pil \ + -I ./pil2-components/lib/std/pil \ + -o ./pil2-components/test/std/direct_update/build/direct_update.pilout \ +&& node ../pil2-proofman-js/src/main_setup.js \ + -a ./pil2-components/test/std/direct_update/build/direct_update.pilout \ + -b ./pil2-components/test/std/direct_update/build \ + -t ./pil2-stark/build/bctree \ +&& cargo run --bin proofman-cli pil-helpers \ + --pilout ./pil2-components/test/std/direct_update/build/direct_update.pilout \ + --path ./pil2-components/test/std/direct_update/rs/src -o \ +&& cargo build \ +&& cargo run --bin proofman-cli verify-constraints \ + --witness-lib ./target/debug/libdirect_update.so \ + --proving-key ./pil2-components/test/std/direct_update/build/provingKey \ +&& 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/proofs \ +&& node ../pil2-proofman-js/src/main_verify -k ./pil2-components/test/std/direct_update/build/provingKey -p ./pil2-components/test/std/direct_update/build/proofs + ------------------------------------ LOOKUP @@ -130,12 +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/direct_update.pil \ - -I ./pil2-components/lib/std/pil \ - -o ./pil2-components/test/std/special/build/direct_update.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 \ @@ -145,14 +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/direct_update.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/special/diff_buses.pil b/pil2-components/test/std/diff_buses/diff_buses.pil similarity index 66% rename from pil2-components/test/std/special/diff_buses.pil rename to pil2-components/test/std/diff_buses/diff_buses.pil index 057d5a7e..0e66edc1 100644 --- a/pil2-components/test/std/special/diff_buses.pil +++ b/pil2-components/test/std/diff_buses/diff_buses.pil @@ -2,24 +2,21 @@ require "std_constants.pil"; require "std_permutation.pil"; require "std_lookup.pil"; -// One can use the following function to set the bus type for the entire std -// set_std_bus_type(PIOP_BUS_SUM); - -airtemplate Air1(const int N = 2**4) { +airtemplate ProdBus(const int N = 2**4) { col witness a,b; permutation_assumes(1, [a], bus_type: PIOP_BUS_PROD); permutation_proves(1, [b], bus_type: PIOP_BUS_PROD); } -airtemplate Air2(const int N = 2**5) { +airtemplate SumBus(const int N = 2**5) { col witness a,b; lookup_assumes(2, [a]); lookup_proves(2, [b]); } -airtemplate Air3(const int N = 2**4) { +airtemplate BothBuses(const int N = 2**4) { col witness a,b,c,d; permutation_assumes(3, [a], bus_type: PIOP_BUS_PROD); @@ -30,7 +27,7 @@ airtemplate Air3(const int N = 2**4) { } airgroup Buses { - Air1(); - Air2(); - Air3(); + ProdBus(); + SumBus(); + BothBuses(); } \ No newline at end of file diff --git a/pil2-components/test/std/diff_buses/rs/Cargo.toml b/pil2-components/test/std/diff_buses/rs/Cargo.toml new file mode 100644 index 00000000..35ec87cf --- /dev/null +++ b/pil2-components/test/std/diff_buses/rs/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "diff-buses" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["dylib"] + +[dependencies] +proofman.workspace = true +proofman-common.workspace = true +proofman-macros.workspace = true +proofman-cli.workspace = true +pil-std-lib = { path = "../../../../lib/std/rs" } + +env_logger.workspace = true +log.workspace = true +num-traits = "0.2" +rand.workspace = true +num-bigint.workspace = true +p3-goldilocks.workspace = true +p3-field.workspace = true + +[build-dependencies] +proofman-cli.workspace = true 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 new file mode 100644 index 00000000..383afeb5 --- /dev/null +++ b/pil2-components/test/std/diff_buses/rs/src/diff_buses_lib.rs @@ -0,0 +1,93 @@ +use std::{error::Error, path::PathBuf, sync::Arc}; + +use pil_std_lib::Std; +use proofman::{WitnessLibrary, WitnessManager}; +use proofman_common::{initialize_logger, ExecutionCtx, ProofCtx, SetupCtx, VerboseMode, WitnessPilout}; + +use p3_field::PrimeField; +use p3_goldilocks::Goldilocks; +use rand::{distributions::Standard, prelude::Distribution}; + +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>>, +} + +impl Default for DiffBusesWitness +where + Standard: Distribution, +{ + fn default() -> Self { + Self::new() + } +} + +impl DiffBusesWitness +where + Standard: Distribution, +{ + pub fn new() -> Self { + 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) { + let wcm = Arc::new(WitnessManager::new(pctx, ectx, sctx)); + + 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); + } +} + +impl WitnessLibrary for DiffBusesWitness +where + Standard: Distribution, +{ + fn start_proof(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { + self.initialize(pctx.clone(), ectx.clone(), sctx.clone()); + + self.wcm.as_ref().unwrap().start_proof(pctx, ectx, sctx); + } + + fn end_proof(&mut self) { + self.wcm.as_ref().unwrap().end_proof(); + } + + fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { + // 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) { + self.wcm.as_ref().unwrap().calculate_witness(stage, pctx, ectx, sctx); + } + + fn pilout(&self) -> WitnessPilout { + Pilout::pilout() + } +} + +#[no_mangle] +pub extern "Rust" fn init_library( + _: Option, + _: Option, + verbose_mode: VerboseMode, +) -> Result>, Box> { + initialize_logger(verbose_mode); + let diff_buses_witness = DiffBusesWitness::new(); + Ok(Box::new(diff_buses_witness)) +} diff --git a/pil2-components/test/std/diff_buses/rs/src/lib.rs b/pil2-components/test/std/diff_buses/rs/src/lib.rs new file mode 100644 index 00000000..36b2748a --- /dev/null +++ b/pil2-components/test/std/diff_buses/rs/src/lib.rs @@ -0,0 +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::*; diff --git a/pil2-components/test/std/diff_buses/rs/src/prod_bus.rs b/pil2-components/test/std/diff_buses/rs/src/prod_bus.rs new file mode 100644 index 00000000..8ca508fa --- /dev/null +++ b/pil2-components/test/std/diff_buses/rs/src/prod_bus.rs @@ -0,0 +1,72 @@ +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::{ProdBusTrace, BUSES_AIRGROUP_ID, PROD_BUS_AIR_IDS}; + +pub struct ProdBus { + _phantom: std::marker::PhantomData, +} + +impl ProdBus +where + Standard: Distribution, +{ + const MY_NAME: &'static str = "ProdBus"; + + pub fn new(wcm: Arc>) -> Arc { + let prod_bus = Arc::new(Self { _phantom: std::marker::PhantomData }); + + wcm.register_component(prod_bus.clone(), Some(BUSES_AIRGROUP_ID), Some(PROD_BUS_AIR_IDS)); + + prod_bus + } + + pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { + let num_rows = pctx.global_info.airs[BUSES_AIRGROUP_ID][PROD_BUS_AIR_IDS[0]].num_rows; + let trace = ProdBusTrace::new(num_rows); + + let air_instance = + AirInstance::new(sctx.clone(), BUSES_AIRGROUP_ID, PROD_BUS_AIR_IDS[0], None, trace.buffer.unwrap()); + let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(BUSES_AIRGROUP_ID, PROD_BUS_AIR_IDS[0], 1); + if is_myne { + pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); + } + } +} + +impl WitnessComponent for ProdBus +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, "ProdBus", 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, PROD_BUS_AIR_IDS[0]).num_rows(); + + let mut trace = ProdBusTrace::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; + } + } + } +} diff --git a/pil2-components/test/std/diff_buses/rs/src/sum_bus.rs b/pil2-components/test/std/diff_buses/rs/src/sum_bus.rs new file mode 100644 index 00000000..8c8a4f29 --- /dev/null +++ b/pil2-components/test/std/diff_buses/rs/src/sum_bus.rs @@ -0,0 +1,63 @@ +use std::sync::Arc; + +use proofman::{WitnessComponent, WitnessManager}; +use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; + +use p3_field::PrimeField; + +use crate::{SumBusTrace, BUSES_AIRGROUP_ID, SUM_BUS_AIR_IDS}; + +pub struct SumBus { + _phantom: std::marker::PhantomData, +} + +impl SumBus { + const MY_NAME: &'static str = "SumBus"; + + pub fn new(wcm: Arc>) -> Arc { + let sum_bus = Arc::new(Self { _phantom: std::marker::PhantomData }); + + wcm.register_component(sum_bus.clone(), Some(BUSES_AIRGROUP_ID), Some(SUM_BUS_AIR_IDS)); + + sum_bus + } + + pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { + let num_rows = pctx.global_info.airs[BUSES_AIRGROUP_ID][SUM_BUS_AIR_IDS[0]].num_rows; + let trace = SumBusTrace::new(num_rows); + + let air_instance = + AirInstance::new(sctx.clone(), BUSES_AIRGROUP_ID, SUM_BUS_AIR_IDS[0], None, trace.buffer.unwrap()); + let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance(BUSES_AIRGROUP_ID, SUM_BUS_AIR_IDS[0], 1); + if is_myne { + pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); + } + } +} + +impl WitnessComponent for SumBus { + fn calculate_witness( + &self, + stage: u32, + air_instance_id: Option, + pctx: Arc>, + _ectx: Arc, + _sctx: Arc, + ) { + log::debug!("{}: ··· Witness computation for AIR '{}' at stage {}", Self::MY_NAME, "SumBus", 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, SUM_BUS_AIR_IDS[0]).num_rows(); + + let mut trace = SumBusTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + + for i in 0..num_rows { + trace[i].a = F::from_canonical_usize(i); + trace[i].b = trace[i].a; + } + } + } +} diff --git a/pil2-components/test/std/direct_update/direct_update.pil b/pil2-components/test/std/direct_update/direct_update.pil new file mode 100644 index 00000000..b699364a --- /dev/null +++ b/pil2-components/test/std/direct_update/direct_update.pil @@ -0,0 +1,91 @@ +require "std_permutation.pil"; +require "std_lookup.pil"; +require "std_range_check.pil"; +require "std_direct.pil"; + +const int OP_BUS_ID1 = 100; +const int OPID1 = 333; + +public a_public[2]; +proofval b_proofval_0; // FIX: Cannot set prooval defined as an array +proofval b_proofval_1; + +airtemplate DirectUpdateProdLocal(const int N = 2**4) { + col witness a[2],b[2],c[2]; + col witness perform_operation; + + airval c_airval[2]; + airval perform_direct_update[2]; + + direct_update_assumes(OP_BUS_ID1, [OPID1, ...a_public, b_proofval_0, b_proofval_1, ...c_airval], sel: perform_direct_update[0], bus_type: PIOP_BUS_PROD); + direct_update_assumes(OP_BUS_ID1, [OPID1, ...a_public, b_proofval_0, b_proofval_1, ...c_airval], sel: perform_direct_update[1], bus_type: PIOP_BUS_PROD); + + permutation_proves(OP_BUS_ID1, [OPID1, ...a, ...b, ...c], sel: perform_operation, bus_type: PIOP_BUS_PROD); +} + +const int OP_BUS_ID2 = 200; +const int OPID2 = 444; + +airtemplate DirectUpdateProdGlobal(const int N = 2**4) { + col witness c[2],d[2]; + col witness perform_operation; + + permutation_assumes(OP_BUS_ID2, [OPID2, ...c, ...d], sel: perform_operation, bus_type: PIOP_BUS_PROD); +} + +airgroup DirectUpdateProd { + DirectUpdateProdLocal(); + + DirectUpdateProdGlobal(); + + public c_public[2]; + proofval d_proofval_0; + proofval d_proofval_1; + proofval perform_global_update_0; + proofval perform_global_update_1; + direct_global_update_proves(OP_BUS_ID2, [OPID2, ...c_public, d_proofval_0, d_proofval_1], sel: perform_global_update_0, bus_type: PIOP_BUS_PROD); + direct_global_update_proves(OP_BUS_ID2, [OPID2, ...c_public, d_proofval_0, d_proofval_1], sel: perform_global_update_1, bus_type: PIOP_BUS_PROD); +} + +const int OP_BUS_ID3 = 300; +const int OPID3 = 555; + +public a_public_s[2]; +proofval b_proofval_0_s; +proofval b_proofval_1_s; + +airtemplate DirectUpdateSumLocal(const int N = 2**5) { + col witness a[2],b[2],c[2]; + col witness perform_operation; + + airval c_airval[2]; + airval perform_direct_update[2]; + + lookup_assumes(OP_BUS_ID3, [OPID3, ...a, ...b, ...c], sel: perform_operation); + + direct_update_proves(OP_BUS_ID3, [OPID3, ...a_public_s, b_proofval_0_s, b_proofval_1_s, ...c_airval], sel: perform_direct_update[0]); + direct_update_proves(OP_BUS_ID3, [OPID3, ...a_public_s, b_proofval_0_s, b_proofval_1_s, ...c_airval], sel: perform_direct_update[1]); +} + +const int OP_BUS_ID4 = 400; +const int OPID4 = 555; + +airtemplate DirectUpdateSumGlobal(const int N = 2**5) { + col witness c[2],d[2]; + col witness perform_operation; + lookup_proves(OP_BUS_ID4, [OPID4, ...c, ...d], mul: perform_operation); +} + +airgroup DirectUpdateSum { + DirectUpdateSumLocal(); + + public c_public_s[2]; + proofval d_proofval_0_s; + proofval d_proofval_1_s; + proofval perform_global_update_0_s; + proofval perform_global_update_1_s; + direct_global_update_assumes(OP_BUS_ID4, [OPID4, ...c_public_s, d_proofval_0_s, d_proofval_1_s], sel: perform_global_update_0_s); + direct_global_update_assumes(OP_BUS_ID4, [OPID4, ...c_public_s, d_proofval_0_s, d_proofval_1_s], sel: perform_global_update_1_s); + + DirectUpdateSumGlobal(); +} \ No newline at end of file diff --git a/pil2-components/test/std/direct_update/rs/Cargo.toml b/pil2-components/test/std/direct_update/rs/Cargo.toml new file mode 100644 index 00000000..df3dd148 --- /dev/null +++ b/pil2-components/test/std/direct_update/rs/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "direct-update" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["dylib"] + +[dependencies] +proofman.workspace = true +proofman-common.workspace = true +proofman-macros.workspace = true +proofman-cli.workspace = true +pil-std-lib = { path = "../../../../lib/std/rs" } + +env_logger.workspace = true +log.workspace = true +num-traits = "0.2" +rand.workspace = true +num-bigint.workspace = true +p3-goldilocks.workspace = true +p3-field.workspace = true + +[build-dependencies] +proofman-cli.workspace = true diff --git a/pil2-components/test/std/direct_update/rs/src/direct_update_lib.rs b/pil2-components/test/std/direct_update/rs/src/direct_update_lib.rs new file mode 100644 index 00000000..eba69b3e --- /dev/null +++ b/pil2-components/test/std/direct_update/rs/src/direct_update_lib.rs @@ -0,0 +1,104 @@ +use std::{error::Error, path::PathBuf, sync::Arc}; + +use pil_std_lib::Std; +use proofman::{WitnessLibrary, WitnessManager}; +use proofman_common::{initialize_logger, ExecutionCtx, ProofCtx, SetupCtx, VerboseMode, WitnessPilout}; + +use p3_field::PrimeField; +use p3_goldilocks::Goldilocks; +use rand::{distributions::Standard, prelude::Distribution}; + +use crate::{DirectUpdateProdLocal, DirectUpdateProdGlobal, DirectUpdateSumLocal, DirectUpdateSumGlobal, Pilout}; + +pub struct DirectUpdateWitness { + pub wcm: Option>>, + pub direct_update_prod_local: Option>>, + pub direct_update_prod_global: Option>>, + pub direct_update_sum_local: Option>>, + pub direct_update_sum_global: Option>>, + pub std_lib: Option>>, +} + +impl Default for DirectUpdateWitness +where + Standard: Distribution, +{ + fn default() -> Self { + Self::new() + } +} + +impl DirectUpdateWitness +where + Standard: Distribution, +{ + pub fn new() -> Self { + DirectUpdateWitness { + wcm: None, + direct_update_prod_local: None, + direct_update_prod_global: None, + direct_update_sum_local: None, + direct_update_sum_global: None, + std_lib: None, + } + } + + pub fn initialize(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { + let wcm = Arc::new(WitnessManager::new(pctx, ectx, sctx)); + + let std_lib = Std::new(wcm.clone()); + let direct_update_prod_local = DirectUpdateProdLocal::new(wcm.clone()); + let direct_update_prod_global = DirectUpdateProdGlobal::new(wcm.clone()); + let direct_update_sum_local = DirectUpdateSumLocal::new(wcm.clone()); + let direct_update_sum_global = DirectUpdateSumGlobal::new(wcm.clone()); + + self.wcm = Some(wcm); + self.direct_update_prod_local = Some(direct_update_prod_local); + self.direct_update_prod_global = Some(direct_update_prod_global); + self.direct_update_sum_local = Some(direct_update_sum_local); + self.direct_update_sum_global = Some(direct_update_sum_global); + self.std_lib = Some(std_lib); + } +} + +impl WitnessLibrary for DirectUpdateWitness +where + Standard: Distribution, +{ + fn start_proof(&mut self, pctx: Arc>, ectx: Arc, sctx: Arc) { + self.initialize(pctx.clone(), ectx.clone(), sctx.clone()); + + self.wcm.as_ref().unwrap().start_proof(pctx, ectx, sctx); + } + + fn end_proof(&mut self) { + self.wcm.as_ref().unwrap().end_proof(); + } + + fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { + // Execute those components that need to be executed + self.direct_update_prod_local.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); + self.direct_update_prod_global.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); + self.direct_update_sum_local.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); + self.direct_update_sum_global.as_ref().unwrap().execute(pctx.clone(), ectx.clone(), sctx.clone()); + } + + fn calculate_witness(&mut self, stage: u32, pctx: Arc>, ectx: Arc, sctx: Arc) { + self.wcm.as_ref().unwrap().calculate_witness(stage, pctx, ectx, sctx); + } + + fn pilout(&self) -> WitnessPilout { + Pilout::pilout() + } +} + +#[no_mangle] +pub extern "Rust" fn init_library( + _: Option, + _: Option, + verbose_mode: VerboseMode, +) -> Result>, Box> { + initialize_logger(verbose_mode); + let direct_update_witness = DirectUpdateWitness::new(); + Ok(Box::new(direct_update_witness)) +} diff --git a/pil2-components/test/std/direct_update/rs/src/direct_update_prod_global.rs b/pil2-components/test/std/direct_update/rs/src/direct_update_prod_global.rs new file mode 100644 index 00000000..09db4983 --- /dev/null +++ b/pil2-components/test/std/direct_update/rs/src/direct_update_prod_global.rs @@ -0,0 +1,126 @@ +use std::sync::Arc; + +use proofman::{WitnessComponent, WitnessManager}; +use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; + +use p3_field::PrimeField; +use num_traits::ToPrimitive; +use rand::{distributions::Standard, prelude::Distribution, Rng}; + +use crate::{DirectUpdateProdGlobalTrace, DIRECT_UPDATE_PROD_AIRGROUP_ID, DIRECT_UPDATE_PROD_GLOBAL_AIR_IDS}; + +pub struct DirectUpdateProdGlobal { + _phantom: std::marker::PhantomData, +} + +impl DirectUpdateProdGlobal +where + Standard: Distribution, +{ + const MY_NAME: &'static str = "DUPG"; + + pub fn new(wcm: Arc>) -> Arc { + let direct_update_prod_global = Arc::new(Self { _phantom: std::marker::PhantomData }); + + wcm.register_component( + direct_update_prod_global.clone(), + Some(DIRECT_UPDATE_PROD_AIRGROUP_ID), + Some(DIRECT_UPDATE_PROD_GLOBAL_AIR_IDS), + ); + + direct_update_prod_global + } + + pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { + let num_rows = + pctx.global_info.airs[DIRECT_UPDATE_PROD_AIRGROUP_ID][DIRECT_UPDATE_PROD_GLOBAL_AIR_IDS[0]].num_rows; + let trace = DirectUpdateProdGlobalTrace::new(num_rows); + + let air_instance = AirInstance::new( + sctx.clone(), + DIRECT_UPDATE_PROD_AIRGROUP_ID, + DIRECT_UPDATE_PROD_GLOBAL_AIR_IDS[0], + None, + trace.buffer.unwrap(), + ); + let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance( + DIRECT_UPDATE_PROD_AIRGROUP_ID, + DIRECT_UPDATE_PROD_GLOBAL_AIR_IDS[0], + 1, + ); + if is_myne { + pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); + } + } +} + +impl WitnessComponent for DirectUpdateProdGlobal +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, + "DirectUpdateProdGlobal", + 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(DIRECT_UPDATE_PROD_AIRGROUP_ID, DIRECT_UPDATE_PROD_GLOBAL_AIR_IDS[0]).num_rows(); + + let mut trace = DirectUpdateProdGlobalTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + + let chosen_index = rng.gen_range(0..=num_rows - 1); + let mut values: [F; 4] = [F::zero(); 4]; + for i in 0..num_rows { + for j in 0..2 { + trace[i].c[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + trace[i].d[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + } + + if i == chosen_index { + trace[i].perform_operation = F::from_bool(true); + values[0] = trace[i].c[0]; + values[1] = trace[i].c[1]; + values[2] = trace[i].d[0]; + values[3] = trace[i].d[1]; + } + } + + // Set public values + pctx.set_public_value_by_name( + values[0].as_canonical_biguint().to_u64().expect("Cannot convert to usize"), + "c_public", + Some(vec![0]), + ); + pctx.set_public_value_by_name( + values[1].as_canonical_biguint().to_u64().expect("Cannot convert to usize"), + "c_public", + Some(vec![1]), + ); + + // Set proof values + pctx.set_proof_value("d_proofval_0", values[2]); + pctx.set_proof_value("d_proofval_1", values[3]); + + // Choose one direct update + let h = rng.gen_bool(0.5); + pctx.set_proof_value("perform_global_update_0", F::from_bool(h)); + pctx.set_proof_value("perform_global_update_1", F::from_bool(!h)); + } + } +} diff --git a/pil2-components/test/std/direct_update/rs/src/direct_update_prod_local.rs b/pil2-components/test/std/direct_update/rs/src/direct_update_prod_local.rs new file mode 100644 index 00000000..1742eb91 --- /dev/null +++ b/pil2-components/test/std/direct_update/rs/src/direct_update_prod_local.rs @@ -0,0 +1,133 @@ +use std::sync::Arc; + +use proofman::{WitnessComponent, WitnessManager}; +use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; + +use p3_field::PrimeField; +use num_traits::ToPrimitive; +use rand::{distributions::Standard, prelude::Distribution, Rng}; + +use crate::{DirectUpdateProdLocalTrace, DIRECT_UPDATE_PROD_AIRGROUP_ID, DIRECT_UPDATE_PROD_LOCAL_AIR_IDS}; + +pub struct DirectUpdateProdLocal { + _phantom: std::marker::PhantomData, +} + +impl DirectUpdateProdLocal +where + Standard: Distribution, +{ + const MY_NAME: &'static str = "DUPL"; + + pub fn new(wcm: Arc>) -> Arc { + let direct_update_prod_local = Arc::new(Self { _phantom: std::marker::PhantomData }); + + wcm.register_component( + direct_update_prod_local.clone(), + Some(DIRECT_UPDATE_PROD_AIRGROUP_ID), + Some(DIRECT_UPDATE_PROD_LOCAL_AIR_IDS), + ); + + direct_update_prod_local + } + + pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { + let num_rows = + pctx.global_info.airs[DIRECT_UPDATE_PROD_AIRGROUP_ID][DIRECT_UPDATE_PROD_LOCAL_AIR_IDS[0]].num_rows; + let trace = DirectUpdateProdLocalTrace::new(num_rows); + + let air_instance = AirInstance::new( + sctx.clone(), + DIRECT_UPDATE_PROD_AIRGROUP_ID, + DIRECT_UPDATE_PROD_LOCAL_AIR_IDS[0], + None, + trace.buffer.unwrap(), + ); + let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance( + DIRECT_UPDATE_PROD_AIRGROUP_ID, + DIRECT_UPDATE_PROD_LOCAL_AIR_IDS[0], + 1, + ); + if is_myne { + pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); + } + } +} + +impl WitnessComponent for DirectUpdateProdLocal +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, + "DirectUpdateProdLocal", + 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(DIRECT_UPDATE_PROD_AIRGROUP_ID, DIRECT_UPDATE_PROD_LOCAL_AIR_IDS[0]).num_rows(); + + let mut trace = DirectUpdateProdLocalTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + + let chosen_index = rng.gen_range(0..=num_rows - 1); + let mut values: [F; 6] = [F::zero(); 6]; + for i in 0..num_rows { + for j in 0..2 { + trace[i].a[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + trace[i].b[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + trace[i].c[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + } + + if i == chosen_index { + trace[i].perform_operation = F::from_bool(true); + values[0] = trace[i].a[0]; + values[1] = trace[i].a[1]; + values[2] = trace[i].b[0]; + values[3] = trace[i].b[1]; + values[4] = trace[i].c[0]; + values[5] = trace[i].c[1]; + } + } + + // Set public values + pctx.set_public_value_by_name( + values[0].as_canonical_biguint().to_u64().expect("Cannot convert to usize"), + "a_public", + Some(vec![0]), + ); + pctx.set_public_value_by_name( + values[1].as_canonical_biguint().to_u64().expect("Cannot convert to usize"), + "a_public", + Some(vec![1]), + ); + + // Set proof values + pctx.set_proof_value("b_proofval_0", values[2]); + pctx.set_proof_value("b_proofval_1", values[3]); + + // Set air values + air_instance.set_airvalue("DirectUpdateProdLocal.c_airval", Some(vec![0]), values[4]); + air_instance.set_airvalue("DirectUpdateProdLocal.c_airval", Some(vec![1]), values[5]); + + // Choose one direct update + let h = rng.gen_bool(0.5); + air_instance.set_airvalue("DirectUpdateProdLocal.perform_direct_update", Some(vec![0]), F::from_bool(h)); + air_instance.set_airvalue("DirectUpdateProdLocal.perform_direct_update", Some(vec![1]), F::from_bool(!h)); + } + } +} diff --git a/pil2-components/test/std/direct_update/rs/src/direct_update_sum_global.rs b/pil2-components/test/std/direct_update/rs/src/direct_update_sum_global.rs new file mode 100644 index 00000000..ffefd4b3 --- /dev/null +++ b/pil2-components/test/std/direct_update/rs/src/direct_update_sum_global.rs @@ -0,0 +1,126 @@ +use std::sync::Arc; + +use proofman::{WitnessComponent, WitnessManager}; +use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; + +use p3_field::PrimeField; +use num_traits::ToPrimitive; +use rand::{distributions::Standard, prelude::Distribution, Rng}; + +use crate::{DirectUpdateSumGlobalTrace, DIRECT_UPDATE_SUM_AIRGROUP_ID, DIRECT_UPDATE_SUM_GLOBAL_AIR_IDS}; + +pub struct DirectUpdateSumGlobal { + _phantom: std::marker::PhantomData, +} + +impl DirectUpdateSumGlobal +where + Standard: Distribution, +{ + const MY_NAME: &'static str = "DUSG"; + + pub fn new(wcm: Arc>) -> Arc { + let direct_update_sum_global = Arc::new(Self { _phantom: std::marker::PhantomData }); + + wcm.register_component( + direct_update_sum_global.clone(), + Some(DIRECT_UPDATE_SUM_AIRGROUP_ID), + Some(DIRECT_UPDATE_SUM_GLOBAL_AIR_IDS), + ); + + direct_update_sum_global + } + + pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { + let num_rows = + pctx.global_info.airs[DIRECT_UPDATE_SUM_AIRGROUP_ID][DIRECT_UPDATE_SUM_GLOBAL_AIR_IDS[0]].num_rows; + let trace = DirectUpdateSumGlobalTrace::new(num_rows); + + let air_instance = AirInstance::new( + sctx.clone(), + DIRECT_UPDATE_SUM_AIRGROUP_ID, + DIRECT_UPDATE_SUM_GLOBAL_AIR_IDS[0], + None, + trace.buffer.unwrap(), + ); + let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance( + DIRECT_UPDATE_SUM_AIRGROUP_ID, + DIRECT_UPDATE_SUM_GLOBAL_AIR_IDS[0], + 1, + ); + if is_myne { + pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); + } + } +} + +impl WitnessComponent for DirectUpdateSumGlobal +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, + "DirectUpdateSumGlobal", + 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(DIRECT_UPDATE_SUM_AIRGROUP_ID, DIRECT_UPDATE_SUM_GLOBAL_AIR_IDS[0]).num_rows(); + + let mut trace = DirectUpdateSumGlobalTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + + let chosen_index = rng.gen_range(0..=num_rows - 1); + let mut values: [F; 4] = [F::zero(); 4]; + for i in 0..num_rows { + for j in 0..2 { + trace[i].c[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + trace[i].d[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + } + + if i == chosen_index { + trace[i].perform_operation = F::from_bool(true); + values[0] = trace[i].c[0]; + values[1] = trace[i].c[1]; + values[2] = trace[i].d[0]; + values[3] = trace[i].d[1]; + } + } + + // Set public values + pctx.set_public_value_by_name( + values[0].as_canonical_biguint().to_u64().expect("Cannot convert to usize"), + "c_public_s", + Some(vec![0]), + ); + pctx.set_public_value_by_name( + values[1].as_canonical_biguint().to_u64().expect("Cannot convert to usize"), + "c_public_s", + Some(vec![1]), + ); + + // Set proof values + pctx.set_proof_value("d_proofval_0_s", values[2]); + pctx.set_proof_value("d_proofval_1_s", values[3]); + + // Choose one direct update + let h = rng.gen_bool(0.5); + pctx.set_proof_value("perform_global_update_0_s", F::from_bool(h)); + pctx.set_proof_value("perform_global_update_1_s", F::from_bool(!h)); + } + } +} diff --git a/pil2-components/test/std/direct_update/rs/src/direct_update_sum_local.rs b/pil2-components/test/std/direct_update/rs/src/direct_update_sum_local.rs new file mode 100644 index 00000000..759addcf --- /dev/null +++ b/pil2-components/test/std/direct_update/rs/src/direct_update_sum_local.rs @@ -0,0 +1,133 @@ +use std::sync::Arc; + +use proofman::{WitnessComponent, WitnessManager}; +use proofman_common::{AirInstance, ExecutionCtx, ProofCtx, SetupCtx}; + +use p3_field::PrimeField; +use num_traits::ToPrimitive; +use rand::{distributions::Standard, prelude::Distribution, Rng}; + +use crate::{DirectUpdateSumLocalTrace, DIRECT_UPDATE_SUM_AIRGROUP_ID, DIRECT_UPDATE_SUM_LOCAL_AIR_IDS}; + +pub struct DirectUpdateSumLocal { + _phantom: std::marker::PhantomData, +} + +impl DirectUpdateSumLocal +where + Standard: Distribution, +{ + const MY_NAME: &'static str = "DUSL"; + + pub fn new(wcm: Arc>) -> Arc { + let direct_update_sum_local = Arc::new(Self { _phantom: std::marker::PhantomData }); + + wcm.register_component( + direct_update_sum_local.clone(), + Some(DIRECT_UPDATE_SUM_AIRGROUP_ID), + Some(DIRECT_UPDATE_SUM_LOCAL_AIR_IDS), + ); + + direct_update_sum_local + } + + pub fn execute(&self, pctx: Arc>, ectx: Arc, sctx: Arc) { + let num_rows = + pctx.global_info.airs[DIRECT_UPDATE_SUM_AIRGROUP_ID][DIRECT_UPDATE_SUM_LOCAL_AIR_IDS[0]].num_rows; + let trace = DirectUpdateSumLocalTrace::new(num_rows); + + let air_instance = AirInstance::new( + sctx.clone(), + DIRECT_UPDATE_SUM_AIRGROUP_ID, + DIRECT_UPDATE_SUM_LOCAL_AIR_IDS[0], + None, + trace.buffer.unwrap(), + ); + let (is_myne, gid) = ectx.dctx.write().unwrap().add_instance( + DIRECT_UPDATE_SUM_AIRGROUP_ID, + DIRECT_UPDATE_SUM_LOCAL_AIR_IDS[0], + 1, + ); + if is_myne { + pctx.air_instance_repo.add_air_instance(air_instance, Some(gid)); + } + } +} + +impl WitnessComponent for DirectUpdateSumLocal +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, + "DirectUpdateSumLocal", + 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(DIRECT_UPDATE_SUM_AIRGROUP_ID, DIRECT_UPDATE_SUM_LOCAL_AIR_IDS[0]).num_rows(); + + let mut trace = DirectUpdateSumLocalTrace::map_buffer(buffer.as_mut_slice(), num_rows, 0).unwrap(); + + let chosen_index = rng.gen_range(0..=num_rows - 1); + let mut values: [F; 6] = [F::zero(); 6]; + for i in 0..num_rows { + for j in 0..2 { + trace[i].a[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + trace[i].b[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + trace[i].c[j] = F::from_canonical_u64(rng.gen_range(0..=(1 << 63) - 1)); + } + + if i == chosen_index { + trace[i].perform_operation = F::from_bool(true); + values[0] = trace[i].a[0]; + values[1] = trace[i].a[1]; + values[2] = trace[i].b[0]; + values[3] = trace[i].b[1]; + values[4] = trace[i].c[0]; + values[5] = trace[i].c[1]; + } + } + + // Set public values + pctx.set_public_value_by_name( + values[0].as_canonical_biguint().to_u64().expect("Cannot convert to usize"), + "a_public_s", + Some(vec![0]), + ); + pctx.set_public_value_by_name( + values[1].as_canonical_biguint().to_u64().expect("Cannot convert to usize"), + "a_public_s", + Some(vec![1]), + ); + + // Set proof values + pctx.set_proof_value("b_proofval_0_s", values[2]); + pctx.set_proof_value("b_proofval_1_s", values[3]); + + // Set air values + air_instance.set_airvalue("DirectUpdateSumLocal.c_airval", Some(vec![0]), values[4]); + air_instance.set_airvalue("DirectUpdateSumLocal.c_airval", Some(vec![1]), values[5]); + + // Choose one direct update + let h = rng.gen_bool(0.5); + air_instance.set_airvalue("DirectUpdateSumLocal.perform_direct_update", Some(vec![0]), F::from_bool(h)); + air_instance.set_airvalue("DirectUpdateSumLocal.perform_direct_update", Some(vec![1]), F::from_bool(!h)); + } + } +} diff --git a/pil2-components/test/std/direct_update/rs/src/lib.rs b/pil2-components/test/std/direct_update/rs/src/lib.rs new file mode 100644 index 00000000..eba27d49 --- /dev/null +++ b/pil2-components/test/std/direct_update/rs/src/lib.rs @@ -0,0 +1,13 @@ +mod direct_update_lib; +mod direct_update_prod_local; +mod direct_update_prod_global; +mod direct_update_sum_local; +mod direct_update_sum_global; +mod pil_helpers; + +pub use direct_update_lib::*; +pub use direct_update_prod_local::*; +pub use direct_update_prod_global::*; +pub use direct_update_sum_local::*; +pub use direct_update_sum_global::*; +pub use pil_helpers::*; diff --git a/pil2-components/test/std/special/direct_update.pil b/pil2-components/test/std/special/direct_update.pil deleted file mode 100644 index 2df7a8cb..00000000 --- a/pil2-components/test/std/special/direct_update.pil +++ /dev/null @@ -1,73 +0,0 @@ -require "std_permutation.pil"; -require "std_lookup.pil"; -require "std_range_check.pil"; -require "std_direct.pil"; - -const int OP_BUS_ID1 = 100; -const int OPID1 = 333; -public input1[10]; - -airtemplate DirectUpdatePermutationAir(const int N = 2**4) { - col witness a[2],b[2],c[2]; - col witness flag; - col witness perform_operation; - - airval d[6]; - - direct_update_assumes(OP_BUS_ID1, [OPID1, ...d, 0], bus_type: PIOP_BUS_PROD); - - permutation_proves(OP_BUS_ID1, [OPID1, ...a, ...b, ...c, flag], sel: perform_operation, bus_type: PIOP_BUS_PROD); -} - -airtemplate DirectUpdatePermutationGlobal(const int N = 2**4) { - col witness a[2],b[2],c[2]; - col witness flag; - col witness perform_operation; - permutation_assumes(OP_BUS_ID1, [OPID1, ...a, ...b, ...c, flag], sel: perform_operation, bus_type: PIOP_BUS_PROD); -} - -airgroup Permutation { - DirectUpdatePermutationAir(); - - DirectUpdatePermutationGlobal(); - for (int i = 0; i < length(input1)/2; i++) { - direct_global_update_proves(OP_BUS_ID1, [OPID1, 0, 0, i, 0, input1[2*i], input1[2*i+1], 0], bus_type: PIOP_BUS_PROD); - } -} - -const int OP_BUS_ID2 = 200; -const int OPID2 = 444; -public input2[20]; - -airtemplate DirectUpdateLookupAir(const int N = 2**5) { - col witness a[2],b[2],c[2]; - col witness flag; - col witness perform_operation; - - airval d[6]; - - lookup_assumes(OP_BUS_ID2, [OPID2, ...a, ...b, ...c, flag], sel: perform_operation); - - range_check(d[0], 0, 2**21-1); - - direct_update_proves(OP_BUS_ID2, [OPID2, ...d, 0]); -} - -airtemplate DirectUpdateLookupGlobal(const int N = 2**5) { - col witness a[2],b[2],c[2]; - col witness flag; - col witness perform_operation; - lookup_proves(OP_BUS_ID2, [OPID2, ...a, ...b, ...c, flag], mul: perform_operation); -} - -airgroup Lookup { - DirectUpdateLookupAir(); - - for (int i = 0; i < length(input2)/2; i++) { - direct_global_update_assumes(OP_BUS_ID2, [OPID2, 0, 0, i, 0, input2[2*i], input2[2*i+1], 0]); - } - - DirectUpdateLookupGlobal(); -} - -// TODO: Do an example with multiple elements in the direct_update \ No newline at end of file