mpc_engine/primitives/
auth_share.rs1use serde::{Deserialize, Serialize};
3
4use crate::{primitives::mac::MAC_LENGTH, Error};
5
6use super::mac::{Mac, MacKey};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct Bit {
11 pub(crate) id: BitID,
12 pub(crate) value: bool,
13}
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct BitID(pub(crate) usize);
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct AuthBit {
24 pub(crate) bit: Bit,
25 pub(crate) macs: Vec<(usize, Mac)>,
26 pub(crate) mac_keys: Vec<BitKey>,
27}
28
29impl AuthBit {
30 pub fn serialize_bit_macs(&self) -> Vec<u8> {
32 let mut result = vec![0u8; (self.macs.len() + 1) * MAC_LENGTH + 1];
33 result[0] = self.bit.value as u8;
34 for (key_holder, mac) in self.macs.iter() {
35 result[1 + key_holder * MAC_LENGTH..1 + (key_holder + 1) * MAC_LENGTH]
36 .copy_from_slice(mac);
37 }
38
39 result
40 }
41
42 pub fn deserialize_bit_macs(bytes: &[u8]) -> Result<(bool, Vec<[u8; MAC_LENGTH]>), Error> {
44 if bytes[0] > 1 {
45 return Err(Error::InvalidSerialization);
46 }
47 let bit_value = bytes[0] != 0;
48 let mac_chunks = bytes[1..].chunks_exact(MAC_LENGTH);
49 if !mac_chunks.remainder().is_empty() {
50 return Err(Error::InvalidSerialization);
51 }
52
53 let mut macs: Vec<[u8; MAC_LENGTH]> = Vec::new();
54 for mac in mac_chunks {
55 macs.push(
56 mac.try_into()
57 .expect("chunks should be of the required length"),
58 )
59 }
60
61 Ok((bit_value, macs))
62 }
63}
64
65#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct BitKey {
68 pub(crate) holder_bit_id: BitID,
69 pub(crate) bit_holder: usize,
70 pub(crate) mac_key: MacKey,
71}