add tests, clean
This commit is contained in:
parent
bc427597a4
commit
1f391e9f11
3 changed files with 50 additions and 22 deletions
|
@ -259,8 +259,10 @@ impl<T: Field> Mul<&T> for LinComb<T> {
|
||||||
|
|
||||||
impl<T: Field> Div<&T> for LinComb<T> {
|
impl<T: Field> Div<&T> for LinComb<T> {
|
||||||
type Output = LinComb<T>;
|
type Output = LinComb<T>;
|
||||||
|
// Clippy warns about multiplication in a method named div. It's okay, here, since we multiply with the inverse.
|
||||||
|
#[allow(clippy::suspicious_arithmetic_impl)]
|
||||||
fn div(self, scalar: &T) -> LinComb<T> {
|
fn div(self, scalar: &T) -> LinComb<T> {
|
||||||
self * &T::one().div(scalar)
|
self * &scalar.inverse_mul().unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -201,6 +201,31 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bits() {
|
||||||
|
assert_eq!(FieldPrime::from(0).bits(), 1);
|
||||||
|
assert_eq!(FieldPrime::from(1).bits(), 1);
|
||||||
|
assert_eq!(FieldPrime::from(2).bits(), 2);
|
||||||
|
assert_eq!(FieldPrime::from(3).bits(), 2);
|
||||||
|
assert_eq!(FieldPrime::from(4).bits(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn to_biguint() {
|
||||||
|
assert_eq!(
|
||||||
|
FieldPrime::try_from(FieldPrime::from(2).to_biguint()),
|
||||||
|
Ok(FieldPrime::from(2))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FieldPrime::try_from(FieldPrime::from(0).to_biguint()),
|
||||||
|
Ok(FieldPrime::from(0))
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
FieldPrime::try_from(FieldPrime::max_value().to_biguint()),
|
||||||
|
Ok(FieldPrime::max_value())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn division_negative() {
|
fn division_negative() {
|
||||||
let res = FieldPrime::from(-54) / FieldPrime::from(12);
|
let res = FieldPrime::from(-54) / FieldPrime::from(12);
|
||||||
|
@ -239,7 +264,9 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn bytes_ser_deser() {
|
fn bytes_ser_deser() {
|
||||||
let fp = FieldPrime::from("101");
|
let fp = FieldPrime::from("101");
|
||||||
|
println!("{}", fp);
|
||||||
let bv = fp.to_byte_vector();
|
let bv = fp.to_byte_vector();
|
||||||
|
println!("{:#?}", bv);
|
||||||
assert_eq!(fp, FieldPrime::from_byte_vector(bv));
|
assert_eq!(fp, FieldPrime::from_byte_vector(bv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -276,6 +303,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bigint_assertions() {
|
fn bigint_assertions() {
|
||||||
|
use num_integer::Integer;
|
||||||
let x = BigInt::parse_bytes(b"65", 10).unwrap();
|
let x = BigInt::parse_bytes(b"65", 10).unwrap();
|
||||||
assert_eq!(&x + &x, BigInt::parse_bytes(b"130", 10).unwrap());
|
assert_eq!(&x + &x, BigInt::parse_bytes(b"130", 10).unwrap());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
@ -93,7 +93,7 @@ pub trait Field:
|
||||||
/// Returns this `Field`'s contents as decimal string
|
/// Returns this `Field`'s contents as decimal string
|
||||||
fn to_dec_string(&self) -> String;
|
fn to_dec_string(&self) -> String;
|
||||||
/// Returns the multiplicative inverse, i.e.: self * self.inverse_mul() = Self::one()
|
/// Returns the multiplicative inverse, i.e.: self * self.inverse_mul() = Self::one()
|
||||||
//fn inverse_mul(&self) -> Option<Self>;
|
fn inverse_mul(&self) -> Option<Self>;
|
||||||
/// Returns the smallest value that can be represented by this field type.
|
/// Returns the smallest value that can be represented by this field type.
|
||||||
fn min_value() -> Self;
|
fn min_value() -> Self;
|
||||||
/// Returns the largest value that can be represented by this field type.
|
/// Returns the largest value that can be represented by this field type.
|
||||||
|
@ -190,7 +190,7 @@ mod prime_field {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size as u32
|
std::cmp::max(size as u32, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_biguint(&self) -> BigUint {
|
fn to_biguint(&self) -> BigUint {
|
||||||
|
@ -200,7 +200,7 @@ mod prime_field {
|
||||||
|
|
||||||
fn to_byte_vector(&self) -> Vec<u8> {
|
fn to_byte_vector(&self) -> Vec<u8> {
|
||||||
use ark_ff::BigInteger;
|
use ark_ff::BigInteger;
|
||||||
self.v.into_repr().to_bytes_be()
|
self.v.into_repr().to_bytes_le()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_byte_vector(bytes: Vec<u8>) -> Self {
|
fn from_byte_vector(bytes: Vec<u8>) -> Self {
|
||||||
|
@ -215,6 +215,13 @@ mod prime_field {
|
||||||
self.to_string()
|
self.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn inverse_mul(&self) -> Option<Self> {
|
||||||
|
use ark_ff::Field;
|
||||||
|
Some(FieldPrime {
|
||||||
|
v: self.v.inverse()?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fn min_value() -> FieldPrime {
|
fn min_value() -> FieldPrime {
|
||||||
FieldPrime { v: Fr::from(0u32) }
|
FieldPrime { v: Fr::from(0u32) }
|
||||||
}
|
}
|
||||||
|
@ -239,13 +246,9 @@ mod prime_field {
|
||||||
v: Fr::from_str(s).map_err(|_| FieldParseError)?,
|
v: Fr::from_str(s).map_err(|_| FieldParseError)?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
fn try_from_str(_: &str, _: u32) -> Result<Self, FieldParseError> {
|
fn try_from_str(s: &str, radix: u32) -> Result<Self, FieldParseError> {
|
||||||
unimplemented!("try from str")
|
let x = BigUint::parse_bytes(s.as_bytes(), radix).ok_or(FieldParseError)?;
|
||||||
// let x = BigInt::parse_bytes(s.as_bytes(), radix).ok_or(FieldParseError)?;
|
FieldPrime::try_from(x).map_err(|_| FieldParseError)
|
||||||
// Ok(FieldPrime {
|
|
||||||
// value: &x - x.div_floor(&*P) * &*P,
|
|
||||||
// v: field_new!()
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
fn to_compact_dec_string(&self) -> String {
|
fn to_compact_dec_string(&self) -> String {
|
||||||
//values up to (p-1)/2 included are represented as positive, values between (p+1)/2 and p-1 as represented as negative by subtracting p
|
//values up to (p-1)/2 included are represented as positive, values between (p+1)/2 and p-1 as represented as negative by subtracting p
|
||||||
|
@ -461,21 +464,16 @@ mod prime_field {
|
||||||
assert!(self <= &bound);
|
assert!(self <= &bound);
|
||||||
assert!(other <= &bound);
|
assert!(other <= &bound);
|
||||||
|
|
||||||
let mut big_res = self.v.into_repr();
|
let left = self.to_biguint();
|
||||||
|
let right = other.to_biguint();
|
||||||
|
|
||||||
use ark_ff::BigInteger;
|
let big_res = left + right;
|
||||||
let carry = big_res.add_nocarry(&other.v.into_repr());
|
|
||||||
|
|
||||||
if carry {
|
// we only go up to 2**(bitwidth - 1) because after that we lose uniqueness of bit decomposition
|
||||||
return None;
|
if big_res > bound.to_biguint() {
|
||||||
}
|
|
||||||
|
|
||||||
if big_res > bound.v.into_repr() {
|
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(FieldPrime {
|
Some(self.clone() * other)
|
||||||
v: Fr::from(big_res),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue