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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::{Block, Key};
use core::{
convert::TryInto,
num::Wrapping,
ops::{Add, Mul},
};
use universal_hash::{consts::U16, NewUniversalHash, Output, UniversalHash};
#[allow(non_snake_case)]
#[derive(Clone)]
#[repr(align(16))]
#[cfg_attr(
all(
any(target_arch = "x86", target_arch = "x86_64"),
not(feature = "force-soft")
),
derive(Copy)
)]
pub struct Polyval {
H: U64x2,
S: U64x2,
}
impl NewUniversalHash for Polyval {
type KeySize = U16;
fn new(h: &Key) -> Self {
Self {
H: h.into(),
S: U64x2::default(),
}
}
}
impl UniversalHash for Polyval {
type BlockSize = U16;
fn update(&mut self, x: &Block) {
let x = U64x2::from(x);
self.S = (self.S + x) * self.H;
}
fn reset(&mut self) {
self.S = U64x2::default();
}
fn finalize(self) -> Output<Self> {
let mut block = Block::default();
for (chunk, i) in block.chunks_mut(8).zip(&[self.S.0, self.S.1]) {
chunk.copy_from_slice(&i.to_le_bytes());
}
Output::new(block)
}
}
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
struct U64x2(u64, u64);
impl From<&Block> for U64x2 {
fn from(bytes: &Block) -> U64x2 {
U64x2(
u64::from_le_bytes(bytes[..8].try_into().unwrap()),
u64::from_le_bytes(bytes[8..].try_into().unwrap()),
)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl Add for U64x2 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
U64x2(self.0 ^ rhs.0, self.1 ^ rhs.1)
}
}
#[allow(clippy::suspicious_arithmetic_impl)]
impl Mul for U64x2 {
type Output = Self;
fn mul(self, rhs: Self) -> Self {
let h0 = self.0;
let h1 = self.1;
let h0r = rev64(h0);
let h1r = rev64(h1);
let h2 = h0 ^ h1;
let h2r = h0r ^ h1r;
let y0 = rhs.0;
let y1 = rhs.1;
let y0r = rev64(y0);
let y1r = rev64(y1);
let y2 = y0 ^ y1;
let y2r = y0r ^ y1r;
let z0 = bmul64(y0, h0);
let z1 = bmul64(y1, h1);
let mut z2 = bmul64(y2, h2);
let mut z0h = bmul64(y0r, h0r);
let mut z1h = bmul64(y1r, h1r);
let mut z2h = bmul64(y2r, h2r);
z2 ^= z0 ^ z1;
z2h ^= z0h ^ z1h;
z0h = rev64(z0h) >> 1;
z1h = rev64(z1h) >> 1;
z2h = rev64(z2h) >> 1;
let v0 = z0;
let mut v1 = z0h ^ z2;
let mut v2 = z1 ^ z2h;
let mut v3 = z1h;
v2 ^= v0 ^ (v0 >> 1) ^ (v0 >> 2) ^ (v0 >> 7);
v1 ^= (v0 << 63) ^ (v0 << 62) ^ (v0 << 57);
v3 ^= v1 ^ (v1 >> 1) ^ (v1 >> 2) ^ (v1 >> 7);
v2 ^= (v1 << 63) ^ (v1 << 62) ^ (v1 << 57);
U64x2(v2, v3)
}
}
fn bmul64(x: u64, y: u64) -> u64 {
let x0 = Wrapping(x & 0x1111_1111_1111_1111);
let x1 = Wrapping(x & 0x2222_2222_2222_2222);
let x2 = Wrapping(x & 0x4444_4444_4444_4444);
let x3 = Wrapping(x & 0x8888_8888_8888_8888);
let y0 = Wrapping(y & 0x1111_1111_1111_1111);
let y1 = Wrapping(y & 0x2222_2222_2222_2222);
let y2 = Wrapping(y & 0x4444_4444_4444_4444);
let y3 = Wrapping(y & 0x8888_8888_8888_8888);
let mut z0 = ((x0 * y0) ^ (x1 * y3) ^ (x2 * y2) ^ (x3 * y1)).0;
let mut z1 = ((x0 * y1) ^ (x1 * y0) ^ (x2 * y3) ^ (x3 * y2)).0;
let mut z2 = ((x0 * y2) ^ (x1 * y1) ^ (x2 * y0) ^ (x3 * y3)).0;
let mut z3 = ((x0 * y3) ^ (x1 * y2) ^ (x2 * y1) ^ (x3 * y0)).0;
z0 &= 0x1111_1111_1111_1111;
z1 &= 0x2222_2222_2222_2222;
z2 &= 0x4444_4444_4444_4444;
z3 &= 0x8888_8888_8888_8888;
z0 | z1 | z2 | z3
}
fn rev64(mut x: u64) -> u64 {
x = ((x & 0x5555_5555_5555_5555) << 1) | ((x >> 1) & 0x5555_5555_5555_5555);
x = ((x & 0x3333_3333_3333_3333) << 2) | ((x >> 2) & 0x3333_3333_3333_3333);
x = ((x & 0x0f0f_0f0f_0f0f_0f0f) << 4) | ((x >> 4) & 0x0f0f_0f0f_0f0f_0f0f);
x = ((x & 0x00ff_00ff_00ff_00ff) << 8) | ((x >> 8) & 0x00ff_00ff_00ff_00ff);
x = ((x & 0xffff_0000_ffff) << 16) | ((x >> 16) & 0xffff_0000_ffff);
(x << 32) | (x >> 32)
}