-
Notifications
You must be signed in to change notification settings - Fork 68
/
bls.rs
136 lines (113 loc) · 4.14 KB
/
bls.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use std::convert::TryFrom;
use blsful::{
Bls12381G1Impl, Bls12381G2Impl, BlsSignatureImpl, PublicKey, Signature, SignatureSchemes,
TimeCryptCiphertext,
};
use elliptic_curve::group::GroupEncoding;
use js_sys::Uint8Array;
use serde::Deserialize;
use tsify::Tsify;
use wasm_bindgen::prelude::*;
use crate::abi::{from_js, from_uint8array, into_uint8array, JsResult};
#[derive(Tsify, Deserialize)]
#[tsify(from_wasm_abi)]
pub enum BlsVariant {
Bls12381G1,
Bls12381G2,
}
struct Bls<C>(C);
impl<C: BlsSignatureImpl> Bls<C>
where
C::PublicKey: TryFrom<Vec<u8>>,
C::Signature: TryFrom<Vec<u8>>,
C::SignatureShare: TryFrom<Vec<u8>>,
{
pub fn combine(signature_shares: Vec<Uint8Array>) -> JsResult<Uint8Array> {
let signature_shares = signature_shares
.into_iter()
.map(from_uint8array)
.collect::<JsResult<Vec<_>>>()?;
let signature = C::core_combine_signature_shares(&signature_shares)?;
into_uint8array(signature.to_bytes())
}
pub fn verify(
public_key: Uint8Array,
message: Uint8Array,
signature: Uint8Array,
) -> JsResult<()> {
let public_key = from_uint8array(public_key)?;
let signature = from_uint8array(signature)?;
let message = from_js::<Vec<u8>>(message)?;
let signature = Signature::<C>::ProofOfPossession(signature);
signature.verify(&PublicKey(public_key), message)?;
Ok(())
}
pub fn encrypt(
encryption_key: Uint8Array,
message: Uint8Array,
identity: Uint8Array,
) -> JsResult<Uint8Array> {
let encryption_key = from_uint8array(encryption_key)?;
let encryption_key = PublicKey::<C>(encryption_key);
let message = from_js::<Vec<u8>>(message)?;
let identity = from_js::<Vec<u8>>(identity)?;
let ciphertext = encryption_key.encrypt_time_lock(
SignatureSchemes::ProofOfPossession,
message,
identity,
)?;
let ciphertext = serde_bare::to_vec(&ciphertext)?;
into_uint8array(ciphertext)
}
pub fn decrypt(ciphertext: Uint8Array, decryption_key: Uint8Array) -> JsResult<Uint8Array> {
let decryption_key = from_uint8array(decryption_key)?;
let ciphertext = from_js::<Vec<u8>>(ciphertext)?;
let ciphertext = serde_bare::from_slice::<TimeCryptCiphertext<C>>(&ciphertext)?;
let message = ciphertext.decrypt(&Signature::ProofOfPossession(decryption_key));
let message =
Option::<Vec<u8>>::from(message).ok_or_else(|| JsError::new("decryption failed"))?;
into_uint8array(message)
}
}
#[wasm_bindgen(js_name = "blsCombine")]
pub fn bls_combine(variant: BlsVariant, signature_shares: Vec<Uint8Array>) -> JsResult<Uint8Array> {
match variant {
BlsVariant::Bls12381G1 => Bls::<Bls12381G1Impl>::combine(signature_shares),
BlsVariant::Bls12381G2 => Bls::<Bls12381G2Impl>::combine(signature_shares),
}
}
#[wasm_bindgen(js_name = "blsVerify")]
pub fn bls_verify(
variant: BlsVariant,
public_key: Uint8Array,
message: Uint8Array,
signature: Uint8Array,
) -> JsResult<()> {
match variant {
BlsVariant::Bls12381G1 => Bls::<Bls12381G1Impl>::verify(public_key, message, signature),
BlsVariant::Bls12381G2 => Bls::<Bls12381G2Impl>::verify(public_key, message, signature),
}
}
#[wasm_bindgen(js_name = "blsEncrypt")]
pub fn bls_encrypt(
variant: BlsVariant,
encryption_key: Uint8Array,
message: Uint8Array,
identity: Uint8Array,
) -> JsResult<Uint8Array> {
match variant {
BlsVariant::Bls12381G1 => Bls::<Bls12381G1Impl>::encrypt(encryption_key, message, identity),
BlsVariant::Bls12381G2 => Bls::<Bls12381G2Impl>::encrypt(encryption_key, message, identity),
}
}
#[wasm_bindgen(js_name = "blsDecrypt")]
pub fn bls_decrypt(
variant: BlsVariant,
ciphertext: Uint8Array,
decryption_key: Uint8Array,
) -> JsResult<Uint8Array> {
match variant {
BlsVariant::Bls12381G1 => Bls::<Bls12381G1Impl>::decrypt(ciphertext, decryption_key),
BlsVariant::Bls12381G2 => Bls::<Bls12381G2Impl>::decrypt(ciphertext, decryption_key),
}
}