Skip to content

Commit

Permalink
Adding fixed to pil-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerTaule committed Jan 31, 2025
1 parent cdfb21b commit 9605ed7
Show file tree
Hide file tree
Showing 35 changed files with 244 additions and 166 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion cli/assets/templates/pil_helpers_trace.rs.tt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// WARNING: This file has been autogenerated from the PILOUT file.
// Manual modifications are not recommended and may be overwritten.
#![allow(non_snake_case)]

use proofman_common as common;
pub use proofman_macros::trace;
pub use proofman_macros::values;
Expand All @@ -26,7 +28,7 @@ use serde::Serialize;
use serde_arrays;
#[derive(Debug, Serialize, Deserialize)]
pub struct {project_name}Publics \{
{{ for column in public_values.values_u64 }}#[serde(with = "serde_arrays")]
{{ for column in public_values.values_u64 }}{{ if column.array }}#[serde(default, with = "serde_arrays")]{{ else }}#[serde(default)]{{ endif }}
pub {column.name}: {column.type},
{{ endfor }}
}
Expand All @@ -47,6 +49,10 @@ values!({ project_name }ProofValues<F> \{
{{ for column in proof_vals.values }} { column.name }: { column.type },{{ endfor }}
});
{{ endfor }} {{ for air_group in air_groups }}{{ for air in air_group.airs }}
trace!({ air.name }Fixed<F> \{
{{ for column in air.fixed }} { column.name }: { column.type },{{ endfor }}
}, { air_group.airgroup_id }, { air.id }, { air.num_rows } );

