Skip to content

Commit

Permalink
Merge pull request #256 from ArtiomTr/enable-bgmw-by-default
Browse files Browse the repository at this point in the history
Enable BGMW by default
  • Loading branch information
sauliusgrigaitis authored Jan 26, 2024
2 parents ec829c5 + 5eacbe8 commit 86c23f4
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion arkworks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ rand = { version = "0.8.5" }
[features]
default = [
"std",
"rand"
"rand",
"bgmw"
]
std = [
"ark-ff/std", "ark-ec/std", "ark-poly/std", "ark-std/std",
Expand Down
29 changes: 22 additions & 7 deletions arkworks/src/eip_4844.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extern crate alloc;

use crate::kzg_proofs::{FFTSettings, KZGSettings};
use crate::kzg_types::{ArkFr, ArkG1, ArkG2};
use crate::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine, ArkG2};
use blst::{blst_fr, blst_p1, blst_p2};
use kzg::common_utils::reverse_bit_order;
use kzg::eip_4844::{
blob_to_kzg_commitment_rust, compute_blob_kzg_proof_rust, compute_kzg_proof_rust,
load_trusted_setup_rust, verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust,
verify_kzg_proof_rust, Blob, Bytes32, Bytes48, CKZGSettings, KZGCommitment, KZGProof,
BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1, BYTES_PER_G2, C_KZG_RET, C_KZG_RET_BADARGS,
C_KZG_RET_OK, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS,
PrecomputationTableManager, BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1, BYTES_PER_G2, C_KZG_RET,
C_KZG_RET_BADARGS, C_KZG_RET_OK, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS,
TRUSTED_SETUP_NUM_G2_POINTS,
};
use kzg::{cfg_into_iter, Fr, G1};
Expand All @@ -28,6 +28,9 @@ use rayon::prelude::*;
#[cfg(feature = "std")]
use kzg::eip_4844::load_trusted_setup_string;

static mut PRECOMPUTATION_TABLES: PrecomputationTableManager<ArkFr, ArkG1, ArkFp, ArkG1Affine> =
PrecomputationTableManager::new();

