@@ -21,6 +21,7 @@ mod real_chacha {
21
21
struct u32x4 ( pub u32 , pub u32 , pub u32 , pub u32 ) ;
22
22
impl :: core:: ops:: Add for u32x4 {
23
23
type Output = u32x4 ;
24
+ #[ inline]
24
25
fn add ( self , rhs : u32x4 ) -> u32x4 {
25
26
u32x4 ( self . 0 . wrapping_add ( rhs. 0 ) ,
26
27
self . 1 . wrapping_add ( rhs. 1 ) ,
@@ -30,6 +31,7 @@ mod real_chacha {
30
31
}
31
32
impl :: core:: ops:: Sub for u32x4 {
32
33
type Output = u32x4 ;
34
+ #[ inline]
33
35
fn sub ( self , rhs : u32x4 ) -> u32x4 {
34
36
u32x4 ( self . 0 . wrapping_sub ( rhs. 0 ) ,
35
37
self . 1 . wrapping_sub ( rhs. 1 ) ,
@@ -39,23 +41,27 @@ mod real_chacha {
39
41
}
40
42
impl :: core:: ops:: BitXor for u32x4 {
41
43
type Output = u32x4 ;
44
+ #[ inline]
42
45
fn bitxor ( self , rhs : u32x4 ) -> u32x4 {
43
46
u32x4 ( self . 0 ^ rhs. 0 , self . 1 ^ rhs. 1 , self . 2 ^ rhs. 2 , self . 3 ^ rhs. 3 )
44
47
}
45
48
}
46
- impl :: core:: ops:: Shr < u32x4 > for u32x4 {
49
+ impl :: core:: ops:: Shr < u8 > for u32x4 {
47
50
type Output = u32x4 ;
48
- fn shr ( self , rhs : u32x4 ) -> u32x4 {
49
- u32x4 ( self . 0 >> rhs. 0 , self . 1 >> rhs. 1 , self . 2 >> rhs. 2 , self . 3 >> rhs. 3 )
51
+ #[ inline]
52
+ fn shr ( self , shr : u8 ) -> u32x4 {
53
+ u32x4 ( self . 0 >> shr, self . 1 >> shr, self . 2 >> shr, self . 3 >> shr)
50
54
}
51
55
}
52
- impl :: core:: ops:: Shl < u32x4 > for u32x4 {
56
+ impl :: core:: ops:: Shl < u8 > for u32x4 {
53
57
type Output = u32x4 ;
54
- fn shl ( self , rhs : u32x4 ) -> u32x4 {
55
- u32x4 ( self . 0 << rhs. 0 , self . 1 << rhs. 1 , self . 2 << rhs. 2 , self . 3 << rhs. 3 )
58
+ #[ inline]
59
+ fn shl ( self , shl : u8 ) -> u32x4 {
60
+ u32x4 ( self . 0 << shl, self . 1 << shl, self . 2 << shl, self . 3 << shl)
56
61
}
57
62
}
58
63
impl u32x4 {
64
+ #[ inline]
59
65
fn from_bytes ( bytes : & [ u8 ] ) -> Self {
60
66
assert_eq ! ( bytes. len( ) , 4 * 4 ) ;
61
67
Self (
@@ -118,31 +124,25 @@ mod real_chacha {
118
124
macro_rules! round{
119
125
( $state: expr) => { {
120
126
$state. a = $state. a + $state. b;
121
- rotate!( $state. d, $state. a, S16 ) ;
127
+ rotate!( $state. d, $state. a, 16 ) ;
122
128
$state. c = $state. c + $state. d;
123
- rotate!( $state. b, $state. c, S12 ) ;
129
+ rotate!( $state. b, $state. c, 12 ) ;
124
130
$state. a = $state. a + $state. b;
125
- rotate!( $state. d, $state. a, S8 ) ;
131
+ rotate!( $state. d, $state. a, 8 ) ;
126
132
$state. c = $state. c + $state. d;
127
- rotate!( $state. b, $state. c, S7 ) ;
133
+ rotate!( $state. b, $state. c, 7 ) ;
128
134
} }
129
135
}
130
136
131
137
macro_rules! rotate {
132
- ( $a: expr, $b: expr, $c : expr) => { {
138
+ ( $a: expr, $b: expr, $rot : expr) => { {
133
139
let v = $a ^ $b;
134
- let r = S32 - $c ;
140
+ let r = 32 - $rot ;
135
141
let right = v >> r;
136
- $a = ( v << $c ) ^ right
142
+ $a = ( v << $rot ) ^ right
137
143
} }
138
144
}
139
145
140
- const S32 : u32x4 = u32x4 ( 32 , 32 , 32 , 32 ) ;
141
- const S16 : u32x4 = u32x4 ( 16 , 16 , 16 , 16 ) ;
142
- const S12 : u32x4 = u32x4 ( 12 , 12 , 12 , 12 ) ;
143
- const S8 : u32x4 = u32x4 ( 8 , 8 , 8 , 8 ) ;
144
- const S7 : u32x4 = u32x4 ( 7 , 7 , 7 , 7 ) ;
145
-
146
146
impl ChaCha20 {
147
147
pub fn new ( key : & [ u8 ] , nonce : & [ u8 ] ) -> ChaCha20 {
148
148
assert ! ( key. len( ) == 16 || key. len( ) == 32 ) ;
0 commit comments