Skip to content

Commit 85869f4

Browse files
authored
add verification hash and file hash functions (#416)
Adding more functions to the thin wasm export, needed for forming shards in particular CC @coyotte508 After the first draft, binary size is 97K for me
1 parent 5afd1ee commit 85869f4

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

hf_xet_thin_wasm/Cargo.lock

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

hf_xet_thin_wasm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ serde-wasm-bindgen = "0.6.5"
1313

1414
deduplication = { path = "../deduplication" }
1515
merklehash = { path = "../merklehash" }
16+
mdb_shard = { path = "../mdb_shard" }

hf_xet_thin_wasm/src/lib.rs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use merklehash::{MerkleHash, xorb_hash};
1+
use merklehash::{DataHashHexParseError, MerkleHash, xorb_hash};
22
use serde::{Deserialize, Serialize};
33
use wasm_bindgen::prelude::*;
44

@@ -65,20 +65,45 @@ fn serialize_result<T: Serialize>(result: &T) -> Result<JsValue, JsValue> {
6565
res
6666
}
6767

68+
/// takes an Array of Objects of the form { "hash": string, "length": number }
69+
/// and returns a string of a hash
6870
#[wasm_bindgen]
6971
pub fn compute_xorb_hash(chunks_array: JsValue) -> Result<String, JsValue> {
7072
let js_chunks: Vec<JsChunk> =
7173
serde_wasm_bindgen::from_value::<Vec<JsChunk>>(chunks_array).map_err(|e| JsValue::from(e.to_string()))?;
7274

7375
let chunks: Vec<(MerkleHash, usize)> = js_chunks
7476
.into_iter()
75-
.map(|jsc| {
76-
Ok::<_, JsValue>((
77-
MerkleHash::from_hex(&jsc.hash).map_err(|e| JsValue::from(e.to_string()))?,
78-
jsc.length as usize,
79-
))
80-
})
81-
.collect::<Result<Vec<_>, _>>()?;
77+
.map(|jsc| Ok((MerkleHash::from_hex(&jsc.hash)?, jsc.length as usize)))
78+
.collect::<Result<_, DataHashHexParseError>>()
79+
.map_err(|e| JsValue::from(e.to_string()))?;
8280

8381
Ok(xorb_hash(&chunks).hex())
8482
}
83+
84+
/// takes an Array of Objects of the form { "hash": string, "length": number }
85+
/// and returns a string of a hash
86+
#[wasm_bindgen]
87+
pub fn compute_file_hash(chunks_array: JsValue) -> Result<String, JsValue> {
88+
let js_chunks =
89+
serde_wasm_bindgen::from_value::<Vec<JsChunk>>(chunks_array).map_err(|e| JsValue::from(e.to_string()))?;
90+
91+
let chunk_list: Vec<(MerkleHash, usize)> = js_chunks
92+
.into_iter()
93+
.map(|jsc| Ok((MerkleHash::from_hex(&jsc.hash)?, jsc.length as usize)))
94+
.collect::<Result<_, DataHashHexParseError>>()
95+
.map_err(|e| JsValue::from(e.to_string()))?;
96+
97+
Ok(merklehash::file_hash(&chunk_list).hex())
98+
}
99+
100+
/// takes an Array of hashes as strings and returns the verification hash for that range of chunk hashes
101+
#[wasm_bindgen]
102+
pub fn compute_verification_hash(chunk_hashes: Vec<String>) -> Result<String, JsValue> {
103+
let chunk_hashes: Vec<MerkleHash> = chunk_hashes
104+
.into_iter()
105+
.map(|hash| MerkleHash::from_hex(&hash))
106+
.collect::<Result<_, DataHashHexParseError>>()
107+
.map_err(|e| JsValue::from(e.to_string()))?;
108+
Ok(mdb_shard::chunk_verification::range_hash_from_chunks(&chunk_hashes).hex())
109+
}

0 commit comments

Comments
 (0)