Add java benchmarking for `compute_kzg_proof` (#122)

This commit is contained in:
Stefan Bratanov 2023-02-08 17:36:40 +00:00 committed by GitHub
parent dc28b03f9d
commit 5cfbc34135
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 16 deletions

View File

@ -40,6 +40,19 @@ public class CKZG4844JNIBenchmark {
} }
} }
@State(Scope.Benchmark)
public static class ComputeKzgProofState {
private byte[] blob;
private byte[] z;
@Setup(Level.Iteration)
public void setUp() {
blob = TestUtils.createRandomBlob();
z = TestUtils.randomBLSFieldElementBytes();
}
}
@State(Scope.Benchmark) @State(Scope.Benchmark)
public static class ComputeAndVerifyState { public static class ComputeAndVerifyState {
@ -97,6 +110,11 @@ public class CKZG4844JNIBenchmark {
return CKZG4844JNI.blobToKzgCommitment(state.blob); return CKZG4844JNI.blobToKzgCommitment(state.blob);
} }
@Benchmark
public byte[] computeKzgProof(final ComputeKzgProofState state) {
return CKZG4844JNI.computeKzgProof(state.blob, state.z);
}
@Benchmark @Benchmark
public byte[] computeAggregateKzgProof(final ComputeAndVerifyState state) { public byte[] computeAggregateKzgProof(final ComputeAndVerifyState state) {
return CKZG4844JNI.computeAggregateKzgProof(state.blobs, state.count); return CKZG4844JNI.computeAggregateKzgProof(state.blobs, state.count);

View File

@ -104,8 +104,7 @@ public class CKZG4844JNI {
* @param g2Count the count of the g2 values * @param g2Count the count of the g2 values
* @throws CKZGException if there is a crypto error * @throws CKZGException if there is a crypto error
*/ */
public static native void loadTrustedSetup(byte[] g1, long g1Count, byte[] g2, public static native void loadTrustedSetup(byte[] g1, long g1Count, byte[] g2, long g2Count);
long g2Count);
/** /**
* Free the current trusted setup. This method will throw an exception if no trusted setup has * Free the current trusted setup. This method will throw an exception if no trusted setup has
@ -116,7 +115,7 @@ public class CKZG4844JNI {
/** /**
* Compute proof at point z for the polynomial represented by blob. * Compute proof at point z for the polynomial represented by blob.
* *
* @param blob blob bytes * @param blob blob bytes
* @param z_bytes a point * @param z_bytes a point
* @return the proof * @return the proof
* @throws CKZGException if there is a crypto error * @throws CKZGException if there is a crypto error
@ -136,15 +135,16 @@ public class CKZG4844JNI {
/** /**
* Verify aggregated proof and commitments for the given blobs * Verify aggregated proof and commitments for the given blobs
* *
* @param blobs blobs as flattened bytes * @param blobs blobs as flattened bytes
* @param commitments_bytes commitments as flattened bytes * @param commitments_bytes commitments as flattened bytes
* @param count the count of the blobs (should be same as the count of the commitments) * @param count the count of the blobs (should be same as the count of the
* @param proof_bytes the proof that needs verifying * commitments)
* @param proof_bytes the proof that needs verifying
* @return true if the proof is valid and false otherwise * @return true if the proof is valid and false otherwise
* @throws CKZGException if there is a crypto error * @throws CKZGException if there is a crypto error
*/ */
public static native boolean verifyAggregateKzgProof(byte[] blobs, byte[] commitments_bytes, long count, public static native boolean verifyAggregateKzgProof(byte[] blobs, byte[] commitments_bytes,
byte[] aggregated_proof_bytes); long count, byte[] aggregated_proof_bytes);
/** /**
* Calculates commitment for a given blob * Calculates commitment for a given blob
@ -158,14 +158,14 @@ public class CKZG4844JNI {
/** /**
* Verify the proof by point evaluation for the given commitment * Verify the proof by point evaluation for the given commitment
* *
* @param commitment_bytes commitment bytes * @param commitment_bytes commitment bytes
* @param z_bytes Z * @param z_bytes Z
* @param y_bytes Y * @param y_bytes Y
* @param proof_bytes the proof that needs verifying * @param proof_bytes the proof that needs verifying
* @return true if the proof is valid and false otherwise * @return true if the proof is valid and false otherwise
* @throws CKZGException if there is a crypto error * @throws CKZGException if there is a crypto error
*/ */
public static native boolean verifyKzgProof(byte[] commitment_bytes, byte[] z_bytes, byte[] y_bytes, public static native boolean verifyKzgProof(byte[] commitment_bytes, byte[] z_bytes,
byte[] proof_bytes); byte[] y_bytes, byte[] proof_bytes);
} }

View File

@ -84,11 +84,18 @@ public class CKZG4844JNITest {
@Test @Test
public void checkComputeKzgProof() { public void checkComputeKzgProof() {
loadTrustedSetup(); loadTrustedSetup();
final byte[] blob = TestUtils.createRandomBlob(); final byte[] blob = TestUtils.createRandomBlob();
final byte[] z_bytes = TestUtils.randomBLSFieldElementBytes(); final byte[] z_bytes = TestUtils.randomBLSFieldElementBytes();
CKZG4844JNI.computeKzgProof(blob, z_bytes);
final byte[] proof = CKZG4844JNI.computeKzgProof(blob, z_bytes);
assertEquals(CKZG4844JNI.BYTES_PER_PROOF, proof.length);
CKZG4844JNI.freeTrustedSetup(); CKZG4844JNI.freeTrustedSetup();
} }
@Test @Test