Skip to content

Commit d3a198b

Browse files
committed
add tests for combine_and_verify that also print the results
Signed-off-by: Michael Lodder <[email protected]>
1 parent 4ab5e93 commit d3a198b

File tree

5 files changed

+432
-105
lines changed

5 files changed

+432
-105
lines changed

packages/wasm/rust/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +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"] }
1720
blsful = { version = "3.0.0-pre8", default-features = false, features = ["rust"] }
21+
base64_light = "0.1.5"
1822
ecdsa = "0.16.9"
1923
generic-array = "1.1.1"
20-
lit-frost = { git = "https://github.com/LIT-Protocol/lit-frost.git", features = ["verify_only"] }
24+
lit-frost = { git = "https://github.com/LIT-Protocol/lit-frost.git" }
2125
getrandom = { version = "0.2", features = ["js"] }
2226
hex = "0.4"
2327
hd-keys-curves-wasm = { version = "1.0.1", default-features = false, features = ["k256", "p256"] }
@@ -33,6 +37,7 @@ k256 = { version = "0.13", features = ["arithmetic", "serde"] }
3337
p256 = { version = "0.13", features = ["arithmetic", "serde"] }
3438
p384 = { version = "0.13", features = ["arithmetic", "serde"] }
3539
sha2 = "0.10"
40+
vsss-rs = { version = "5.1.0", optional = true }
3641

3742
wee_alloc = { version = "0.4.5", optional = true }
3843

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
}

packages/wasm/rust/src/combine.rs

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -51,48 +51,52 @@ pub struct SignedDataOutput {
5151
}
5252

5353
#[derive(Deserialize)]
54-
enum SignableOutput {
54+
#[cfg_attr(feature = "test-shares", derive(Serialize))]
55+
pub(crate) enum SignableOutput {
5556
EcdsaSignedMessageShare(EcdsaSignedMessageShare),
5657
FrostSignedMessageShare(FrostSignedMessageShare),
5758
BlsSignedMessageShare(BlsSignedMessageShare),
5859
}
5960

6061
#[derive(Clone, Deserialize)]
61-
struct EcdsaSignedMessageShare {
62-
digest: String,
63-
result: String,
64-
share_id: String,
65-
peer_id: String,
66-
signature_share: String,
67-
big_r: String,
68-
compressed_public_key: String,
69-
public_key: String,
70-
sig_type: String,
62+
#[cfg_attr(feature = "test-shares", derive(Serialize))]
63+
pub(crate) struct EcdsaSignedMessageShare {
64+
pub(crate) digest: String,
65+
pub(crate) result: String,
66+
pub(crate) share_id: String,
67+
pub(crate) peer_id: String,
68+
pub(crate) signature_share: String,
69+
pub(crate) big_r: String,
70+
pub(crate) compressed_public_key: String,
71+
pub(crate) public_key: String,
72+
pub(crate) sig_type: String,
7173
}
7274

7375
#[derive(Deserialize)]
74-
struct FrostSignedMessageShare {
75-
message: String,
76-
result: String,
77-
share_id: String,
78-
peer_id: String,
79-
signature_share: String,
80-
signing_commitments: String,
81-
verifying_share: String,
82-
public_key: String,
83-
sig_type: String,
76+
#[cfg_attr(feature = "test-shares", derive(Serialize))]
77+
pub(crate) struct FrostSignedMessageShare {
78+
pub(crate) message: String,
79+
pub(crate) result: String,
80+
pub(crate) share_id: String,
81+
pub(crate) peer_id: String,
82+
pub(crate) signature_share: String,
83+
pub(crate) signing_commitments: String,
84+
pub(crate) verifying_share: String,
85+
pub(crate) public_key: String,
86+
pub(crate) sig_type: String,
8487
}
8588

8689
#[derive(Deserialize)]
87-
struct BlsSignedMessageShare {
88-
message: String,
89-
result: String,
90-
peer_id: String,
91-
share_id: String,
92-
signature_share: String,
93-
verifying_share: String,
94-
public_key: String,
95-
sig_type: String,
90+
#[cfg_attr(feature = "test-shares", derive(Serialize))]
91+
pub(crate) struct BlsSignedMessageShare {
92+
pub(crate) message: String,
93+
pub(crate) result: String,
94+
pub(crate) peer_id: String,
95+
pub(crate) share_id: String,
96+
pub(crate) signature_share: String,
97+
pub(crate) verifying_share: String,
98+
pub(crate) public_key: String,
99+
pub(crate) sig_type: String,
96100
}
97101

98102
/// A signature share
@@ -174,7 +178,7 @@ where
174178
}
175179
}
176180

177-
fn x_coordinate<C>(point: &C::ProjectivePoint) -> C::Scalar
181+
pub(crate) fn x_coordinate<C>(point: &C::ProjectivePoint) -> C::Scalar
178182
where
179183
C: PrimeCurve + CurveArithmetic,
180184
{

packages/wasm/rust/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ pub mod bls;
33
mod combine;
44
pub mod ecdsa;
55
pub mod sev_snp;
6+
#[cfg(feature = "test-shares")]
7+
pub mod test;
68

79
use wasm_bindgen::prelude::*;
810

0 commit comments

Comments
 (0)