mirror of
https://github.com/Qortal/pirate-librustzcash.git
synced 2025-02-01 08:12:14 +00:00
ff: Add PrimeField: From<u64> constraint
This commit is contained in:
parent
b6457a905b
commit
fd79de5408
@ -34,6 +34,12 @@ impl fmt::Display for Fr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<u64> for Fr {
|
||||||
|
fn from(v: u64) -> Fr {
|
||||||
|
Fr(Wrapping((v % MODULUS_R.0 as u64) as u32))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ConditionallySelectable for Fr {
|
impl ConditionallySelectable for Fr {
|
||||||
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self {
|
||||||
Fr(Wrapping(u32::conditional_select(
|
Fr(Wrapping(u32::conditional_select(
|
||||||
|
@ -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 {
|
impl From<#name> for #repr {
|
||||||
fn from(e: #name) -> #repr {
|
fn from(e: #name) -> #repr {
|
||||||
e.into_repr()
|
e.into_repr()
|
||||||
|
@ -256,7 +256,7 @@ impl fmt::Display for PrimeFieldDecodingError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This represents an element of a prime field.
|
/// 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
|
/// The prime field can be converted back and forth into this biginteger
|
||||||
/// representation.
|
/// representation.
|
||||||
type Repr: PrimeFieldRepr + From<Self>;
|
type Repr: PrimeFieldRepr + From<Self>;
|
||||||
@ -274,7 +274,7 @@ pub trait PrimeField: Field {
|
|||||||
|
|
||||||
let mut res = Self::zero();
|
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;
|
let mut first_digit = true;
|
||||||
|
|
||||||
@ -290,7 +290,7 @@ pub trait PrimeField: Field {
|
|||||||
}
|
}
|
||||||
|
|
||||||
res.mul_assign(&ten);
|
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 => {
|
None => {
|
||||||
return None;
|
return None;
|
||||||
|
@ -456,7 +456,7 @@ pub struct Fq(FqRepr);
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_b_coeff() {
|
fn test_b_coeff() {
|
||||||
assert_eq!(Fq::from_repr(FqRepr::from(4)).unwrap(), B_COEFF);
|
assert_eq!(Fq::from(4), B_COEFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -1586,7 +1586,7 @@ fn test_fq_is_valid() {
|
|||||||
assert!(!a.is_valid());
|
assert!(!a.is_valid());
|
||||||
a.0.sub_noborrow(&FqRepr::from(1));
|
a.0.sub_noborrow(&FqRepr::from(1));
|
||||||
assert!(a.is_valid());
|
assert!(a.is_valid());
|
||||||
assert!(Fq(FqRepr::from(0)).is_valid());
|
assert!(Fq::from(0).is_valid());
|
||||||
assert!(Fq(FqRepr([
|
assert!(Fq(FqRepr([
|
||||||
0xdf4671abd14dab3e,
|
0xdf4671abd14dab3e,
|
||||||
0xe2dc0c9f534fbd33,
|
0xe2dc0c9f534fbd33,
|
||||||
@ -2193,10 +2193,7 @@ fn test_fq_root_of_unity() {
|
|||||||
use ff::SqrtField;
|
use ff::SqrtField;
|
||||||
|
|
||||||
assert_eq!(Fq::S, 1);
|
assert_eq!(Fq::S, 1);
|
||||||
assert_eq!(
|
assert_eq!(Fq::multiplicative_generator(), Fq::from(2));
|
||||||
Fq::multiplicative_generator(),
|
|
||||||
Fq::from_repr(FqRepr::from(2)).unwrap()
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Fq::multiplicative_generator().pow_vartime([
|
Fq::multiplicative_generator().pow_vartime([
|
||||||
0xdcff7fffffffd555u64,
|
0xdcff7fffffffd555u64,
|
||||||
@ -2225,9 +2222,7 @@ fn test_fq_ordering() {
|
|||||||
// FqRepr's ordering is well-tested, but we still need to make sure the Fq
|
// FqRepr's ordering is well-tested, but we still need to make sure the Fq
|
||||||
// elements aren't being compared in Montgomery form.
|
// elements aren't being compared in Montgomery form.
|
||||||
for i in 0..100 {
|
for i in 0..100 {
|
||||||
assert!(
|
assert!(Fq::from(i + 1) > Fq::from(i));
|
||||||
Fq::from_repr(FqRepr::from(i + 1)).unwrap() > Fq::from_repr(FqRepr::from(i)).unwrap()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -364,7 +364,7 @@ fn test_fq2_squaring() {
|
|||||||
a.square(),
|
a.square(),
|
||||||
Fq2 {
|
Fq2 {
|
||||||
c0: Fq::zero(),
|
c0: Fq::zero(),
|
||||||
c1: Fq::from_repr(FqRepr::from(2)).unwrap(),
|
c1: Fq::from(2),
|
||||||
}
|
}
|
||||||
); // 2u
|
); // 2u
|
||||||
|
|
||||||
|
@ -368,7 +368,7 @@ fn test_fr_is_valid() {
|
|||||||
assert!(!a.is_valid());
|
assert!(!a.is_valid());
|
||||||
a.0.sub_noborrow(&FrRepr::from(1));
|
a.0.sub_noborrow(&FrRepr::from(1));
|
||||||
assert!(a.is_valid());
|
assert!(a.is_valid());
|
||||||
assert!(Fr(FrRepr::from(0)).is_valid());
|
assert!(Fr::from(0).is_valid());
|
||||||
assert!(Fr(FrRepr([
|
assert!(Fr(FrRepr([
|
||||||
0xffffffff00000000,
|
0xffffffff00000000,
|
||||||
0x53bda402fffe5bfe,
|
0x53bda402fffe5bfe,
|
||||||
@ -961,10 +961,7 @@ fn test_fr_root_of_unity() {
|
|||||||
use ff::SqrtField;
|
use ff::SqrtField;
|
||||||
|
|
||||||
assert_eq!(Fr::S, 32);
|
assert_eq!(Fr::S, 32);
|
||||||
assert_eq!(
|
assert_eq!(Fr::multiplicative_generator(), Fr::from(7));
|
||||||
Fr::multiplicative_generator(),
|
|
||||||
Fr::from_repr(FrRepr::from(7)).unwrap()
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Fr::multiplicative_generator().pow_vartime([
|
Fr::multiplicative_generator().pow_vartime([
|
||||||
0xfffe5bfeffffffffu64,
|
0xfffe5bfeffffffffu64,
|
||||||
|
@ -191,7 +191,7 @@ fn test_g1_uncompressed_invalid_vectors() {
|
|||||||
loop {
|
loop {
|
||||||
let mut x3b = x.square();
|
let mut x3b = x.square();
|
||||||
x3b.mul_assign(&x);
|
x3b.mul_assign(&x);
|
||||||
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
|
x3b.add_assign(&Fq::from(4)); // TODO: perhaps expose coeff_b through API?
|
||||||
|
|
||||||
let y = x3b.sqrt();
|
let y = x3b.sqrt();
|
||||||
if y.is_some().into() {
|
if y.is_some().into() {
|
||||||
@ -331,8 +331,8 @@ fn test_g2_uncompressed_invalid_vectors() {
|
|||||||
let mut x3b = x.square();
|
let mut x3b = x.square();
|
||||||
x3b.mul_assign(&x);
|
x3b.mul_assign(&x);
|
||||||
x3b.add_assign(&Fq2 {
|
x3b.add_assign(&Fq2 {
|
||||||
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
|
c0: Fq::from(4),
|
||||||
c1: Fq::from_repr(FqRepr::from(4)).unwrap(),
|
c1: Fq::from(4),
|
||||||
}); // TODO: perhaps expose coeff_b through API?
|
}); // TODO: perhaps expose coeff_b through API?
|
||||||
|
|
||||||
let y = x3b.sqrt();
|
let y = x3b.sqrt();
|
||||||
@ -428,7 +428,7 @@ fn test_g1_compressed_invalid_vectors() {
|
|||||||
loop {
|
loop {
|
||||||
let mut x3b = x.square();
|
let mut x3b = x.square();
|
||||||
x3b.mul_assign(&x);
|
x3b.mul_assign(&x);
|
||||||
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
|
x3b.add_assign(&Fq::from(4)); // TODO: perhaps expose coeff_b through API?
|
||||||
|
|
||||||
if x3b.sqrt().is_some().into() {
|
if x3b.sqrt().is_some().into() {
|
||||||
x.add_assign(&Fq::one());
|
x.add_assign(&Fq::one());
|
||||||
@ -452,7 +452,7 @@ fn test_g1_compressed_invalid_vectors() {
|
|||||||
loop {
|
loop {
|
||||||
let mut x3b = x.square();
|
let mut x3b = x.square();
|
||||||
x3b.mul_assign(&x);
|
x3b.mul_assign(&x);
|
||||||
x3b.add_assign(&Fq::from_repr(FqRepr::from(4)).unwrap()); // TODO: perhaps expose coeff_b through API?
|
x3b.add_assign(&Fq::from(4)); // TODO: perhaps expose coeff_b through API?
|
||||||
|
|
||||||
if x3b.sqrt().is_some().into() {
|
if x3b.sqrt().is_some().into() {
|
||||||
// We know this is on the curve, but it's likely not going to be in the correct subgroup.
|
// We know this is on the curve, but it's likely not going to be in the correct subgroup.
|
||||||
@ -558,8 +558,8 @@ fn test_g2_compressed_invalid_vectors() {
|
|||||||
let mut x3b = x.square();
|
let mut x3b = x.square();
|
||||||
x3b.mul_assign(&x);
|
x3b.mul_assign(&x);
|
||||||
x3b.add_assign(&Fq2 {
|
x3b.add_assign(&Fq2 {
|
||||||
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
|
c0: Fq::from(4),
|
||||||
c1: Fq::from_repr(FqRepr::from(4)).unwrap(),
|
c1: Fq::from(4),
|
||||||
}); // TODO: perhaps expose coeff_b through API?
|
}); // TODO: perhaps expose coeff_b through API?
|
||||||
|
|
||||||
if x3b.sqrt().is_some().into() {
|
if x3b.sqrt().is_some().into() {
|
||||||
@ -589,8 +589,8 @@ fn test_g2_compressed_invalid_vectors() {
|
|||||||
let mut x3b = x.square();
|
let mut x3b = x.square();
|
||||||
x3b.mul_assign(&x);
|
x3b.mul_assign(&x);
|
||||||
x3b.add_assign(&Fq2 {
|
x3b.add_assign(&Fq2 {
|
||||||
c0: Fq::from_repr(FqRepr::from(4)).unwrap(),
|
c0: Fq::from(4),
|
||||||
c1: Fq::from_repr(FqRepr::from(4)).unwrap(),
|
c1: Fq::from(4),
|
||||||
}); // TODO: perhaps expose coeff_b through API?
|
}); // TODO: perhaps expose coeff_b through API?
|
||||||
|
|
||||||
if x3b.sqrt().is_some().into() {
|
if x3b.sqrt().is_some().into() {
|
||||||
|
@ -119,7 +119,7 @@ pub fn from_str_tests<F: PrimeField>() {
|
|||||||
let n = rng.next_u64();
|
let n = rng.next_u64();
|
||||||
|
|
||||||
let a = F::from_str(&format!("{}", n)).unwrap();
|
let a = F::from_str(&format!("{}", n)).unwrap();
|
||||||
let b = F::from_repr(n.into()).unwrap();
|
let b = F::from(n);
|
||||||
|
|
||||||
assert_eq!(a, b);
|
assert_eq!(a, b);
|
||||||
}
|
}
|
||||||
|
@ -278,6 +278,15 @@ impl ::std::fmt::Display for Fs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<u64> for Fs {
|
||||||
|
#[inline(always)]
|
||||||
|
fn from(val: u64) -> Fs {
|
||||||
|
let mut raw = [0u64; 4];
|
||||||
|
raw[0] = val;
|
||||||
|
Fs(FsRepr(raw)) * Fs(R2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Fs> for FsRepr {
|
impl From<Fs> for FsRepr {
|
||||||
fn from(e: Fs) -> FsRepr {
|
fn from(e: Fs) -> FsRepr {
|
||||||
e.into_repr()
|
e.into_repr()
|
||||||
@ -514,7 +523,7 @@ impl Field for Fs {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn zero() -> Self {
|
fn zero() -> Self {
|
||||||
Fs(FsRepr::from(0))
|
Fs::from(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -1683,10 +1692,7 @@ fn test_fs_num_bits() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_fs_root_of_unity() {
|
fn test_fs_root_of_unity() {
|
||||||
assert_eq!(Fs::S, 1);
|
assert_eq!(Fs::S, 1);
|
||||||
assert_eq!(
|
assert_eq!(Fs::multiplicative_generator(), Fs::from(6));
|
||||||
Fs::multiplicative_generator(),
|
|
||||||
Fs::from_repr(FsRepr::from(6)).unwrap()
|
|
||||||
);
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Fs::multiplicative_generator().pow_vartime([
|
Fs::multiplicative_generator().pow_vartime([
|
||||||
0x684b872f6b7b965bu64,
|
0x684b872f6b7b965bu64,
|
||||||
|
@ -24,7 +24,7 @@ impl<E: JubjubEngine> ValueCommitment<E> {
|
|||||||
pub fn cm(&self, params: &E::Params) -> edwards::Point<E, PrimeOrder> {
|
pub fn cm(&self, params: &E::Params) -> edwards::Point<E, PrimeOrder> {
|
||||||
params
|
params
|
||||||
.generator(FixedGenerators::ValueCommitmentValue)
|
.generator(FixedGenerators::ValueCommitmentValue)
|
||||||
.mul(self.value, params)
|
.mul(E::Fs::from(self.value), params)
|
||||||
.add(
|
.add(
|
||||||
¶ms
|
¶ms
|
||||||
.generator(FixedGenerators::ValueCommitmentRandomness)
|
.generator(FixedGenerators::ValueCommitmentRandomness)
|
||||||
@ -291,7 +291,7 @@ impl<E: JubjubEngine> Note<E> {
|
|||||||
let rho = self.cm_full_point(params).add(
|
let rho = self.cm_full_point(params).add(
|
||||||
¶ms
|
¶ms
|
||||||
.generator(FixedGenerators::NullifierPosition)
|
.generator(FixedGenerators::NullifierPosition)
|
||||||
.mul(position, params),
|
.mul(E::Fs::from(position), params),
|
||||||
params,
|
params,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use pairing::bls12_381::Bls12;
|
use pairing::bls12_381::Bls12;
|
||||||
use zcash_primitives::jubjub::{
|
use zcash_primitives::jubjub::{
|
||||||
edwards, fs::FsRepr, FixedGenerators, JubjubBls12, JubjubParams, Unknown,
|
edwards, fs::Fs, FixedGenerators, JubjubBls12, JubjubParams, Unknown,
|
||||||
};
|
};
|
||||||
use zcash_primitives::transaction::components::Amount;
|
use zcash_primitives::transaction::components::Amount;
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ fn compute_value_balance(
|
|||||||
// Compute it in the exponent
|
// Compute it in the exponent
|
||||||
let mut value_balance = params
|
let mut value_balance = params
|
||||||
.generator(FixedGenerators::ValueCommitmentValue)
|
.generator(FixedGenerators::ValueCommitmentValue)
|
||||||
.mul(FsRepr::from(abs), params);
|
.mul(Fs::from(abs), params);
|
||||||
|
|
||||||
// Negate if necessary
|
// Negate if necessary
|
||||||
if is_negative {
|
if is_negative {
|
||||||
|
Loading…
Reference in New Issue
Block a user