Skip to content

Commit 80b49b2

Browse files
authored
Merge pull request #779 from LIT-Protocol/ml/combine_all_signatures
add combiner for all
2 parents b98c1da + d3a198b commit 80b49b2

File tree

7 files changed

+928
-159
lines changed

7 files changed

+928
-159
lines changed

packages/wasm/rust/Cargo.toml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ repository = "https://github.com/LIT-Protocol/js-sdk"
1111
crate-type = ["cdylib", "rlib"]
1212

1313
[features]
14+
default = ["verify-only"]
15+
verify-only = ["lit-frost/verify_only"]
16+
test-shares = ["lit-frost/default", "vsss-rs"]
1417

1518
[dependencies]
1619
wasm-bindgen = { version = "0.2", features = ["serde-serialize"] }
17-
blsful = { version = "3.0.0-pre8", default-features = false, features = ["rust"] }
18-
base64_light = "0.1"
20+
blsful = { version = "3.0.0-pre8", default-features = false, features = ["rust"] }
21+
base64_light = "0.1.5"
22+
ecdsa = "0.16.9"
23+
generic-array = "1.1.1"
24+
lit-frost = { git = "https://github.com/LIT-Protocol/lit-frost.git" }
1925
getrandom = { version = "0.2", features = ["js"] }
2026
hex = "0.4"
2127
hd-keys-curves-wasm = { version = "1.0.1", default-features = false, features = ["k256", "p256"] }
@@ -26,9 +32,12 @@ serde_bare = "0.5"
2632
serde-wasm-bindgen = "0.6"
2733

2834
elliptic-curve = "0.13"
29-
k256 = { version = "0.13", features = ["arithmetic"] }
30-
p256 = { version = "0.13", features = ["arithmetic"] }
35+
elliptic-curve-tools = "0.1.2"
36+
k256 = { version = "0.13", features = ["arithmetic", "serde"] }
37+
p256 = { version = "0.13", features = ["arithmetic", "serde"] }
38+
p384 = { version = "0.13", features = ["arithmetic", "serde"] }
3139
sha2 = "0.10"
40+
vsss-rs = { version = "5.1.0", optional = true }
3241

3342
wee_alloc = { version = "0.4.5", optional = true }
3443

