File tree Expand file tree Collapse file tree 2 files changed +9
-5
lines changed Expand file tree Collapse file tree 2 files changed +9
-5
lines changed Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ pub trait UnsignedMathOps<T: Unsigned<T>> {
2727}
2828
2929pub trait SignedMathOps < T : Signed < T > > {
30- fn mod_inv ( self , m : T ) -> T ;
30+ fn mod_inv ( self , m : T ) -> Option < T > ;
3131}
3232
3333impl < T : Integer < T > > IntegerMathOps < T > for T {
@@ -84,7 +84,7 @@ impl<T: Unsigned<T>> UnsignedMathOps<T> for T {
8484
8585impl < T : Signed < T > > SignedMathOps < T > for T {
8686 // Modular multiplicative inverse
87- fn mod_inv ( self , m : T ) -> T {
87+ fn mod_inv ( self , m : T ) -> Option < T > {
8888 let mut t = T :: ZERO ;
8989 let mut newt = T :: ONE ;
9090 let mut r = m;
@@ -96,9 +96,13 @@ impl<T: Signed<T>> SignedMathOps<T> for T {
9696 ( r, newr) = ( newr, r - quotient * newr) ;
9797 }
9898
99+ if r > T :: ONE {
100+ return None ;
101+ }
99102 if t < T :: ZERO {
100103 t = t + m;
101104 }
102- t
105+
106+ Some ( t)
103107 }
104108}
Original file line number Diff line number Diff line change @@ -118,15 +118,15 @@ impl Technique {
118118
119119 fn inverse ( & self ) -> Technique {
120120 let m = self . m ;
121- let a = self . a . mod_inv ( m) ;
121+ let a = self . a . mod_inv ( m) . unwrap ( ) ;
122122 let c = m - ( a * self . c ) % m;
123123 Technique { a, c, m }
124124 }
125125
126126 fn power ( & self , e : i128 ) -> Technique {
127127 let m = self . m ;
128128 let a = self . a . mod_pow ( e, m) ;
129- let c = ( ( ( a - 1 ) * ( self . a - 1 ) . mod_inv ( m) % m) * self . c ) % m;
129+ let c = ( ( ( a - 1 ) * ( self . a - 1 ) . mod_inv ( m) . unwrap ( ) % m) * self . c ) % m;
130130 Technique { a, c, m }
131131 }
132132
You can’t perform that action at this time.
0 commit comments