Skip to content

Commit 95dca6b

Browse files
committed
feat: musig2 support
1 parent de1c320 commit 95dca6b

File tree

8 files changed

+128
-5
lines changed

8 files changed

+128
-5
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ tempfile = "3.3.0"
1818
lazy_static = "1.4.0"
1919
openssl = "0.10.60"
2020
sha2 = "0.10.6"
21-
meesign-crypto = { git = "https://github.com/crocs-muni/meesign-crypto", rev = "b0e7f14", default-features = false, features = ["elgamal-encrypt"] }
21+
meesign-crypto = { git = "https://github.com/crocs-muni/meesign-crypto", rev = "ccf6b1d", default-features = false, features = ["elgamal-encrypt"] }
2222

2323
[build-dependencies]
2424
tonic-build = "0.10"

proto/meesign.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum ProtocolType {
2828
GG18 = 0;
2929
ELGAMAL = 1;
3030
FROST = 2;
31+
MUSIG2 = 3;
3132
}
3233

3334
enum KeyType {

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod proto {
3131
meesign_crypto::proto::ProtocolType::Gg18 => ProtocolType::Gg18,
3232
meesign_crypto::proto::ProtocolType::Elgamal => ProtocolType::Elgamal,
3333
meesign_crypto::proto::ProtocolType::Frost => ProtocolType::Frost,
34+
meesign_crypto::proto::ProtocolType::Musig2 => ProtocolType::Musig2,
3435
}
3536
}
3637
}
@@ -41,14 +42,15 @@ mod proto {
4142
ProtocolType::Gg18 => meesign_crypto::proto::ProtocolType::Gg18,
4243
ProtocolType::Elgamal => meesign_crypto::proto::ProtocolType::Elgamal,
4344
ProtocolType::Frost => meesign_crypto::proto::ProtocolType::Frost,
45+
ProtocolType::Musig2 => meesign_crypto::proto::ProtocolType::Musig2,
4446
}
4547
}
4648
}
4749

