Skip to content

Commit

Permalink
chore!: modify getTreeKeyHash to use hashCommitment (#31)
Browse files Browse the repository at this point in the history
* postfix wasm

* make fail

* make pass again

* update rust-verkle dep to 7688f0aedfb147d3d391abfe8495e46c46d72ce0

* modify rust code to match new API

* modify ts code to match new rust API

* change getTreeKeyHash to use `hashCommitment`

* Update package-lock

---------

Co-authored-by: Holger Drewes <Holger.Drewes@gmail.com>
Co-authored-by: acolytec3 <17355484+acolytec3@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 1, 2024
1 parent 3ddf5b3 commit 9d16a7b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 37 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src.rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ default = ["console_error_panic_hook"]
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.3.0"
wasm-bindgen = { version = "0.2.90", features = ["serde-serialize"] }
verkle-trie = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
verkle-spec = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
ipa-multipoint = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
banderwagon = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
ffi_interface = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
verkle-trie = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
verkle-spec = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
ipa-multipoint = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
banderwagon = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
ffi_interface = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
ark-ff = "0.4.0"
ark-serialize = { version = "^0.4.0", default-features = false }

Expand Down
21 changes: 12 additions & 9 deletions src.rs/src/verkle_ffi_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ impl Context {
let address = js_value_to_bytes::<32>(address.into())?;
let tree_index_le = js_value_to_bytes::<32>(tree_index_le.into())?;

let key =
ffi_interface::get_tree_key(&self.inner.committer, address, tree_index_le, sub_index);
let key = ffi_interface::get_tree_key(&self.inner, address, tree_index_le, sub_index);

Ok(bytes_to_js_value(key).into())
}
Expand All @@ -49,7 +48,7 @@ impl Context {
scalars.extend(js_value_to_bytes::<32>(scalar.into())?);
}

let commitment = ffi_interface::commit_to_scalars(&self.inner.committer, &scalars)
let commitment = ffi_interface::commit_to_scalars(&self.inner, &scalars)
.map_err(|err| JsError::new(&format!("could not commit to scalars: {:?}", err)))?;

Ok(bytes_to_js_value(commitment).into())
Expand All @@ -75,7 +74,7 @@ impl Context {
scalars.extend([0u8; 16]);
}

let commitment = ffi_interface::commit_to_scalars(&self.inner.committer, &scalars)
let commitment = ffi_interface::commit_to_scalars(&self.inner, &scalars)
.map_err(|err| JsError::new(&format!("could not commit to scalars: {:?}", err)))?;

Ok(bytes_to_js_value(commitment).into())
Expand Down Expand Up @@ -122,13 +121,17 @@ impl Context {
///
/// This method does not return a scalar value, it returns 32 bytes.
///
/// Note: We plan to deprecate this method from the public API in favour of using hash commitment
/// This method will only be used internally once that is done.
#[wasm_bindgen(js_name = "deprecateSerializeCommitment")]
/// Note: This method is used to serialize the root node before placing it inside
/// of the block header.
/// The reason we use this method instead of `hashCommitment` is because
/// we want to be able to deserialize a commitment from the block header.
///
/// This is not possible with `hashCommitment` as it is a one way function.
#[wasm_bindgen(js_name = "serializeCommitment")]
pub fn serialize_commitment(&self, commitment: Uint8Array) -> Result<Uint8Array, JsError> {
let commitment = js_value_to_bytes::<64>(commitment.into())?;

let hash = ffi_interface::deprecated_serialize_commitment(commitment);
let hash = ffi_interface::serialize_commitment(commitment);

Ok(bytes_to_js_value(hash).into())
}
Expand Down Expand Up @@ -158,7 +161,7 @@ impl Context {
let new_scalar_value = js_value_to_bytes::<32>(new_scalar_value.into())?;

let updated_commitment = ffi_interface::update_commitment(
&self.inner.committer,
&self.inner,
commitment,
commitment_index,
old_scalar_value,
Expand Down
6 changes: 3 additions & 3 deletions src.ts/tests/ffi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('bindings', () => {
const key = ffi.getTreeKey(address, treeIndexLE, subIndex)
const keyHex = bytesToHex(key)

const expected = '0x76a014d14e338c57342cda5187775c6b75e7f0ef292e81b176c7a5a700273700'
const expected = '0xff7e3916badeb510dfcdad458726273319280742e553d8d229bd676428147300'

expect(keyHex).toBe(expected)
})
Expand All @@ -55,7 +55,7 @@ describe('bindings', () => {
const hash = verkleCrypto.getTreeKeyHash(address, treeIndexLE)
const hashHex = bytesToHex(hash)

const expected = '0x76a014d14e338c57342cda5187775c6b75e7f0ef292e81b176c7a5a70027373a'
const expected = '0xff7e3916badeb510dfcdad458726273319280742e553d8d229bd676428147303'

expect(hashHex).toBe(expected)
})
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('bindings', () => {
])
const commitment = ffi.commitToScalars([scalar])

const commitmentHash = ffi.deprecateSerializeCommitment(commitment)
const commitmentHash = ffi.serializeCommitment(commitment)
const commitmentHashHex = bytesToHex(commitmentHash)

const expected = '0x6d40cf3d3097cb19b0ff686a068d53fb1250cc98bbd33766cf2cce00acb8b0a6'
Expand Down
14 changes: 2 additions & 12 deletions src.ts/verkleFFIBindings/verkleFFI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,9 @@ export function getTreeKeyHash(
}

// Commit to the chunks and compute a 32 byte value that we will denote as the hash
//
// Note: This 32 byte value is not a Scalar. It is just a 32 byte value.
//
// Note: that the .reverse() below is an implementation detail of the underlying
// Note: serialization code returning big endian.
//
// TODO: We want to eventually replace deprecateSerializeCommitment with `hashCommitment`
// TODO: This is a breaking change, so requires more coordination between different implementations
// TODO: once that is done, we can remove the .reverse and the deprecateSerializeCommitment method.
//
const commitment = verkleFFI.commitTo16ByteScalars(chunks)
const serializedCommitment = verkleFFI.deprecateSerializeCommitment(commitment).reverse()
return serializedCommitment
const hash = verkleFFI.hashCommitment(commitment)
return hash
}

function concatenateUint8Arrays(array1: Uint8Array, array2: Uint8Array): Uint8Array {
Expand Down

0 comments on commit 9d16a7b

Please sign in to comment.