From 09cc80bfa33778dbcdb9d7b991450d3d85e2b271 Mon Sep 17 00:00:00 2001 From: Dragan Pilipovic Date: Thu, 1 Feb 2024 19:54:06 +0100 Subject: [PATCH] add groupToField and return uncompressed serialized commitments --- ipa-multipoint/ipa_multipoint_jni/src/lib.rs | 24 +++++++++++++++---- .../ipamultipoint/LibIpaMultipoint.java | 11 +++++++-- .../ipa_multipoint/LibIpaMultipointTest.java | 12 +++++----- .../PedersenCommitmentTest.java | 4 ++-- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/ipa-multipoint/ipa_multipoint_jni/src/lib.rs b/ipa-multipoint/ipa_multipoint_jni/src/lib.rs index 0a83d4d7..71c1d966 100644 --- a/ipa-multipoint/ipa_multipoint_jni/src/lib.rs +++ b/ipa-multipoint/ipa_multipoint_jni/src/lib.rs @@ -22,7 +22,7 @@ use once_cell::sync::Lazy; // TODO: Use a pointer here instead. This is only being used so that the interface does not get changed. // TODO: and bindings do not need to be modified. -pub static CONFIG: Lazy = Lazy::new(|| ffi_interface::Context::default()); +pub static CONFIG: Lazy = Lazy::new(ffi_interface::Context::default); /// Pedersen hash receives an address and a trie index and returns a hash calculated this way: /// H(constant || address_low || address_high || trie_index_low || trie_index_high) @@ -86,9 +86,7 @@ pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaM let commitment = ffi_interface::commit_to_scalars(committer, &input).unwrap(); - let hash = ffi_interface::hash_commitment(commitment); - - env.byte_array_from_slice(&hash) + env.byte_array_from_slice(&commitment) .expect("Couldn't convert to byte array") } @@ -112,3 +110,21 @@ pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaM env.byte_array_from_slice(&hash) .expect("Couldn't convert to byte array") } + +#[no_mangle] +pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaMultipoint_groupToField( + env: JNIEnv, + _class: JClass<'_>, + input: jbyteArray, +) -> jbyteArray { + let commitment = env + .convert_byte_array(input) + .expect("Cannot convert jbyteArray to rust array"); + + let commitment_bytes = commitment.try_into().unwrap(); + + let hash = ffi_interface::hash_commitment(commitment_bytes); + + env.byte_array_from_slice(&hash) + .expect("Couldn't convert to byte array") +} diff --git a/ipa-multipoint/src/main/java/org/hyperledger/besu/nativelib/ipamultipoint/LibIpaMultipoint.java b/ipa-multipoint/src/main/java/org/hyperledger/besu/nativelib/ipamultipoint/LibIpaMultipoint.java index 382ccfa9..f7a517b6 100644 --- a/ipa-multipoint/src/main/java/org/hyperledger/besu/nativelib/ipamultipoint/LibIpaMultipoint.java +++ b/ipa-multipoint/src/main/java/org/hyperledger/besu/nativelib/ipamultipoint/LibIpaMultipoint.java @@ -47,17 +47,24 @@ public class LibIpaMultipoint { * Evaluates a polynomial of degree 255 (uniquely defined by 256 values) at a specific point on the curve. * @param input [Fr,Fr,Fr...] - * @return group_to_field(commitment) + * @return commitment.to_bytes() - uncompressed serialization */ public static native byte[] commit(byte[] input); /** * Evaluates a polynomial of degree 255 (uniquely defined by 256 values) at a specific point on the curve. * @param input [Fr,Fr,Fr...] - * @return commitment.to_bytes() + * @return commitment.to_bytes() - compressed serialization */ public static native byte[] commitRoot(byte[] input); + /** + * Serializaes group element to field. + * @param input C uncompressed serialization = 64bytes + * @return Fr = 32 bytes + */ + public static native byte[] groupToField(byte[] input); + /** * Pedersen hash as specified in https://notes.ethereum.org/@vbuterin/verkle_tree_eip * @param input Expects 64byte value as input encoded as byte[] diff --git a/ipa-multipoint/src/test/java/org/hyperledger/besu/nativelib/ipa_multipoint/LibIpaMultipointTest.java b/ipa-multipoint/src/test/java/org/hyperledger/besu/nativelib/ipa_multipoint/LibIpaMultipointTest.java index eb92d804..e97e8d49 100644 --- a/ipa-multipoint/src/test/java/org/hyperledger/besu/nativelib/ipa_multipoint/LibIpaMultipointTest.java +++ b/ipa-multipoint/src/test/java/org/hyperledger/besu/nativelib/ipa_multipoint/LibIpaMultipointTest.java @@ -27,8 +27,8 @@ public class LibIpaMultipointTest { @Test public void testCallLibrary() { Bytes32 input = Bytes32.fromHexString("0x0000fe0c00000000000000000000000000000000000000000000000000000000"); - Bytes32 result = Bytes32.wrap(LibIpaMultipoint.commit(input.toArray())); - Bytes32 expected = Bytes32.fromHexString("0x11169fb6b9dab0b5984ce0b02c9f2c9a3a5adf6f9a95b597bca42ac2a8d8e89f"); + Bytes result = Bytes.wrap(LibIpaMultipoint.commit(input.toArray())); + Bytes expected = Bytes.fromHexString("0x0c7f8df856f6860c9f2c6cb0f86c10228e511cca1c4a08263189d629940cb189706cbaa63c436901b6355e10a524337d97688fa5b0cf6b2b91b98e654547f728"); assertThat(result).isEqualTo(expected.reverse()); } @@ -48,8 +48,8 @@ public void testCallLibraryWithManyElements() { arr[i] = element; } Bytes input = Bytes.concatenate(arr); - Bytes32 result = Bytes32.wrap(LibIpaMultipoint.commit(input.toArray())); - Bytes32 expected = Bytes32.fromHexString("0x26fa8d679863c1d1a3e66d8653b28be718961f521b22d9589a3f32258c1c8a1b"); + Bytes result = Bytes.wrap(LibIpaMultipoint.commit(input.toArray())); + Bytes expected = Bytes.fromHexString("0x0128b513cfb016d3d836b5fa4a8a1260395d4ca831d65027aa74b832d92e0d6d9beb8d5e42b78b99e4eb233e7eca6276c6f4bd235b35c091546e2a2119bc1455"); assertThat(result).isEqualTo(expected); } @@ -61,8 +61,8 @@ public void testCallLibraryWithMaxElements() { arr[i] = element; } Bytes input = Bytes.concatenate(arr); - Bytes32 result = Bytes32.wrap(LibIpaMultipoint.commit(input.toArray())); - Bytes32 expected = Bytes32.fromHexString("0x43d0f14a66ab88f418cd17e688402fbc2658b8b2211fe7951c584230c5ad8b14"); + Bytes result = Bytes.wrap(LibIpaMultipoint.commit(input.toArray())); + Bytes expected = Bytes.fromHexString("0xcfb8d6fe536dec3d72ae549a0b58c7d2d119e7dd58adb2663369275307cd5a1f8adafed4044dbdc9ba9fb4f7ea0e44ab14c1c47297633015d175d7dcaffeb843"); assertThat(result).isEqualTo(expected); } diff --git a/ipa-multipoint/src/test/java/org/hyperledger/besu/nativelib/ipa_multipoint/PedersenCommitmentTest.java b/ipa-multipoint/src/test/java/org/hyperledger/besu/nativelib/ipa_multipoint/PedersenCommitmentTest.java index 6a9b4bb3..e91d68fa 100644 --- a/ipa-multipoint/src/test/java/org/hyperledger/besu/nativelib/ipa_multipoint/PedersenCommitmentTest.java +++ b/ipa-multipoint/src/test/java/org/hyperledger/besu/nativelib/ipa_multipoint/PedersenCommitmentTest.java @@ -41,8 +41,8 @@ public void TestPolynomialCommitments(TestData testData) { FrBytes.add(Bytes32.leftPad(Bytes.wrap(decimalBigInt.toByteArray())).reverse()); } byte[] input = Bytes.concatenate(FrBytes).toArray(); - BigInteger result = Bytes32.wrap(LibIpaMultipoint.commit(input)).toBigInteger(ByteOrder.LITTLE_ENDIAN); + BigInteger result = Bytes32.wrap(LibIpaMultipoint.groupToField(LibIpaMultipoint.commit(input))).toBigInteger(ByteOrder.LITTLE_ENDIAN); BigInteger expected = new BigInteger(testData.commitment); assertThat(result).isEqualTo(expected); } -} \ No newline at end of file +}