4850
impl ProtocolType {
4951
pub fn index_offset(&self) -> u32 {
5052
match self {
51-
ProtocolType::Gg18 | ProtocolType::Elgamal => 0,
53+
ProtocolType::Gg18 | ProtocolType::Elgamal | ProtocolType::Musig2 => 0,
5254
ProtocolType::Frost => 1,
5355
}
5456
}

src/protocols/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ use crate::proto::ProtocolType;
44
pub mod elgamal;
55
pub mod frost;
66
pub mod gg18;
7+
pub mod musig2;
78

89
impl ProtocolType {
910
pub fn check_threshold(self, threshold: u32, group_size: u32) -> bool {
1011
match self {
1112
ProtocolType::Gg18 | ProtocolType::Elgamal | ProtocolType::Frost => {
1213
threshold >= 2 && threshold <= group_size
1314
}
15+
ProtocolType::Musig2 => threshold >= 2 && threshold == group_size,
1416
}
1517
}
1618
}

src/protocols/musig2.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
use crate::communicator::Communicator;
2+
use crate::proto::ProtocolType;
3+
use crate::protocols::Protocol;
4+
use meesign_crypto::proto::{Message, ProtocolGroupInit, ProtocolInit};
5+
use meesign_crypto::protocol::musig2 as protocol;
6+
7+
pub struct MuSig2Group {
8+
parties: u32,
9+
round: u16,
10+
}
11+
12+
impl MuSig2Group {
13+
pub fn new(parties: u32) -> Self {
14+
Self { parties, round: 0 }
15+
}
16+
}
17+
18+
impl Protocol for MuSig2Group {
19+
fn initialize(&mut self, communicator: &mut Communicator, _: &[u8]) {
20+
communicator.set_active_devices();
21+
let parties = self.parties;
22+
communicator.send_all(|idx| {
23+
(ProtocolGroupInit {
24+
protocol_type: meesign_crypto::proto::ProtocolType::Musig2 as i32,
25+
index: idx,
26+
parties,
27+
threshold: parties,
28+
})
29+
.encode_to_vec()
30+
});
31+
32+
self.round = 1;
33+
}
34+
35+
fn advance(&mut self, communicator: &mut Communicator) {
36+
assert!((0..self.last_round()).contains(&self.round));
37+
38+
communicator.relay();
39+
self.round += 1;
40+
}
41+
42+
fn finalize(&mut self, communicator: &mut Communicator) -> Option<Vec<u8>> {
43+
assert_eq!(self.last_round(), self.round);
44+
self.round += 1;
45+
communicator.get_final_message()
46+
}
47+
48+
fn round(&self) -> u16 {
49+
self.round
50+
}
51+
52+
fn last_round(&self) -> u16 {
53+
protocol::KEYGEN_ROUNDS
54+
}
55+
56+
fn get_type(&self) -> ProtocolType {
57+
ProtocolType::Musig2
58+
}
59+
}
60+
61+
pub struct MuSig2Sign {
62+
round: u16,
63+
}
64+
65+
impl MuSig2Sign {
66+
pub fn new() -> Self {
67+
Self { round: 0 }
68+
}
69+
}
70+
71+
impl Protocol for MuSig2Sign {
72+
fn initialize(&mut self, communicator: &mut Communicator, data: &[u8]) {
73+
communicator.set_active_devices();
74+
let participant_indices = communicator.get_protocol_indices();
75+
communicator.send_all(|idx| {
76+
(ProtocolInit {
77+
protocol_type: meesign_crypto::proto::ProtocolType::Musig2 as i32,
78+
indices: participant_indices.clone(),
79+
index: idx,
80+
data: Vec::from(data),
81+
})
82+
.encode_to_vec()
83+
});
84+
85+
self.round = 1;
86+
}
87+
88+
fn advance(&mut self, communicator: &mut Communicator) {
89+
assert!((0..self.last_round()).contains(&self.round));
90+
91+
communicator.relay();
92+
self.round += 1;
93+
}
94+
95+
fn finalize(&mut self, communicator: &mut Communicator) -> Option<Vec<u8>> {
96+
assert_eq!(self.last_round(), self.round);
97+
self.round += 1;
98+
communicator.get_final_message()
99+
}
100+
101+
fn round(&self) -> u16 {
102+
self.round
103+
}
104+
105+
fn last_round(&self) -> u16 {
106+
protocol::SIGN_ROUNDS
107+
}
108+
109+
fn get_type(&self) -> ProtocolType {
110+
ProtocolType::Musig2
111+
}
112+
}

src/tasks/group.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::proto::{KeyType, ProtocolType, TaskType};
55
use crate::protocols::elgamal::ElgamalGroup;
66
use crate::protocols::frost::FROSTGroup;
77
use crate::protocols::gg18::GG18Group;
8+
use crate::protocols::musig2::MuSig2Group;
89
use crate::protocols::Protocol;
910
use crate::tasks::{Task, TaskResult, TaskStatus};
1011
use crate::{get_timestamp, utils};
@@ -51,6 +52,9 @@ impl GroupTask {
5152
(ProtocolType::Frost, KeyType::SignChallenge) => {
5253
Box::new(FROSTGroup::new(devices_len, threshold))
5354
}
55+
(ProtocolType::Musig2, KeyType::SignChallenge) => {
56+
Box::new(MuSig2Group::new(devices_len))
57+
}
5458
(ProtocolType::Elgamal, KeyType::Decrypt) => {
5559
Box::new(ElgamalGroup::new(devices_len, threshold))
5660
}

src/tasks/sign.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::group::Group;
44
use crate::proto::{ProtocolType, SignRequest, TaskType};
55
use crate::protocols::frost::FROSTSign;
66
use crate::protocols::gg18::GG18Sign;
7+
use crate::protocols::musig2::MuSig2Sign;
78
use crate::protocols::Protocol;
89
use crate::tasks::{Task, TaskResult, TaskStatus};
910
use crate::{get_timestamp, utils};
@@ -46,6 +47,7 @@ impl SignTask {
4647
protocol: match protocol_type {
4748
ProtocolType::Gg18 => Box::new(GG18Sign::new()),
4849
ProtocolType::Frost => Box::new(FROSTSign::new()),
50+
ProtocolType::Musig2 => Box::new(MuSig2Sign::new()),
4951
_ => {
5052
warn!("Protocol type {:?} does not support signing", protocol_type);
5153
return Err("Unsupported protocol type for signing".into());

0 commit comments

Comments
 (0)