@@ -43,7 +52,6 @@ sev = { version = "2.0.2", default-features = false, features = [
4352
rand = "0.8"
4453
serde_bytes = "0.11.14"
4554
tsify = { version = "0.4.5", default-features = false, features = ["js"] }
46-
jubjub-plus = { version = "0.10.4" }
4755

4856
web-sys = { version = "0.3", features = ["console"] }
4957

packages/wasm/rust/src/bls.rs

Lines changed: 62 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use wasm_bindgen::prelude::*;
1+
use base64_light::{base64_decode, base64_encode_bytes};
22
use js_sys::Uint8Array;
3-
use serde::{ Deserialize };
4-
use base64_light::{ base64_decode, base64_encode_bytes };
5-
use tsify::Tsify;
63
use lit_bls_wasm::{
7-
encrypt,
8-
decrypt_with_signature_shares,
9-
combine_signature_shares,
10-
verify_signature,
4+
combine_signature_shares, decrypt_with_signature_shares, encrypt, verify_signature,
115
};
6+
use serde::Deserialize;
7+
use tsify::Tsify;
8+
use wasm_bindgen::prelude::*;
129

1310
type JsResult<T> = Result<T, JsValue>;
1411

@@ -17,102 +14,94 @@ type JsResult<T> = Result<T, JsValue>;
1714
// -----------------------------------------------------------------------
1815
#[wasm_bindgen(js_name = "blsCombine")]
1916
pub fn bls_combine(signature_shares: JsValue) -> Result<String, String> {
20-
let shares: Vec<String> = serde_wasm_bindgen
21-
::from_value(signature_shares)
22-
.map_err(|e| format!("Failed to parse shares: {}", e))?;
17+
let shares: Vec<String> = serde_wasm_bindgen::from_value(signature_shares)
18+
.map_err(|e| format!("Failed to parse shares: {}", e))?;
2319

24-
let combined_signature = combine_signature_shares(
25-
serde_wasm_bindgen::to_value(&shares).unwrap()
26-
).map_err(|e| format!("Failed to combine signature shares: {}", e))?;
20+
let combined_signature =
21+
combine_signature_shares(serde_wasm_bindgen::to_value(&shares).unwrap())
22+
.map_err(|e| format!("Failed to combine signature shares: {}", e))?;
2723

28-
Ok(combined_signature)
24+
Ok(combined_signature)
2925
}
3026

3127
// -----------------------------------------------------------------------
3228
// 2. blsVerify
3329
// -----------------------------------------------------------------------
3430
#[wasm_bindgen(js_name = "blsVerify")]
3531
pub fn bls_verify(
36-
public_key: Uint8Array, // buffer, but will be converted to hex string
37-
message: Uint8Array, // buffer, but will be converted to hex string
38-
signature: String // this is the result from bls_combine. It's a hex string
32+
public_key: Uint8Array, // buffer, but will be converted to hex string
33+
message: Uint8Array, // buffer, but will be converted to hex string
34+
signature: String, // this is the result from bls_combine. It's a hex string
3935
) -> JsResult<()> {
40-
// check if signature is a valid hex string
41-
if !signature.chars().all(|c| c.is_ascii_hexdigit()) {
42-
return Err(JsValue::from_str("Signature must be a hex string"));
43-
}
44-
// convert public_key to hex string
45-
let public_key_hex = hex::encode(public_key.to_vec());
46-
47-
// convert message to base64 string
48-
let message_base64 = base64_encode_bytes(&message.to_vec());
49-
50-
// Validate all inputs are hex
51-
if !public_key_hex.chars().all(|c| c.is_ascii_hexdigit()) {
52-
return Err(JsValue::from_str("Public key must be a hex string"));
53-
}
54-
55-
if !signature.chars().all(|c| c.is_ascii_hexdigit()) {
56-
return Err(JsValue::from_str("Signature must be a hex string"));
57-
}
58-
59-
let signature_bytes = hex
60-
::decode(&signature)
61-
.map_err(|e|
62-
JsValue::from_str(&format!("Failed to decode signature hex: {}", e))
63-
)?;
64-
65-
let signature_base64 = base64_encode_bytes(&signature_bytes);
66-
67-
verify_signature(&public_key_hex, &message_base64, &signature_base64).map_err(
68-
|e| JsValue::from_str(&format!("Verification failed: {}", e))
69-
)
36+
// check if signature is a valid hex string
37+
if !signature.chars().all(|c| c.is_ascii_hexdigit()) {
38+
return Err(JsValue::from_str("Signature must be a hex string"));
39+
}
40+
// convert public_key to hex string
41+
let public_key_hex = hex::encode(public_key.to_vec());
42+
43+
// convert message to base64 string
44+
let message_base64 = base64_encode_bytes(&message.to_vec());
45+
46+
// Validate all inputs are hex
47+
if !public_key_hex.chars().all(|c| c.is_ascii_hexdigit()) {
48+
return Err(JsValue::from_str("Public key must be a hex string"));
49+
}
50+
51+
if !signature.chars().all(|c| c.is_ascii_hexdigit()) {
52+
return Err(JsValue::from_str("Signature must be a hex string"));
53+
}
54+
55+
let signature_bytes = hex::decode(&signature)
56+
.map_err(|e| JsValue::from_str(&format!("Failed to decode signature hex: {}", e)))?;
57+
58+
let signature_base64 = base64_encode_bytes(&signature_bytes);
59+
60+
verify_signature(&public_key_hex, &message_base64, &signature_base64)
61+
.map_err(|e| JsValue::from_str(&format!("Verification failed: {}", e)))
7062
}
7163

7264
// -----------------------------------------------------------------------
7365
// 3. blsEncrypt
7466
// -----------------------------------------------------------------------
7567
#[wasm_bindgen(js_name = "blsEncrypt")]
7668
pub fn bls_encrypt(
77-
encryption_key: Uint8Array,
78-
message: Uint8Array,
79-
identity: Uint8Array
69+
encryption_key: Uint8Array,
70+
message: Uint8Array,
71+
identity: Uint8Array,
8072
) -> JsResult<Uint8Array> {
81-
let encryption_key_hex = hex::encode(encryption_key.to_vec());
82-
let message_base64 = base64_encode_bytes(&message.to_vec());
83-
let identity_base64 = base64_encode_bytes(&identity.to_vec());
73+
let encryption_key_hex = hex::encode(encryption_key.to_vec());
74+
let message_base64 = base64_encode_bytes(&message.to_vec());
75+
let identity_base64 = base64_encode_bytes(&identity.to_vec());
8476

85-
let ciphertext = encrypt(
86-
&encryption_key_hex,
87-
&message_base64,
88-
&identity_base64
89-
).map_err(|e| JsValue::from_str(&format!("Encryption failed: {}", e)))?;
77+
let ciphertext = encrypt(&encryption_key_hex, &message_base64, &identity_base64)
78+
.map_err(|e| JsValue::from_str(&format!("Encryption failed: {}", e)))?;
9079

91-
let decoded_ciphertext = base64_decode(&ciphertext);
80+
let decoded_ciphertext = base64_decode(&ciphertext);
9281

93-
Ok(Uint8Array::from(decoded_ciphertext.as_slice()))
82+
Ok(Uint8Array::from(decoded_ciphertext.as_slice()))
9483
}
9584

9685
// -----------------------------------------------------------------------
9786
// 4. blsDecrypt
9887
// -----------------------------------------------------------------------
9988
#[wasm_bindgen(js_name = "blsDecrypt")]
10089
pub fn bls_decrypt(
101-
ciphertext: Uint8Array,
102-
signature_shares: JsValue // this is the result from bls_combine. It's a hex string
90+
ciphertext: Uint8Array,
91+
signature_shares: JsValue, // this is the result from bls_combine. It's a hex string
10392
) -> JsResult<Uint8Array> {
104-
let ciphertext_base64 = base64_encode_bytes(&ciphertext.to_vec());
93+
let ciphertext_base64 = base64_encode_bytes(&ciphertext.to_vec());
10594

106-
let shares: Vec<String> = serde_wasm_bindgen
107-
::from_value(signature_shares)
108-
.map_err(|e| format!("[blsDecrypt] Failed to parse shares: {}", e))?;
95+
let shares: Vec<String> = serde_wasm_bindgen::from_value(signature_shares)
96+
.map_err(|e| format!("[blsDecrypt] Failed to parse shares: {}", e))?;
10997

110-
let plaintext = decrypt_with_signature_shares(
111-
&ciphertext_base64,
112-
serde_wasm_bindgen::to_value(&shares).unwrap()
113-
).map_err(|e| JsValue::from_str(&format!("Decryption failed: {}", e)))?;
98+
let plaintext = decrypt_with_signature_shares(
99+
&ciphertext_base64,
100+
serde_wasm_bindgen::to_value(&shares).unwrap(),
101+
)
102+
.map_err(|e| JsValue::from_str(&format!("Decryption failed: {}", e)))?;
114103

115-
let decoded_plaintext = base64_decode(&plaintext);
104+
let decoded_plaintext = base64_decode(&plaintext);
116105

117-
Ok(Uint8Array::from(decoded_plaintext.as_slice()))
106+
Ok(Uint8Array::from(decoded_plaintext.as_slice()))
118107
}

0 commit comments

Comments
 (0)