[Java bindings] Improve javadoc + update dependencies (#213)

This commit is contained in:
Stefan Bratanov 2023-03-15 16:20:04 +00:00 committed by GitHub
parent 5580f355ae
commit 0fb17c2063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 50 additions and 27 deletions

View File

@ -1,8 +1,8 @@
plugins { plugins {
id "application" id "application"
id "java-test-fixtures" id "java-test-fixtures"
id "me.champeau.jmh" version "0.6.8" id "me.champeau.jmh" version "0.7.0"
id "com.diffplug.spotless" version "6.15.0" id "com.diffplug.spotless" version "6.17.0"
} }
repositories { repositories {
@ -27,6 +27,16 @@ dependencies {
testFixturesImplementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonVersion}") testFixturesImplementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:${jacksonVersion}")
} }
javadoc {
options.addStringOption("Xdoclint:all,-missing", "-quiet")
}
check {
dependsOn {
javadoc
}
}
spotless { spotless {
java { java {
googleJavaFormat() googleJavaFormat()

View File

@ -30,8 +30,8 @@ void throw_c_kzg_exception(JNIEnv *env, C_KZG_RET error_code, const char *messag
{ {
jclass exception_class = (*env)->FindClass(env, "ethereum/ckzg4844/CKZGException"); jclass exception_class = (*env)->FindClass(env, "ethereum/ckzg4844/CKZGException");
jstring error_message = (*env)->NewStringUTF(env, message); jstring error_message = (*env)->NewStringUTF(env, message);
jmethodID exception_init = (*env)->GetMethodID(env, exception_class, "<init>", "(ILjava/lang/String;)V"); jmethodID exception_constructor = (*env)->GetMethodID(env, exception_class, "<init>", "(ILjava/lang/String;)V");
jobject exception = (*env)->NewObject(env, exception_class, exception_init, error_code, error_message); jobject exception = (*env)->NewObject(env, exception_class, exception_constructor, error_code, error_message);
(*env)->Throw(env, exception); (*env)->Throw(env, exception);
} }
@ -230,24 +230,24 @@ JNIEXPORT jobject JNICALL Java_ethereum_ckzg4844_CKZG4844JNI_computeKzgProof(JNI
return NULL; return NULL;
} }
jclass tuple_class = (*env)->FindClass(env, "ethereum/ckzg4844/Tuple"); jclass tuple_class = (*env)->FindClass(env, "ethereum/ckzg4844/ByteArrayTuple");
if (tuple_class == NULL) if (tuple_class == NULL)
{ {
throw_exception(env, "Failed to find Tuple class."); throw_exception(env, "Failed to find ByteArrayTuple class.");
return NULL; return NULL;
} }
jmethodID tuple_constructor = (*env)->GetMethodID(env, tuple_class, "<init>", "([B[B)V"); jmethodID tuple_constructor = (*env)->GetMethodID(env, tuple_class, "<init>", "([B[B)V");
if (tuple_constructor == NULL) if (tuple_constructor == NULL)
{ {
throw_exception(env, "Failed to find Tuple constructor."); throw_exception(env, "Failed to find ByteArrayTuple constructor.");
return NULL; return NULL;
} }
jobject tuple = (*env)->NewObject(env, tuple_class, tuple_constructor, proof, y); jobject tuple = (*env)->NewObject(env, tuple_class, tuple_constructor, proof, y);
if (tuple == NULL) if (tuple == NULL)
{ {
throw_exception(env, "Failed to instantiate new Tuple."); throw_exception(env, "Failed to instantiate new ByteArrayTuple.");
return NULL; return NULL;
} }

Binary file not shown.

View File

@ -1,5 +1,6 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

12
bindings/java/gradlew vendored
View File

@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop. # Darwin, MinGW, and NonStop.
# #
# (3) This script is generated from the Groovy template # (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project. # within the Gradle project.
# #
# You can find Gradle at https://github.com/gradle/gradle/. # You can find Gradle at https://github.com/gradle/gradle/.
@ -80,10 +80,10 @@ do
esac esac
done done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit # This is normally unused
# shellcheck disable=SC2034
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/} APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
@ -143,12 +143,16 @@ fi
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #( case $MAX_FD in #(
max*) max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
MAX_FD=$( ulimit -H -n ) || MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit" warn "Could not query maximum file descriptor limit"
esac esac
case $MAX_FD in #( case $MAX_FD in #(
'' | soft) :;; #( '' | soft) :;; #(
*) *)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
ulimit -n "$MAX_FD" || ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD" warn "Could not set maximum file descriptor limit to $MAX_FD"
esac esac

