Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.88 KB | None | 0 0
  1. use std::ops::*;
  2. use std::convert::Into;
  3. use std::convert::From;
  4. use std::fmt;
  5.  
  6. pub const FIELD_SZ: u32 = 65521;
  7.  
  8. /*
  9. #[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Debug, Default)]
  10. pub struct GF(u8);
  11.  
  12. impl GF {
  13.   pub fn new(val: u8) -> GF {
  14.     GF(val)
  15.   }
  16. }
  17.  
  18. impl Sub for GF {
  19.   type Output = GF;
  20.   fn sub(self, other: GF) -> GF {
  21.     self.0 ^ other.0
  22.   }
  23. }
  24.  
  25. impl SubAssign for GF {
  26.   fn sub(&mut self, other: GF) {
  27.     *self = self.0 ^ other.0;
  28.   }
  29. }
  30.  
  31. impl Add for GF {
  32.   type Output = GF;
  33.   fn add(self, other: GF) -> GF {
  34.     self.0 ^ other.0
  35.   }
  36. }
  37. */
  38.  
  39. // TODO: CRYPTOGRAPHICALLY HARDEN
  40. // IMPORTANT!
  41.  
  42. #[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Default)]
  43. pub struct GF(u32);
  44.  
  45. impl GF {
  46.   pub fn new(val: u32) -> GF {
  47.     GF(val % FIELD_SZ)
  48.   }
  49.   pub fn new8(val: u8) -> GF {
  50.     GF(val as u32 % FIELD_SZ)
  51.   }
  52.  
  53.   pub fn to_byte_pair(self) -> (u8, u8) {
  54.     ((self.0 >> 8) as u8, self.0 as u8)
  55.   }
  56.  
  57.   pub fn pow(self, exp: u32) -> GF {
  58.     let mut e = exp;
  59.     let mut base = self;
  60.     let mut res = GF::new(1);
  61.     while e > 0 {
  62.       while e & 1 == 0 {
  63.         e /= 2;
  64.         base = base*base % FIELD_SZ;
  65.       }
  66.       e -= 1;
  67.       res = res*base % FIELD_SZ;
  68.     }
  69.     res
  70.   }
  71. }
  72.  
  73. impl fmt::Debug for GF {
  74.   fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  75.     write!(f, "GF({})", self.0 as i64 - FIELD_SZ as i64/2)
  76.   }
  77. }
  78.  
  79. impl fmt::Display for GF {
  80.   fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  81.     (self.0 as i64 - FIELD_SZ as i64/2).fmt(f)
  82.   }
  83. }
  84.  
  85. impl Sub for GF {
  86.   type Output = GF;
  87.   fn sub(self, other: GF) -> GF {
  88.     GF::new(self.0 + (FIELD_SZ-other.0))
  89.   }
  90. }
  91.  
  92. impl SubAssign for GF {
  93.   fn sub_assign(&mut self, other: GF) {
  94.     *self = *self - other;
  95.   }
  96. }
  97.  
  98. impl Add for GF {
  99.   type Output = GF;
  100.   fn add(self, other: GF) -> GF {
  101.     GF::new(self.0 + other.0)
  102.   }
  103. }
  104.  
  105. impl AddAssign for GF {
  106.   fn add_assign(&mut self, other: GF)  {
  107.     *self = *self + other;
  108.   }
  109. }
  110.  
  111. impl Mul for GF {
  112.   type Output = GF;
  113.   fn mul(self, other: GF) -> GF {
  114.     GF::new(self.0 * other.0)
  115.  }
  116. }
  117.  
  118. impl MulAssign for GF {
  119.   fn mul_assign(&mut self, other: GF) {
  120.     *self = *self * other;
  121.   }
  122. }
  123.  
  124. // maybe implement div
  125.  
  126. impl Neg for GF {
  127.   type Output = GF;
  128.  
  129.   fn neg(self) -> GF {
  130.     GF(FIELD_SZ-self.0)
  131.   }
  132. }
  133.  
  134. impl Rem<u32> for GF {
  135.   type Output = GF;
  136.  
  137.   fn rem(self, other: u32) -> GF {
  138.     let GF(n) = self;
  139.     GF(n%other)
  140.   }
  141. }
  142.  
  143. impl From<i64> for GF {
  144.   fn from(val: i64) -> GF {
  145.     if val < 0 {
  146.       GF((val % FIELD_SZ as i64) as u32 + FIELD_SZ)
  147.     } else {
  148.       GF(val as u32 % FIELD_SZ)
  149.     }
  150.   }
  151. }
  152.  
  153. impl From<u32> for GF {
  154.   fn from(val: u32) -> GF {
  155.     GF(val % FIELD_SZ)
  156.   }
  157. }
  158.  
  159. impl Into<u32> for GF {
  160.   fn into(self) -> u32 {
  161.     self.0 as u32
  162.   }
  163. }
  164.  
  165. impl Into<u16> for GF {
  166.   fn into(self) -> u16 {
  167.     self.0 as u16
  168.   }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement