Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangxiecrypto committed Jul 26, 2024
1 parent aa7d949 commit ce63663
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 73 deletions.
30 changes: 13 additions & 17 deletions pcs/benches/brakedown_pcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<BabyBearExetension> = rand::thread_rng()
let point: Vec<EF> = rand::thread_rng()
.sample_iter(FieldUniformSampler::new())
.take(num_vars)
.collect();

let eval = poly.evaluate_ext(&point);

type Hash = Sha256;
let pp =
BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, BabyBearExetension>::setup(
num_vars,
Some(code_spec),
);
let pp = BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, EF>::setup(
num_vars,
Some(code_spec),
);

let mut trans = Transcript::<FF>::new();
let mut comm = BrakedownPolyCommitment::default();
Expand All @@ -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<FF>,
ExpanderCodeSpec,
BabyBearExetension,
>::commit(&pp, &poly)
(comm, state) =
BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, EF>::commit(&pp, &poly)
})
});

c.bench_function(&format!("num_vars: {}, opening time: ", num_vars), |b| {
b.iter(|| {
proof = BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, BabyBearExetension>::open(
proof = BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, EF>::open(
&pp, &comm, &state, &point, &mut trans,
)
})
Expand All @@ -70,7 +66,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
&format!("num_vars: {}, verification time: ", num_vars),
|b| {
b.iter(|| {
BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec,BabyBearExetension>::verify(
BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, EF>::verify(
&pp, &comm, &point, eval, &proof, &mut trans,
)
})
Expand Down
6 changes: 3 additions & 3 deletions pcs/examples/brakedown_pcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, EF>::setup(
Expand Down
37 changes: 6 additions & 31 deletions pcs/src/utils/code/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pcs/tests/test_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FF32> = ExpanderCode::new(spec, 5000, &mut rng);

let message_len = brakedown_code.message_len;
Expand Down
40 changes: 19 additions & 21 deletions pcs/tests/test_pcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,45 +24,39 @@ 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::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, BabyBearExetension>::setup(
num_vars,
Some(code_spec),
);
let pp = BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, EF>::setup(
num_vars,
Some(code_spec),
);

let mut trans = Transcript::<FF>::new();

let (comm, state) =
BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, BabyBearExetension>::commit(
&pp, &poly,
);
BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, EF>::commit(&pp, &poly);

let point: Vec<BabyBearExetension> = rand::thread_rng()
let point: Vec<EF> = rand::thread_rng()
.sample_iter(FieldUniformSampler::new())
.take(num_vars)
.collect();

let proof =
BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, BabyBearExetension>::open(
&pp, &comm, &state, &point, &mut trans,
);
let proof = BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, EF>::open(
&pp, &comm, &state, &point, &mut trans,
);

let buffer = proof.to_bytes().unwrap();

let eval = poly.evaluate_ext(&point);

let mut trans = Transcript::<FF>::new();

let proof = BrakedownOpenProof::<FF, Hash, BabyBearExetension>::from_bytes(&buffer).unwrap();
let proof = BrakedownOpenProof::<FF, Hash, EF>::from_bytes(&buffer).unwrap();

let check =
BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, BabyBearExetension>::verify(
&pp, &comm, &point, eval, &proof, &mut trans,
);
let check = BrakedownPCS::<FF, Hash, ExpanderCode<FF>, ExpanderCodeSpec, EF>::verify(
&pp, &comm, &point, eval, &proof, &mut trans,
);

assert!(check);
}

0 comments on commit ce63663

Please sign in to comment.