Skip to content

Commit

Permalink
Merge pull request AleoNet#250 from AleoHQ/feat/safe-field-reprs
Browse files Browse the repository at this point in the history
Updates trait `PrimeField` repr methods to correct convention
  • Loading branch information
howardwu committed Jul 10, 2021
2 parents cb0552a + aa65a6d commit 858a77f
Show file tree
Hide file tree
Showing 50 changed files with 145 additions and 145 deletions.
2 changes: 1 addition & 1 deletion algorithms/benches/msm/variable_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn variable_base(c: &mut Criterion) {

let mut rng = XorShiftRng::seed_from_u64(234872845u64);

let v = (0..SAMPLES).map(|_| Fr::rand(&mut rng).into_repr()).collect::<Vec<_>>();
let v = (0..SAMPLES).map(|_| Fr::rand(&mut rng).to_repr()).collect::<Vec<_>>();
let g = (0..SAMPLES)
.map(|_| G1Projective::rand(&mut rng).into_affine())
.collect::<Vec<_>>();
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/commitment/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<G: Group, const NUM_WINDOWS: usize, const WINDOW_SIZE: usize> CommitmentSch
let mut output = self.parameters.crh.hash(&input)?;

// Compute h^r.
let mut scalar_bits = BitIteratorBE::new(randomness.into_repr()).collect::<Vec<_>>();
let mut scalar_bits = BitIteratorBE::new(randomness.to_repr()).collect::<Vec<_>>();
scalar_bits.reverse();
for (bit, power) in scalar_bits.into_iter().zip(&self.parameters.random_base) {
if bit {
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/msm/fixed_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl FixedBaseMSM {
multiples_of_g: &[Vec<T>],
scalar: &T::ScalarField,
) -> T {
let mut scalar_val = scalar.into_repr().to_bits_be();
let mut scalar_val = scalar.to_repr().to_bits_be();
scalar_val.reverse();

let mut res = multiples_of_g[0][0];
Expand Down
4 changes: 2 additions & 2 deletions algorithms/src/msm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn variable_base_test_with_bls12() {

let mut rng = XorShiftRng::seed_from_u64(234872845u64);

let v = (0..SAMPLES).map(|_| Fr::rand(&mut rng).into_repr()).collect::<Vec<_>>();
let v = (0..SAMPLES).map(|_| Fr::rand(&mut rng).to_repr()).collect::<Vec<_>>();
let g = (0..SAMPLES)
.map(|_| G1Projective::rand(&mut rng).into_affine())
.collect::<Vec<_>>();
Expand All @@ -61,7 +61,7 @@ fn variable_base_test_with_bls12_unequal_numbers() {
let mut rng = XorShiftRng::seed_from_u64(234872845u64);

let v = (0..SAMPLES - 1)
.map(|_| Fr::rand(&mut rng).into_repr())
.map(|_| Fr::rand(&mut rng).to_repr())
.collect::<Vec<_>>();
let g = (0..SAMPLES)
.map(|_| G1Projective::rand(&mut rng).into_affine())
Expand Down
30 changes: 15 additions & 15 deletions algorithms/src/msm/variable_base/cuda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,15 +376,15 @@ mod tests {
let output: Vec<Fq> = run_roundtrip("mul_test", &inputs[..]);
for (input, output) in inputs.iter().zip(output.iter()) {
let rust_out = input[0] * &input[1];
let output = output.into_repr_raw();
let rust_out = rust_out.into_repr_raw();
let output = output.to_repr_unchecked();
let rust_out = rust_out.to_repr_unchecked();

if rust_out != output {
eprintln!("test failed: {:?} != {:?}", rust_out.as_ref(), output.as_ref());
eprintln!(
"inputs {:?}, {:?}",
input[0].into_repr_raw().as_ref(),
input[1].into_repr_raw().as_ref()
input[0].to_repr_unchecked().as_ref(),
input[1].to_repr_unchecked().as_ref()
);
assert_eq!(rust_out.as_ref(), output.as_ref());
}
Expand All @@ -398,12 +398,12 @@ mod tests {
let output: Vec<Fq> = run_roundtrip("sqr_test", &inputs[..]);
for (input, output) in inputs.iter().zip(output.iter()) {
let rust_out = input[0].square();
let output = output.into_repr_raw();
let rust_out = rust_out.into_repr_raw();
let output = output.to_repr_unchecked();
let rust_out = rust_out.to_repr_unchecked();

if rust_out != output {
eprintln!("test failed: {:?} != {:?}", rust_out.as_ref(), output.as_ref());
eprintln!("inputs {:?}", input[0].into_repr_raw().as_ref());
eprintln!("inputs {:?}", input[0].to_repr_unchecked().as_ref());
assert_eq!(rust_out.as_ref(), output.as_ref());
}
}
Expand All @@ -417,15 +417,15 @@ mod tests {

for (input, output) in inputs.iter().zip(output.iter()) {
let rust_out = input[0] + &input[1];
let output = output.into_repr_raw();
let rust_out = rust_out.into_repr_raw();
let output = output.to_repr_unchecked();
let rust_out = rust_out.to_repr_unchecked();

if rust_out != output {
eprintln!("test failed: {:?} != {:?}", rust_out.as_ref(), output.as_ref());
eprintln!(
"inputs {:?}, {:?}",
input[0].into_repr_raw().as_ref(),
input[1].into_repr_raw().as_ref()
input[0].to_repr_unchecked().as_ref(),
input[1].to_repr_unchecked().as_ref()
);
assert_eq!(rust_out.as_ref(), output.as_ref());
}
Expand Down Expand Up @@ -519,14 +519,14 @@ mod tests {

for (input, output) in inputs.iter().zip(output.iter()) {
let rust_out = input[0] - &input[1];
let output = output.into_repr_raw();
let rust_out = rust_out.into_repr_raw();
let output = output.to_repr_unchecked();
let rust_out = rust_out.to_repr_unchecked();
if rust_out != output {
eprintln!("test failed: {:?} != {:?}", rust_out.as_ref(), output.as_ref());
eprintln!(
"inputs {:?}, {:?}",
input[0].into_repr_raw().as_ref(),
input[1].into_repr_raw().as_ref()
input[0].to_repr_unchecked().as_ref(),
input[1].to_repr_unchecked().as_ref()
);
assert_eq!(rust_out.as_ref(), output.as_ref());
}
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/msm/variable_base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ mod tests {
fn test_data(seed: u64, samples: usize) -> (Vec<G1Affine>, Vec<BigInteger256>) {
let mut rng = XorShiftRng::seed_from_u64(seed);

let v = (0..samples).map(|_| Fr::rand(&mut rng).into_repr()).collect::<Vec<_>>();
let v = (0..samples).map(|_| Fr::rand(&mut rng).to_repr()).collect::<Vec<_>>();
let g = (0..samples)
.map(|_| G1Projective::rand(&mut rng).into_affine())
.collect::<Vec<_>>();
Expand Down
2 changes: 1 addition & 1 deletion algorithms/src/msm/variable_base/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn msm_standard<G: AffineCurve>(
};

let num_bits = <G::ScalarField as PrimeField>::Parameters::MODULUS_BITS as usize;
let fr_one = G::ScalarField::one().into_repr();
let fr_one = G::ScalarField::one().to_repr();

let zero = G::zero().into_projective();
let window_starts: Vec<_> = (0..num_bits).step_by(c).collect();
Expand Down
8 changes: 4 additions & 4 deletions algorithms/src/snark/gm17/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,20 @@ where

let input_assignment = full_input_assignment[1..prover.num_public_variables]
.iter()
.map(|s| s.into_repr())
.map(|s| s.to_repr())
.collect::<Vec<_>>();

let aux_assignment = cfg_into_iter!(full_input_assignment[prover.num_public_variables..])
.map(|s| s.into_repr())
.map(|s| s.to_repr())
.collect::<Vec<_>>();
drop(full_input_assignment);

let h_input = h[0..prover.num_public_variables]
.iter()
.map(|s| s.into_repr())
.map(|s| s.to_repr())
.collect::<Vec<_>>();
let h_aux = cfg_into_iter!(h[prover.num_public_variables..])
.map(|s| s.into_repr())
.map(|s| s.to_repr())
.collect::<Vec<_>>();
drop(h);

Expand Down
6 changes: 3 additions & 3 deletions algorithms/src/snark/groth16/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,16 @@ where
.public_variables
.iter()
.skip(1)
.map(|s| s.into_repr())
.map(|s| s.to_repr())
.collect::<Vec<_>>();

let aux_assignment = cfg_into_iter!(prover.private_variables)
.map(|s| s.into_repr())
.map(|s| s.to_repr())
.collect::<Vec<_>>();

let assignment = [&input_assignment[..], &aux_assignment[..]].concat();

let h_assignment = cfg_into_iter!(h).map(|s| s.into_repr()).collect::<Vec<_>>();
let h_assignment = cfg_into_iter!(h).map(|s| s.to_repr()).collect::<Vec<_>>();

// Compute A
let a_acc_time = start_timer!(|| "Compute A");
Expand Down
4 changes: 2 additions & 2 deletions curves/benches/bls12_377/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub(crate) fn bench_fq_into_repr(c: &mut Criterion) {
c.bench_function("bls12_377: fq_into_repr", |c| {
c.iter(|| {
count = (count + 1) % SAMPLES;
v[count].into_repr()
v[count].to_repr()
})
});
}
Expand All @@ -303,7 +303,7 @@ pub(crate) fn bench_fq_from_repr(c: &mut Criterion) {

let mut rng = XorShiftRng::seed_from_u64(1231275789u64);

let v: Vec<FqRepr> = (0..SAMPLES).map(|_| Fq::rand(&mut rng).into_repr()).collect();
let v: Vec<FqRepr> = (0..SAMPLES).map(|_| Fq::rand(&mut rng).to_repr()).collect();

let mut count = 0;
c.bench_function("bls12_377: fq_from_repr", |c| {
Expand Down
4 changes: 2 additions & 2 deletions curves/benches/bls12_377/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub(crate) fn bench_fr_into_repr(c: &mut Criterion) {
c.bench_function("bls12_377: fr_into_repr", |c| {
c.iter(|| {
count = (count + 1) % SAMPLES;
v[count].into_repr()
v[count].to_repr()
})
});
}
Expand All @@ -303,7 +303,7 @@ pub(crate) fn bench_fr_from_repr(c: &mut Criterion) {

let mut rng = XorShiftRng::seed_from_u64(1231275789u64);

let v: Vec<FrRepr> = (0..SAMPLES).map(|_| Fr::rand(&mut rng).into_repr()).collect();
let v: Vec<FrRepr> = (0..SAMPLES).map(|_| Fr::rand(&mut rng).to_repr()).collect();

let mut count = 0;
c.bench_function("bls12_377: fr_from_repr", |c| {
Expand Down
4 changes: 2 additions & 2 deletions curves/benches/bw6_761/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub fn bench_fq_into_repr(c: &mut Criterion) {
c.bench_function("bw6_761: fq_into_repr", |c| {
c.iter(|| {
count = (count + 1) % SAMPLES;
v[count].into_repr()
v[count].to_repr()
})
});
}
Expand All @@ -303,7 +303,7 @@ pub fn bench_fq_from_repr(c: &mut Criterion) {

let mut rng = XorShiftRng::seed_from_u64(1231275789u64);

let v: Vec<FqRepr> = (0..SAMPLES).map(|_| Fq::rand(&mut rng).into_repr()).collect();
let v: Vec<FqRepr> = (0..SAMPLES).map(|_| Fq::rand(&mut rng).to_repr()).collect();

let mut count = 0;
c.bench_function("bw6_761: fq_from_repr", |c| {
Expand Down
4 changes: 2 additions & 2 deletions curves/benches/bw6_761/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub fn bench_fr_into_repr(c: &mut Criterion) {
c.bench_function("bw6_761: fr_into_repr", |c| {
c.iter(|| {
count = (count + 1) % SAMPLES;
v[count].into_repr()
v[count].to_repr()
})
});
}
Expand All @@ -303,7 +303,7 @@ pub fn bench_fr_from_repr(c: &mut Criterion) {

let mut rng = XorShiftRng::seed_from_u64(1231275789u64);

let v: Vec<FrRepr> = (0..SAMPLES).map(|_| Fr::rand(&mut rng).into_repr()).collect();
let v: Vec<FrRepr> = (0..SAMPLES).map(|_| Fr::rand(&mut rng).to_repr()).collect();

let mut count = 0;
c.bench_function("bw6_761: fr_from_repr", |c| {
Expand Down
2 changes: 1 addition & 1 deletion curves/src/bls12_377/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ fn test_bilinearity() {

let ans1 = Bls12_377::pairing(sa, b);
let ans2 = Bls12_377::pairing(a, sb);
let ans3 = Bls12_377::pairing(a, b).pow(s.into_repr());
let ans3 = Bls12_377::pairing(a, b).pow(s.to_repr());

assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
Expand Down
2 changes: 1 addition & 1 deletion curves/src/bw6_761/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn test_bilinearity() {

let ans1 = BW6_761::pairing(sa, b);
let ans2 = BW6_761::pairing(a, sb);
let ans3 = BW6_761::pairing(a, b).pow(s.into_repr());
let ans3 = BW6_761::pairing(a, b).pow(s.to_repr());

assert_eq!(ans1, ans2);
assert_eq!(ans2, ans3);
Expand Down
2 changes: 1 addition & 1 deletion curves/src/templates/short_weierstrass_jacobian/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl<P: Parameters> Mul<P::ScalarField> for Affine<P> {
type Output = Self;

fn mul(self, other: P::ScalarField) -> Self {
self.mul_bits(BitIteratorBE::new(other.into_repr())).into()
self.mul_bits(BitIteratorBE::new(other.to_repr())).into()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ impl<P: Parameters> Mul<P::ScalarField> for Projective<P> {

let mut found_one = false;

for i in BitIteratorBE::new(other.into_repr()) {
for i in BitIteratorBE::new(other.to_repr()) {
if found_one {
res.double_in_place();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ impl<P: Parameters> Mul<P::ScalarField> for Affine<P> {
type Output = Self;

fn mul(self, other: P::ScalarField) -> Self {
self.mul_bits(BitIteratorBE::new(other.into_repr())).into()
self.mul_bits(BitIteratorBE::new(other.to_repr())).into()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl<P: Parameters> Mul<P::ScalarField> for Projective<P> {

let mut found_one = false;

for i in BitIteratorBE::new(other.into_repr()) {
for i in BitIteratorBE::new(other.to_repr()) {
if found_one {
res.double_in_place();
} else {
Expand Down
2 changes: 1 addition & 1 deletion curves/src/templates/twisted_edwards_extended/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl<P: Parameters> Mul<P::ScalarField> for Affine<P> {
type Output = Self;

fn mul(self, other: P::ScalarField) -> Self {
self.mul_bits(BitIteratorBE::new(other.into_repr())).into()
self.mul_bits(BitIteratorBE::new(other.to_repr())).into()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl<P: Parameters> Mul<P::ScalarField> for Projective<P> {

let mut found_one = false;

for i in BitIteratorBE::new(other.into_repr()) {
for i in BitIteratorBE::new(other.to_repr()) {
if found_one {
res.double_in_place();
} else {
Expand Down
4 changes: 2 additions & 2 deletions dpc/src/testnet1/record/encoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ impl<C: Testnet1Components, P: MontgomeryParameters + TwistedEdwardsParameters,

// Process birth_program_id and death_program_id. (Assumption 2 and 3 applies)

let birth_program_id_biginteger = Self::OuterField::read_le(birth_program_id)?.into_repr();
let death_program_id_biginteger = Self::OuterField::read_le(death_program_id)?.into_repr();
let birth_program_id_biginteger = Self::OuterField::read_le(birth_program_id)?.to_repr();
let death_program_id_biginteger = Self::OuterField::read_le(death_program_id)?.to_repr();

let mut birth_program_id_bits = Vec::with_capacity(Self::INNER_FIELD_BITSIZE);
let mut death_program_id_bits = Vec::with_capacity(Self::INNER_FIELD_BITSIZE);
Expand Down
4 changes: 2 additions & 2 deletions dpc/src/testnet2/record/encoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ impl<C: Testnet2Components, P: MontgomeryParameters + TwistedEdwardsParameters,

// Process birth_program_id and death_program_id. (Assumption 2 and 3 applies)

let birth_program_id_biginteger = Self::OuterField::read_le(birth_program_id)?.into_repr();
let death_program_id_biginteger = Self::OuterField::read_le(death_program_id)?.into_repr();
let birth_program_id_biginteger = Self::OuterField::read_le(birth_program_id)?.to_repr();
let death_program_id_biginteger = Self::OuterField::read_le(death_program_id)?.to_repr();

let mut birth_program_id_bits = Vec::with_capacity(Self::INNER_FIELD_BITSIZE);
let mut death_program_id_bits = Vec::with_capacity(Self::INNER_FIELD_BITSIZE);
Expand Down

0 comments on commit 858a77f

Please sign in to comment.