trace!({ air.name }Trace<F> \{
{{ for column in air.columns }} { column.name }: { column.type },{{ endfor }}
}, { air_group.airgroup_id }, { air.id }, { air.num_rows } );
Expand Down
3 changes: 2 additions & 1 deletion cli/src/commands/get_constraints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// extern crate env_logger;
use clap::Parser;
use p3_goldilocks::Goldilocks;
use proofman_common::initialize_logger;
use std::path::PathBuf;
use colored::Colorize;
Expand All @@ -24,7 +25,7 @@ impl GetConstraintsCmd {
println!();

let global_info = GlobalInfo::new(&self.proving_key);
let setups = Arc::new(SetupsVadcop::new(&global_info, false, false));
let setups = Arc::new(SetupsVadcop::<Goldilocks>::new(&global_info, false, false));

initialize_logger(proofman_common::VerboseMode::Info);

Expand Down
20 changes: 18 additions & 2 deletions cli/src/commands/pil_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct AirCtx {
name: String,
num_rows: u32,
columns: Vec<ColumnCtx>,
fixed: Vec<ColumnCtx>,
stages_columns: Vec<StageColumnCtx>,
custom_columns: Vec<CustomCommitsCtx>,
air_values: Vec<ValuesCtx>,
Expand All @@ -61,7 +62,7 @@ struct AirCtx {
#[derive(Clone, Debug, Serialize)]
struct ValuesCtx {
values: Vec<ColumnCtx>,
values_u64: Vec<ColumnCtx>,
values_u64: Vec<Column64Ctx>,
values_default: Vec<ColumnCtx>,
}

Expand All @@ -77,6 +78,13 @@ struct ColumnCtx {
r#type: String,
}

#[derive(Clone, Debug, Serialize)]
struct Column64Ctx {
name: String,
array: bool,
r#type: String,
}

#[derive(Default, Clone, Debug, Serialize)]
struct StageColumnCtx {
stage_id: usize,
Expand Down Expand Up @@ -136,6 +144,7 @@ impl PilHelpersCmd {
name: air.name.as_ref().unwrap().to_string(),
num_rows: air.num_rows.unwrap(),
columns: Vec::new(),
fixed: Vec::new(),
stages_columns: vec![StageColumnCtx::default(); pilout.num_challenges.len() - 1],
custom_columns: Vec::new(),
air_values: Vec::new(),
Expand Down Expand Up @@ -223,7 +232,11 @@ impl PilHelpersCmd {
.rev()
.fold("u64".to_string(), |acc, &length| format!("[{}; {}]", acc, length))
};
publics[0].values_u64.push(ColumnCtx { name: name.to_owned(), r#type: r#type_64 });
publics[0].values_u64.push(Column64Ctx {
name: name.to_owned(),
r#type: r#type_64,
array: !symbol.lengths.is_empty(),
});

let default = "0".to_string();
let r#type_default = if symbol.lengths.is_empty() {
Expand Down Expand Up @@ -262,6 +275,7 @@ impl PilHelpersCmd {
|| symbol.r#type == SymbolType::AirGroupValue as i32)
&& symbol.stage.is_some()
&& ((symbol.r#type == SymbolType::WitnessCol as i32)
|| (symbol.r#type == SymbolType::FixedCol as i32)
|| (symbol.r#type == SymbolType::AirValue as i32)
|| (symbol.r#type == SymbolType::AirGroupValue as i32)
|| (symbol.r#type == SymbolType::CustomCol as i32 && symbol.stage.unwrap() == 0))
Expand Down Expand Up @@ -298,6 +312,8 @@ impl PilHelpersCmd {
.columns
.push(ColumnCtx { name: name.to_owned(), r#type: ext_type });
}
} else if symbol.r#type == SymbolType::FixedCol as i32 {
air.fixed.push(ColumnCtx { name: name.to_owned(), r#type });
} else if symbol.r#type == SymbolType::AirValue as i32 {
if air.air_values.is_empty() {
air.air_values.push(ValuesCtx {
Expand Down
2 changes: 1 addition & 1 deletion common/src/air_instances_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ impl<F: Field> AirInstancesRepository<F> {
let mut count = 0;
for (index, air_instance) in air_instances.iter() {
if air_instance.airgroup_id == airgroup_id && air_instance.air_id == air_id {
count += 1;
if count == air_instance_id {
return Some(*index);
}
count += 1;
}
}

Expand Down
4 changes: 2 additions & 2 deletions common/src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct GlobalConstraintInfo {
pub value: [u64; 3usize],
}

pub fn get_constraints_lines_str(sctx: Arc<SetupCtx>, airgroup_id: usize, air_id: usize) -> Vec<String> {
pub fn get_constraints_lines_str<F: Clone>(sctx: Arc<SetupCtx<F>>, airgroup_id: usize, air_id: usize) -> Vec<String> {
let setup = sctx.get_setup(airgroup_id, air_id);

let p_setup = (&setup.p_setup).into();
Expand Down Expand Up @@ -64,7 +64,7 @@ pub fn get_constraints_lines_str(sctx: Arc<SetupCtx>, airgroup_id: usize, air_id
constraints_lines_str
}

pub fn get_global_constraints_lines_str(sctx: Arc<SetupCtx>) -> Vec<String> {
pub fn get_global_constraints_lines_str<F: Clone>(sctx: Arc<SetupCtx<F>>) -> Vec<String> {
let n_global_constraints = get_n_global_constraints_c(sctx.get_global_bin());

let mut global_constraints_sizes = vec![0u64; n_global_constraints as usize];
Expand Down
2 changes: 1 addition & 1 deletion common/src/custom_commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::Setup;
pub fn get_custom_commit_trace(
commit_id: u64,
step: u64,
setup: &Setup,
setup: &Setup<Goldilocks>,
buffer: Vec<Goldilocks>,
buffer_ext: Vec<Goldilocks>,
buffer_str: &str,
Expand Down
2 changes: 1 addition & 1 deletion common/src/proof_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<F: Field> ProofCtx<F> {
}
}

pub fn set_weights(&mut self, sctx: &SetupCtx) {
pub fn set_weights(&mut self, sctx: &SetupCtx<F>) {
for (airgroup_id, air_group) in self.global_info.airs.iter().enumerate() {
for (air_id, _) in air_group.iter().enumerate() {
let setup = sctx.get_setup(airgroup_id, air_id);
Expand Down
6 changes: 3 additions & 3 deletions common/src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ pub trait Prover<F: Field> {
fn new_transcript(&self) -> FFITranscript;
fn num_stages(&self) -> u32;
fn get_challenges(&self, stage_id: u32, pctx: Arc<ProofCtx<F>>, transcript: &FFITranscript) -> Vec<Vec<F>>;
fn calculate_stage(&mut self, stage_id: u32, sctx: Arc<SetupCtx>, pctx: Arc<ProofCtx<F>>);
fn calculate_stage(&mut self, stage_id: u32, sctx: Arc<SetupCtx<F>>, pctx: Arc<ProofCtx<F>>);
fn commit_stage(&mut self, stage_id: u32, pctx: Arc<ProofCtx<F>>) -> ProverStatus;
fn commit_custom_commits_stage(&mut self, stage_id: u32, pctx: Arc<ProofCtx<F>>) -> Vec<u64>;
fn calculate_xdivxsub(&mut self, pctx: Arc<ProofCtx<F>>, challenge: Vec<F>);
fn calculate_lev(&mut self, pctx: Arc<ProofCtx<F>>, challenge: Vec<F>);
fn opening_stage(&mut self, opening_id: u32, sctx: Arc<SetupCtx>, pctx: Arc<ProofCtx<F>>) -> ProverStatus;
fn opening_stage(&mut self, opening_id: u32, sctx: Arc<SetupCtx<F>>, pctx: Arc<ProofCtx<F>>) -> ProverStatus;

fn get_buff_helper_size(&self, pctx: Arc<ProofCtx<F>>) -> usize;
fn get_proof(&self) -> *mut c_void;
Expand All @@ -53,7 +53,7 @@ pub trait Prover<F: Field> {
fn get_transcript_values(&self, stage: u64, pctx: Arc<ProofCtx<F>>) -> Vec<F>;
fn get_transcript_values_u64(&self, stage: u64, pctx: Arc<ProofCtx<F>>) -> Vec<u64>;
fn calculate_hash(&self, values: Vec<F>) -> Vec<F>;
fn verify_constraints(&self, sctx: Arc<SetupCtx>, pctx: Arc<ProofCtx<F>>) -> Vec<ConstraintInfo>;
fn verify_constraints(&self, sctx: Arc<SetupCtx<F>>, pctx: Arc<ProofCtx<F>>) -> Vec<ConstraintInfo>;

fn get_proof_challenges(&self, global_steps: Vec<usize>, global_challenges: Vec<F>) -> Vec<F>;
}
38 changes: 18 additions & 20 deletions common/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use std::sync::atomic::Ordering;
use std::sync::RwLock;

use proofman_starks_lib_c::{
get_const_tree_size_c, get_const_size_c, prover_helpers_new_c, expressions_bin_new_c, stark_info_new_c,
load_const_tree_c, load_const_pols_c, calculate_const_tree_c, stark_info_free_c, expressions_bin_free_c,
prover_helpers_free_c, get_map_totaln_c, write_const_tree_c,
get_const_tree_size_c, prover_helpers_new_c, expressions_bin_new_c, stark_info_new_c, load_const_tree_c,
load_const_pols_c, calculate_const_tree_c, stark_info_free_c, expressions_bin_free_c, prover_helpers_free_c,
get_map_totaln_c, write_const_tree_c,
};
use proofman_util::create_buffer_fast_u8;
use proofman_util::create_buffer_fast;

use crate::GlobalInfo;
use crate::ProofType;
Expand All @@ -33,11 +33,11 @@ impl From<&SetupC> for *mut c_void {
}

#[derive(Debug)]
pub struct Pols {
pub values: RwLock<Vec<u8>>,
pub struct Pols<F: Clone> {
pub values: RwLock<Vec<F>>,
}

impl Default for Pols {
impl<F: Clone> Default for Pols<F> {
fn default() -> Self {
Self { values: RwLock::new(Vec::new()) }
}
Expand All @@ -46,21 +46,21 @@ impl Default for Pols {
/// Air instance context for managing air instances (traces)
#[derive(Debug)]
#[allow(dead_code)]
pub struct Setup {
pub struct Setup<F: Clone> {
pub airgroup_id: usize,
pub air_id: usize,
pub p_setup: SetupC,
pub stark_info: StarkInfo,
pub const_pols: Pols,
pub const_tree: Pols,
pub const_pols: Pols<F>,
pub const_tree: Pols<F>,
pub prover_buffer_size: u64,
pub write_const_tree: AtomicBool,
pub setup_path: PathBuf,
pub setup_type: ProofType,
pub air_name: String,
}

impl Setup {
impl<F: Clone> Setup<F> {
const MY_NAME: &'static str = "Setup";

pub fn new(global_info: &GlobalInfo, airgroup_id: usize, air_id: usize, setup_type: &ProofType) -> Self {
Expand Down Expand Up @@ -96,8 +96,8 @@ impl Setup {
airgroup_id,
stark_info,
p_setup: SetupC { p_stark_info, p_expressions_bin, p_prover_helpers },
const_pols: Pols::default(),
const_tree: Pols::default(),
const_pols: Pols::<F>::default(),
const_tree: Pols::<F>::default(),
prover_buffer_size,
write_const_tree: AtomicBool::new(false),
setup_path: setup_path.clone(),
Expand All @@ -122,12 +122,10 @@ impl Setup {

let const_pols_path = self.setup_path.display().to_string() + ".const";

let p_stark_info = self.p_setup.p_stark_info;

let const_size = get_const_size_c(p_stark_info) as usize;
let const_pols = create_buffer_fast_u8(const_size);
let const_size = self.stark_info.n_constants * (1 << self.stark_info.stark_struct.n_bits);
let const_pols = create_buffer_fast(const_size as usize);

load_const_pols_c(const_pols.as_ptr() as *mut u8, const_pols_path.as_str(), const_size as u64);
load_const_pols_c(const_pols.as_ptr() as *mut u8, const_pols_path.as_str(), const_size * 8);
*self.const_pols.values.write().unwrap() = const_pols;
}

Expand All @@ -147,14 +145,14 @@ impl Setup {

let const_tree_size = get_const_tree_size_c(p_stark_info) as usize;

let const_tree = create_buffer_fast_u8(const_tree_size);
let const_tree = create_buffer_fast(const_tree_size);

let valid_root = if PathBuf::from(&const_pols_tree_path).exists() {
load_const_tree_c(
p_stark_info,
const_tree.as_ptr() as *mut u8,
const_pols_tree_path.as_str(),
const_tree_size as u64,
(const_tree_size * 8) as u64,
verkey_path.as_str(),
)
} else {
Expand Down
Loading

0 comments on commit 9605ed7

Please sign in to comment.