|
| 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 | +} |
0 commit comments