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

Change the rust interface to take references #356

Merged
merged 3 commits into from
Sep 5, 2023
Merged
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
39 changes: 10 additions & 29 deletions bindings/rust/benches/kzg_benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
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,51 +55,32 @@ 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,
)
})
});

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)
})
});

Expand Down
66 changes: 31 additions & 35 deletions bindings/rust/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -571,15 +567,15 @@ 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();

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 Down Expand Up @@ -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()),
}
Expand All @@ -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);
Expand Down Expand Up @@ -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()),
}
Expand Down Expand Up @@ -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()),
}
Expand Down Expand Up @@ -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()),
}
Expand Down
9 changes: 3 additions & 6 deletions bindings/rust/src/bindings/serde_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down