#[cfg(feature = "std")]
pub fn load_trusted_setup_filename_rust(filepath: &str) -> Result<KZGSettings, String> {
let mut file = File::open(filepath).map_err(|_| "Unable to open file".to_string())?;
Expand Down Expand Up @@ -178,9 +181,14 @@ pub unsafe extern "C" fn load_trusted_setup(
let g1_bytes = core::slice::from_raw_parts(g1_bytes, n1 * BYTES_PER_G1);
let g2_bytes = core::slice::from_raw_parts(g2_bytes, n2 * BYTES_PER_G2);
TRUSTED_SETUP_NUM_G1_POINTS = g1_bytes.len() / BYTES_PER_G1;
let settings = handle_ckzg_badargs!(load_trusted_setup_rust(g1_bytes, g2_bytes));
let mut settings = handle_ckzg_badargs!(load_trusted_setup_rust(g1_bytes, g2_bytes));

let c_settings = kzg_settings_to_c(&settings);

PRECOMPUTATION_TABLES.save_precomputation(settings.precomputation.take(), &c_settings);

*out = c_settings;

*out = kzg_settings_to_c(&settings);
C_KZG_RET_OK
}

Expand All @@ -202,12 +210,17 @@ pub unsafe extern "C" fn load_trusted_setup_file(
// deallocate its KZGSettings pointer when no exception is thrown).
return C_KZG_RET_BADARGS;
}
let settings = handle_ckzg_badargs!(load_trusted_setup_rust(
let mut settings = handle_ckzg_badargs!(load_trusted_setup_rust(
g1_bytes.as_slice(),
g2_bytes.as_slice()
));

*out = kzg_settings_to_c(&settings);
let c_settings = kzg_settings_to_c(&settings);

PRECOMPUTATION_TABLES.save_precomputation(settings.precomputation.take(), &c_settings);

*out = c_settings;

C_KZG_RET_OK
}

Expand All @@ -218,6 +231,8 @@ pub unsafe extern "C" fn free_trusted_setup(s: *mut CKZGSettings) {
return;
}

PRECOMPUTATION_TABLES.remove_precomputation(&*s);

let max_width = (*s).max_width as usize;
let roots = Box::from_raw(core::slice::from_raw_parts_mut(
(*s).roots_of_unity,
Expand Down
5 changes: 4 additions & 1 deletion arkworks/src/kzg_proofs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![allow(non_camel_case_types)]

extern crate alloc;
use super::utils::{blst_poly_into_pc_poly, PolyData};
use crate::consts::{G1_GENERATOR, G2_GENERATOR};
use crate::kzg_types::{ArkFp, ArkFr, ArkG1Affine};
use crate::kzg_types::{ArkFr as BlstFr, ArkG1, ArkG2};
use alloc::sync::Arc;
use ark_bls12_381::Bls12_381;
use ark_ec::pairing::Pairing;
use ark_ec::CurveGroup;
Expand Down Expand Up @@ -46,7 +49,7 @@ pub struct KZGSettings {
pub fs: FFTSettings,
pub secret_g1: Vec<ArkG1>,
pub secret_g2: Vec<ArkG2>,
pub precomputation: Option<PrecomputationTable<ArkFr, ArkG1, ArkFp, ArkG1Affine>>,
pub precomputation: Option<Arc<PrecomputationTable<ArkFr, ArkG1, ArkFp, ArkG1Affine>>>,
}

pub fn generate_trusted_setup(len: usize, secret: [u8; 32usize]) -> (Vec<ArkG1>, Vec<ArkG2>) {
Expand Down
7 changes: 5 additions & 2 deletions arkworks/src/kzg_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ use kzg::{
};
use std::ops::{AddAssign, Mul, Neg, Sub};

extern crate alloc;
use alloc::sync::Arc;

fn bytes_be_to_uint64(inp: &[u8]) -> u64 {
u64::from_be_bytes(inp.try_into().expect("Input wasn't 8 elements..."))
}
Expand Down Expand Up @@ -630,7 +633,7 @@ impl KZGSettings<ArkFr, ArkG1, ArkG2, LFFTSettings, PolyData, ArkFp, ArkG1Affine
secret_g1: secret_g1.to_vec(),
secret_g2: secret_g2.to_vec(),
fs: fft_settings.clone(),
precomputation: precompute(secret_g1).ok().flatten(),
precomputation: precompute(secret_g1).ok().flatten().map(Arc::new),
})
}

Expand Down Expand Up @@ -793,7 +796,7 @@ impl KZGSettings<ArkFr, ArkG1, ArkG2, LFFTSettings, PolyData, ArkFp, ArkG1Affine
}

fn get_precomputation(&self) -> Option<&PrecomputationTable<ArkFr, ArkG1, ArkFp, ArkG1Affine>> {
self.precomputation.as_ref()
self.precomputation.as_ref().map(|v| v.as_ref())
}
}

Expand Down
2 changes: 1 addition & 1 deletion blst/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ rand = { version = "0.8.5", optional = true }
rayon = { version = "1.8.0", optional = true }
smallvec = { version = "1.11.1", features = ["const_generics"] }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
siphasher = { version = "1.0.0", default-features = false }

