From ce63663f5aefbf60c1779628b28d422cdad73d5a Mon Sep 17 00:00:00 2001 From: Xiang Xie Date: Fri, 26 Jul 2024 23:16:09 +0800 Subject: [PATCH] refine --- pcs/benches/brakedown_pcs.rs | 30 +++++++++++-------------- pcs/examples/brakedown_pcs.rs | 6 ++--- pcs/src/utils/code/expander.rs | 37 +++++-------------------------- pcs/tests/test_code.rs | 2 +- pcs/tests/test_pcs.rs | 40 ++++++++++++++++------------------ 5 files changed, 42 insertions(+), 73 deletions(-) diff --git a/pcs/benches/brakedown_pcs.rs b/pcs/benches/brakedown_pcs.rs index 60a2cb92..75e9915b 100644 --- a/pcs/benches/brakedown_pcs.rs +++ b/pcs/benches/brakedown_pcs.rs @@ -15,6 +15,9 @@ use rand::Rng; use sha2::Sha256; type FF = BabyBear; +type EF = BabyBearExetension; +type Hash = Sha256; +const BASE_FIELD_BITS: usize = 31; pub fn criterion_benchmark(c: &mut Criterion) { let num_vars = 20; @@ -25,21 +28,19 @@ pub fn criterion_benchmark(c: &mut Criterion) { let poly = DenseMultilinearExtension::from_evaluations_vec(num_vars, evaluations); - let code_spec = ExpanderCodeSpec::new(128, 0.1195, 0.0284, 1.9, 60, 10); + let code_spec = ExpanderCodeSpec::new(0.1195, 0.0284, 1.9, BASE_FIELD_BITS, 10); - let point: Vec = rand::thread_rng() + let point: Vec = rand::thread_rng() .sample_iter(FieldUniformSampler::new()) .take(num_vars) .collect(); let eval = poly.evaluate_ext(&point); - type Hash = Sha256; - let pp = - BrakedownPCS::, ExpanderCodeSpec, BabyBearExetension>::setup( - num_vars, - Some(code_spec), - ); + let pp = BrakedownPCS::, ExpanderCodeSpec, EF>::setup( + num_vars, + Some(code_spec), + ); let mut trans = Transcript::::new(); let mut comm = BrakedownPolyCommitment::default(); @@ -48,19 +49,14 @@ pub fn criterion_benchmark(c: &mut Criterion) { c.bench_function(&format!("num_vars: {}, commit time: ", num_vars), |b| { b.iter(|| { - (comm, state) = BrakedownPCS::< - FF, - Hash, - ExpanderCode, - ExpanderCodeSpec, - BabyBearExetension, - >::commit(&pp, &poly) + (comm, state) = + BrakedownPCS::, ExpanderCodeSpec, EF>::commit(&pp, &poly) }) }); c.bench_function(&format!("num_vars: {}, opening time: ", num_vars), |b| { b.iter(|| { - proof = BrakedownPCS::, ExpanderCodeSpec, BabyBearExetension>::open( + proof = BrakedownPCS::, ExpanderCodeSpec, EF>::open( &pp, &comm, &state, &point, &mut trans, ) }) @@ -70,7 +66,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { &format!("num_vars: {}, verification time: ", num_vars), |b| { b.iter(|| { - BrakedownPCS::, ExpanderCodeSpec,BabyBearExetension>::verify( + BrakedownPCS::, ExpanderCodeSpec, EF>::verify( &pp, &comm, &point, eval, &proof, &mut trans, ) }) diff --git a/pcs/examples/brakedown_pcs.rs b/pcs/examples/brakedown_pcs.rs index 36a89886..c3db6bda 100644 --- a/pcs/examples/brakedown_pcs.rs +++ b/pcs/examples/brakedown_pcs.rs @@ -13,6 +13,8 @@ use sha2::Sha256; type FF = BabyBear; type EF = BabyBearExetension; +type Hash = Sha256; +const BASE_FIELD_BITS: usize = 31; fn main() { let num_vars = 24; @@ -23,9 +25,7 @@ fn main() { let poly = DenseMultilinearExtension::from_evaluations_vec(num_vars, evaluations); - let code_spec = ExpanderCodeSpec::new(128, 0.1195, 0.0284, 1.9, 60, 10); - - type Hash = Sha256; + let code_spec = ExpanderCodeSpec::new(0.1195, 0.0284, 1.9, BASE_FIELD_BITS, 10); let start = Instant::now(); let pp = BrakedownPCS::, ExpanderCodeSpec, EF>::setup( diff --git a/pcs/src/utils/code/expander.rs b/pcs/src/utils/code/expander.rs index 449c14ae..21803d8b 100644 --- a/pcs/src/utils/code/expander.rs +++ b/pcs/src/utils/code/expander.rs @@ -19,9 +19,6 @@ use super::LinearCodeSpec; /// BrakedownCode Specification #[derive(Clone, Debug, Default, Serialize, Deserialize)] pub struct ExpanderCodeSpec { - // Security parameter - lambda: usize, - // Code parameter alpha alpha: f64, @@ -31,8 +28,8 @@ pub struct ExpanderCodeSpec { // Inversion of ideal code rate r: f64, - // Size of the field. - field_size_bits: usize, + // Size of the base field. + base_field_bits: usize, // The threshold to call ReedSoloman Code. recursion_threshold: usize, @@ -48,11 +45,11 @@ impl ExpanderCodeSpec { /// Create an instance of BrakedownCodeSpec #[inline] pub fn new( - lambda: usize, + // lambda: usize, alpha: f64, beta: f64, r: f64, - field_size_bits: usize, + base_field_bits: usize, recursion_threshold: usize, ) -> Self { assert!(r != 0.0); @@ -67,38 +64,16 @@ impl ExpanderCodeSpec { assert!((1f64 - alpha) * r > (1f64 + 2f64 * beta)); Self { - lambda, alpha, beta, r, - field_size_bits, + base_field_bits, recursion_threshold, distance, rate, } } - /// Return the size of the field. - #[inline] - pub fn field_size_bits(&self) -> usize { - self.field_size_bits - } - - /// Return the recursion threshold - #[inline] - pub fn recursion_threshold(&self) -> usize { - self.recursion_threshold - } - - /// The expected size of the extension field. - /// The soundness error specified by the security parameter for proximity test: (1-delta/3)^num_opening + (codeword_len/|F|) - /// The expeted extension field size is bounded by (codeword_len/|F|) - #[inline] - pub fn extension_field_size(&self, message_len: usize) -> usize { - let n = message_len; - self.codeword_len(n) * ceil(f64::powf(2f64, self.lambda as f64)) - } - /// The relative distance of the code pub fn distance(&self) -> f64 { self.distance @@ -145,7 +120,7 @@ impl ExpanderCodeSpec { // Return the num of nonzere elements in each row of B_n #[inline] fn d_n(&self, message_len: usize) -> usize { - let log2_q = self.field_size_bits as f64; + let log2_q = self.base_field_bits as f64; let n = message_len as f64; let alpha = self.alpha; let beta = self.beta; diff --git a/pcs/tests/test_code.rs b/pcs/tests/test_code.rs index a2c39c51..f26eca6c 100644 --- a/pcs/tests/test_code.rs +++ b/pcs/tests/test_code.rs @@ -14,7 +14,7 @@ fn linearity_check() { let mut rng = rand::thread_rng(); let field_distr = FieldUniformSampler::new(); - let spec = ExpanderCodeSpec::new(128, 0.1195, 0.0284, 1.420, 31, 30); + let spec = ExpanderCodeSpec::new(0.1195, 0.0284, 1.420, 31, 30); let brakedown_code: ExpanderCode = ExpanderCode::new(spec, 5000, &mut rng); let message_len = brakedown_code.message_len; diff --git a/pcs/tests/test_pcs.rs b/pcs/tests/test_pcs.rs index 001082d6..1f26605b 100644 --- a/pcs/tests/test_pcs.rs +++ b/pcs/tests/test_pcs.rs @@ -10,6 +10,10 @@ use rand::Rng; use sha2::Sha256; type FF = BabyBear; +type EF = BabyBearExetension; +type Hash = Sha256; +const BASE_FIELD_BITS: usize = 31; + #[test] fn pcs_test() { let num_vars = 10; @@ -20,32 +24,27 @@ fn pcs_test() { let poly = DenseMultilinearExtension::from_evaluations_vec(num_vars, evaluations); - type Hash = Sha256; - - let code_spec = ExpanderCodeSpec::new(128, 0.1195, 0.0284, 1.9, 60, 10); + // let code_spec = ExpanderCodeSpec::new(128, 0.1195, 0.0284, 1.9, 60, 10); + let code_spec = ExpanderCodeSpec::new(0.1195, 0.0284, 1.9, BASE_FIELD_BITS, 10); - let pp = - BrakedownPCS::, ExpanderCodeSpec, BabyBearExetension>::setup( - num_vars, - Some(code_spec), - ); + let pp = BrakedownPCS::, ExpanderCodeSpec, EF>::setup( + num_vars, + Some(code_spec), + ); let mut trans = Transcript::::new(); let (comm, state) = - BrakedownPCS::, ExpanderCodeSpec, BabyBearExetension>::commit( - &pp, &poly, - ); + BrakedownPCS::, ExpanderCodeSpec, EF>::commit(&pp, &poly); - let point: Vec = rand::thread_rng() + let point: Vec = rand::thread_rng() .sample_iter(FieldUniformSampler::new()) .take(num_vars) .collect(); - let proof = - BrakedownPCS::, ExpanderCodeSpec, BabyBearExetension>::open( - &pp, &comm, &state, &point, &mut trans, - ); + let proof = BrakedownPCS::, ExpanderCodeSpec, EF>::open( + &pp, &comm, &state, &point, &mut trans, + ); let buffer = proof.to_bytes().unwrap(); @@ -53,12 +52,11 @@ fn pcs_test() { let mut trans = Transcript::::new(); - let proof = BrakedownOpenProof::::from_bytes(&buffer).unwrap(); + let proof = BrakedownOpenProof::::from_bytes(&buffer).unwrap(); - let check = - BrakedownPCS::, ExpanderCodeSpec, BabyBearExetension>::verify( - &pp, &comm, &point, eval, &proof, &mut trans, - ); + let check = BrakedownPCS::, ExpanderCodeSpec, EF>::verify( + &pp, &comm, &point, eval, &proof, &mut trans, + ); assert!(check); }