View File

@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0 set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=. if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0 set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME% set APP_HOME=%DIRNAME%

View File

@ -125,7 +125,7 @@ public class CKZG4844JNIBenchmark {
} }
@Benchmark @Benchmark
public Tuple computeKzgProof(final ComputeKzgProofState state) { public ByteArrayTuple computeKzgProof(final ComputeKzgProofState state) {
return CKZG4844JNI.computeKzgProof(state.blob, state.z); return CKZG4844JNI.computeKzgProof(state.blob, state.z);
} }

View File

@ -1,10 +1,11 @@
package ethereum.ckzg4844; package ethereum.ckzg4844;
public class Tuple { /** A tuple holding 2 byte arrays. */
public class ByteArrayTuple {
private final byte[] first; private final byte[] first;
private final byte[] second; private final byte[] second;
public Tuple(byte[] first, byte[] second) { public ByteArrayTuple(byte[] first, byte[] second) {
this.first = first; this.first = first;
this.second = second; this.second = second;
} }
@ -17,7 +18,7 @@ public class Tuple {
return second; return second;
} }
public static Tuple of(byte[] first, byte[] second) { public static ByteArrayTuple of(byte[] first, byte[] second) {
return new Tuple(first, second); return new ByteArrayTuple(first, second);
} }
} }

View File

