Skip to content

Commit

Permalink
Adding the direct_update and diff_buses tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hecmas committed Dec 20, 2024
1 parent a1c68fa commit b413660
Show file tree
Hide file tree
Showing 20 changed files with 1,218 additions and 102 deletions.
64 changes: 64 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
64 changes: 50 additions & 14 deletions pil2-components/test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -30,7 +27,7 @@ airtemplate Air3(const int N = 2**4) {
}

airgroup Buses {
Air1();
Air2();
Air3();
ProdBus();
SumBus();
BothBuses();
}
25 changes: 25 additions & 0 deletions pil2-components/test/std/diff_buses/rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
74 changes: 74 additions & 0 deletions pil2-components/test/std/diff_buses/rs/src/both_buses.rs
Original file line number Diff line number Diff line change
@@ -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<F: PrimeField> {
_phantom: std::marker::PhantomData<F>,
}

impl<F: PrimeField> BothBuses<F>
where
Standard: Distribution<F>,
{
const MY_NAME: &'static str = "BothBuses";

pub fn new(wcm: Arc<WitnessManager<F>>) -> Arc<Self> {
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<ProofCtx<F>>, ectx: Arc<ExecutionCtx>, sctx: Arc<SetupCtx>) {
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<F: PrimeField> WitnessComponent<F> for BothBuses<F>
where
Standard: Distribution<F>,
{
fn calculate_witness(
&self,
stage: u32,
air_instance_id: Option<usize>,
pctx: Arc<ProofCtx<F>>,
_ectx: Arc<ExecutionCtx>,
_sctx: Arc<SetupCtx>,
) {
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;
}
}
}
}
Loading

0 comments on commit b413660

Please sign in to comment.