mirror of
https://github.com/logos-storage/proof-aggregation.git
synced 2026-05-05 17:29:25 +00:00
remove the hardcoded circuit params and refactor
This commit is contained in:
parent
7b2c084339
commit
d63a309e02
@ -14,26 +14,6 @@ pub struct CircuitParams{
|
|||||||
pub n_samples: usize,
|
pub n_samples: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
// hardcoded default constants
|
|
||||||
const DEFAULT_MAX_DEPTH:usize = 32;
|
|
||||||
const DEFAULT_MAX_LOG2_N_SLOTS:usize = 8;
|
|
||||||
const DEFAULT_BLOCK_TREE_DEPTH:usize = 5;
|
|
||||||
const DEFAULT_N_FIELD_ELEMS_PER_CELL:usize = 272;
|
|
||||||
const DEFAULT_N_SAMPLES:usize = 5;
|
|
||||||
|
|
||||||
/// Implement the Default trait for Params using the hardcoded constants
|
|
||||||
impl Default for CircuitParams {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self{
|
|
||||||
max_depth: DEFAULT_MAX_DEPTH,
|
|
||||||
max_log2_n_slots: DEFAULT_MAX_LOG2_N_SLOTS,
|
|
||||||
block_tree_depth: DEFAULT_BLOCK_TREE_DEPTH,
|
|
||||||
n_field_elems_per_cell: DEFAULT_N_FIELD_ELEMS_PER_CELL,
|
|
||||||
n_samples: DEFAULT_N_SAMPLES,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CircuitParams {
|
impl CircuitParams {
|
||||||
/// Creates a new `CircuitParams` struct from environment.
|
/// Creates a new `CircuitParams` struct from environment.
|
||||||
///
|
///
|
||||||
@ -45,12 +25,12 @@ impl CircuitParams {
|
|||||||
///
|
///
|
||||||
/// Returns an error if any environment variable is missing or fails to parse.
|
/// Returns an error if any environment variable is missing or fails to parse.
|
||||||
pub fn from_env() -> Result<Self> {
|
pub fn from_env() -> Result<Self> {
|
||||||
let MAX_DEPTH = env::var("MAX_DEPTH")
|
let max_depth = env::var("MAX_DEPTH")
|
||||||
.context("MAX_DEPTH is not set")?
|
.context("MAX_DEPTH is not set")?
|
||||||
.parse::<usize>()
|
.parse::<usize>()
|
||||||
.context("MAX_DEPTH must be a valid usize")?;
|
.context("MAX_DEPTH must be a valid usize")?;
|
||||||
|
|
||||||
let MAX_LOG2_N_SLOTS = env::var("MAX_LOG2_N_SLOTS")
|
let max_log2_n_slots = env::var("MAX_LOG2_N_SLOTS")
|
||||||
.context("MAX_LOG2_N_SLOTS is not set")?
|
.context("MAX_LOG2_N_SLOTS is not set")?
|
||||||
.parse::<usize>()
|
.parse::<usize>()
|
||||||
.context("MAX_LOG2_N_SLOTS must be a valid usize")?;
|
.context("MAX_LOG2_N_SLOTS must be a valid usize")?;
|
||||||
@ -71,8 +51,8 @@ impl CircuitParams {
|
|||||||
.context("N_SAMPLES must be a valid usize")?;
|
.context("N_SAMPLES must be a valid usize")?;
|
||||||
|
|
||||||
Ok(CircuitParams {
|
Ok(CircuitParams {
|
||||||
max_depth: MAX_DEPTH,
|
max_depth,
|
||||||
max_log2_n_slots: MAX_LOG2_N_SLOTS,
|
max_log2_n_slots,
|
||||||
block_tree_depth,
|
block_tree_depth,
|
||||||
n_field_elems_per_cell,
|
n_field_elems_per_cell,
|
||||||
n_samples,
|
n_samples,
|
||||||
|
|||||||
@ -2,6 +2,5 @@ pub mod circuits;
|
|||||||
// pub mod merkle_tree;
|
// pub mod merkle_tree;
|
||||||
// pub mod recursion;
|
// pub mod recursion;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod params;
|
|
||||||
|
|
||||||
pub type Result<T> = core::result::Result<T, error::CircuitError>;
|
pub type Result<T> = core::result::Result<T, error::CircuitError>;
|
||||||
|
|||||||
@ -2,4 +2,5 @@ pub mod cyclic;
|
|||||||
pub mod circuits;
|
pub mod circuits;
|
||||||
pub mod simple;
|
pub mod simple;
|
||||||
pub mod tree1;
|
pub mod tree1;
|
||||||
pub mod tree2;
|
pub mod tree2;
|
||||||
|
pub mod params;
|
||||||
|
|||||||
@ -6,7 +6,7 @@ use codex_plonky2_circuits::circuits::sample_cells::Cell;
|
|||||||
use plonky2_field::types::Sample;
|
use plonky2_field::types::Sample;
|
||||||
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
|
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
|
||||||
use crate::merkle_tree::merkle_safe::{MerkleProof, MerkleTree};
|
use crate::merkle_tree::merkle_safe::{MerkleProof, MerkleTree};
|
||||||
use crate::params::{TestParams, HF};
|
use crate::params::{InputParams,Params, HF};
|
||||||
use crate::sponge::hash_bytes_no_padding;
|
use crate::sponge::hash_bytes_no_padding;
|
||||||
use crate::utils::{bits_le_padded_to_usize, calculate_cell_index_bits, usize_to_bits_le};
|
use crate::utils::{bits_le_padded_to_usize, calculate_cell_index_bits, usize_to_bits_le};
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ pub struct SlotTree<
|
|||||||
pub tree: MerkleTree<F, D>, // slot tree
|
pub tree: MerkleTree<F, D>, // slot tree
|
||||||
pub block_trees: Vec<MerkleTree<F,D>>, // vec of block trees
|
pub block_trees: Vec<MerkleTree<F,D>>, // vec of block trees
|
||||||
pub cell_data: Vec<Cell<F, D>>, // cell data as field elements
|
pub cell_data: Vec<Cell<F, D>>, // cell data as field elements
|
||||||
pub params: TestParams, // parameters
|
pub params: InputParams, // parameters
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<
|
impl<
|
||||||
@ -27,7 +27,7 @@ impl<
|
|||||||
const D: usize,
|
const D: usize,
|
||||||
> SlotTree<F, D> {
|
> SlotTree<F, D> {
|
||||||
/// Create a slot tree with fake data, for testing only
|
/// Create a slot tree with fake data, for testing only
|
||||||
pub fn new_default(params: &TestParams) -> Self {
|
pub fn new_default(params: &InputParams) -> Self {
|
||||||
// generate fake cell data
|
// generate fake cell data
|
||||||
let cell_data = (0..params.n_cells)
|
let cell_data = (0..params.n_cells)
|
||||||
.map(|_| new_random_cell(params))
|
.map(|_| new_random_cell(params))
|
||||||
@ -36,7 +36,7 @@ impl<
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new slot tree with the supplied cell data and parameters
|
/// Create a new slot tree with the supplied cell data and parameters
|
||||||
pub fn new(cells: Vec<Cell<F, D>>, params: TestParams) -> Self {
|
pub fn new(cells: Vec<Cell<F, D>>, params: InputParams) -> Self {
|
||||||
let leaves: Vec<HashOut<F>> = cells
|
let leaves: Vec<HashOut<F>> = cells
|
||||||
.iter()
|
.iter()
|
||||||
.map(|element| hash_bytes_no_padding::<F,D,HF>(&element.data))
|
.map(|element| hash_bytes_no_padding::<F,D,HF>(&element.data))
|
||||||
@ -106,7 +106,7 @@ pub struct DatasetTree<
|
|||||||
> {
|
> {
|
||||||
pub tree: MerkleTree<F,D>, // dataset tree
|
pub tree: MerkleTree<F,D>, // dataset tree
|
||||||
pub slot_trees: Vec<SlotTree<F, D>>, // vec of slot trees
|
pub slot_trees: Vec<SlotTree<F, D>>, // vec of slot trees
|
||||||
pub params: TestParams, // parameters
|
pub params: InputParams, // parameters
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Dataset Merkle proof struct, containing the dataset proof and sampled proofs.
|
/// Dataset Merkle proof struct, containing the dataset proof and sampled proofs.
|
||||||
@ -127,7 +127,7 @@ impl<
|
|||||||
const D: usize,
|
const D: usize,
|
||||||
> DatasetTree<F, D> {
|
> DatasetTree<F, D> {
|
||||||
/// Dataset tree with fake data, for testing only
|
/// Dataset tree with fake data, for testing only
|
||||||
pub fn new_default(params: &TestParams) -> Self {
|
pub fn new_default(params: &InputParams) -> Self {
|
||||||
let mut slot_trees = vec![];
|
let mut slot_trees = vec![];
|
||||||
let n_slots = 1 << params.dataset_depth_test();
|
let n_slots = 1 << params.dataset_depth_test();
|
||||||
for _ in 0..n_slots {
|
for _ in 0..n_slots {
|
||||||
@ -137,7 +137,7 @@ impl<
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create data for only the specified slot index in params
|
/// Create data for only the specified slot index in params
|
||||||
pub fn new_for_testing(params: &TestParams) -> Self {
|
pub fn new_for_testing(params: &InputParams) -> Self {
|
||||||
let mut slot_trees = vec![];
|
let mut slot_trees = vec![];
|
||||||
// let n_slots = 1 << params.dataset_depth();
|
// let n_slots = 1 << params.dataset_depth();
|
||||||
let n_slots = params.n_slots;
|
let n_slots = params.n_slots;
|
||||||
@ -172,7 +172,7 @@ impl<
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Same as default but with supplied slot trees
|
/// Same as default but with supplied slot trees
|
||||||
pub fn new(slot_trees: Vec<SlotTree<F, D>>, params: TestParams) -> Self {
|
pub fn new(slot_trees: Vec<SlotTree<F, D>>, params: InputParams) -> Self {
|
||||||
// get the roots of slot trees
|
// get the roots of slot trees
|
||||||
let slot_roots = slot_trees
|
let slot_roots = slot_trees
|
||||||
.iter()
|
.iter()
|
||||||
@ -248,7 +248,7 @@ impl<
|
|||||||
pub fn new_random_cell<
|
pub fn new_random_cell<
|
||||||
F: RichField + Extendable<D> + Poseidon2,
|
F: RichField + Extendable<D> + Poseidon2,
|
||||||
const D: usize,
|
const D: usize,
|
||||||
>(params: &TestParams) -> Cell<F,D> {
|
>(params: &InputParams) -> Cell<F,D> {
|
||||||
let data = (0..params.n_field_elems_per_cell())
|
let data = (0..params.n_field_elems_per_cell())
|
||||||
.map(|_| F::rand())
|
.map(|_| F::rand())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|||||||
@ -4,7 +4,7 @@ use plonky2_field::extension::Extendable;
|
|||||||
use plonky2_field::types::Field;
|
use plonky2_field::types::Field;
|
||||||
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
|
use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
|
||||||
use codex_plonky2_circuits::circuits::params::CircuitParams;
|
use codex_plonky2_circuits::circuits::params::CircuitParams;
|
||||||
use crate::params::TestParams;
|
use crate::params::{Params,InputParams};
|
||||||
use crate::utils::{bits_le_padded_to_usize, calculate_cell_index_bits, ceiling_log2, usize_to_bits_le};
|
use crate::utils::{bits_le_padded_to_usize, calculate_cell_index_bits, ceiling_log2, usize_to_bits_le};
|
||||||
use crate::merkle_tree::merkle_safe::MerkleProof;
|
use crate::merkle_tree::merkle_safe::MerkleProof;
|
||||||
use codex_plonky2_circuits::circuits::sample_cells::{MerklePath, SampleCircuit, SampleCircuitInput, SampleTargets};
|
use codex_plonky2_circuits::circuits::sample_cells::{MerklePath, SampleCircuit, SampleCircuitInput, SampleTargets};
|
||||||
@ -21,7 +21,7 @@ use crate::params::{C, D, F, HF};
|
|||||||
pub fn gen_testing_circuit_input<
|
pub fn gen_testing_circuit_input<
|
||||||
F: RichField + Extendable<D> + Poseidon2,
|
F: RichField + Extendable<D> + Poseidon2,
|
||||||
const D: usize,
|
const D: usize,
|
||||||
>(params: &TestParams) -> SampleCircuitInput<F,D>{
|
>(params: &InputParams) -> SampleCircuitInput<F,D>{
|
||||||
let dataset_t = DatasetTree::<F, D>::new_for_testing(¶ms);
|
let dataset_t = DatasetTree::<F, D>::new_for_testing(¶ms);
|
||||||
|
|
||||||
let slot_index = params.testing_slot_index; // samples the specified slot
|
let slot_index = params.testing_slot_index; // samples the specified slot
|
||||||
@ -57,7 +57,7 @@ pub fn gen_testing_circuit_input<
|
|||||||
pub fn verify_circuit_input<
|
pub fn verify_circuit_input<
|
||||||
F: RichField + Extendable<D> + Poseidon2,
|
F: RichField + Extendable<D> + Poseidon2,
|
||||||
const D: usize,
|
const D: usize,
|
||||||
>(circ_input: SampleCircuitInput<F,D>, params: &TestParams) -> bool{
|
>(circ_input: SampleCircuitInput<F,D>, params: &InputParams) -> bool{
|
||||||
let slot_index = circ_input.slot_index.to_canonical_u64();
|
let slot_index = circ_input.slot_index.to_canonical_u64();
|
||||||
let slot_root = circ_input.slot_root.clone();
|
let slot_root = circ_input.slot_root.clone();
|
||||||
// check dataset level proof
|
// check dataset level proof
|
||||||
@ -102,7 +102,7 @@ pub fn verify_circuit_input<
|
|||||||
pub fn verify_cell_proof<
|
pub fn verify_cell_proof<
|
||||||
F: RichField + Extendable<D> + Poseidon2,
|
F: RichField + Extendable<D> + Poseidon2,
|
||||||
const D: usize,
|
const D: usize,
|
||||||
>(circ_input: &SampleCircuitInput<F,D>, params: &TestParams, cell_index: usize, ctr: usize) -> anyhow::Result<bool> {
|
>(circ_input: &SampleCircuitInput<F,D>, params: &InputParams, cell_index: usize, ctr: usize) -> anyhow::Result<bool> {
|
||||||
let mut block_path_bits = usize_to_bits_le(cell_index, params.max_depth);
|
let mut block_path_bits = usize_to_bits_le(cell_index, params.max_depth);
|
||||||
let last_index = params.n_cells - 1;
|
let last_index = params.n_cells - 1;
|
||||||
let mut block_last_bits = usize_to_bits_le(last_index, params.max_depth);
|
let mut block_last_bits = usize_to_bits_le(last_index, params.max_depth);
|
||||||
@ -156,16 +156,17 @@ pub fn build_circuit(n_samples: usize, slot_index: usize) -> anyhow::Result<(Cir
|
|||||||
/// returns the proof, circuit data, and targets
|
/// returns the proof, circuit data, and targets
|
||||||
pub fn build_circuit_with_targets(n_samples: usize, slot_index: usize) -> anyhow::Result<(CircuitData<F, C, D>, PartialWitness<F>, SampleTargets)>{
|
pub fn build_circuit_with_targets(n_samples: usize, slot_index: usize) -> anyhow::Result<(CircuitData<F, C, D>, PartialWitness<F>, SampleTargets)>{
|
||||||
// get input
|
// get input
|
||||||
let mut params = TestParams::default();
|
let mut params = Params::default();
|
||||||
params.n_samples = n_samples;
|
let mut input_params = params.input_params;
|
||||||
params.testing_slot_index = slot_index;
|
input_params.n_samples = n_samples;
|
||||||
let circ_input = gen_testing_circuit_input::<F,D>(¶ms);
|
input_params.testing_slot_index = slot_index;
|
||||||
|
let circ_input = gen_testing_circuit_input::<F,D>(&input_params);
|
||||||
|
|
||||||
// Create the circuit
|
// Create the circuit
|
||||||
let config = CircuitConfig::standard_recursion_config();
|
let config = CircuitConfig::standard_recursion_config();
|
||||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||||
|
|
||||||
let mut circuit_params = CircuitParams::default();
|
let mut circuit_params = params.circuit_params;
|
||||||
circuit_params.n_samples = n_samples;
|
circuit_params.n_samples = n_samples;
|
||||||
|
|
||||||
// build the circuit
|
// build the circuit
|
||||||
@ -193,10 +194,10 @@ pub fn prove_circuit(data: &CircuitData<F, C, D>, pw: &PartialWitness<F>) -> any
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// returns exactly M default circuit input
|
/// returns exactly M default circuit input
|
||||||
pub fn get_m_default_circ_input<const M: usize>() -> [SampleCircuitInput<codex_plonky2_circuits::params::F,D>; M]{
|
pub fn get_m_default_circ_input<const M: usize>() -> [SampleCircuitInput<F,D>; M]{
|
||||||
let params = TestParams::default();
|
let params = Params::default().input_params;
|
||||||
let one_circ_input = gen_testing_circuit_input::<codex_plonky2_circuits::params::F,D>(¶ms);
|
let one_circ_input = gen_testing_circuit_input::<F,D>(¶ms);
|
||||||
let circ_input: [SampleCircuitInput<codex_plonky2_circuits::params::F,D>; M] = (0..M)
|
let circ_input: [SampleCircuitInput<F,D>; M] = (0..M)
|
||||||
.map(|_| one_circ_input.clone())
|
.map(|_| one_circ_input.clone())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.try_into().unwrap();
|
.try_into().unwrap();
|
||||||
@ -217,7 +218,7 @@ mod tests {
|
|||||||
// Test sample cells (non-circuit)
|
// Test sample cells (non-circuit)
|
||||||
#[test]
|
#[test]
|
||||||
fn test_gen_verify_proof(){
|
fn test_gen_verify_proof(){
|
||||||
let params = TestParams::default();
|
let params = Params::default().input_params;
|
||||||
let w = gen_testing_circuit_input::<F,D>(¶ms);
|
let w = gen_testing_circuit_input::<F,D>(¶ms);
|
||||||
assert!(verify_circuit_input::<F,D>(w, ¶ms));
|
assert!(verify_circuit_input::<F,D>(w, ¶ms));
|
||||||
}
|
}
|
||||||
@ -226,15 +227,16 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_proof_in_circuit() -> anyhow::Result<()> {
|
fn test_proof_in_circuit() -> anyhow::Result<()> {
|
||||||
// get input
|
// get input
|
||||||
let mut params = TestParams::default();
|
let mut params = Params::default();
|
||||||
params.n_samples = 10;
|
let mut input_params = params.input_params;
|
||||||
let circ_input = gen_testing_circuit_input::<F,D>(¶ms);
|
input_params.n_samples = 10;
|
||||||
|
let circ_input = gen_testing_circuit_input::<F,D>(&input_params);
|
||||||
|
|
||||||
// Create the circuit
|
// Create the circuit
|
||||||
let config = CircuitConfig::standard_recursion_config();
|
let config = CircuitConfig::standard_recursion_config();
|
||||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||||
|
|
||||||
let mut circuit_params = CircuitParams::default();
|
let mut circuit_params = params.circuit_params;
|
||||||
circuit_params.n_samples = 10;
|
circuit_params.n_samples = 10;
|
||||||
|
|
||||||
// build the circuit
|
// build the circuit
|
||||||
|
|||||||
@ -13,7 +13,6 @@ pub const D: usize = 2;
|
|||||||
pub type C = PoseidonGoldilocksConfig;
|
pub type C = PoseidonGoldilocksConfig;
|
||||||
pub type F = <C as GenericConfig<D>>::F; // this is the goldilocks field
|
pub type F = <C as GenericConfig<D>>::F; // this is the goldilocks field
|
||||||
pub type HF = PoseidonHash;
|
pub type HF = PoseidonHash;
|
||||||
// pub type HP = <PoseidonHash as plonky2::plonk::config::Hasher<F>>::Permutation;
|
|
||||||
|
|
||||||
// hardcoded default params for generating proof input
|
// hardcoded default params for generating proof input
|
||||||
const DEFAULT_MAX_DEPTH: usize = 32; // depth of big tree (slot tree depth, includes block tree depth)
|
const DEFAULT_MAX_DEPTH: usize = 32; // depth of big tree (slot tree depth, includes block tree depth)
|
||||||
@ -33,12 +32,12 @@ const DEFAULT_N_CELLS: usize = 512; // number of cells in each slot
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Params {
|
pub struct Params {
|
||||||
pub circuit_params: CircuitParams,
|
pub circuit_params: CircuitParams,
|
||||||
pub test: TestParams,
|
pub input_params: InputParams,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// test params
|
/// test params
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct TestParams{
|
pub struct InputParams{
|
||||||
pub max_depth: usize,
|
pub max_depth: usize,
|
||||||
pub max_slots: usize,
|
pub max_slots: usize,
|
||||||
pub cell_size: usize,
|
pub cell_size: usize,
|
||||||
@ -52,9 +51,9 @@ pub struct TestParams{
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Implement the Default trait for Params using the hardcoded constants
|
/// Implement the Default trait for Params using the hardcoded constants
|
||||||
impl Default for TestParams {
|
impl Default for Params {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
TestParams {
|
let input_params = InputParams {
|
||||||
max_depth: DEFAULT_MAX_DEPTH,
|
max_depth: DEFAULT_MAX_DEPTH,
|
||||||
max_slots: DEFAULT_MAX_SLOTS,
|
max_slots: DEFAULT_MAX_SLOTS,
|
||||||
cell_size: DEFAULT_CELL_SIZE,
|
cell_size: DEFAULT_CELL_SIZE,
|
||||||
@ -65,37 +64,18 @@ impl Default for TestParams {
|
|||||||
n_slots: DEFAULT_N_SLOTS,
|
n_slots: DEFAULT_N_SLOTS,
|
||||||
testing_slot_index: DEFAULT_SLOT_INDEX,
|
testing_slot_index: DEFAULT_SLOT_INDEX,
|
||||||
n_cells: DEFAULT_N_CELLS,
|
n_cells: DEFAULT_N_CELLS,
|
||||||
|
};
|
||||||
|
let circuit_params = input_params.get_circuit_params();
|
||||||
|
|
||||||
|
Params{
|
||||||
|
circuit_params,
|
||||||
|
input_params,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Implement a new function to create Params with custom values
|
/// Implement a new function to create Params with custom values
|
||||||
impl TestParams {
|
impl InputParams {
|
||||||
pub fn new(
|
|
||||||
max_depth: usize,
|
|
||||||
max_slots: usize,
|
|
||||||
cell_size: usize,
|
|
||||||
block_size: usize,
|
|
||||||
n_samples: usize,
|
|
||||||
entropy: usize,
|
|
||||||
seed: usize,
|
|
||||||
n_slots: usize,
|
|
||||||
testing_slot_index: usize,
|
|
||||||
n_cells: usize,
|
|
||||||
) -> Self {
|
|
||||||
TestParams {
|
|
||||||
max_depth,
|
|
||||||
max_slots,
|
|
||||||
cell_size,
|
|
||||||
block_size,
|
|
||||||
n_samples,
|
|
||||||
entropy,
|
|
||||||
seed,
|
|
||||||
n_slots,
|
|
||||||
testing_slot_index,
|
|
||||||
n_cells,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// GOLDILOCKS_F_SIZE
|
// GOLDILOCKS_F_SIZE
|
||||||
pub fn goldilocks_f_size(&self) -> usize {
|
pub fn goldilocks_f_size(&self) -> usize {
|
||||||
64
|
64
|
||||||
@ -141,6 +121,15 @@ impl TestParams {
|
|||||||
ceiling_log2(self.n_slots)
|
ceiling_log2(self.n_slots)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_circuit_params(&self) -> CircuitParams{
|
||||||
|
CircuitParams{
|
||||||
|
max_depth: self.max_depth,
|
||||||
|
max_log2_n_slots: self.dataset_max_depth(),
|
||||||
|
block_tree_depth: self.bot_depth(),
|
||||||
|
n_field_elems_per_cell: self.n_field_elems_per_cell(),
|
||||||
|
n_samples:self.n_samples,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn log2(x: usize) -> usize {
|
pub fn log2(x: usize) -> usize {
|
||||||
@ -156,7 +145,7 @@ pub fn ceiling_log2(x: usize) -> usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// load test params from env
|
/// load test params from env
|
||||||
impl TestParams {
|
impl InputParams {
|
||||||
pub fn from_env() -> Result<Self> {
|
pub fn from_env() -> Result<Self> {
|
||||||
let max_depth = env::var("MAXDEPTH")
|
let max_depth = env::var("MAXDEPTH")
|
||||||
.context("MAXDEPTH not set")?
|
.context("MAXDEPTH not set")?
|
||||||
@ -208,7 +197,7 @@ impl TestParams {
|
|||||||
.parse::<usize>()
|
.parse::<usize>()
|
||||||
.context("Invalid NCELLS")?;
|
.context("Invalid NCELLS")?;
|
||||||
|
|
||||||
Ok(TestParams {
|
Ok(InputParams {
|
||||||
max_depth,
|
max_depth,
|
||||||
max_slots,
|
max_slots,
|
||||||
cell_size,
|
cell_size,
|
||||||
@ -226,18 +215,12 @@ impl TestParams {
|
|||||||
/// load params from env
|
/// load params from env
|
||||||
impl Params {
|
impl Params {
|
||||||
pub fn from_env() -> Result<Self> {
|
pub fn from_env() -> Result<Self> {
|
||||||
let test_params = TestParams::from_env()?;
|
let input_params = InputParams::from_env()?;
|
||||||
let circuit_params = CircuitParams{
|
let circuit_params = input_params.get_circuit_params();
|
||||||
max_depth: test_params.max_depth,
|
|
||||||
max_log2_n_slots: test_params.dataset_max_depth(),
|
|
||||||
block_tree_depth: test_params.bot_depth(),
|
|
||||||
n_field_elems_per_cell: test_params.n_field_elems_per_cell(),
|
|
||||||
n_samples:test_params.n_samples,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Params{
|
Ok(Params{
|
||||||
circuit_params,
|
circuit_params,
|
||||||
test: test_params,
|
input_params,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -8,7 +8,7 @@ use std::fs::File;
|
|||||||
use std::io::{BufReader, Write};
|
use std::io::{BufReader, Write};
|
||||||
use plonky2_field::types::{Field, PrimeField64};
|
use plonky2_field::types::{Field, PrimeField64};
|
||||||
use crate::gen_input::gen_testing_circuit_input;
|
use crate::gen_input::gen_testing_circuit_input;
|
||||||
use crate::params::TestParams;
|
use crate::params::InputParams;
|
||||||
|
|
||||||
/// export circuit input to json file
|
/// export circuit input to json file
|
||||||
pub fn export_circ_input_to_json<
|
pub fn export_circ_input_to_json<
|
||||||
@ -32,7 +32,7 @@ pub fn export_circ_input_to_json<
|
|||||||
pub fn generate_and_export_circ_input_to_json<
|
pub fn generate_and_export_circ_input_to_json<
|
||||||
F: RichField + Extendable<D> + Poseidon2 + Serialize,
|
F: RichField + Extendable<D> + Poseidon2 + Serialize,
|
||||||
const D: usize,
|
const D: usize,
|
||||||
>(params: &TestParams, filename: &str) -> anyhow::Result<()> {
|
>(params: &InputParams, filename: &str) -> anyhow::Result<()> {
|
||||||
|
|
||||||
let circ_input = gen_testing_circuit_input::<F,D>(params);
|
let circ_input = gen_testing_circuit_input::<F,D>(params);
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@ use plonky2_poseidon2::poseidon2_hash::poseidon2::Poseidon2;
|
|||||||
use codex_plonky2_circuits::circuits::sample_cells::SampleCircuitInput;
|
use codex_plonky2_circuits::circuits::sample_cells::SampleCircuitInput;
|
||||||
use plonky2::plonk::proof::CompressedProofWithPublicInputs;
|
use plonky2::plonk::proof::CompressedProofWithPublicInputs;
|
||||||
use serde_json::to_writer_pretty;
|
use serde_json::to_writer_pretty;
|
||||||
use crate::params::TestParams;
|
use crate::params::InputParams;
|
||||||
|
|
||||||
// Function to export proof with public input to json file
|
// Function to export proof with public input to json file
|
||||||
fn export_proof_with_pi_to_json<F, C, const D: usize>(
|
fn export_proof_with_pi_to_json<F, C, const D: usize>(
|
||||||
@ -46,7 +46,7 @@ pub fn read_bytes_from_file<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::params::{C, D, F, HF};
|
use crate::params::{C, D, F, HF, InputParams, Params};
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use codex_plonky2_circuits::circuits::params::CircuitParams;
|
use codex_plonky2_circuits::circuits::params::CircuitParams;
|
||||||
use codex_plonky2_circuits::circuits::sample_cells::SampleCircuit;
|
use codex_plonky2_circuits::circuits::sample_cells::SampleCircuit;
|
||||||
@ -61,7 +61,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_export_circ_input_to_json() -> anyhow::Result<()> {
|
fn test_export_circ_input_to_json() -> anyhow::Result<()> {
|
||||||
// Create Params
|
// Create Params
|
||||||
let params = TestParams::default();
|
let params = Params::default().input_params;
|
||||||
// Export the circuit input to JSON
|
// Export the circuit input to JSON
|
||||||
generate_and_export_circ_input_to_json::<F,D>(¶ms, "input.json")?;
|
generate_and_export_circ_input_to_json::<F,D>(¶ms, "input.json")?;
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_export_import_circ_input() -> anyhow::Result<()> {
|
fn test_export_import_circ_input() -> anyhow::Result<()> {
|
||||||
// Create Params instance
|
// Create Params instance
|
||||||
let params = TestParams::default();
|
let params = Params::default().input_params;
|
||||||
|
|
||||||
// Export the circuit input to JSON
|
// Export the circuit input to JSON
|
||||||
let original_circ_input = gen_testing_circuit_input(¶ms);
|
let original_circ_input = gen_testing_circuit_input(¶ms);
|
||||||
@ -109,13 +109,11 @@ mod tests {
|
|||||||
// reads the json input from file and runs the circuit
|
// reads the json input from file and runs the circuit
|
||||||
#[test]
|
#[test]
|
||||||
fn test_read_json_and_run_circuit() -> anyhow::Result<()> {
|
fn test_read_json_and_run_circuit() -> anyhow::Result<()> {
|
||||||
let params = TestParams::default();
|
|
||||||
|
|
||||||
// Create the circuit
|
// Create the circuit
|
||||||
let config = CircuitConfig::standard_recursion_config();
|
let config = CircuitConfig::standard_recursion_config();
|
||||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||||
|
|
||||||
let circuit_params = CircuitParams::default();
|
let circuit_params = Params::default().circuit_params;
|
||||||
|
|
||||||
let circ = SampleCircuit::<F,D,HF>::new(circuit_params.clone());
|
let circ = SampleCircuit::<F,D,HF>::new(circuit_params.clone());
|
||||||
let mut targets = circ.sample_slot_circuit_with_public_input(&mut builder)?;
|
let mut targets = circ.sample_slot_circuit_with_public_input(&mut builder)?;
|
||||||
@ -152,7 +150,7 @@ mod tests {
|
|||||||
// NOTE: expects that the json input proof uses the default params
|
// NOTE: expects that the json input proof uses the default params
|
||||||
#[test]
|
#[test]
|
||||||
fn test_read_json_and_verify() -> anyhow::Result<()> {
|
fn test_read_json_and_verify() -> anyhow::Result<()> {
|
||||||
let params = TestParams::default();
|
let params = Params::default().input_params;
|
||||||
|
|
||||||
// Import the circuit input from JSON
|
// Import the circuit input from JSON
|
||||||
let imported_circ_input: SampleCircuitInput<F, D> = import_circ_input_from_json("input.json")?;
|
let imported_circ_input: SampleCircuitInput<F, D> = import_circ_input_from_json("input.json")?;
|
||||||
@ -171,13 +169,14 @@ mod tests {
|
|||||||
// test out custom default gate and generator serializers to export/import circuit data
|
// test out custom default gate and generator serializers to export/import circuit data
|
||||||
#[test]
|
#[test]
|
||||||
fn test_circuit_data_serializer() -> anyhow::Result<()> {
|
fn test_circuit_data_serializer() -> anyhow::Result<()> {
|
||||||
let params = TestParams::default();
|
let params = Params::default();
|
||||||
|
let input_params = params.input_params;
|
||||||
|
|
||||||
// Create the circuit
|
// Create the circuit
|
||||||
let config = CircuitConfig::standard_recursion_config();
|
let config = CircuitConfig::standard_recursion_config();
|
||||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||||
|
|
||||||
let circuit_params = CircuitParams::default();
|
let circuit_params = params.circuit_params;
|
||||||
let circ = SampleCircuit::<F,D,HF>::new(circuit_params.clone());
|
let circ = SampleCircuit::<F,D,HF>::new(circuit_params.clone());
|
||||||
let mut targets = circ.sample_slot_circuit_with_public_input(&mut builder)?;
|
let mut targets = circ.sample_slot_circuit_with_public_input(&mut builder)?;
|
||||||
|
|
||||||
@ -185,7 +184,7 @@ mod tests {
|
|||||||
let mut pw = PartialWitness::new();
|
let mut pw = PartialWitness::new();
|
||||||
|
|
||||||
// gen circ input
|
// gen circ input
|
||||||
let imported_circ_input: SampleCircuitInput<F, D> = gen_testing_circuit_input::<F,D>(¶ms);
|
let imported_circ_input: SampleCircuitInput<F, D> = gen_testing_circuit_input::<F,D>(&input_params);
|
||||||
circ.sample_slot_assign_witness(&mut pw, &targets, &imported_circ_input)?;
|
circ.sample_slot_assign_witness(&mut pw, &targets, &imported_circ_input)?;
|
||||||
|
|
||||||
// Build the circuit
|
// Build the circuit
|
||||||
@ -223,13 +222,14 @@ mod tests {
|
|||||||
// test proof with public input serialization
|
// test proof with public input serialization
|
||||||
#[test]
|
#[test]
|
||||||
fn test_proof_with_pi_serializer() -> anyhow::Result<()> {
|
fn test_proof_with_pi_serializer() -> anyhow::Result<()> {
|
||||||
let params = TestParams::default();
|
let params = Params::default();
|
||||||
|
let input_params = params.input_params;
|
||||||
|
|
||||||
// Create the circuit
|
// Create the circuit
|
||||||
let config = CircuitConfig::standard_recursion_config();
|
let config = CircuitConfig::standard_recursion_config();
|
||||||
let mut builder = CircuitBuilder::<F, D>::new(config);
|
let mut builder = CircuitBuilder::<F, D>::new(config);
|
||||||
|
|
||||||
let circuit_params = CircuitParams::default();
|
let circuit_params = params.circuit_params;
|
||||||
let circ = SampleCircuit::<F,D,HF>::new(circuit_params.clone());
|
let circ = SampleCircuit::<F,D,HF>::new(circuit_params.clone());
|
||||||
let mut targets = circ.sample_slot_circuit_with_public_input(&mut builder)?;
|
let mut targets = circ.sample_slot_circuit_with_public_input(&mut builder)?;
|
||||||
|
|
||||||
@ -237,7 +237,7 @@ mod tests {
|
|||||||
let mut pw = PartialWitness::new();
|
let mut pw = PartialWitness::new();
|
||||||
|
|
||||||
// gen circ input
|
// gen circ input
|
||||||
let imported_circ_input: SampleCircuitInput<F, D> = gen_testing_circuit_input::<F,D>(¶ms);
|
let imported_circ_input: SampleCircuitInput<F, D> = gen_testing_circuit_input::<F,D>(&input_params);
|
||||||
circ.sample_slot_assign_witness(&mut pw, &targets, &imported_circ_input)?;
|
circ.sample_slot_assign_witness(&mut pw, &targets, &imported_circ_input)?;
|
||||||
|
|
||||||
// Build the circuit
|
// Build the circuit
|
||||||
|
|||||||
@ -10,7 +10,7 @@ fn main() -> Result<()> {
|
|||||||
let params = Params::from_env()?;
|
let params = Params::from_env()?;
|
||||||
|
|
||||||
// generate circuit input with given parameters
|
// generate circuit input with given parameters
|
||||||
let circ_input = gen_testing_circuit_input::<F,D>(¶ms.test);
|
let circ_input = gen_testing_circuit_input::<F,D>(¶ms.input_params);
|
||||||
|
|
||||||
// export circuit parameters to json file
|
// export circuit parameters to json file
|
||||||
let filename= "input.json";
|
let filename= "input.json";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user