Skip to content

Commit

Permalink
modify code to match new rust-verkle api + modify commitRoot to now u…
Browse files Browse the repository at this point in the history
…se groupToField instead of commitment.to_bytes

Signed-off-by: Kevaundray Wedderburn <kevtheappdev@gmail.com>
  • Loading branch information
kevaundray committed Feb 18, 2024
1 parent 586ee1f commit fe9b749
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions ipa-multipoint/ipa_multipoint_jni/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaM
) -> jbyteArray {
let input = env.convert_byte_array(input).unwrap();

let committer = &CONFIG.committer;

let mut input: [u8; 64] = match input.try_into() {
Ok(input) => input,
Err(_) => {
Expand All @@ -65,7 +63,7 @@ pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaM
}
reverse_last_32_bytes(&mut input);

let hash = ffi_interface::get_tree_key_hash_flat_input(committer, input);
let hash = ffi_interface::get_tree_key_hash_flat_input(&CONFIG, input);
env.byte_array_from_slice(&hash).unwrap()
}

Expand All @@ -82,16 +80,20 @@ pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaM
.convert_byte_array(input)
.expect("Cannot convert jbyteArray to rust array");

let committer = &CONFIG.committer;

let commitment = ffi_interface::commit_to_scalars(committer, &input).unwrap();
let commitment = ffi_interface::commit_to_scalars(&CONFIG, &input).unwrap();

env.byte_array_from_slice(&commitment)
.expect("Couldn't convert to byte array")
}

/// Commit_root receives a list of 32 byte scalars and returns a 32 byte commitment.to_bytes()
/// This is ported from rust-verkle.
/// Commit_root receives a list of 32 byte scalars and returns a 32 byte array
///
/// The 32 bytes represents groupToField(commitment).to_bytes()
///
/// TODO: We can remove this method as its the same as calling commit and then groupToField
/// TODO: This has not been done because it is a breaking API change, that changes the bindings.
/// TODO: Once downstream besu-verkle switches to using commit and then groupToField, making this
/// TODO: method unused, then we can remove it.
#[no_mangle]
pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaMultipoint_commitRoot(
env: JNIEnv,
Expand All @@ -102,10 +104,8 @@ pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaM
.convert_byte_array(input)
.expect("Cannot convert jbyteArray to rust array");

let committer = &CONFIG.committer;

let commitment = ffi_interface::commit_to_scalars(committer, &input).unwrap();
let hash = ffi_interface::deprecated_serialize_commitment(commitment);
let commitment = ffi_interface::commit_to_scalars(&CONFIG, &input).unwrap();
let hash = ffi_interface::hash_commitment(commitment);

env.byte_array_from_slice(&hash)
.expect("Couldn't convert to byte array")
Expand Down Expand Up @@ -145,10 +145,27 @@ pub extern "system" fn Java_org_hyperledger_besu_nativelib_ipamultipoint_LibIpaM
.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();
let (old_commitment_bytes, indexes, old_scalars, new_scalars) =
match ffi_interface::deserialize_update_commitment_sparse(input) {
Ok(decomposed_input) => decomposed_input,
Err(err) => {
env.throw_new(
"java/text/ParseException",
format!("Could not deserialize the input, error : {:?}", err),
)
.expect("Failed to throw exception");
return std::ptr::null_mut(); // Return null pointer to indicate an error
}
};

let updated_commitment = ffi_interface::update_commitment_sparse(
&CONFIG,
old_commitment_bytes,
indexes,
old_scalars,
new_scalars,
)
.unwrap();

env.byte_array_from_slice(&updated_commitment)
.expect("Couldn't convert to byte array")
Expand Down

0 comments on commit fe9b749

Please sign in to comment.