File tree 2 files changed +18
-0
lines changed
2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change 2
2
name = " ct"
3
3
version = " 0.1.0"
4
4
authors = [
" Łukasz Niemier <[email protected] >" ]
5
+
6
+ description = " Constatn time numeric operations in Rust"
7
+ license = " MIT"
8
+
9
+ repository = " https://github.com/hauleth/ct"
Original file line number Diff line number Diff line change @@ -4,6 +4,14 @@ use std::mem::size_of;
4
4
#[ derive( Clone , Copy , Debug ) ]
5
5
pub struct Ct < T > ( T ) ;
6
6
7
+ impl < T > Ct < T > where T : Neg < Output =T > + BitAnd < Output =T > + BitXor < Output =T > + Copy {
8
+ pub fn select ( self , a : T , b : T ) -> Self {
9
+ let mask = -self ;
10
+ let ret = mask & ( a ^ b) ;
11
+ ret ^ a
12
+ }
13
+ }
14
+
7
15
macro_rules! implement {
8
16
( Eq for $( $t: ty) ,* ) => {
9
17
$(
@@ -32,6 +40,11 @@ macro_rules! implement {
32
40
33
41
fn $fun( self , other: Self ) -> Self :: Output { Ct ( $t:: $fun( self . 0 , other. 0 ) ) }
34
42
}
43
+ impl <$t> $op<$t> for Ct <$t> where $t: $op {
44
+ type Output = Ct <$t:: Output >;
45
+
46
+ fn $fun( self , other: $t) -> Self :: Output { Ct ( $t:: $fun( self . 0 , other) ) }
47
+ }
35
48
} ;
36
49
( unary $op: ident for Ct <$t: ident> with $fun: ident) => {
37
50
impl <$t> $op for Ct <$t> where $t: $op {
You can’t perform that action at this time.
0 commit comments