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
use crate::octet::Octet;
use crate::octets::add_assign;
use crate::octets::fused_addassign_mul_scalar;
use crate::octets::mulassign_scalar;
#[cfg(feature = "serde_support")]
use serde::{Deserialize, Serialize};
use std::ops::AddAssign;
#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Symbol {
value: Vec<u8>,
}
impl Symbol {
pub fn new(value: Vec<u8>) -> Symbol {
Symbol { value }
}
pub fn zero<T>(size: T) -> Symbol
where
T: Into<usize>,
{
Symbol {
value: vec![0; size.into()],
}
}
#[cfg(feature = "benchmarking")]
pub fn len(&self) -> usize {
self.value.len()
}
pub fn as_bytes(&self) -> &[u8] {
&self.value
}
pub fn into_bytes(self) -> Vec<u8> {
self.value
}
pub fn mulassign_scalar(&mut self, scalar: &Octet) {
mulassign_scalar(&mut self.value, scalar);
}
pub fn fused_addassign_mul_scalar(&mut self, other: &Symbol, scalar: &Octet) {
fused_addassign_mul_scalar(&mut self.value, &other.value, scalar);
}
}
impl<'a> AddAssign<&'a Symbol> for Symbol {
fn add_assign(&mut self, other: &'a Symbol) {
add_assign(&mut self.value, &other.value);
}
}
#[cfg(test)]
mod tests {
use rand::Rng;
use crate::symbol::Symbol;
#[test]
fn add_assign() {
let symbol_size = 41;
let mut data1: Vec<u8> = vec![0; symbol_size];
let mut data2: Vec<u8> = vec![0; symbol_size];
let mut result: Vec<u8> = vec![0; symbol_size];
for i in 0..symbol_size {
data1[i] = rand::thread_rng().gen();
data2[i] = rand::thread_rng().gen();
result[i] = data1[i] ^ data2[i];
}
let mut symbol1 = Symbol::new(data1);
let symbol2 = Symbol::new(data2);
symbol1 += &symbol2;
assert_eq!(result, symbol1.into_bytes());
}
}