Use U512 division for Fp randomness, add Fr interpretation to API.

This commit is contained in:
Sean Bowe 2016-09-18 11:22:53 -06:00
parent b582ba749b
commit dab7fdf411
No known key found for this signature in database
GPG Key ID: 95684257D8F8B031
3 changed files with 17 additions and 18 deletions

View File

@ -43,6 +43,12 @@ impl U512 {
U512(res)
}
/// Get a random U512
pub fn random<R: Rng>(rng: &mut R) -> U512
{
U512(rng.gen())
}
pub fn get_bit(&self, n: usize) -> Option<bool> {
if n >= 512 {
None
@ -62,6 +68,8 @@ impl U512 {
let mut r = U256::zero();
for i in (0..512).rev() {
// NB: modulo's first two bits are always unset
// so this will never destroy information
mul2(&mut r.0);
assert!(r.set_bit(0, self.get_bit(i).unwrap()));
if &r >= modulo {
@ -186,23 +194,7 @@ impl U256 {
/// Produce a random number (mod `modulo`)
pub fn random<R: Rng>(rng: &mut R, modulo: &U256) -> U256
{
let mut res;
loop {
res = U256(rng.gen());
for (i, x) in modulo.bits().enumerate() {
if !x {
assert!(res.set_bit(255 - i, false));
} else {
break;
}
}
if &res < modulo { break; }
}
res
U512::random(rng).divrem(modulo).1
}
pub fn is_zero(&self) -> bool {

View File

@ -4,7 +4,7 @@ use super::FieldElement;
use rustc_serialize::{Encodable, Encoder, Decodable, Decoder};
use arith::U256;
use arith::{U512, U256};
macro_rules! field_impl {
($name:ident, $modulus:expr, $rsquared:expr, $rcubed:expr, $one:expr, $inv:expr) => {
@ -69,6 +69,10 @@ macro_rules! field_impl {
}
}
pub fn interpret(buf: &[u8; 64]) -> Self {
$name::new(U512::interpret(buf).divrem(&U256($modulus)).1).unwrap()
}
/// Returns the modulus
#[inline]
pub fn modulus() -> U256 {

View File

@ -24,6 +24,9 @@ impl Fr {
pub fn from_str(s: &str) -> Option<Self> { fields::Fr::from_str(s).map(|e| Fr(e)) }
pub fn inverse(&self) -> Option<Self> { self.0.inverse().map(|e| Fr(e)) }
pub fn is_zero(&self) -> bool { self.0.is_zero() }
pub fn interpret(buf: &[u8; 64]) -> Fr {
Fr(fields::Fr::interpret(buf))
}
}
impl Add<Fr> for Fr {