Skip to content

Commit 502f1d0

Browse files
committed
rustfmt: Run on crypto/poly1305.rs
1 parent 61098c4 commit 502f1d0

File tree

1 file changed

+143
-93
lines changed

1 file changed

+143
-93
lines changed

lightning/src/crypto/poly1305.rs

Lines changed: 143 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,33 @@ use crate::prelude::*;
1313

1414
#[derive(Clone, Copy)]
1515
pub struct Poly1305 {
16-
r : [u32; 5],
17-
h : [u32; 5],
18-
pad : [u32; 4],
19-
leftover : usize,
20-
buffer : [u8; 16],
21-
finalized : bool,
16+
r: [u32; 5],
17+
h: [u32; 5],
18+
pad: [u32; 4],
19+
leftover: usize,
20+
buffer: [u8; 16],
21+
finalized: bool,
2222
}
2323

2424
impl Poly1305 {
2525
pub fn new(key: &[u8]) -> Poly1305 {
2626
assert!(key.len() == 32);
27-
let mut poly = Poly1305{ r: [0u32; 5], h: [0u32; 5], pad: [0u32; 4], leftover: 0, buffer: [0u8; 16], finalized: false };
27+
let mut poly = Poly1305 {
28+
r: [0u32; 5],
29+
h: [0u32; 5],
30+
pad: [0u32; 4],
31+
leftover: 0,
32+
buffer: [0u8; 16],
33+
finalized: false,
34+
};
2835

2936
// r &= 0xffffffc0ffffffc0ffffffc0fffffff
30-
poly.r[0] = (u32::from_le_bytes(key[ 0.. 4].try_into().expect("len is 4")) ) & 0x3ffffff;
31-
poly.r[1] = (u32::from_le_bytes(key[ 3.. 7].try_into().expect("len is 4")) >> 2) & 0x3ffff03;
32-
poly.r[2] = (u32::from_le_bytes(key[ 6..10].try_into().expect("len is 4")) >> 4) & 0x3ffc0ff;
33-
poly.r[3] = (u32::from_le_bytes(key[ 9..13].try_into().expect("len is 4")) >> 6) & 0x3f03fff;
34-
poly.r[4] = (u32::from_le_bytes(key[12..16].try_into().expect("len is 4")) >> 8) & 0x00fffff;
37+
poly.r[0] = (u32::from_le_bytes(key[0..4].try_into().expect("len is 4"))) & 0x3ffffff;
38+
poly.r[1] = (u32::from_le_bytes(key[3..7].try_into().expect("len is 4")) >> 2) & 0x3ffff03;
39+
poly.r[2] = (u32::from_le_bytes(key[6..10].try_into().expect("len is 4")) >> 4) & 0x3ffc0ff;
40+
poly.r[3] = (u32::from_le_bytes(key[9..13].try_into().expect("len is 4")) >> 6) & 0x3f03fff;
41+
poly.r[4] =
42+
(u32::from_le_bytes(key[12..16].try_into().expect("len is 4")) >> 8) & 0x00fffff;
3543

3644
poly.pad[0] = u32::from_le_bytes(key[16..20].try_into().expect("len is 4"));
3745
poly.pad[1] = u32::from_le_bytes(key[20..24].try_into().expect("len is 4"));
@@ -42,7 +50,7 @@ impl Poly1305 {
4250
}
4351

4452
fn block(&mut self, m: &[u8]) {
45-
let hibit : u32 = if self.finalized { 0 } else { 1 << 24 };
53+
let hibit: u32 = if self.finalized { 0 } else { 1 << 24 };
4654

4755
let r0 = self.r[0];
4856
let r1 = self.r[1];
@@ -62,27 +70,58 @@ impl Poly1305 {
6270
let mut h4 = self.h[4];
6371

6472
// h += m
65-
h0 += (u32::from_le_bytes(m[ 0.. 4].try_into().expect("len is 4")) ) & 0x3ffffff;
66-
h1 += (u32::from_le_bytes(m[ 3.. 7].try_into().expect("len is 4")) >> 2) & 0x3ffffff;
67-
h2 += (u32::from_le_bytes(m[ 6..10].try_into().expect("len is 4")) >> 4) & 0x3ffffff;
68-
h3 += (u32::from_le_bytes(m[ 9..13].try_into().expect("len is 4")) >> 6) & 0x3ffffff;
73+
h0 += (u32::from_le_bytes(m[0..4].try_into().expect("len is 4"))) & 0x3ffffff;
74+
h1 += (u32::from_le_bytes(m[3..7].try_into().expect("len is 4")) >> 2) & 0x3ffffff;
75+
h2 += (u32::from_le_bytes(m[6..10].try_into().expect("len is 4")) >> 4) & 0x3ffffff;
76+
h3 += (u32::from_le_bytes(m[9..13].try_into().expect("len is 4")) >> 6) & 0x3ffffff;
6977
h4 += (u32::from_le_bytes(m[12..16].try_into().expect("len is 4")) >> 8) | hibit;
7078

7179
// h *= r
72-
let d0 = (h0 as u64 * r0 as u64) + (h1 as u64 * s4 as u64) + (h2 as u64 * s3 as u64) + (h3 as u64 * s2 as u64) + (h4 as u64 * s1 as u64);
73-
let mut d1 = (h0 as u64 * r1 as u64) + (h1 as u64 * r0 as u64) + (h2 as u64 * s4 as u64) + (h3 as u64 * s3 as u64) + (h4 as u64 * s2 as u64);
74-
let mut d2 = (h0 as u64 * r2 as u64) + (h1 as u64 * r1 as u64) + (h2 as u64 * r0 as u64) + (h3 as u64 * s4 as u64) + (h4 as u64 * s3 as u64);
75-
let mut d3 = (h0 as u64 * r3 as u64) + (h1 as u64 * r2 as u64) + (h2 as u64 * r1 as u64) + (h3 as u64 * r0 as u64) + (h4 as u64 * s4 as u64);
76-
let mut d4 = (h0 as u64 * r4 as u64) + (h1 as u64 * r3 as u64) + (h2 as u64 * r2 as u64) + (h3 as u64 * r1 as u64) + (h4 as u64 * r0 as u64);
80+
let d0 = (h0 as u64 * r0 as u64)
81+
+ (h1 as u64 * s4 as u64)
82+
+ (h2 as u64 * s3 as u64)
83+
+ (h3 as u64 * s2 as u64)
84+
+ (h4 as u64 * s1 as u64);
85+
let mut d1 = (h0 as u64 * r1 as u64)
86+
+ (h1 as u64 * r0 as u64)
87+
+ (h2 as u64 * s4 as u64)
88+
+ (h3 as u64 * s3 as u64)
89+
+ (h4 as u64 * s2 as u64);
90+
let mut d2 = (h0 as u64 * r2 as u64)
91+
+ (h1 as u64 * r1 as u64)
92+
+ (h2 as u64 * r0 as u64)
93+
+ (h3 as u64 * s4 as u64)
94+
+ (h4 as u64 * s3 as u64);
95+
let mut d3 = (h0 as u64 * r3 as u64)
96+
+ (h1 as u64 * r2 as u64)
97+
+ (h2 as u64 * r1 as u64)
98+
+ (h3 as u64 * r0 as u64)
99+
+ (h4 as u64 * s4 as u64);
100+
let mut d4 = (h0 as u64 * r4 as u64)
101+
+ (h1 as u64 * r3 as u64)
102+
+ (h2 as u64 * r2 as u64)
103+
+ (h3 as u64 * r1 as u64)
104+
+ (h4 as u64 * r0 as u64);
77105

78106
// (partial) h %= p
79-
let mut c : u32;
80-
c = (d0 >> 26) as u32; h0 = d0 as u32 & 0x3ffffff;
81-
d1 += c as u64; c = (d1 >> 26) as u32; h1 = d1 as u32 & 0x3ffffff;
82-
d2 += c as u64; c = (d2 >> 26) as u32; h2 = d2 as u32 & 0x3ffffff;
83-
d3 += c as u64; c = (d3 >> 26) as u32; h3 = d3 as u32 & 0x3ffffff;
84-
d4 += c as u64; c = (d4 >> 26) as u32; h4 = d4 as u32 & 0x3ffffff;
85-
h0 += c * 5; c = h0 >> 26; h0 &= 0x3ffffff;
107+
let mut c: u32;
108+
c = (d0 >> 26) as u32;
109+
h0 = d0 as u32 & 0x3ffffff;
110+
d1 += c as u64;
111+
c = (d1 >> 26) as u32;
112+
h1 = d1 as u32 & 0x3ffffff;
113+
d2 += c as u64;
114+
c = (d2 >> 26) as u32;
115+
h2 = d2 as u32 & 0x3ffffff;
116+
d3 += c as u64;
117+
c = (d3 >> 26) as u32;
118+
h3 = d3 as u32 & 0x3ffffff;
119+
d4 += c as u64;
120+
c = (d4 >> 26) as u32;
121+
h4 = d4 as u32 & 0x3ffffff;
122+
h0 += c * 5;
123+
c = h0 >> 26;
124+
h0 &= 0x3ffffff;
86125
h1 += c;
87126

88127
self.h[0] = h0;
@@ -95,7 +134,7 @@ impl Poly1305 {
95134
pub fn finish(&mut self) {
96135
if self.leftover > 0 {
97136
self.buffer[self.leftover] = 1;
98-
for i in self.leftover+1..16 {
137+
for i in self.leftover + 1..16 {
99138
self.buffer[i] = 0;
100139
}
101140
self.finalized = true;
@@ -110,19 +149,36 @@ impl Poly1305 {
110149
let mut h3 = self.h[3];
111150
let mut h4 = self.h[4];
112151

113-
let mut c : u32;
114-
c = h1 >> 26; h1 &= 0x3ffffff;
115-
h2 += c; c = h2 >> 26; h2 &= 0x3ffffff;
116-
h3 += c; c = h3 >> 26; h3 &= 0x3ffffff;
117-
h4 += c; c = h4 >> 26; h4 &= 0x3ffffff;
118-
h0 += c * 5; c = h0 >> 26; h0 &= 0x3ffffff;
119-
h1 += c;
152+
let mut c: u32;
153+
c = h1 >> 26;
154+
h1 &= 0x3ffffff;
155+
h2 += c;
156+
c = h2 >> 26;
157+
h2 &= 0x3ffffff;
158+
h3 += c;
159+
c = h3 >> 26;
160+
h3 &= 0x3ffffff;
161+
h4 += c;
162+
c = h4 >> 26;
163+
h4 &= 0x3ffffff;
164+
h0 += c * 5;
165+
c = h0 >> 26;
166+
h0 &= 0x3ffffff;
167+
h1 += c;
120168

121169
// compute h + -p
122-
let mut g0 = h0.wrapping_add(5); c = g0 >> 26; g0 &= 0x3ffffff;
123-
let mut g1 = h1.wrapping_add(c); c = g1 >> 26; g1 &= 0x3ffffff;
124-
let mut g2 = h2.wrapping_add(c); c = g2 >> 26; g2 &= 0x3ffffff;
125-
let mut g3 = h3.wrapping_add(c); c = g3 >> 26; g3 &= 0x3ffffff;
170+
let mut g0 = h0.wrapping_add(5);
171+
c = g0 >> 26;
172+
g0 &= 0x3ffffff;
173+
let mut g1 = h1.wrapping_add(c);
174+
c = g1 >> 26;
175+
g1 &= 0x3ffffff;
176+
let mut g2 = h2.wrapping_add(c);
177+
c = g2 >> 26;
178+
g2 &= 0x3ffffff;
179+
let mut g3 = h3.wrapping_add(c);
180+
c = g3 >> 26;
181+
g3 &= 0x3ffffff;
126182
let mut g4 = h4.wrapping_add(c).wrapping_sub(1 << 26);
127183

128184
// select h if h < p, or h + -p if h >= p
@@ -140,17 +196,21 @@ impl Poly1305 {
140196
h4 = (h4 & mask) | g4;
141197

142198
// h = h % (2^128)
143-
h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;
144-
h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
199+
h0 = ((h0) | (h1 << 26)) & 0xffffffff;
200+
h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
145201
h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
146-
h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
202+
h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
147203

148204
// h = mac = (h + pad) % (2^128)
149-
let mut f : u64;
150-
f = h0 as u64 + self.pad[0] as u64 ; h0 = f as u32;
151-
f = h1 as u64 + self.pad[1] as u64 + (f >> 32); h1 = f as u32;
152-
f = h2 as u64 + self.pad[2] as u64 + (f >> 32); h2 = f as u32;
153-
f = h3 as u64 + self.pad[3] as u64 + (f >> 32); h3 = f as u32;
205+
let mut f: u64;
206+
f = h0 as u64 + self.pad[0] as u64;
207+
h0 = f as u32;
208+
f = h1 as u64 + self.pad[1] as u64 + (f >> 32);
209+
h1 = f as u32;
210+
f = h2 as u64 + self.pad[2] as u64 + (f >> 32);
211+
h2 = f as u32;
212+
f = h3 as u64 + self.pad[3] as u64 + (f >> 32);
213+
h3 = f as u32;
154214

155215
self.h[0] = h0;
156216
self.h[1] = h1;
@@ -165,7 +225,7 @@ impl Poly1305 {
165225
if self.leftover > 0 {
166226
let want = min(16 - self.leftover, m.len());
167227
for i in 0..want {
168-
self.buffer[self.leftover+i] = m[i];
228+
self.buffer[self.leftover + i] = m[i];
169229
}
170230
m = &m[want..];
171231
self.leftover += want;
@@ -194,7 +254,7 @@ impl Poly1305 {
194254

195255
pub fn raw_result(&mut self, output: &mut [u8]) {
196256
assert!(output.len() >= 16);
197-
if !self.finalized{
257+
if !self.finalized {
198258
self.finish();
199259
}
200260
output[0..4].copy_from_slice(&self.h[0].to_le_bytes());
@@ -219,35 +279,27 @@ mod test {
219279
#[test]
220280
fn test_nacl_vector() {
221281
let key = [
222-
0xee,0xa6,0xa7,0x25,0x1c,0x1e,0x72,0x91,
223-
0x6d,0x11,0xc2,0xcb,0x21,0x4d,0x3c,0x25,
224-
0x25,0x39,0x12,0x1d,0x8e,0x23,0x4e,0x65,
225-
0x2d,0x65,0x1f,0xa4,0xc8,0xcf,0xf8,0x80,
282+
0xee, 0xa6, 0xa7, 0x25, 0x1c, 0x1e, 0x72, 0x91, 0x6d, 0x11, 0xc2, 0xcb, 0x21, 0x4d,
283+
0x3c, 0x25, 0x25, 0x39, 0x12, 0x1d, 0x8e, 0x23, 0x4e, 0x65, 0x2d, 0x65, 0x1f, 0xa4,
284+
0xc8, 0xcf, 0xf8, 0x80,
226285
];
227286

228287
let msg = [
229-
0x8e,0x99,0x3b,0x9f,0x48,0x68,0x12,0x73,
230-
0xc2,0x96,0x50,0xba,0x32,0xfc,0x76,0xce,
231-
0x48,0x33,0x2e,0xa7,0x16,0x4d,0x96,0xa4,
232-
0x47,0x6f,0xb8,0xc5,0x31,0xa1,0x18,0x6a,
233-
0xc0,0xdf,0xc1,0x7c,0x98,0xdc,0xe8,0x7b,
234-
0x4d,0xa7,0xf0,0x11,0xec,0x48,0xc9,0x72,
235-
0x71,0xd2,0xc2,0x0f,0x9b,0x92,0x8f,0xe2,
236-
0x27,0x0d,0x6f,0xb8,0x63,0xd5,0x17,0x38,
237-
0xb4,0x8e,0xee,0xe3,0x14,0xa7,0xcc,0x8a,
238-
0xb9,0x32,0x16,0x45,0x48,0xe5,0x26,0xae,
239-
0x90,0x22,0x43,0x68,0x51,0x7a,0xcf,0xea,
240-
0xbd,0x6b,0xb3,0x73,0x2b,0xc0,0xe9,0xda,
241-
0x99,0x83,0x2b,0x61,0xca,0x01,0xb6,0xde,
242-
0x56,0x24,0x4a,0x9e,0x88,0xd5,0xf9,0xb3,
243-
0x79,0x73,0xf6,0x22,0xa4,0x3d,0x14,0xa6,
244-
0x59,0x9b,0x1f,0x65,0x4c,0xb4,0x5a,0x74,
245-
0xe3,0x55,0xa5,
288+
0x8e, 0x99, 0x3b, 0x9f, 0x48, 0x68, 0x12, 0x73, 0xc2, 0x96, 0x50, 0xba, 0x32, 0xfc,
289+
0x76, 0xce, 0x48, 0x33, 0x2e, 0xa7, 0x16, 0x4d, 0x96, 0xa4, 0x47, 0x6f, 0xb8, 0xc5,
290+
0x31, 0xa1, 0x18, 0x6a, 0xc0, 0xdf, 0xc1, 0x7c, 0x98, 0xdc, 0xe8, 0x7b, 0x4d, 0xa7,
291+
0xf0, 0x11, 0xec, 0x48, 0xc9, 0x72, 0x71, 0xd2, 0xc2, 0x0f, 0x9b, 0x92, 0x8f, 0xe2,
292+
0x27, 0x0d, 0x6f, 0xb8, 0x63, 0xd5, 0x17, 0x38, 0xb4, 0x8e, 0xee, 0xe3, 0x14, 0xa7,
293+
0xcc, 0x8a, 0xb9, 0x32, 0x16, 0x45, 0x48, 0xe5, 0x26, 0xae, 0x90, 0x22, 0x43, 0x68,
294+
0x51, 0x7a, 0xcf, 0xea, 0xbd, 0x6b, 0xb3, 0x73, 0x2b, 0xc0, 0xe9, 0xda, 0x99, 0x83,
295+
0x2b, 0x61, 0xca, 0x01, 0xb6, 0xde, 0x56, 0x24, 0x4a, 0x9e, 0x88, 0xd5, 0xf9, 0xb3,
296+
0x79, 0x73, 0xf6, 0x22, 0xa4, 0x3d, 0x14, 0xa6, 0x59, 0x9b, 0x1f, 0x65, 0x4c, 0xb4,
297+
0x5a, 0x74, 0xe3, 0x55, 0xa5,
246298
];
247299

248300
let expected = [
249-
0xf3,0xff,0xc7,0x70,0x3f,0x94,0x00,0xe5,
250-
0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9,
301+
0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5, 0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33,
302+
0x05, 0xd9,
251303
];
252304

253305
let mut mac = [0u8; 16];
@@ -273,36 +325,34 @@ mod test {
273325
#[test]
274326
fn donna_self_test() {
275327
let wrap_key = [
276-
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
277-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
278-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328+
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
329+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
330+
0x00, 0x00, 0x00, 0x00,
280331
];
281332

282333
let wrap_msg = [
283-
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
284-
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
334+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
335+
0xff, 0xff,
285336
];
286337

287338
let wrap_mac = [
288-
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
289-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
339+
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
340+
0x00, 0x00,
290341
];
291342

292343
let mut mac = [0u8; 16];
293344
poly1305(&wrap_key, &wrap_msg, &mut mac);
294345
assert_eq!(&mac[..], &wrap_mac[..]);
295346

296347
let total_key = [
297-
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff,
298-
0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xff, 0xff,
299-
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
300-
0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
348+
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9,
349+
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
350+
0x00, 0x00, 0x00, 0x00,
301351
];
302352

303353
let total_mac = [
304-
0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd,
305-
0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62, 0x3d, 0x39,
354+
0x64, 0xaf, 0xe2, 0xe8, 0xd6, 0xad, 0x7b, 0xbd, 0xd2, 0x87, 0xf9, 0x7c, 0x44, 0x62,
355+
0x3d, 0x39,
306356
];
307357

308358
let mut tpoly = Poly1305::new(&total_key);
@@ -323,17 +373,17 @@ mod test {
323373
let key = b"this is 32-byte key for Poly1305";
324374
let msg = [0u8; 32];
325375
let expected = [
326-
0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6,
327-
0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07,
376+
0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6, 0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc,
377+
0x03, 0x07,
328378
];
329379
let mut mac = [0u8; 16];
330380
poly1305(key, &msg, &mut mac);
331381
assert_eq!(&mac[..], &expected[..]);
332382

333383
let msg = b"Hello world!";
334-
let expected= [
335-
0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16,
336-
0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0,
384+
let expected = [
385+
0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16, 0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2,
386+
0xb2, 0xf0,
337387
];
338388
poly1305(key, msg, &mut mac);
339389
assert_eq!(&mac[..], &expected[..]);

0 commit comments

Comments
 (0)