@ -63,11 +63,15 @@ public class CKZG4844JNI {
} }
} }
/** Scalar field modulus of BLS12-381 */
public static final BigInteger BLS_MODULUS = public static final BigInteger BLS_MODULUS =
new BigInteger( new BigInteger(
"52435875175126190479447740508185965837690552500527637822603658699938581184513"); "52435875175126190479447740508185965837690552500527637822603658699938581184513");
/** The number of bytes in a KZG commitment */
public static final int BYTES_PER_COMMITMENT = 48; public static final int BYTES_PER_COMMITMENT = 48;
/** The number of bytes in a KZG proof */
public static final int BYTES_PER_PROOF = 48; public static final int BYTES_PER_PROOF = 48;
/** Bytes used to encode a BLS scalar field element */
public static final int BYTES_PER_FIELD_ELEMENT = 32; public static final int BYTES_PER_FIELD_ELEMENT = 32;
private CKZG4844JNI() {} private CKZG4844JNI() {}
@ -117,6 +121,7 @@ public class CKZG4844JNI {
* *
* @param resource the resource name that contains the trusted setup * @param resource the resource name that contains the trusted setup
* @param clazz the class to use to get the resource * @param clazz the class to use to get the resource
* @param <T> the type of the class
* @throws CKZGException if there is a crypto error * @throws CKZGException if there is a crypto error
* @throws IllegalArgumentException if the resource does not exist * @throws IllegalArgumentException if the resource does not exist
*/ */
@ -156,10 +161,10 @@ public class CKZG4844JNI {
* *
* @param blob blob bytes * @param blob blob bytes
* @param z_bytes a point * @param z_bytes a point
* @return a tuple of the proof and the value y = f(z) * @return an instance of {@link ByteArrayTuple} holding the proof and the value y = f(z)
* @throws CKZGException if there is a crypto error * @throws CKZGException if there is a crypto error
*/ */
public static native Tuple computeKzgProof(byte[] blob, byte[] z_bytes); public static native ByteArrayTuple computeKzgProof(byte[] blob, byte[] z_bytes);
/** /**
* Given a blob, return the KZG proof that is used to verify it against the commitment * Given a blob, return the KZG proof that is used to verify it against the commitment
@ -203,6 +208,7 @@ public class CKZG4844JNI {
* @param blobs flattened blobs bytes * @param blobs flattened blobs bytes
* @param commitments_bytes flattened commitments bytes * @param commitments_bytes flattened commitments bytes
* @param proofs_bytes flattened proofs bytes * @param proofs_bytes flattened proofs bytes
* @param count the number of blobs (should be same as the number of proofs and commitments)
* @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
*/ */

View File

@ -70,7 +70,8 @@ public class CKZG4844JNITest {
if (PRESET != Preset.MAINNET) return; if (PRESET != Preset.MAINNET) return;
try { try {
Tuple tuple = CKZG4844JNI.computeKzgProof(test.getInput().getBlob(), test.getInput().getZ()); ByteArrayTuple tuple =
CKZG4844JNI.computeKzgProof(test.getInput().getBlob(), test.getInput().getZ());
assertArrayEquals(test.getOutput().getFirst(), tuple.getFirst()); assertArrayEquals(test.getOutput().getFirst(), tuple.getFirst());
assertArrayEquals(test.getOutput().getSecond(), tuple.getSecond()); assertArrayEquals(test.getOutput().getSecond(), tuple.getSecond());
} catch (CKZGException ex) { } catch (CKZGException ex) {
@ -191,7 +192,7 @@ public class CKZG4844JNITest {
loadTrustedSetup(); loadTrustedSetup();
final byte[] blob = TestUtils.createRandomBlob(); final byte[] blob = TestUtils.createRandomBlob();
final byte[] z_bytes = TestUtils.randomBLSFieldElementBytes(); final byte[] z_bytes = TestUtils.randomBLSFieldElementBytes();
final Tuple tuple = CKZG4844JNI.computeKzgProof(blob, z_bytes); final ByteArrayTuple tuple = CKZG4844JNI.computeKzgProof(blob, z_bytes);
assertEquals(CKZG4844JNI.BYTES_PER_PROOF, tuple.getFirst().length); assertEquals(CKZG4844JNI.BYTES_PER_PROOF, tuple.getFirst().length);
assertEquals(CKZG4844JNI.BYTES_PER_FIELD_ELEMENT, tuple.getSecond().length); assertEquals(CKZG4844JNI.BYTES_PER_FIELD_ELEMENT, tuple.getSecond().length);
CKZG4844JNI.freeTrustedSetup(); CKZG4844JNI.freeTrustedSetup();
@ -278,8 +279,7 @@ public class CKZG4844JNITest {
assertEquals(C_KZG_BADARGS, exception.getError()); assertEquals(C_KZG_BADARGS, exception.getError());
assertEquals( assertEquals(
String.format("Invalid commitment size. Expected 48 bytes but got 49."), "Invalid commitment size. Expected 48 bytes but got 49.", exception.getErrorMessage());
exception.getErrorMessage());
exception = exception =
assertThrows( assertThrows(

View File

@ -1,6 +1,6 @@
package ethereum.ckzg4844.test_formats; package ethereum.ckzg4844.test_formats;
import ethereum.ckzg4844.Tuple; import ethereum.ckzg4844.ByteArrayTuple;
import java.util.List; import java.util.List;
import org.apache.tuweni.bytes.Bytes; import org.apache.tuweni.bytes.Bytes;
@ -25,12 +25,12 @@ public class ComputeKzgProofTest {
return input; return input;
} }
public Tuple getOutput() { public ByteArrayTuple getOutput() {
if (output == null) { if (output == null) {
return null; return null;
} }
byte[] proof = Bytes.fromHexString(output.get(0)).toArray(); byte[] proof = Bytes.fromHexString(output.get(0)).toArray();
byte[] y = Bytes.fromHexString(output.get(1)).toArray(); byte[] y = Bytes.fromHexString(output.get(1)).toArray();
return Tuple.of(proof, y); return ByteArrayTuple.of(proof, y);
} }
} }