[dev-dependencies]
criterion = "0.5.1"
Expand All @@ -23,6 +22,7 @@ rand = "0.8.5"
default = [
"std",
"rand",
"bgmw"
]
std = [
"hex/std",
Expand Down
50 changes: 5 additions & 45 deletions blst/src/eip_4844.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
extern crate alloc;

use alloc::boxed::Box;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::hash::{Hash, Hasher};
use core::ptr::null_mut;
use kzg::common_utils::reverse_bit_order;
use kzg::eip_4844::{
blob_to_kzg_commitment_rust, compute_blob_kzg_proof_rust, compute_kzg_proof_rust,
load_trusted_setup_rust, verify_blob_kzg_proof_batch_rust, verify_blob_kzg_proof_rust,
verify_kzg_proof_rust,
verify_kzg_proof_rust, PrecomputationTableManager,
};
use kzg::msm::precompute::PrecomputationTable;
use kzg::{cfg_into_iter, Fr, G1};
#[cfg(feature = "std")]
use libc::FILE;
use siphasher::sip::SipHasher;
#[cfg(feature = "std")]
use std::fs::File;
#[cfg(feature = "std")]
Expand Down Expand Up @@ -45,43 +40,8 @@ use crate::types::kzg_settings::FsKZGSettings;
#[cfg(feature = "parallel")]
use rayon::prelude::*;

struct PrecomputationTableManager {
tables: BTreeMap<u64, Arc<PrecomputationTable<FsFr, FsG1, FsFp, FsG1Affine>>>,
}

impl PrecomputationTableManager {
pub const fn new() -> Self {
Self {
tables: BTreeMap::new(),
}
}

pub fn save_precomputation(&mut self, settings: &mut FsKZGSettings, c_settings: &CKZGSettings) {
if let Some(precomputation) = settings.precomputation.take() {
self.tables
.insert(Self::get_key(c_settings), precomputation);
}
}

pub fn remove_precomputation(&mut self, c_settings: &CKZGSettings) {
self.tables.remove(&Self::get_key(c_settings));
}

pub fn get_precomputation(
&self,
c_settings: &CKZGSettings,
) -> Option<Arc<PrecomputationTable<FsFr, FsG1, FsFp, FsG1Affine>>> {
self.tables.get(&Self::get_key(c_settings)).cloned()
}

fn get_key(settings: &CKZGSettings) -> u64 {
let mut hasher = SipHasher::new();
settings.g1_values.hash(&mut hasher);
hasher.finish()
}
}

static mut PRECOMPUTATION_TABLES: PrecomputationTableManager = PrecomputationTableManager::new();
static mut PRECOMPUTATION_TABLES: PrecomputationTableManager<FsFr, FsG1, FsFp, FsG1Affine> =
PrecomputationTableManager::new();

#[cfg(feature = "std")]
pub fn load_trusted_setup_filename_rust(filepath: &str) -> Result<FsKZGSettings, String> {
Expand Down Expand Up @@ -236,7 +196,7 @@ pub unsafe extern "C" fn load_trusted_setup(

let c_settings = kzg_settings_to_c(&settings);

PRECOMPUTATION_TABLES.save_precomputation(&mut settings, &c_settings);
PRECOMPUTATION_TABLES.save_precomputation(settings.precomputation.take(), &c_settings);

*out = c_settings;
C_KZG_RET_OK
Expand Down Expand Up @@ -267,7 +227,7 @@ pub unsafe extern "C" fn load_trusted_setup_file(

let c_settings = kzg_settings_to_c(&settings);

PRECOMPUTATION_TABLES.save_precomputation(&mut settings, &c_settings);
PRECOMPUTATION_TABLES.save_precomputation(settings.precomputation.take(), &c_settings);

*out = c_settings;

Expand Down
1 change: 1 addition & 0 deletions constantine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rand = "0.8.5"
default = [
"std",
"rand",
"bgmw"
]
std = [
"hex/std",
Expand Down
33 changes: 25 additions & 8 deletions constantine/src/eip_4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,26 @@ use std::io::Read;
use kzg::eip_4844::load_trusted_setup_string;

use kzg::eip_4844::{
Blob, Bytes32, Bytes48, CKZGSettings, KZGCommitment, KZGProof, BYTES_PER_FIELD_ELEMENT,
BYTES_PER_G1, BYTES_PER_G2, C_KZG_RET, C_KZG_RET_BADARGS, C_KZG_RET_OK,
FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS, TRUSTED_SETUP_NUM_G2_POINTS,
Blob, Bytes32, Bytes48, CKZGSettings, KZGCommitment, KZGProof, PrecomputationTableManager,
BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1, BYTES_PER_G2, C_KZG_RET, C_KZG_RET_BADARGS,
C_KZG_RET_OK, FIELD_ELEMENTS_PER_BLOB, TRUSTED_SETUP_NUM_G1_POINTS,
TRUSTED_SETUP_NUM_G2_POINTS,
};

use crate::types::fft_settings::CtFFTSettings;
use crate::types::fp::CtFp;
use crate::types::fr::CtFr;
use crate::types::g1::CtG1;
use crate::types::g1::{CtG1, CtG1Affine};

use crate::types::g2::CtG2;
use crate::types::kzg_settings::CtKZGSettings;

#[cfg(feature = "parallel")]
use rayon::prelude::*;

static mut PRECOMPUTATION_TABLES: PrecomputationTableManager<CtFr, CtG1, CtFp, CtG1Affine> =
PrecomputationTableManager::new();

#[cfg(feature = "std")]
pub fn load_trusted_setup_filename_rust(filepath: &str) -> Result<CtKZGSettings, String> {
let mut file = File::open(filepath).map_err(|_| "Unable to open file".to_string())?;
Expand Down Expand Up @@ -186,9 +191,14 @@ pub unsafe extern "C" fn load_trusted_setup(
let g1_bytes = core::slice::from_raw_parts(g1_bytes, n1 * BYTES_PER_G1);
let g2_bytes = core::slice::from_raw_parts(g2_bytes, n2 * BYTES_PER_G2);
TRUSTED_SETUP_NUM_G1_POINTS = g1_bytes.len() / BYTES_PER_G1;
let settings = handle_ckzg_badargs!(load_trusted_setup_rust(g1_bytes, g2_bytes));
let mut settings = handle_ckzg_badargs!(load_trusted_setup_rust(g1_bytes, g2_bytes));

let c_settings = kzg_settings_to_c(&settings);

PRECOMPUTATION_TABLES.save_precomputation(settings.precomputation.take(), &c_settings);

*out = c_settings;

*out = kzg_settings_to_c(&settings);
C_KZG_RET_OK
}

Expand All @@ -210,12 +220,17 @@ pub unsafe extern "C" fn load_trusted_setup_file(
// deallocate its KZGSettings pointer when no exception is thrown).
return C_KZG_RET_BADARGS;
}
let settings = handle_ckzg_badargs!(load_trusted_setup_rust(
let mut settings = handle_ckzg_badargs!(load_trusted_setup_rust(
g1_bytes.as_slice(),
g2_bytes.as_slice()
));

*out = kzg_settings_to_c(&settings);
let c_settings = kzg_settings_to_c(&settings);

PRECOMPUTATION_TABLES.save_precomputation(settings.precomputation.take(), &c_settings);

*out = c_settings;

C_KZG_RET_OK
}

Expand Down Expand Up @@ -251,6 +266,8 @@ pub unsafe extern "C" fn free_trusted_setup(s: *mut CKZGSettings) {
return;
}

PRECOMPUTATION_TABLES.remove_precomputation(&*s);

let max_width = (*s).max_width as usize;
let roots = Box::from_raw(core::slice::from_raw_parts_mut(
(*s).roots_of_unity,
Expand Down
7 changes: 4 additions & 3 deletions constantine/src/types/kzg_settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate alloc;

use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;

use kzg::msm::precompute::{precompute, PrecomputationTable};
Expand All @@ -22,7 +23,7 @@ pub struct CtKZGSettings {
pub fs: CtFFTSettings,
pub secret_g1: Vec<CtG1>,
pub secret_g2: Vec<CtG2>,
pub precomputation: Option<PrecomputationTable<CtFr, CtG1, CtFp, CtG1Affine>>,
pub precomputation: Option<Arc<PrecomputationTable<CtFr, CtG1, CtFp, CtG1Affine>>>,
}

impl KZGSettings<CtFr, CtG1, CtG2, CtFFTSettings, CtPoly, CtFp, CtG1Affine> for CtKZGSettings {
Expand All @@ -36,7 +37,7 @@ impl KZGSettings<CtFr, CtG1, CtG2, CtFFTSettings, CtPoly, CtFp, CtG1Affine> for
secret_g1: secret_g1.to_vec(),
secret_g2: secret_g2.to_vec(),
fs: fft_settings.clone(),
precomputation: precompute(secret_g1).ok().flatten(),
precomputation: precompute(secret_g1).ok().flatten().map(Arc::new),
})
}

Expand Down Expand Up @@ -202,6 +203,6 @@ impl KZGSettings<CtFr, CtG1, CtG2, CtFFTSettings, CtPoly, CtFp, CtG1Affine> for
}

fn get_precomputation(&self) -> Option<&PrecomputationTable<CtFr, CtG1, CtFp, CtG1Affine>> {
self.precomputation.as_ref()
self.precomputation.as_ref().map(|v| v.as_ref())
}
}
4 changes: 3 additions & 1 deletion kzg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sha2 = { version = "0.10.6", default-features = false }
num_cpus = { version = "1.16.0", optional = true }
rayon = { version = "1.8.0", optional = true }
threadpool = { version = "^1.8.1", optional = true }
siphasher = { version = "1.0.0", default-features = false }

[features]
default = [
Expand All @@ -22,7 +23,8 @@ parallel = [
"dep:threadpool"
]
std = [
"sha2/std"
"sha2/std",
"siphasher/std"
]
rand = []
arkmsm = []
Expand Down
Loading

0 comments on commit 86c23f4

Please sign in to comment.