Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use boxes & refs in Rust bindings #325

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 21 additions & 39 deletions bindings/rust/benches/kzg_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn generate_random_field_element(rng: &mut ThreadRng) -> Bytes32 {
arr.into()
}

fn generate_random_blob(rng: &mut ThreadRng) -> Blob {
fn generate_random_blob(rng: &mut ThreadRng) -> Box<Blob> {
let mut arr = [0u8; BYTES_PER_BLOB];
rng.fill(&mut arr[..]);
// Ensure that the blob is canonical by ensuring that
Expand All @@ -31,12 +31,12 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let kzg_settings = Arc::new(KzgSettings::load_trusted_setup_file(trusted_setup_file).unwrap());

let blobs: Vec<Blob> = (0..max_count)
.map(|_| generate_random_blob(&mut rng))
.map(|_| *generate_random_blob(&mut rng))
.collect();
let commitments: Vec<Bytes48> = blobs
.iter()
.map(|blob| {
KzgCommitment::blob_to_kzg_commitment(blob.clone(), &kzg_settings)
KzgCommitment::blob_to_kzg_commitment(blob, &kzg_settings)
.unwrap()
.to_bytes()
})
Expand All @@ -45,7 +45,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.iter()
.zip(commitments.iter())
.map(|(blob, commitment)| {
KzgProof::compute_blob_kzg_proof(blob.clone(), *commitment, &kzg_settings)
KzgProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings)
.unwrap()
.to_bytes()
})
Expand All @@ -55,52 +55,35 @@ pub fn criterion_benchmark(c: &mut Criterion) {
.collect();

c.bench_function("blob_to_kzg_commitment", |b| {
b.iter(|| {
KzgCommitment::blob_to_kzg_commitment(blobs.first().unwrap().clone(), &kzg_settings)
})
let blob = blobs.first().unwrap();
b.iter(|| KzgCommitment::blob_to_kzg_commitment(blob, &kzg_settings))
});

c.bench_function("compute_kzg_proof", |b| {
b.iter(|| {
KzgProof::compute_kzg_proof(
blobs.first().unwrap().clone(),
*fields.first().unwrap(),
&kzg_settings,
)
})
let blob = blobs.first().unwrap();
let z = fields.first().unwrap();
b.iter(|| KzgProof::compute_kzg_proof(blob, z, &kzg_settings))
});

c.bench_function("compute_blob_kzg_proof", |b| {
b.iter(|| {
KzgProof::compute_blob_kzg_proof(
blobs.first().unwrap().clone(),
*commitments.first().unwrap(),
&kzg_settings,
)
})
let blob = blobs.first().unwrap();
let commitment = commitments.first().unwrap();
b.iter(|| KzgProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings))
});

c.bench_function("verify_kzg_proof", |b| {
b.iter(|| {
KzgProof::verify_kzg_proof(
*commitments.first().unwrap(),
*fields.first().unwrap(),
*fields.first().unwrap(),
*proofs.first().unwrap(),
&kzg_settings,
)
})
let commitment = commitments.first().unwrap();
let z = fields.first().unwrap();
let y = fields.first().unwrap();
let proof = proofs.first().unwrap();
b.iter(|| KzgProof::verify_kzg_proof(commitment, z, y, proof, &kzg_settings))
});

c.bench_function("verify_blob_kzg_proof", |b| {
b.iter(|| {
KzgProof::verify_blob_kzg_proof(
blobs.first().unwrap().clone(),
*commitments.first().unwrap(),
*proofs.first().unwrap(),
&kzg_settings,
)
})
let blob = blobs.first().unwrap();
let commitment = commitments.first().unwrap();
let proof = proofs.first().unwrap();
b.iter(|| KzgProof::verify_blob_kzg_proof(blob, commitment, proof, &kzg_settings))
});

let mut group = c.benchmark_group("verify_blob_kzg_proof_batch");
Expand Down Expand Up @@ -130,7 +113,6 @@ pub fn criterion_benchmark(c: &mut Criterion) {
&proofs_subset,
&kzg_settings,
)
.unwrap();
},
BatchSize::LargeInput,
);
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const MINIMAL_FIELD_ELEMENTS_PER_BLOB: usize = 4;
/// Compiles blst.
//
// NOTE: This code is taken from https://github.com/supranational/blst `build.rs` `main`. The crate
// is not used as a depedency to avoid double link issues on dependants.
// is not used as a dependency to avoid double link issues on dependants.
fn compile_blst(blst_base_dir: PathBuf) {
// account for cross-compilation [by examining environment variables]
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
Expand Down
89 changes: 48 additions & 41 deletions bindings/rust/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl KZGSettings {
.as_os_str()
.to_str()
.ok_or(Error::InvalidTrustedSetup(format!(
"Unsuported non unicode file path"
"Unsupported non unicode file path"
)))?
.as_bytes()
};
Expand Down Expand Up @@ -180,7 +180,7 @@ impl KZGSettings {
};

// We don't really care if this succeeds.
let _uncheched_close_result = unsafe { libc::fclose(file_ptr) };
let _unchecked_close_result = unsafe { libc::fclose(file_ptr) };
drop(file_path);

result
Expand All @@ -194,7 +194,7 @@ impl Drop for KZGSettings {
}

impl Blob {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
pub fn from_bytes(bytes: &[u8]) -> Result<Box<Self>, Error> {
if bytes.len() != BYTES_PER_BLOB {
return Err(Error::InvalidBytesLength(format!(
"Invalid byte length. Expected {} got {}",
Expand All @@ -204,10 +204,10 @@ impl Blob {
}
let mut new_bytes = [0; BYTES_PER_BLOB];
new_bytes.copy_from_slice(bytes);
Ok(Self { bytes: new_bytes })
Ok(Box::new(Self { bytes: new_bytes }))
}

pub fn from_hex(hex_str: &str) -> Result<Self, Error> {
pub fn from_hex(hex_str: &str) -> Result<Box<Self>, Error> {
Self::from_bytes(&hex_to_bytes(hex_str)?)
}
}
Expand Down Expand Up @@ -277,8 +277,8 @@ impl KZGProof {
}

pub fn compute_kzg_proof(
blob: Blob,
z_bytes: Bytes32,
blob: &Blob,
z_bytes: &Bytes32,
kzg_settings: &KZGSettings,
) -> Result<(Self, Bytes32), Error> {
let mut kzg_proof = MaybeUninit::<KZGProof>::uninit();
Expand All @@ -287,8 +287,8 @@ impl KZGProof {
let res = compute_kzg_proof(
kzg_proof.as_mut_ptr(),
y_out.as_mut_ptr(),
&blob,
&z_bytes,
blob,
z_bytes,
kzg_settings,
);
if let C_KZG_RET::C_KZG_OK = res {
Expand All @@ -300,16 +300,16 @@ impl KZGProof {
}

pub fn compute_blob_kzg_proof(
blob: Blob,
commitment_bytes: Bytes48,
blob: &Blob,
commitment_bytes: &Bytes48,
kzg_settings: &KZGSettings,
) -> Result<Self, Error> {
let mut kzg_proof = MaybeUninit::<KZGProof>::uninit();
unsafe {
let res = compute_blob_kzg_proof(
kzg_proof.as_mut_ptr(),
&blob,
&commitment_bytes,
blob,
commitment_bytes,
kzg_settings,
);
if let C_KZG_RET::C_KZG_OK = res {
Expand All @@ -321,20 +321,20 @@ impl KZGProof {
}

pub fn verify_kzg_proof(
commitment_bytes: Bytes48,
z_bytes: Bytes32,
y_bytes: Bytes32,
proof_bytes: Bytes48,
commitment_bytes: &Bytes48,
z_bytes: &Bytes32,
y_bytes: &Bytes32,
proof_bytes: &Bytes48,
kzg_settings: &KZGSettings,
) -> Result<bool, Error> {
let mut verified: MaybeUninit<bool> = MaybeUninit::uninit();
unsafe {
let res = verify_kzg_proof(
verified.as_mut_ptr(),
&commitment_bytes,
&z_bytes,
&y_bytes,
&proof_bytes,
commitment_bytes,
z_bytes,
y_bytes,
proof_bytes,
kzg_settings,
);
if let C_KZG_RET::C_KZG_OK = res {
Expand All @@ -346,22 +346,23 @@ impl KZGProof {
}

pub fn verify_blob_kzg_proof(
blob: Blob,
commitment_bytes: Bytes48,
proof_bytes: Bytes48,
blob: &Blob,
commitment_bytes: &Bytes48,
proof_bytes: &Bytes48,
kzg_settings: &KZGSettings,
) -> Result<bool, Error> {
let mut verified: MaybeUninit<bool> = MaybeUninit::uninit();
let mut verified: bool = false;
unsafe {
let res = verify_blob_kzg_proof(
verified.as_mut_ptr(),
&blob,
&commitment_bytes,
&proof_bytes,
&mut verified,
blob,
commitment_bytes,
proof_bytes,
kzg_settings,
);
println!("foobar\n");
if let C_KZG_RET::C_KZG_OK = res {
Ok(verified.assume_init())
Ok(verified)
} else {
Err(Error::CError(res))
}
Expand Down Expand Up @@ -429,7 +430,7 @@ impl KZGCommitment {
hex::encode(self.bytes)
}

pub fn blob_to_kzg_commitment(blob: Blob, kzg_settings: &KZGSettings) -> Result<Self, Error> {
pub fn blob_to_kzg_commitment(blob: &Blob, kzg_settings: &KZGSettings) -> Result<Self, Error> {
let mut kzg_commitment: MaybeUninit<KZGCommitment> = MaybeUninit::uninit();
unsafe {
let res = blob_to_kzg_commitment(
Expand Down Expand Up @@ -464,6 +465,12 @@ impl From<[u8; BYTES_PER_BLOB]> for Blob {
}
}

impl From<[u8; BYTES_PER_BLOB]> for Box<Blob> {
fn from(value: [u8; BYTES_PER_BLOB]) -> Self {
Box::new(Blob { bytes: value })
}
}

impl From<[u8; 32]> for Bytes32 {
fn from(value: [u8; 32]) -> Self {
Self { bytes: value }
Expand Down Expand Up @@ -541,7 +548,7 @@ mod tests {
verify_blob_kzg_proof, verify_blob_kzg_proof_batch, verify_kzg_proof,
};

fn generate_random_blob(rng: &mut ThreadRng) -> Blob {
fn generate_random_blob(rng: &mut ThreadRng) -> Box<Blob> {
let mut arr = [0u8; BYTES_PER_BLOB];
rng.fill(&mut arr[..]);
// Ensure that the blob is canonical by ensuring that
Expand All @@ -559,20 +566,20 @@ mod tests {

let num_blobs: usize = rng.gen_range(1..16);
let mut blobs: Vec<Blob> = (0..num_blobs)
.map(|_| generate_random_blob(&mut rng))
.map(|_| *generate_random_blob(&mut rng))
.collect();

let commitments: Vec<Bytes48> = blobs
.iter()
.map(|blob| KZGCommitment::blob_to_kzg_commitment(blob.clone(), &kzg_settings).unwrap())
.map(|blob| KZGCommitment::blob_to_kzg_commitment(&blob, &kzg_settings).unwrap())
.map(|commitment| commitment.to_bytes())
.collect();

let proofs: Vec<Bytes48> = blobs
.iter()
.zip(commitments.iter())
.map(|(blob, commitment)| {
KZGProof::compute_blob_kzg_proof(blob.clone(), *commitment, &kzg_settings).unwrap()
KZGProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings).unwrap()
})
.map(|proof| proof.to_bytes())
.collect();
Expand All @@ -592,7 +599,7 @@ mod tests {
.unwrap_err();
assert!(matches!(error, Error::MismatchLength(_)));

let incorrect_blob = generate_random_blob(&mut rng);
let incorrect_blob = *generate_random_blob(&mut rng);
blobs.push(incorrect_blob);

assert!(!KZGProof::verify_blob_kzg_proof_batch(
Expand Down Expand Up @@ -641,7 +648,7 @@ mod tests {
continue;
};

match KZGCommitment::blob_to_kzg_commitment(blob, &kzg_settings) {
match KZGCommitment::blob_to_kzg_commitment(&blob, &kzg_settings) {
Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
_ => assert!(test.get_output().is_none()),
}
Expand All @@ -668,7 +675,7 @@ mod tests {
continue;
};

match KZGProof::compute_kzg_proof(blob, z, &kzg_settings) {
match KZGProof::compute_kzg_proof(&blob, &z, &kzg_settings) {
Ok((proof, y)) => {
assert_eq!(proof.bytes, test.get_output().unwrap().0.bytes);
assert_eq!(y.bytes, test.get_output().unwrap().1.bytes);
Expand Down Expand Up @@ -701,7 +708,7 @@ mod tests {
continue;
};

match KZGProof::compute_blob_kzg_proof(blob, commitment, &kzg_settings) {
match KZGProof::compute_blob_kzg_proof(&blob, &commitment, &kzg_settings) {
Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
_ => assert!(test.get_output().is_none()),
}
Expand Down Expand Up @@ -733,7 +740,7 @@ mod tests {
continue;
};

match KZGProof::verify_kzg_proof(commitment, z, y, proof, &kzg_settings) {
match KZGProof::verify_kzg_proof(&commitment, &z, &y, &proof, &kzg_settings) {
Ok(res) => assert_eq!(res, test.get_output().unwrap()),
_ => assert!(test.get_output().is_none()),
}
Expand Down Expand Up @@ -764,7 +771,7 @@ mod tests {
continue;
};

match KZGProof::verify_blob_kzg_proof(blob, commitment, proof, &kzg_settings) {
match KZGProof::verify_blob_kzg_proof(&blob, &commitment, &proof, &kzg_settings) {
Ok(res) => assert_eq!(res, test.get_output().unwrap()),
_ => assert!(test.get_output().is_none()),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Input<'a> {
}

impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> {
pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
Blob::from_hex(self.blob)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Input<'a> {
}

impl Input<'_> {
pub fn get_blob(&self) -> Result<Blob, Error> {
pub fn get_blob(&self) -> Result<Box<Blob>, Error> {
Blob::from_hex(self.blob)
}

Expand Down
Loading
Loading