Skip to content

Commit 9d16a7b

Browse files
kevaundrayholgerd77acolytec3
authored
chore!: modify getTreeKeyHash to use hashCommitment (#31)
* 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 <[email protected]> Co-authored-by: acolytec3 <[email protected]>
1 parent 3ddf5b3 commit 9d16a7b

File tree

6 files changed

+31
-37
lines changed

6 files changed

+31
-37
lines changed

Cargo.lock

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src.rs/Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ default = ["console_error_panic_hook"]
1515
serde = { version = "1.0", features = ["derive"] }
1616
serde-wasm-bindgen = "0.3.0"
1717
wasm-bindgen = { version = "0.2.90", features = ["serde-serialize"] }
18-
verkle-trie = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
19-
verkle-spec = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
20-
ipa-multipoint = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
21-
banderwagon = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
22-
ffi_interface = { git = "https://github.com/crate-crypto/rust-verkle", rev = "6036bde9a8f416648213c59ad0c857b2a6f226f3" }
18+
verkle-trie = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
19+
verkle-spec = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
20+
ipa-multipoint = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
21+
banderwagon = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
22+
ffi_interface = { git = "https://github.com/crate-crypto/rust-verkle", rev = "7688f0aedfb147d3d391abfe8495e46c46d72ce0" }
2323
ark-ff = "0.4.0"
2424
ark-serialize = { version = "^0.4.0", default-features = false }
2525

src.rs/src/verkle_ffi_api.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ impl Context {
3434
let address = js_value_to_bytes::<32>(address.into())?;
3535
let tree_index_le = js_value_to_bytes::<32>(tree_index_le.into())?;
3636

37-
let key =
38-
ffi_interface::get_tree_key(&self.inner.committer, address, tree_index_le, sub_index);
37+
let key = ffi_interface::get_tree_key(&self.inner, address, tree_index_le, sub_index);
3938

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

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

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

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

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

131-
let hash = ffi_interface::deprecated_serialize_commitment(commitment);
134+
let hash = ffi_interface::serialize_commitment(commitment);
132135

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

160163
let updated_commitment = ffi_interface::update_commitment(
161-
&self.inner.committer,
164+
&self.inner,
162165
commitment,
163166
commitment_index,
164167
old_scalar_value,

src.ts/tests/ffi.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('bindings', () => {
3333
const key = ffi.getTreeKey(address, treeIndexLE, subIndex)
3434
const keyHex = bytesToHex(key)
3535

36-
const expected = '0x76a014d14e338c57342cda5187775c6b75e7f0ef292e81b176c7a5a700273700'
36+
const expected = '0xff7e3916badeb510dfcdad458726273319280742e553d8d229bd676428147300'
3737

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

58-
const expected = '0x76a014d14e338c57342cda5187775c6b75e7f0ef292e81b176c7a5a70027373a'
58+
const expected = '0xff7e3916badeb510dfcdad458726273319280742e553d8d229bd676428147303'
5959

6060
expect(hashHex).toBe(expected)
6161
})
@@ -144,7 +144,7 @@ describe('bindings', () => {
144144
])
145145
const commitment = ffi.commitToScalars([scalar])
146146

147-
const commitmentHash = ffi.deprecateSerializeCommitment(commitment)
147+
const commitmentHash = ffi.serializeCommitment(commitment)
148148
const commitmentHashHex = bytesToHex(commitmentHash)
149149

150150
const expected = '0x6d40cf3d3097cb19b0ff686a068d53fb1250cc98bbd33766cf2cce00acb8b0a6'

src.ts/verkleFFIBindings/verkleFFI.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,9 @@ export function getTreeKeyHash(
9090
}
9191

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

10898
function concatenateUint8Arrays(array1: Uint8Array, array2: Uint8Array): Uint8Array {

0 commit comments

Comments
 (0)