1- // Copyright 2015 blake2-rfc Developers
2- //
3- // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4- // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5- // http://opensource.org/licenses/MIT>, at your option. This file may not be
6- // copied, modified, or distributed except according to those terms.
7-
8- mod simd_opt;
9- mod simdint;
10- mod simdop;
11- mod simdty;
12-
13- pub ( crate ) use self :: simdty:: { u32x4, u64x4} ;
14-
15- pub ( crate ) trait Vector4 < T > : Copy {
16- fn gather ( src : & [ T ] , i0 : usize , i1 : usize , i2 : usize , i3 : usize ) -> Self ;
17-
18- #[ allow( clippy:: wrong_self_convention) ]
19- fn from_le ( self ) -> Self ;
20- fn to_le ( self ) -> Self ;
21-
22- fn wrapping_add ( self , rhs : Self ) -> Self ;
23-
24- fn rotate_right_const ( self , n : u32 ) -> Self ;
25-
26- fn shuffle_left_1 ( self ) -> Self ;
27- fn shuffle_left_2 ( self ) -> Self ;
28- fn shuffle_left_3 ( self ) -> Self ;
29-
30- #[ inline( always) ]
31- fn shuffle_right_1 ( self ) -> Self {
32- self . shuffle_left_3 ( )
33- }
34- #[ inline( always) ]
35- fn shuffle_right_2 ( self ) -> Self {
36- self . shuffle_left_2 ( )
37- }
38- #[ inline( always) ]
39- fn shuffle_right_3 ( self ) -> Self {
40- self . shuffle_left_1 ( )
41- }
42- }
1+ use core:: ops:: { Add , BitXor , Shl , Shr } ;
2+
3+ #[ cfg( feature = "zeroize" ) ]
4+ use digest:: zeroize:: Zeroize ;
435
446macro_rules! impl_vector4 {
457 ( $vec: ident, $word: ident) => {
46- impl Vector4 <$word> for $vec {
8+ #[ derive( Clone , Copy , Debug ) ]
9+ #[ repr( C ) ]
10+ pub ( crate ) struct $vec(
11+ pub ( crate ) $word,
12+ pub ( crate ) $word,
13+ pub ( crate ) $word,
14+ pub ( crate ) $word,
15+ ) ;
16+
17+ impl $vec {
18+ #[ inline( always) ]
19+ pub ( crate ) fn new( e0: $word, e1: $word, e2: $word, e3: $word) -> Self {
20+ Self ( e0, e1, e2, e3)
21+ }
22+
4723 #[ inline( always) ]
48- fn gather( src: & [ $word] , i0: usize , i1: usize , i2: usize , i3: usize ) -> Self {
24+ pub ( crate ) fn gather(
25+ src: & [ $word] ,
26+ i0: usize ,
27+ i1: usize ,
28+ i2: usize ,
29+ i3: usize ,
30+ ) -> Self {
4931 $vec:: new( src[ i0] , src[ i1] , src[ i2] , src[ i3] )
5032 }
5133
5234 #[ cfg( target_endian = "little" ) ]
5335 #[ inline( always) ]
54- fn from_le( self ) -> Self {
36+ #[ allow( clippy:: wrong_self_convention) ]
37+ pub ( crate ) fn from_le( self ) -> Self {
5538 self
5639 }
5740
5841 #[ cfg( not( target_endian = "little" ) ) ]
5942 #[ inline( always) ]
60- fn from_le( self ) -> Self {
43+ pub ( crate ) fn from_le( self ) -> Self {
6144 $vec:: new(
6245 $word:: from_le( self . 0 ) ,
6346 $word:: from_le( self . 1 ) ,
@@ -68,13 +51,13 @@ macro_rules! impl_vector4 {
6851
6952 #[ cfg( target_endian = "little" ) ]
7053 #[ inline( always) ]
71- fn to_le( self ) -> Self {
54+ pub ( crate ) fn to_le( self ) -> Self {
7255 self
7356 }
7457
7558 #[ cfg( not( target_endian = "little" ) ) ]
7659 #[ inline( always) ]
77- fn to_le( self ) -> Self {
60+ pub ( crate ) fn to_le( self ) -> Self {
7861 $vec:: new(
7962 self . 0 . to_le( ) ,
8063 self . 1 . to_le( ) ,
@@ -84,55 +67,118 @@ macro_rules! impl_vector4 {
8467 }
8568
8669 #[ inline( always) ]
87- fn wrapping_add( self , rhs: Self ) -> Self {
70+ pub ( crate ) fn wrapping_add( self , rhs: Self ) -> Self {
8871 self + rhs
8972 }
9073
9174 #[ inline( always) ]
92- fn rotate_right_const( self , n: u32 ) -> Self {
93- simd_opt:: $vec:: rotate_right_const( self , n)
75+ pub ( crate ) fn rotate_right_const( self , n: u32 ) -> Self {
76+ $vec:: new(
77+ self . 0 . rotate_right( n) ,
78+ self . 1 . rotate_right( n) ,
79+ self . 2 . rotate_right( n) ,
80+ self . 3 . rotate_right( n) ,
81+ )
9482 }
9583
96- #[ cfg( feature = "simd" ) ]
9784 #[ inline( always) ]
98- fn shuffle_left_1( self ) -> Self {
99- use crate :: simd:: simdint:: simd_shuffle4;
100- const IDX : [ u32 ; 4 ] = [ 1 , 2 , 3 , 0 ] ;
101- unsafe { simd_shuffle4( self , self , IDX ) }
85+ pub ( crate ) fn shuffle_left_1( self ) -> Self {
86+ $vec:: new( self . 1 , self . 2 , self . 3 , self . 0 )
10287 }
10388
104- #[ cfg( not( feature = "simd" ) ) ]
10589 #[ inline( always) ]
106- fn shuffle_left_1 ( self ) -> Self {
107- $vec:: new( self . 1 , self . 2 , self . 3 , self . 0 )
90+ pub ( crate ) fn shuffle_left_2 ( self ) -> Self {
91+ $vec:: new( self . 2 , self . 3 , self . 0 , self . 1 )
10892 }
10993
110- #[ cfg( feature = "simd" ) ]
11194 #[ inline( always) ]
112- fn shuffle_left_2( self ) -> Self {
113- use crate :: simd:: simdint:: simd_shuffle4;
114- const IDX : [ u32 ; 4 ] = [ 2 , 3 , 0 , 1 ] ;
115- unsafe { simd_shuffle4( self , self , IDX ) }
95+ pub ( crate ) fn shuffle_left_3( self ) -> Self {
96+ $vec:: new( self . 3 , self . 0 , self . 1 , self . 2 )
11697 }
11798
118- #[ cfg( not( feature = "simd" ) ) ]
11999 #[ inline( always) ]
120- fn shuffle_left_2( self ) -> Self {
121- $vec:: new( self . 2 , self . 3 , self . 0 , self . 1 )
100+ pub ( crate ) fn shuffle_right_1( self ) -> Self {
101+ self . shuffle_left_3( )
102+ }
103+ #[ inline( always) ]
104+ pub ( crate ) fn shuffle_right_2( self ) -> Self {
105+ self . shuffle_left_2( )
106+ }
107+ #[ inline( always) ]
108+ pub ( crate ) fn shuffle_right_3( self ) -> Self {
109+ self . shuffle_left_1( )
122110 }
123111
124- #[ cfg( feature = "simd" ) ]
125112 #[ inline( always) ]
126- fn shuffle_left_3( self ) -> Self {
127- use crate :: simd:: simdint:: simd_shuffle4;
128- const IDX : [ u32 ; 4 ] = [ 3 , 0 , 1 , 2 ] ;
129- unsafe { simd_shuffle4( self , self , IDX ) }
113+ pub ( crate ) fn as_bytes( & self ) -> & [ u8 ] {
114+ let p = self as * const Self as * const u8 ;
115+ unsafe { core:: slice:: from_raw_parts( p, core:: mem:: size_of:: <Self >( ) ) }
130116 }
117+ }
118+
119+ impl Add for $vec {
120+ type Output = Self ;
131121
132- #[ cfg( not( feature = "simd" ) ) ]
133122 #[ inline( always) ]
134- fn shuffle_left_3( self ) -> Self {
135- $vec:: new( self . 3 , self . 0 , self . 1 , self . 2 )
123+ fn add( self , rhs: Self ) -> Self :: Output {
124+ $vec:: new(
125+ self . 0 . wrapping_add( rhs. 0 ) ,
126+ self . 1 . wrapping_add( rhs. 1 ) ,
127+ self . 2 . wrapping_add( rhs. 2 ) ,
128+ self . 3 . wrapping_add( rhs. 3 ) ,
129+ )
130+ }
131+ }
132+
133+ impl BitXor for $vec {
134+ type Output = Self ;
135+
136+ #[ inline( always) ]
137+ fn bitxor( self , rhs: Self ) -> Self :: Output {
138+ $vec:: new(
139+ self . 0 ^ rhs. 0 ,
140+ self . 1 ^ rhs. 1 ,
141+ self . 2 ^ rhs. 2 ,
142+ self . 3 ^ rhs. 3 ,
143+ )
144+ }
145+ }
146+
147+ impl Shl <$vec> for $vec {
148+ type Output = Self ;
149+
150+ #[ inline( always) ]
151+ fn shl( self , rhs: Self ) -> Self :: Output {
152+ $vec:: new(
153+ self . 0 << rhs. 0 ,
154+ self . 1 << rhs. 1 ,
155+ self . 2 << rhs. 2 ,
156+ self . 3 << rhs. 3 ,
157+ )
158+ }
159+ }
160+
161+ impl Shr <$vec> for $vec {
162+ type Output = Self ;
163+
164+ #[ inline( always) ]
165+ fn shr( self , rhs: Self ) -> Self :: Output {
166+ $vec:: new(
167+ self . 0 >> rhs. 0 ,
168+ self . 1 >> rhs. 1 ,
169+ self . 2 >> rhs. 2 ,
170+ self . 3 >> rhs. 3 ,
171+ )
172+ }
173+ }
174+
175+ #[ cfg( feature = "zeroize" ) ]
176+ impl Zeroize for $vec {
177+ fn zeroize( & mut self ) {
178+ self . 0 . zeroize( ) ;
179+ self . 1 . zeroize( ) ;
180+ self . 2 . zeroize( ) ;
181+ self . 3 . zeroize( ) ;
136182 }
137183 }
138184 } ;
0 commit comments