1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
use ff::{ Field, PrimeField, PrimeFieldRepr, SqrtField }; use std::fmt; use rand::{Rand, Rng}; use std::ops::{Add, Sub, Mul, Neg, Div, AddAssign, SubAssign, MulAssign, DivAssign}; use num::bigint::{BigUint}; #[derive(Copy, Clone, Debug)] pub struct Wrap<T:Field>(pub T); impl<T:Field> fmt::Display for Wrap<T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.0) } } impl<T:Field> Wrap<T> { pub fn new(f:T) -> Self { Wrap(f) } pub fn zero() -> Self { Wrap(T::zero()) } pub fn one() -> Self { Wrap(T::one()) } pub fn minusone() -> Self { -Wrap(T::one()) } pub fn inverse(&self) -> Option<Self> { self.0.inverse().map(|e| Wrap(e)) } pub fn into_inner(&self) -> T { self.0 } pub fn is_zero(&self) -> bool { self.0.is_zero() } pub fn double(&self) -> Self { let mut t = self.0.clone(); t.double(); Wrap(t) } pub fn negate(&self) -> Self { let mut t = self.0.clone(); t.negate(); Wrap(t) } pub fn square(&self) -> Self { let mut t = self.0.clone(); t.square(); Wrap(t) } } impl<T:SqrtField> Wrap<T> { pub fn sqrt(self) -> Option<Self> { self.0.sqrt().map(|x| Wrap(x)) } } impl<T:PrimeField> Wrap<T> { fn num_bytes() -> usize { ((T::NUM_BITS >> 3) + if T::NUM_BITS & 7 == 0 { 0 } else { 1 }) as usize } pub fn into_repr(&self) -> T::Repr { self.0.into_repr() } pub fn into_binary_be(&self) -> Vec<u8> { let t_bytes = Self::num_bytes(); let mut buff = vec![0u8;t_bytes]; self.into_repr().write_be(&mut buff[..]).unwrap(); buff } pub fn from_binary_be(blob: &[u8]) -> Self { let t_bytes = Self::num_bytes(); let mut order = vec![0u8;t_bytes]; T::char().write_be(&mut order[..]).unwrap(); let order = BigUint::from_bytes_be(order.as_ref()); let x = BigUint::from_bytes_be(blob); let remainder = (x % order).to_bytes_be(); let mut rem_buff = vec![0u8;t_bytes]; rem_buff[t_bytes-remainder.len()..].clone_from_slice(&remainder); let mut repr = T::zero().into_raw_repr(); repr.read_be(&rem_buff[..]).unwrap(); Wrap(T::from_repr(repr).unwrap()) } pub fn from_other<G:PrimeField>(n: Wrap<G>) -> Self { let g_bytes = Wrap::<G>::num_bytes(); let mut buff = vec![0u8;g_bytes]; n.0.into_repr().write_be(&mut buff[..]).unwrap(); Self::from_binary_be(buff.as_ref()) } } impl<T:Field> PartialEq for Wrap<T> { fn eq(&self, other: &Self) -> bool { self.0 == other.0 } } impl<T:PrimeField> Into<BigUint> for Wrap<T> { fn into(self) -> BigUint { let bytes = self.into_binary_be(); BigUint::from_bytes_be(&bytes[..]) } } impl<T:PrimeField> From<u64> for Wrap<T> { fn from(n: u64) -> Self { let mut repr = T::zero().into_raw_repr(); repr.as_mut()[0] = n; Wrap::new(T::from_repr(repr).unwrap()) } } impl<T:PrimeField> From<bool> for Wrap<T> { fn from(b: bool) -> Self { if b { Wrap::one() } else { Wrap::zero() } } } impl<T:PrimeField> From<&str> for Wrap<T> { fn from(s: &str) -> Self { Wrap::new(T::from_str(s).unwrap()) } } impl<T:PrimeField> From<i64> for Wrap<T> { fn from(n: i64) -> Self { let mut repr = T::zero().into_raw_repr(); repr.as_mut()[0] = n.abs() as u64; if n >= 0 { Wrap::new(T::from_repr(repr).unwrap()) } else { -Wrap::new(T::from_repr(repr).unwrap()) } } } impl<T:Field> Add<Wrap<T>> for Wrap<T> { type Output = Wrap<T>; fn add(self, other: Wrap<T>) -> Self::Output { let mut res = self; res.0.add_assign(&other.0); res } } impl<T:Field> AddAssign<Wrap<T>> for Wrap<T> { fn add_assign(&mut self, other: Wrap<T>) { self.0.add_assign(&other.0); } } impl<T:Field> Sub<Wrap<T>> for Wrap<T> { type Output = Wrap<T>; fn sub(self, other: Wrap<T>) -> Self::Output { let mut res = self; res.0.sub_assign(&other.0); res } } impl<T:Field> SubAssign<Wrap<T>> for Wrap<T> { fn sub_assign(&mut self, other: Wrap<T>) { self.0.sub_assign(&other.0); } } impl<T:Field> Mul<Wrap<T>> for Wrap<T> { type Output = Wrap<T>; fn mul(self, other: Wrap<T>) -> Self::Output { let mut res = self; res.0.mul_assign(&other.0); res } } impl<T:Field> MulAssign<Wrap<T>> for Wrap<T> { fn mul_assign(&mut self, other: Wrap<T>) { self.0.mul_assign(&other.0); } } impl<T:Field> Div<Wrap<T>> for Wrap<T> { type Output = Wrap<T>; fn div(self, other: Wrap<T>) -> Self::Output { let mut res = self; res.0.mul_assign(&other.0.inverse().unwrap()); res } } impl<T:Field> DivAssign<Wrap<T>> for Wrap<T> { fn div_assign(&mut self, other: Wrap<T>) { self.0.mul_assign(&other.0.inverse().unwrap()); } } impl<T:Field> Neg for Wrap<T> { type Output = Wrap<T>; fn neg(self) -> Self::Output { let mut res = self; res.0.negate(); res } } impl<T:Field> Rand for Wrap<T> { fn rand<R: Rng>(rng: &mut R) -> Self { Wrap(rng.gen()) } } #[cfg(test)] mod signal_test { use super::*; use bellman::pairing::bn256::{Fr}; use rand::{Rng, thread_rng}; #[test] fn test_wrap() { let mut rng = thread_rng(); let order = Into::<BigUint>::into(Wrap::<Fr>::minusone()) + BigUint::from(1u64); let a : Wrap<Fr> = rng.gen(); let b : Wrap<Fr> = rng.gen(); assert!(Into::<BigUint>::into(a+b) == (Into::<BigUint>::into(a) + Into::<BigUint>::into(b)) % &order); assert!(Into::<BigUint>::into(a*b) == (Into::<BigUint>::into(a) * Into::<BigUint>::into(b)) % &order); assert!(Into::<BigUint>::into(a-b) == (&order + Into::<BigUint>::into(a) - Into::<BigUint>::into(b)) % &order); assert!(BigUint::from(1u64) == (Into::<BigUint>::into(a.inverse().unwrap()) * Into::<BigUint>::into(a)) % &order); } }