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

View File

@ -278,8 +278,8 @@ impl KZGProof {
} }
pub fn compute_kzg_proof( pub fn compute_kzg_proof(
blob: Blob, blob: &Blob,
z_bytes: Bytes32, z_bytes: &Bytes32,
kzg_settings: &KZGSettings, kzg_settings: &KZGSettings,
) -> Result<(Self, Bytes32), Error> { ) -> Result<(Self, Bytes32), Error> {
let mut kzg_proof = MaybeUninit::<KZGProof>::uninit(); let mut kzg_proof = MaybeUninit::<KZGProof>::uninit();
@ -288,8 +288,8 @@ impl KZGProof {
let res = compute_kzg_proof( let res = compute_kzg_proof(
kzg_proof.as_mut_ptr(), kzg_proof.as_mut_ptr(),
y_out.as_mut_ptr(), y_out.as_mut_ptr(),
&blob, blob,
&z_bytes, z_bytes,
kzg_settings, kzg_settings,
); );
if let C_KZG_RET::C_KZG_OK = res { if let C_KZG_RET::C_KZG_OK = res {
@ -301,16 +301,16 @@ impl KZGProof {
} }
pub fn compute_blob_kzg_proof( pub fn compute_blob_kzg_proof(
blob: Blob, blob: &Blob,
commitment_bytes: Bytes48, commitment_bytes: &Bytes48,
kzg_settings: &KZGSettings, kzg_settings: &KZGSettings,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let mut kzg_proof = MaybeUninit::<KZGProof>::uninit(); let mut kzg_proof = MaybeUninit::<KZGProof>::uninit();
unsafe { unsafe {
let res = compute_blob_kzg_proof( let res = compute_blob_kzg_proof(
kzg_proof.as_mut_ptr(), kzg_proof.as_mut_ptr(),
&blob, blob,
&commitment_bytes, commitment_bytes,
kzg_settings, kzg_settings,
); );
if let C_KZG_RET::C_KZG_OK = res { if let C_KZG_RET::C_KZG_OK = res {
@ -322,20 +322,20 @@ impl KZGProof {
} }
pub fn verify_kzg_proof( pub fn verify_kzg_proof(
commitment_bytes: Bytes48, commitment_bytes: &Bytes48,
z_bytes: Bytes32, z_bytes: &Bytes32,
y_bytes: Bytes32, y_bytes: &Bytes32,
proof_bytes: Bytes48, proof_bytes: &Bytes48,
kzg_settings: &KZGSettings, kzg_settings: &KZGSettings,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
let mut verified: MaybeUninit<bool> = MaybeUninit::uninit(); let mut verified: MaybeUninit<bool> = MaybeUninit::uninit();
unsafe { unsafe {
let res = verify_kzg_proof( let res = verify_kzg_proof(
verified.as_mut_ptr(), verified.as_mut_ptr(),
&commitment_bytes, commitment_bytes,
&z_bytes, z_bytes,
&y_bytes, y_bytes,
&proof_bytes, proof_bytes,
kzg_settings, kzg_settings,
); );
if let C_KZG_RET::C_KZG_OK = res { if let C_KZG_RET::C_KZG_OK = res {
@ -347,18 +347,18 @@ impl KZGProof {
} }
pub fn verify_blob_kzg_proof( pub fn verify_blob_kzg_proof(
blob: Blob, blob: &Blob,
commitment_bytes: Bytes48, commitment_bytes: &Bytes48,
proof_bytes: Bytes48, proof_bytes: &Bytes48,
kzg_settings: &KZGSettings, kzg_settings: &KZGSettings,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
let mut verified: MaybeUninit<bool> = MaybeUninit::uninit(); let mut verified: MaybeUninit<bool> = MaybeUninit::uninit();
unsafe { unsafe {
let res = verify_blob_kzg_proof( let res = verify_blob_kzg_proof(
verified.as_mut_ptr(), verified.as_mut_ptr(),
&blob, blob,
&commitment_bytes, commitment_bytes,
&proof_bytes, proof_bytes,
kzg_settings, kzg_settings,
); );
if let C_KZG_RET::C_KZG_OK = res { if let C_KZG_RET::C_KZG_OK = res {
@ -430,14 +430,10 @@ impl KZGCommitment {
hex::encode(self.bytes) 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(); let mut kzg_commitment: MaybeUninit<KZGCommitment> = MaybeUninit::uninit();
unsafe { unsafe {
let res = blob_to_kzg_commitment( let res = blob_to_kzg_commitment(kzg_commitment.as_mut_ptr(), blob, kzg_settings);
kzg_commitment.as_mut_ptr(),
blob.as_ptr() as *const Blob,
kzg_settings,
);
if let C_KZG_RET::C_KZG_OK = res { if let C_KZG_RET::C_KZG_OK = res {
Ok(kzg_commitment.assume_init()) Ok(kzg_commitment.assume_init())
} else { } else {
@ -571,7 +567,7 @@ mod tests {
let commitments: Vec<Bytes48> = blobs let commitments: Vec<Bytes48> = blobs
.iter() .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()) .map(|commitment| commitment.to_bytes())
.collect(); .collect();
@ -579,7 +575,7 @@ mod tests {
.iter() .iter()
.zip(commitments.iter()) .zip(commitments.iter())
.map(|(blob, commitment)| { .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()) .map(|proof| proof.to_bytes())
.collect(); .collect();
@ -648,7 +644,7 @@ mod tests {
continue; 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), Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
_ => assert!(test.get_output().is_none()), _ => assert!(test.get_output().is_none()),
} }
@ -675,7 +671,7 @@ mod tests {
continue; continue;
}; };
match KZGProof::compute_kzg_proof(blob, z, &kzg_settings) { match KZGProof::compute_kzg_proof(&blob, &z, &kzg_settings) {
Ok((proof, y)) => { Ok((proof, y)) => {
assert_eq!(proof.bytes, test.get_output().unwrap().0.bytes); assert_eq!(proof.bytes, test.get_output().unwrap().0.bytes);
assert_eq!(y.bytes, test.get_output().unwrap().1.bytes); assert_eq!(y.bytes, test.get_output().unwrap().1.bytes);
@ -706,7 +702,7 @@ mod tests {
continue; 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), Ok(res) => assert_eq!(res.bytes, test.get_output().unwrap().bytes),
_ => assert!(test.get_output().is_none()), _ => assert!(test.get_output().is_none()),
} }
@ -738,7 +734,7 @@ mod tests {
continue; 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()), Ok(res) => assert_eq!(res, test.get_output().unwrap()),
_ => assert!(test.get_output().is_none()), _ => assert!(test.get_output().is_none()),
} }
@ -769,7 +765,7 @@ mod tests {
continue; 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()), Ok(res) => assert_eq!(res, test.get_output().unwrap()),
_ => assert!(test.get_output().is_none()), _ => assert!(test.get_output().is_none()),
} }

View File

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