Skip to content

Commit

Permalink
feat(ffi_interface): add update_commitment_sparse and clippy fixes (#78)
Browse files Browse the repository at this point in the history
* feat(ffi_interface): add update_commitment_sparse and clippy recommendations

* cargo fmt

* Update ffi_interface/src/lib.rs

---------

Co-authored-by: kevaundray <[email protected]>
  • Loading branch information
dragan2234 and kevaundray authored Jan 30, 2024
1 parent 185ccf1 commit 52f6b9a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 14 deletions.
6 changes: 3 additions & 3 deletions banderwagon/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ mod test {

let mut points = vec![];
let mut point = Element::prime_subgroup_generator();
for i in 0..16 {
let byts = hex::encode(&point.to_bytes());
for (i, _) in expected_bit_string.into_iter().enumerate() {
let byts = hex::encode(point.to_bytes());
assert_eq!(byts, expected_bit_string[i], "index {} does not match", i);

points.push(point);
Expand Down Expand Up @@ -330,7 +330,7 @@ mod test {
let element1 = Element(res);
let bytes1 = element1.to_bytes();

if let Some(_) = Element::from_bytes(&bytes1) {
if Element::from_bytes(&bytes1).is_some() {
panic!("point contains a point at infinity and should not have passed deserialization")
}
}
Expand Down
12 changes: 4 additions & 8 deletions ffi_interface/src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,19 @@ mod test {
#[test]
fn interop_commit() {
let scalars_le: Vec<_> = (0..256)
.map(|i| {
.flat_map(|i| {
let val = Fr::from((i + 1) as u128);
fr_to_le_bytes(-val)
})
.flatten()
.collect();

let scalars_be: Vec<_> = (0..256)
.map(|i| {
.flat_map(|i| {
let val = Fr::from((i + 1) as u128);
let mut arr = fr_to_le_bytes(-val);
arr.reverse();
arr
})
.flatten()
.collect();

// The previous implementation will return the hash in big endian format
Expand All @@ -188,21 +186,19 @@ mod test {
#[test]
fn interop_commit_root() {
let scalars_le: Vec<_> = (0..256)
.map(|i| {
.flat_map(|i| {
let val = Fr::from((i + 1) as u128);
fr_to_le_bytes(-val)
})
.flatten()
.collect();

let scalars_be: Vec<_> = (0..256)
.map(|i| {
.flat_map(|i| {
let val = Fr::from((i + 1) as u128);
let mut arr = fr_to_le_bytes(-val);
arr.reverse();
arr
})
.flatten()
.collect();

let expected_hash =
Expand Down
69 changes: 69 additions & 0 deletions ffi_interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,34 @@ pub fn update_commitment(
Ok((delta_commitment + old_commitment).to_bytes_uncompressed())
}

/// Update commitment for sparse vector.
pub fn update_commitment_sparse(
committer: &DefaultCommitter,
old_commitment_bytes: CommitmentBytes,
// There can only be at most 256 elements in a verkle branch
commitment_index_vec: Vec<usize>,
old_scalar_bytes_vec: Vec<ScalarBytes>,
new_scalar_bytes_vec: Vec<ScalarBytes>,
) -> Result<CommitmentBytes, Error> {
let old_commitment = Element::from_bytes_unchecked_uncompressed(old_commitment_bytes);

let mut delta_values: Vec<(Fr, usize)> = Vec::new();

// For each index in commitment_index, we compute the delta value.
for index in 0..commitment_index_vec.len() {
let old_scalar = fr_from_le_bytes(&old_scalar_bytes_vec[index]).unwrap();
let new_scalar = fr_from_le_bytes(&new_scalar_bytes_vec[index]).unwrap();

let tuple = (new_scalar - old_scalar, commitment_index_vec[index]);

delta_values.push(tuple);
}

let delta_commitment = committer.commit_sparse(delta_values);

Ok((delta_commitment + old_commitment).to_bytes_uncompressed())
}

/// Hashes a commitment
///
/// Note: This commitment can be used as the `commitment root`
Expand Down Expand Up @@ -289,6 +317,7 @@ pub fn create_proof(input: Vec<u8>) -> Vec<u8> {

#[cfg(test)]
mod tests {
use banderwagon::Fr;
use ipa_multipoint::{
committer::{Committer, DefaultCommitter},
crs::CRS,
Expand Down Expand Up @@ -330,6 +359,46 @@ mod tests {
assert_eq!(updated_commitment, naive_update.to_bytes_uncompressed())
}

#[test]
fn commitment_exists_sparse_update() {
let crs = CRS::default();
let committer = DefaultCommitter::new(&crs.G);

let a_0 = banderwagon::Fr::from(123u128);
let a_1 = banderwagon::Fr::from(123u128);
let a_2 = banderwagon::Fr::from(246u128);

let a_zero = banderwagon::Fr::from(0u128);

// Compute C = a_0 * G_0
let commitment = committer.scalar_mul(a_0, 0);

let naive_update = commitment + committer.scalar_mul(a_1, 1) + committer.scalar_mul(a_2, 2);

let val_indices: Vec<(Fr, usize)> = vec![(a_1, 1), (a_2, 2)];

let new_commitment = commitment + committer.commit_sparse(val_indices);

assert_eq!(naive_update, new_commitment);

let commitment_index_vec = vec![1, 2];

let old_scalar_bytes_vec = vec![fr_to_le_bytes(a_zero), fr_to_le_bytes(a_zero)];
let new_scalar_bytes_vec = vec![fr_to_le_bytes(a_1), fr_to_le_bytes(a_2)];

// Now lets do it using the update_commitment_sparse method
let updated_commitment = super::update_commitment_sparse(
&committer,
commitment.to_bytes_uncompressed(),
commitment_index_vec,
old_scalar_bytes_vec,
new_scalar_bytes_vec,
)
.unwrap();

assert_eq!(updated_commitment, naive_update.to_bytes_uncompressed());
}

#[test]
fn from_be_to_be_bytes() {
let value = banderwagon::Fr::from(123456u128);
Expand Down
4 changes: 2 additions & 2 deletions verkle-trie/benches/benchmarks/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn generate_set_of_keys(n: u32) -> impl Iterator<Item = [u8; 32]> {
hasher.update(&arr[..]);
hasher.update(b"seed");

let res: [u8; 32] = hasher.finalize().try_into().unwrap();
let res: [u8; 32] = hasher.finalize().into();
res
})
}
Expand All @@ -33,7 +33,7 @@ pub fn generate_diff_set_of_keys(n: u32) -> impl Iterator<Item = [u8; 32]> {
let mut hasher = Sha256::new();
hasher.update(i.to_be_bytes());

let res: [u8; 32] = hasher.finalize().try_into().unwrap();
let res: [u8; 32] = hasher.finalize().into();
res
})
}
2 changes: 1 addition & 1 deletion verkle-trie/tests/trie_fuzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl BasicPRNG {
let mut hasher = sha2::Sha256::new();
hasher.update(&self.counter.to_le_bytes()[..]);
hasher.update(&self.seed[..]);
let res: [u8; 32] = hasher.finalize().try_into().unwrap();
let res: [u8; 32] = hasher.finalize().into();

self.counter += 1;

Expand Down

0 comments on commit 52f6b9a

Please sign in to comment.