Change the rust interface to take references (#356)

This commit is contained in:
Pawan Dhananjay 2023-09-05 14:43:46 -07:00 committed by GitHub
parent d35b0f3854
commit 5b55a54d5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 70 deletions

View File

@ -36,7 +36,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
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()
})
@ -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()
})
@ -55,38 +55,24 @@ 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)
})
b.iter(|| KzgCommitment::blob_to_kzg_commitment(&blobs[0], &kzg_settings))
});
c.bench_function("compute_kzg_proof", |b| {
b.iter(|| {
KzgProof::compute_kzg_proof(
blobs.first().unwrap().clone(),
*fields.first().unwrap(),
&kzg_settings,
)
})
b.iter(|| KzgProof::compute_kzg_proof(&blobs[0], &fields[0], &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,
)
})
b.iter(|| KzgProof::compute_blob_kzg_proof(&blobs[0], &commitments[0], &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(),
&commitments[0],
&fields[0],
&fields[0],
&proofs[0],
&kzg_settings,
)
})
@ -94,12 +80,7 @@ pub fn criterion_benchmark(c: &mut Criterion) {
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,
)
KzgProof::verify_blob_kzg_proof(&blobs[0], &commitments[0], &proofs[0], &kzg_settings)
})
});

View File

@ -278,8 +278,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();
@ -288,8 +288,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 {
@ -301,16 +301,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 {
@ -322,20 +322,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 {
@ -347,18 +347,18 @@ 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();
unsafe {
let res = verify_blob_kzg_proof(
verified.as_mut_ptr(),
&blob,
&commitment_bytes,
&proof_bytes,
blob,
commitment_bytes,
proof_bytes,
kzg_settings,
);
if let C_KZG_RET::C_KZG_OK = res {
@ -430,14 +430,10 @@ 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(
kzg_commitment.as_mut_ptr(),
blob.as_ptr() as *const Blob,
kzg_settings,
);
let res = blob_to_kzg_commitment(kzg_commitment.as_mut_ptr(), blob, kzg_settings);
if let C_KZG_RET::C_KZG_OK = res {
Ok(kzg_commitment.assume_init())
} else {
@ -571,7 +567,7 @@ mod tests {
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();
@ -579,7 +575,7 @@ mod tests {
.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();
@ -648,7 +644,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()),
}
@ -675,7 +671,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);
@ -706,7 +702,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()),
}
@ -738,7 +734,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()),
}
@ -769,7 +765,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()),
}

View File

@ -123,11 +123,9 @@ mod tests {
// generate blob, commitment, proof
let mut rng = rand::thread_rng();
let blob = generate_random_blob(&mut rng);
let commitment =
KZGCommitment::blob_to_kzg_commitment(blob.clone(), &kzg_settings).unwrap();
let commitment = KZGCommitment::blob_to_kzg_commitment(&blob, &kzg_settings).unwrap();
let proof =
KZGProof::compute_blob_kzg_proof(blob.clone(), commitment.to_bytes(), &kzg_settings)
.unwrap();
KZGProof::compute_blob_kzg_proof(&blob, &commitment.to_bytes(), &kzg_settings).unwrap();
// check blob serialization
let blob_serialized = serde_json::to_string(&blob).unwrap();
@ -175,8 +173,7 @@ mod tests {
// generate blob just to calculate a commitment
let mut rng = rand::thread_rng();
let blob = generate_random_blob(&mut rng);
let commitment =
KZGCommitment::blob_to_kzg_commitment(blob.clone(), &kzg_settings).unwrap();
let commitment = KZGCommitment::blob_to_kzg_commitment(&blob, &kzg_settings).unwrap();
// check blob serialization
let blob_serialized = serde_json::to_string(&commitment.to_bytes()).unwrap();