Skip to content

Commit

Permalink
ipa-multipoint : add update sparse commitment (#149)
Browse files Browse the repository at this point in the history
Signed-off-by: Dragan Pilipovic <draganpilipovic1997bp@gmail.com>
  • Loading branch information
dragan2234 authored Feb 15, 2024
1 parent 1ea1f9f commit 7198352
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ipa-multipoint/ipa_multipoint_jni/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repository = "https://github.com/hyperledger/besu-native"
edition = "2018"

[dependencies]
ffi_interface = { git = "https://github.com/crate-crypto/rust-verkle", rev = "185ccf14678ae4d2277547e2f2615933f10553b5" }
ffi_interface = { git = "https://github.com/crate-crypto/rust-verkle", rev = "bb5af2f2fe9788d49d2896b9614a3125f8227818" }
jni = { version = "0.19.0", features = [
"invocation",
] } # We use invocation in tests.
Expand Down
25 changes: 25 additions & 0 deletions ipa-multipoint/ipa_multipoint_jni/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,28 @@ pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaM
env.byte_array_from_slice(&hash)
.expect("Couldn't convert to byte array")
}

/// Update commitment sparse
/// Expects byteArray of fixed 64bytes for the commitment
/// and dynamic tuple (old_scalar(32 bytes), new_scalar(32 bytes), index(1 byte)) in this sequence
/// Bytearray is processed with ffi_interface::deserialize_update_commitment_sparse and sent to ffi_interface::update_commitment_sparse.
/// We get updated commitemnt and return it as 64 bytes.
/// If Commitment is empty we should pass https://github.com/crate-crypto/rust-verkle/blob/bb5af2f2fe9788d49d2896b9614a3125f8227818/ffi_interface/src/lib.rs#L57
#[no_mangle]
pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaMultipoint_updateCommitmentSparse(
env: JNIEnv,
_class: JClass<'_>,
input: jbyteArray,
) -> jbyteArray {
let input = env
.convert_byte_array(input)
.expect("Cannot convert jbyteArray to rust array");

let committer = &CONFIG.committer;

let (old_commitment_bytes,indexes,old_scalars,new_scalars) = ffi_interface::deserialize_update_commitment_sparse(input);
let updated_commitment = ffi_interface::update_commitment_sparse(committer, old_commitment_bytes, indexes, old_scalars, new_scalars).unwrap();

env.byte_array_from_slice(&updated_commitment)
.expect("Couldn't convert to byte array")
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,14 @@ public class LibIpaMultipoint {
* @return 32bytes as byte[]
*/
public static native byte[] pedersenHash(byte[] input);

/**
* Update Commitment sparse
* @param input Expects byteArray of fixed 64bytes for the commitment
* and dynamic tuple (old_scalar(32 bytes), new_scalar(32 bytes), index(1 byte)) in this sequence
* Bytearray is processed with ffi_interface::deserialize_update_commitment_sparse and sent to ffi_interface::update_commitment_sparse.
* If Commitment is empty we should pass https://github.com/crate-crypto/rust-verkle/blob/bb5af2f2fe9788d49d2896b9614a3125f8227818/ffi_interface/src/lib.rs#L57
* @return Updated commitemnt and return it as 64 bytes.
*/
public static native byte[] updateCommitmentSparse(byte[] input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,45 @@ public void testCallLibraryPedersenHash() {
Bytes result = Bytes.of(LibIpaMultipoint.pedersenHash(total));
assertThat(result).isEqualTo(Bytes32.fromHexString("0xff6e8f1877fd27f91772a4cec41d99d2f835d7320e929b8d509c5fa7ce095c51"));
}
}

@Test
public void testUpdateCommitmentSparseIdentityCommitment() {
// Numbers and result is taken from: https://github.com/crate-crypto/rust-verkle/blob/bb5af2f2fe9788d49d2896b9614a3125f8227818/ffi_interface/src/lib.rs#L576
// Identity element
Bytes oldCommitment = Bytes.fromHexString("0x00000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000");

Bytes oldScalar1 = Bytes.fromHexString("0x0200000000000000000000000000000000000000000000000000000000000000");
Bytes newScalar1 = Bytes.fromHexString("0x1300000000000000000000000000000000000000000000000000000000000000");
Bytes index1 = Bytes.fromHexString("0x07");

Bytes oldScalar2 = Bytes.fromHexString("0x0200000000000000000000000000000000000000000000000000000000000000");
Bytes newScalar2 = Bytes.fromHexString("0x1100000000000000000000000000000000000000000000000000000000000000");
Bytes index2 = Bytes.fromHexString("0x08");

Bytes input = Bytes.concatenate(oldCommitment, oldScalar1, newScalar1, index1, oldScalar2, newScalar2, index2);

Bytes result = Bytes.of(LibIpaMultipoint.updateCommitmentSparse(input.toArray()));

assertThat(result).isEqualTo(Bytes.fromHexString("6cf7264f1fff79a21b1be098e66e2457f2cba14c36c33a794566f85be8e6c61dc2a29760223e7c568af4ca13a08535d3e66ba7e2dd1e053894f1fdccdc560a54"));
}

@Test
public void testUpdateCommitmentSparseNonIdentityCommitment() {
// These values are taken from: https://github.com/crate-crypto/rust-verkle/blob/bb5af2f2fe9788d49d2896b9614a3125f8227818/ffi_interface/src/lib.rs#L494
Bytes oldCommitment = Bytes.fromHexString("c2a169fe13aab966d6642801727c8534e40b355372890e18a9880f66b88e143a37fe18000aaf81d4536b64ec3266678c56baf81645d4cfd5133a908247ab8445");
Bytes oldScalar1 = Bytes.fromHexString("0x0400000000000000000000000000000000000000000000000000000000000000");
Bytes newScalar1 = Bytes.fromHexString("0x7f00000000000000000000000000000000000000000000000000000000000000");
Bytes index1 = Bytes.fromHexString("0x01");

Bytes oldScalar2 = Bytes.fromHexString("0x0900000000000000000000000000000000000000000000000000000000000000");
Bytes newScalar2 = Bytes.fromHexString("0xff00000000000000000000000000000000000000000000000000000000000000");
Bytes index2 = Bytes.fromHexString("0x02");

Bytes input = Bytes.concatenate(oldCommitment, oldScalar1, newScalar1, index1, oldScalar2, newScalar2, index2);

Bytes result = Bytes.of(LibIpaMultipoint.updateCommitmentSparse(input.toArray()));

assertThat(result).isEqualTo(Bytes.fromHexString("2dd3bb69da79ecd91a74b188bfddc74827a995dec07e5308f8215f08d69e77330b11628c6d3313a7781b74850e64cb6ac706290da79e56ff311a10214d14dc36"));

}
}

0 comments on commit 7198352

Please sign in to comment.