11//! Limb addition
22
3- use crate :: { Checked , CheckedAdd , Limb , WideWord , Word , Wrapping , Zero } ;
3+ use crate :: { Checked , CheckedAdd , Limb , WideWord , Word , Wrapping , WrappingAdd , Zero } ;
44use core:: ops:: { Add , AddAssign } ;
55use subtle:: CtOption ;
66
@@ -28,55 +28,25 @@ impl Limb {
2828 }
2929}
3030
31- impl CheckedAdd for Limb {
31+ impl Add for Limb {
3232 type Output = Self ;
3333
3434 #[ inline]
35- fn checked_add ( & self , rhs : Self ) -> CtOption < Self > {
36- let ( result, carry) = self . adc ( rhs, Limb :: ZERO ) ;
37- CtOption :: new ( result, carry. is_zero ( ) )
38- }
39- }
40-
41- impl Add for Wrapping < Limb > {
42- type Output = Self ;
43-
44- fn add ( self , rhs : Self ) -> Wrapping < Limb > {
45- Wrapping ( self . 0 . wrapping_add ( rhs. 0 ) )
46- }
47- }
48-
49- impl Add < & Wrapping < Limb > > for Wrapping < Limb > {
50- type Output = Wrapping < Limb > ;
51-
52- fn add ( self , rhs : & Wrapping < Limb > ) -> Wrapping < Limb > {
53- Wrapping ( self . 0 . wrapping_add ( rhs. 0 ) )
54- }
55- }
56-
57- impl Add < Wrapping < Limb > > for & Wrapping < Limb > {
58- type Output = Wrapping < Limb > ;
59-
60- fn add ( self , rhs : Wrapping < Limb > ) -> Wrapping < Limb > {
61- Wrapping ( self . 0 . wrapping_add ( rhs. 0 ) )
62- }
63- }
64-
65- impl Add < & Wrapping < Limb > > for & Wrapping < Limb > {
66- type Output = Wrapping < Limb > ;
67-
68- fn add ( self , rhs : & Wrapping < Limb > ) -> Wrapping < Limb > {
69- Wrapping ( self . 0 . wrapping_add ( rhs. 0 ) )
35+ fn add ( self , rhs : Self ) -> Self {
36+ self . checked_add ( rhs)
37+ . expect ( "attempted to add with overflow" )
7038 }
7139}
7240
7341impl AddAssign for Wrapping < Limb > {
42+ #[ inline]
7443 fn add_assign ( & mut self , other : Self ) {
7544 * self = * self + other;
7645 }
7746}
7847
7948impl AddAssign < & Wrapping < Limb > > for Wrapping < Limb > {
49+ #[ inline]
8050 fn add_assign ( & mut self , other : & Self ) {
8151 * self = * self + other;
8252 }
@@ -85,6 +55,7 @@ impl AddAssign<&Wrapping<Limb>> for Wrapping<Limb> {
8555impl Add for Checked < Limb > {
8656 type Output = Self ;
8757
58+ #[ inline]
8859 fn add ( self , rhs : Self ) -> Checked < Limb > {
8960 Checked (
9061 self . 0
@@ -96,6 +67,7 @@ impl Add for Checked<Limb> {
9667impl Add < & Checked < Limb > > for Checked < Limb > {
9768 type Output = Checked < Limb > ;
9869
70+ #[ inline]
9971 fn add ( self , rhs : & Checked < Limb > ) -> Checked < Limb > {
10072 Checked (
10173 self . 0
@@ -107,6 +79,7 @@ impl Add<&Checked<Limb>> for Checked<Limb> {
10779impl Add < Checked < Limb > > for & Checked < Limb > {
10880 type Output = Checked < Limb > ;
10981
82+ #[ inline]
11083 fn add ( self , rhs : Checked < Limb > ) -> Checked < Limb > {
11184 Checked (
11285 self . 0
@@ -118,6 +91,7 @@ impl Add<Checked<Limb>> for &Checked<Limb> {
11891impl Add < & Checked < Limb > > for & Checked < Limb > {
11992 type Output = Checked < Limb > ;
12093
94+ #[ inline]
12195 fn add ( self , rhs : & Checked < Limb > ) -> Checked < Limb > {
12296 Checked (
12397 self . 0
@@ -127,17 +101,36 @@ impl Add<&Checked<Limb>> for &Checked<Limb> {
127101}
128102
129103impl AddAssign for Checked < Limb > {
104+ #[ inline]
130105 fn add_assign ( & mut self , other : Self ) {
131106 * self = * self + other;
132107 }
133108}
134109
135110impl AddAssign < & Checked < Limb > > for Checked < Limb > {
111+ #[ inline]
136112 fn add_assign ( & mut self , other : & Self ) {
137113 * self = * self + other;
138114 }
139115}
140116
117+ impl CheckedAdd for Limb {
118+ type Output = Self ;
119+
120+ #[ inline]
121+ fn checked_add ( & self , rhs : Self ) -> CtOption < Self > {
122+ let ( result, carry) = self . adc ( rhs, Limb :: ZERO ) ;
123+ CtOption :: new ( result, carry. is_zero ( ) )
124+ }
125+ }
126+
127+ impl WrappingAdd for Limb {
128+ #[ inline]
129+ fn wrapping_add ( & self , v : & Self ) -> Self {
130+ self . wrapping_add ( * v)
131+ }
132+ }
133+
141134#[ cfg( test) ]
142135mod tests {
143136 use crate :: { CheckedAdd , Limb } ;
0 commit comments