ff: Add PrimeField: From<u64> constraint

This commit is contained in:
Jack Grigg
2020-03-27 23:19:58 +13:00
parent b6457a905b
commit fd79de5408
11 changed files with 50 additions and 37 deletions

View File

@@ -853,6 +853,15 @@ fn prime_field_impl(
}
}
impl From<u64> for #name {
#[inline(always)]
fn from(val: u64) -> #name {
let mut raw = [0u64; #limbs];
raw[0] = val;
#name(#repr(raw)) * #name(R2)
}
}
impl From<#name> for #repr {
fn from(e: #name) -> #repr {
e.into_repr()

View File

@@ -256,7 +256,7 @@ impl fmt::Display for PrimeFieldDecodingError {
}
/// This represents an element of a prime field.
pub trait PrimeField: Field {
pub trait PrimeField: Field + From<u64> {
/// The prime field can be converted back and forth into this biginteger
/// representation.
type Repr: PrimeFieldRepr + From<Self>;
@@ -274,7 +274,7 @@ pub trait PrimeField: Field {
let mut res = Self::zero();
let ten = Self::from_repr(Self::Repr::from(10)).unwrap();
let ten = Self::from(10);
let mut first_digit = true;
@@ -290,7 +290,7 @@ pub trait PrimeField: Field {
}
res.mul_assign(&ten);
res.add_assign(&Self::from_repr(Self::Repr::from(u64::from(c))).unwrap());
res.add_assign(&Self::from(u64::from(c)));
}
None => {
return None;