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
pub trait Share: Clone {
    fn size(&self) -> usize;
    fn with_size(size: usize) -> Self;
}

#[derive(Debug, Clone)]
pub struct ShamirShare {
    pub id: u8,
    pub body: Vec<u8>,
}

impl Share for ShamirShare {
    fn size(&self) -> usize {
        self.body.len()
    }
    fn with_size(size: usize) -> Self {
        Self {
            id: 0,
            body: vec![0u8; size],
        }
    }
}

#[derive(Debug, Clone)]
pub struct RabinShare {
    pub id: u8,
    pub length: usize,
    pub body: Vec<u8>,
}

impl Share for RabinShare {
    fn size(&self) -> usize {
        self.length
    }
    fn with_size(size: usize) -> Self {
        Self {
            id: 0,
            length: 0,
            body: vec![0u8; size],
        }
    }
}

#[derive(Clone)]
pub struct KrawczykShare {
    pub id: u8,
    pub length: usize,
    pub key: [u8; 44],
    pub body: Vec<u8>,
}

impl Share for KrawczykShare {
    fn size(&self) -> usize {
        self.length
    }
    fn with_size(size: usize) -> Self {
        Self {
            id: 0,
            length: 0,
            key: [0u8; 44],
            body: vec![0u8; size],
        }
    }
}

pub trait ShareVec {
    fn size(&self) -> usize;

    fn with_size(n: usize, size: usize) -> Self;
}

impl<S: Share> ShareVec for Vec<S> {
    fn size(&self) -> usize {
        let original_length = self[0].size();
        if self.iter().all(|s| s.size() == original_length) {
            original_length
        } else {
            panic!("size Error")
        }
    }

    fn with_size(n: usize, size: usize) -> Self {
        vec![S::with_size(size); n]
    }
}