Skip to content

Commit

Permalink
fibonacci sequence generated from Powdr is proved by stwo successful,…
Browse files Browse the repository at this point in the history
… sequence length is 6. stwo use data parallel structure to prove, which means it can prove many fibonacci instance (32 in code) at the same time, now I just get the fibonacci sequence from powdr, copy it to 32 identical instances, then use stwo to prove, many manul process still #1783
  • Loading branch information
Shuang Wu authored and ShuangWu121 committed Sep 24, 2024
1 parent 240a790 commit 5e1a653
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
85 changes: 85 additions & 0 deletions backend/src/stwo/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,89 @@ pub fn generate_stwo_trace<T>(witness: &[(String, Vec<T>)], log_n_instances: u32



}

pub fn generate_parallel_stwo_trace_by_witness_repitition<T: Clone>(witness: &[(String, Vec<T>)], log_n_instances: u32
)-> ColumnVec<CircleEvaluation<SimdBackend, BaseField, BitReversedOrder>> {

let trace: Vec<PackedBaseField> = witness
.iter()
.flat_map(|(_, vec)| {
vec.iter().flat_map(|mersenne| {

let ptr = mersenne as *const T as *const u32;

let value = unsafe {
*ptr // Dereference the pointer to get the u32 value
};

// Repeat the value 32 times
let repeated = vec![value; 32];

// Split the repeated vector into two chunks of 16 elements each
let chunk1: [u32; N_LANES] = repeated[0..16]
.try_into()
.expect("Chunk should be of size N_LANES");
let chunk2: [u32; N_LANES] = repeated[16..32]
.try_into()
.expect("Chunk should be of size N_LANES");

// Convert chunks to PackedBaseField
// Note: We use unsafe block because PackedBaseField::load is unsafe
unsafe {
vec![
PackedBaseField::load(chunk1.as_ptr()),
PackedBaseField::load(chunk2.as_ptr()),
]
}
})
})
.collect(); // Collect the flattened iterator into a Vec<PackedBaseField>


println!("from generate stwo trace trace");
println!("{:?}", trace);

let mut trace_stwo= (0..6)//fibonacci length
.map(|_| Col::<SimdBackend, BaseField>::zeros(1 << log_n_instances))
.collect_vec();


// column x
trace_stwo[0].data[0]= trace[0];
trace_stwo[0].data[1]= trace[1];

println!("from generate stwo trace trace 64 ......");
println!("{:?}", trace[64]);

println!("from generate stwo trace trace 65 ......");
println!("{:?}", trace[65]);

trace_stwo[1].data[0]= trace[64];
trace_stwo[1].data[1]= trace[65];

trace_stwo[2].data[0]= trace[66];
trace_stwo[2].data[1]= trace[67];

trace_stwo[3].data[0]= trace[68];
trace_stwo[3].data[1]= trace[69];

trace_stwo[4].data[0]= trace[70];
trace_stwo[4].data[1]= trace[71];

trace_stwo[5].data[0]= trace[72];
trace_stwo[5].data[1]= trace[73];

println!("from generate stwo trace trace_stwo repititions");
println!("{:?}", trace_stwo);

let domain = CanonicCoset::new(5).circle_domain();
trace_stwo
.into_iter()
.map(|eval| CircleEvaluation::<SimdBackend, BaseField, BitReversedOrder>::new(domain, eval))
.collect_vec()




}
9 changes: 6 additions & 3 deletions backend/src/stwo/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use powdr_executor::witgen::WitgenCallback;
use powdr_number::{DegreeType, FieldElement, KnownField};
use super::circuit_builder::PowdrCircuit;
use super::circuit_builder::generate_stwo_trace;
use super::circuit_builder::generate_parallel_stwo_trace_by_witness_repitition;
use super::circuit_builder::WideFibonacciComponent;
use super::circuit_builder::WideFibonacciEval;

Expand Down Expand Up @@ -135,7 +136,7 @@ impl<F: FieldElement> StwoProver<F> {
) {

const LOG_N_INSTANCES: u32 = 5;
const FIB_SEQUENCE_LENGTH: usize=32;
const FIB_SEQUENCE_LENGTH: usize=6;



Expand All @@ -159,10 +160,12 @@ impl<F: FieldElement> StwoProver<F> {
let circuit = PowdrCircuit::new(&self.analyzed)
.with_witgen_callback(witgen_callback)
.with_witness(witness);
print!("witness from powdr {:?}", witness );

let trace = generate_stwo_trace(witness,LOG_N_INSTANCES);
//let trace = generate_stwo_trace(witness,LOG_N_INSTANCES);
let trace=generate_parallel_stwo_trace_by_witness_repitition(witness, LOG_N_INSTANCES);

println!("this is from the generate stwo trace in circle domain \n {:?}",generate_stwo_trace(witness,LOG_N_INSTANCES));
println!("this is from the generate stwo trace in repitition \n {:?}",generate_parallel_stwo_trace_by_witness_repitition(witness,LOG_N_INSTANCES));

let mut tree_builder = commitment_scheme.tree_builder();
tree_builder.extend_evals(trace);
Expand Down

0 comments on commit 5e1a653

Please sign in to comment.