6
6
// See: <https://bearssl.org/gitweb/?p=BearSSL;a=blob;f=src/hash/ghash_ctmul64.c>
7
7
8
8
use super :: Backend ;
9
- use crate :: field:: {
10
- clmul:: { self , Clmul } ,
11
- Block ,
12
- } ;
13
- use core:: { convert:: TryInto , ops:: BitXor } ;
9
+ use crate :: field:: Block ;
10
+ use core:: { convert:: TryInto , ops:: Add } ;
14
11
15
12
/// 2 x `u64` values emulating an XMM register
16
13
#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
17
- pub struct U64x2 ( [ u64 ; 2 ] ) ;
14
+ pub struct U64x2 ( u64 , u64 ) ;
18
15
19
16
impl From < Block > for U64x2 {
20
17
fn from ( bytes : Block ) -> U64x2 {
21
- U64x2 ( [
18
+ U64x2 (
22
19
u64:: from_le_bytes ( bytes[ ..8 ] . try_into ( ) . unwrap ( ) ) ,
23
20
u64:: from_le_bytes ( bytes[ 8 ..] . try_into ( ) . unwrap ( ) ) ,
24
- ] )
21
+ )
25
22
}
26
23
}
27
24
@@ -36,66 +33,63 @@ impl From<u128> for U64x2 {
36
33
fn from ( x : u128 ) -> U64x2 {
37
34
let lo = ( x & 0xFFFF_FFFFF ) as u64 ;
38
35
let hi = ( x >> 64 ) as u64 ;
39
- U64x2 ( [ lo, hi] )
36
+ U64x2 ( lo, hi)
40
37
}
41
38
}
42
39
43
40
impl From < U64x2 > for u128 {
44
41
fn from ( u64x2 : U64x2 ) -> u128 {
45
- u128:: from ( u64x2. 0 [ 0 ] ) | ( u128:: from ( u64x2. 0 [ 1 ] ) << 64 )
42
+ u128:: from ( u64x2. 0 ) | ( u128:: from ( u64x2. 1 ) << 64 )
46
43
}
47
44
}
48
45
49
- impl BitXor for U64x2 {
46
+ impl Add for U64x2 {
50
47
type Output = Self ;
51
48
52
- fn bitxor ( self , rhs : Self ) -> Self :: Output {
53
- U64x2 ( [ self . 0 [ 0 ] ^ rhs. 0 [ 0 ] , self . 0 [ 1 ] ^ rhs. 0 [ 1 ] ] )
49
+ /// Adds two POLYVAL field elements.
50
+ fn add ( self , rhs : Self ) -> Self {
51
+ U64x2 ( self . 0 ^ rhs. 0 , self . 1 ^ rhs. 1 )
54
52
}
55
53
}
56
54
57
- impl Clmul for U64x2 {
58
- fn clmul < I > ( self , other : Self , imm : I ) -> Self
59
- where
60
- I : Into < clmul:: PseudoOp > ,
61
- {
55
+ impl Backend for U64x2 {
56
+ fn clmul ( self , other : Self , imm : u8 ) -> Self {
62
57
let ( a, b) = match imm. into ( ) {
63
- clmul:: PseudoOp :: PCLMULLQLQDQ => ( self . 0 [ 0 ] , other. 0 [ 0 ] ) ,
64
- clmul:: PseudoOp :: PCLMULHQLQDQ => ( self . 0 [ 1 ] , other. 0 [ 0 ] ) ,
65
- clmul:: PseudoOp :: PCLMULLQHQDQ => ( self . 0 [ 0 ] , other. 0 [ 1 ] ) ,
66
- clmul:: PseudoOp :: PCLMULHQHQDQ => ( self . 0 [ 1 ] , other. 0 [ 1 ] ) ,
58
+ 0x00 => ( self . 0 , other. 0 ) ,
59
+ 0x01 => ( self . 1 , other. 0 ) ,
60
+ 0x10 => ( self . 0 , other. 1 ) ,
61
+ 0x11 => ( self . 1 , other. 1 ) ,
62
+ _ => unreachable ! ( ) ,
67
63
} ;
68
64
69
- let mut result = [ 0u64 ; 2 ] ;
65
+ let mut result = U64x2 ( 0 , 0 ) ;
70
66
71
67
for i in 0 ..64 {
72
68
if b & ( 1 << i) != 0 {
73
- result[ 1 ] ^= a;
69
+ result. 1 ^= a;
74
70
}
75
71
76
- result[ 0 ] >>= 1 ;
72
+ result. 0 >>= 1 ;
77
73
78
- if result[ 1 ] & 1 != 0 {
79
- result[ 0 ] ^= 1 << 63 ;
74
+ if result. 1 & 1 != 0 {
75
+ result. 0 ^= 1 << 63 ;
80
76
}
81
77
82
- result[ 1 ] >>= 1 ;
78
+ result. 1 >>= 1 ;
83
79
}
84
80
85
- U64x2 ( result)
81
+ result
86
82
}
87
- }
88
83
89
- impl Backend for U64x2 {
90
84
fn shuffle ( self ) -> Self {
91
- U64x2 ( [ self . 0 [ 1 ] , self . 0 [ 0 ] ] )
85
+ U64x2 ( self . 1 , self . 0 )
92
86
}
93
87
94
88
fn shl64 ( self ) -> Self {
95
- U64x2 ( [ 0 , self . 0 [ 0 ] ] )
89
+ U64x2 ( 0 , self . 0 )
96
90
}
97
91
98
92
fn shr64 ( self ) -> Self {
99
- U64x2 ( [ self . 0 [ 1 ] , 0 ] )
93
+ U64x2 ( self . 1 , 0 )
100
94
}
101
95
}
0 commit comments