Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patchy fixes for elliptic pairing precompiles #3

Merged
merged 3 commits into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/fields/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ macro_rules! field_impl {
}
}

/// Converts a U256 to an Fr regardless of modulus.
pub fn new_mul_factor(mut a: U256) -> Option<Self> {
if true {
a.mul(&U256($rsquared), &U256($modulus), $inv);
Some($name(a))
} else {
None
}
}

pub fn interpret(buf: &[u8; 64]) -> Self {
$name::new(U512::interpret(buf).divrem(&U256($modulus)).1).unwrap()
}
Expand All @@ -80,6 +90,12 @@ macro_rules! field_impl {
U256($modulus)
}

#[inline]
#[allow(dead_code)]
pub fn inv(&self) -> u64 {
$inv
}

pub fn raw(&self) -> &U256 {
&self.0
}
Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate rand;
extern crate rustc_serialize;
extern crate byteorder;

mod arith;
pub mod arith;
mod fields;
mod groups;

Expand Down Expand Up @@ -30,7 +30,7 @@ impl Fr {
pub fn from_slice(slice: &[u8]) -> Result<Self, FieldError> {
arith::U256::from_slice(slice)
.map_err(|_| FieldError::InvalidSliceLength) // todo: maybe more sensful error handling
.and_then(|x| fields::Fr::new(x).ok_or(FieldError::NotMember))
.and_then(|x| fields::Fr::new_mul_factor(x).ok_or(FieldError::NotMember))
.map(|x| Fr(x))
}
pub fn to_big_endian(&self, slice: &mut [u8]) -> Result<(), FieldError> {
Expand Down Expand Up @@ -92,8 +92,11 @@ impl Fq {
.map(|x| Fq(x))
}
pub fn to_big_endian(&self, slice: &mut [u8]) -> Result<(), FieldError> {
self.0.raw().to_big_endian(slice).map_err(|_| FieldError::InvalidSliceLength)
}
let mut a: arith::U256 = self.0.into();
// convert from Montgomery representation
a.mul(&fields::Fq::one().raw(), &fields::Fq::modulus(), self.0.inv());
a.to_big_endian(slice).map_err(|_| FieldError::InvalidSliceLength)
}
}

impl Add<Fq> for Fq {
Expand Down