Skip to content

Commit

Permalink
🐛 Modify code associated PrimeField implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sheagrief committed Sep 22, 2023
1 parent 73f8406 commit 889e438
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 40 deletions.
2 changes: 1 addition & 1 deletion arkworks/algebra/ec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ pub trait AffineCurve:
+ From<<Self as AffineCurve>::Projective>
{
const COFACTOR: &'static [u64];
type ScalarField: PrimeField + SquareRootField + Into<<Self::ScalarField as PrimeField>::BigInt>;
type ScalarField: PrimeField + SquareRootField;
type BaseField: Field;
type Projective: ProjectiveCurve<Affine = Self, ScalarField = Self::ScalarField, BaseField = Self::BaseField>
+ From<Self>
Expand Down
46 changes: 26 additions & 20 deletions arkworks/poly-commit/src/ipa_pc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<G: AffineCurve, D: Digest, P: UVPolynomial<G::ScalarField>> InnerProductArg

if randomizer.is_some() {
assert!(hiding_generator.is_some());
comm += &hiding_generator.unwrap().mul(randomizer.unwrap());
comm += &hiding_generator.unwrap().scalar_mul(randomizer.unwrap());
}

comm
Expand Down Expand Up @@ -110,7 +110,10 @@ impl<G: AffineCurve, D: Digest, P: UVPolynomial<G::ScalarField>> InnerProductArg
for (labeled_commitment, value) in labeled_commitments.zip(values) {
let commitment = labeled_commitment.commitment();
combined_v += &(cur_challenge * &value);
combined_commitment_proj += &labeled_commitment.commitment().comm.mul(cur_challenge);
combined_commitment_proj += &labeled_commitment
.commitment()
.comm
.scalar_mul(cur_challenge);
cur_challenge = opening_challenges(opening_challenge_counter);
opening_challenge_counter += 1;

Expand All @@ -120,7 +123,8 @@ impl<G: AffineCurve, D: Digest, P: UVPolynomial<G::ScalarField>> InnerProductArg
if let Some(degree_bound) = degree_bound {
let shift = point.pow([(vk.supported_degree() - degree_bound) as u64]);
combined_v += &(cur_challenge * &value * &shift);
combined_commitment_proj += &commitment.shifted_comm.unwrap().mul(cur_challenge);
combined_commitment_proj +=
&commitment.shifted_comm.unwrap().scalar_mul(cur_challenge);
}

cur_challenge = opening_challenges(opening_challenge_counter);
Expand All @@ -137,7 +141,8 @@ impl<G: AffineCurve, D: Digest, P: UVPolynomial<G::ScalarField>> InnerProductArg
let hiding_challenge = Self::compute_random_oracle_challenge(
&ark_ff::to_bytes![combined_commitment, point, combined_v, hiding_comm].unwrap(),
);
combined_commitment_proj += &(hiding_comm.mul(hiding_challenge) - &vk.s.mul(rand));
combined_commitment_proj +=
&(hiding_comm.scalar_mul(hiding_challenge) - &vk.s.scalar_mul(rand));
combined_commitment = combined_commitment_proj.into_affine();
}

Expand All @@ -147,7 +152,7 @@ impl<G: AffineCurve, D: Digest, P: UVPolynomial<G::ScalarField>> InnerProductArg
&ark_ff::to_bytes![combined_commitment, point, combined_v].unwrap(),
);

let h_prime = vk.h.mul(round_challenge);
let h_prime = vk.h.scalar_mul(round_challenge);

let mut round_commitment_proj =
combined_commitment_proj + &h_prime.mul(&combined_v.into_repr());
Expand All @@ -160,8 +165,8 @@ impl<G: AffineCurve, D: Digest, P: UVPolynomial<G::ScalarField>> InnerProductArg
&ark_ff::to_bytes![round_challenge, l, r].unwrap(),
);
round_challenges.push(round_challenge);
round_commitment_proj +=
&(l.mul(round_challenge.inverse().unwrap()) + &r.mul(round_challenge));
round_commitment_proj += &(l.scalar_mul(round_challenge.inverse().unwrap())
+ &r.scalar_mul(round_challenge));
}

let check_poly = SuccinctCheckPolynomial::<G::ScalarField>(round_challenges);
Expand Down Expand Up @@ -239,7 +244,7 @@ impl<G: AffineCurve, D: Digest, P: UVPolynomial<G::ScalarField>> InnerProductArg
coeff: G::ScalarField,
) -> Option<G::Projective> {
if let Some(new_comm) = new_comm {
let coeff_new_comm = new_comm.mul(coeff);
let coeff_new_comm = new_comm.scalar_mul(coeff);
return Some(combined_comm.map_or(coeff_new_comm, |c| c + &coeff_new_comm));
};

Expand Down Expand Up @@ -414,7 +419,7 @@ where
));

let randomness = if let Some(h) = hiding_bound {
Randomness::rand(h, degree_bound.is_some(), None, rng)
<Randomness<G> as PCRandomness>::rand(h, degree_bound.is_some(), None, rng)
} else {
Randomness::empty()
};
Expand Down Expand Up @@ -493,7 +498,7 @@ where
let commitment = labeled_commitment.commitment();

combined_polynomial += (cur_challenge, polynomial);
combined_commitment_proj += &commitment.comm.mul(cur_challenge);
combined_commitment_proj += &commitment.comm.scalar_mul(cur_challenge);

if hiding_bound.is_some() {
has_hiding = true;
Expand Down Expand Up @@ -521,7 +526,8 @@ where
if let Some(degree_bound) = degree_bound {
let shifted_polynomial = Self::shift_polynomial(ck, polynomial, degree_bound);
combined_polynomial += (cur_challenge, &shifted_polynomial);
combined_commitment_proj += &commitment.shifted_comm.unwrap().mul(cur_challenge);
combined_commitment_proj +=
&commitment.shifted_comm.unwrap().scalar_mul(cur_challenge);

if hiding_bound.is_some() {
let shifted_rand = randomness.shifted_rand;
Expand Down Expand Up @@ -583,8 +589,8 @@ where
);
combined_polynomial += (hiding_challenge, &hiding_polynomial);
combined_rand += &(hiding_challenge * &hiding_rand);
combined_commitment_proj +=
&(hiding_commitment.unwrap().mul(hiding_challenge) - &ck.s.mul(combined_rand));
combined_commitment_proj += &(hiding_commitment.unwrap().scalar_mul(hiding_challenge)
- &ck.s.scalar_mul(combined_rand));

end_timer!(hiding_time);
}
Expand All @@ -605,7 +611,7 @@ where
&ark_ff::to_bytes![combined_commitment, point, combined_v].unwrap(),
);

let h_prime = ck.h.mul(round_challenge).into_affine();
let h_prime = ck.h.scalar_mul(round_challenge).into_affine();

// Pads the coefficients with zeroes to get the number of coeff to be d+1
let mut coeffs = combined_polynomial.coeffs().to_vec();
Expand Down Expand Up @@ -646,10 +652,10 @@ where
let (key_proj_l, _) = key_proj.split_at_mut(n / 2);

let l = Self::cm_commit(key_l, coeffs_r, None, None)
+ &h_prime.mul(Self::inner_product(coeffs_r, z_l));
+ &h_prime.scalar_mul(Self::inner_product(coeffs_r, z_l));

let r = Self::cm_commit(key_r, coeffs_l, None, None)
+ &h_prime.mul(Self::inner_product(coeffs_l, z_r));
+ &h_prime.scalar_mul(Self::inner_product(coeffs_l, z_r));

let lr = G::Projective::batch_normalization_into_affine(&[l, r]);
l_vec.push(lr[0]);
Expand All @@ -670,7 +676,7 @@ where

ark_std::cfg_iter_mut!(key_proj_l)
.zip(key_r)
.for_each(|(k_l, k_r)| *k_l += &(k_r.mul(round_challenge)));
.for_each(|(k_l, k_r)| *k_l += &(k_r.scalar_mul(round_challenge)));

coeffs = coeffs_l;
z = z_l;
Expand Down Expand Up @@ -809,7 +815,7 @@ where

let check_poly = P::from_coefficients_vec(check_poly.unwrap().compute_coeffs());
combined_check_poly += (randomizer, &check_poly);
combined_final_key += &p.final_comm_key.mul(randomizer);
combined_final_key += &p.final_comm_key.scalar_mul(randomizer);

randomizer = u128::rand(rng).into();
end_timer!(lc_time);
Expand Down Expand Up @@ -901,7 +907,7 @@ where
);

let commitment = cur_comm.commitment();
combined_comm += &commitment.comm.mul(*coeff);
combined_comm += &commitment.comm.scalar_mul(*coeff);
combined_shifted_comm = Self::combine_shifted_comm(
combined_shifted_comm,
commitment.shifted_comm,
Expand Down Expand Up @@ -995,7 +1001,7 @@ where
}

let commitment = cur_comm.commitment();
combined_comm += &commitment.comm.mul(*coeff);
combined_comm += &commitment.comm.scalar_mul(*coeff);
combined_shifted_comm = Self::combine_shifted_comm(
combined_shifted_comm,
commitment.shifted_comm,
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ mod input_circuit;
// mod marlin;
mod preprocessing;
mod reveal;
// mod share;
mod share;
mod she;
// mod wire;
mod wire;

use ark_bls12_377::{Bls12_377, Fr, FrParameters};
use ark_crypto_primitives::CommitmentScheme;
Expand Down
6 changes: 1 addition & 5 deletions src/wire/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,7 @@ impl<F: PrimeField, S: FieldShare<F>> FftField for MpcField<F, S> {
}
}

impl<F: PrimeField, S: FieldShare<F>> PrimeField for MpcField<F, S>
where
Self: From<<F as PrimeField>::BigInt>,
Self: Into<<F as PrimeField>::BigInt>,
{
impl<F: PrimeField, S: FieldShare<F>> PrimeField for MpcField<F, S> {
type Params = F::Params;

type BigInt = F::BigInt;
Expand Down
53 changes: 41 additions & 12 deletions src/wire/pairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ pub struct MpcG1Affine<E: ExtendedPairingEngine, PS: PairingShare<E>> {
pub val: MpcGroup<E::GroupedG1Affine, PS::G1AffineShare>,
}

#[derive(Debug, Derivative, Clone, Copy, PartialEq, Eq)]
#[derivative(Hash(bound = "E::G1Affine: Hash"))]
#[derive(Debug, Derivative, Clone, Copy, Eq)]
#[derivative(
PartialEq(bound = "E::G1Affine: PartialEq"),
Hash(bound = "E::G1Affine: Hash")
)]
pub struct MpcG1Projective<E: ExtendedPairingEngine, PS: PairingShare<E>> {
pub val: MpcGroup<E::GroupedG1Projective, PS::G1ProjectiveShare>,
}
Expand All @@ -63,8 +66,11 @@ pub struct MpcG2Affine<E: ExtendedPairingEngine, PS: PairingShare<E>> {
pub val: MpcGroup<E::GroupedG2Affine, PS::G2AffineShare>,
}

#[derive(Debug, Derivative, Clone, Copy, PartialEq, Eq)]
#[derivative(Hash(bound = "E::G1Affine: Hash"))]
#[derive(Debug, Derivative, Clone, Copy, Eq)]
#[derivative(
PartialEq(bound = "E::G1Affine: PartialEq"),
Hash(bound = "E::G1Affine: Hash")
)]
pub struct MpcG2Projective<E: ExtendedPairingEngine, PS: PairingShare<E>> {
pub val: MpcGroup<E::GroupedG2Projective, PS::G2ProjectiveShare>,
}
Expand Down Expand Up @@ -405,7 +411,7 @@ macro_rules! impl_ext_field_wrapper {
todo!()
}
}
impl<F: SquareRootField, S: ExtFieldShare<F>> Field for $wrap<F, S> {
impl<F: Field, S: ExtFieldShare<F>> Field for $wrap<F, S> {
type BasePrimeField = MpcField<F::BasePrimeField, S::Base>;
fn extension_degree() -> u64 {
todo!()
Expand Down Expand Up @@ -552,12 +558,12 @@ macro_rules! impl_aff_proj {
todo!()
}

// fn mul<S: Into<<Self::ScalarField as PrimeField>::BigInt>>(
// self,
// other: S,
// ) -> Self::Projective {
// todo!()
// }
fn mul<S: Into<<Self::ScalarField as PrimeField>::BigInt>>(
&self,
other: S,
) -> Self::Projective {
todo!()
}

fn mul_by_cofactor_to_projective(&self) -> Self::Projective {
todo!()
Expand All @@ -568,7 +574,30 @@ macro_rules! impl_aff_proj {
}
}

impl<E: ExtendedPairingEngine, PS: PairingShare<E>> ProjectiveCurve for $w_pro<E, PS> {}
impl<E: ExtendedPairingEngine, PS: PairingShare<E>> ProjectiveCurve for $w_pro<E, PS> {
type ScalarField = MpcField<E::Fr, PS::FrShare>;

// aff?pro?
const COFACTOR: &'static [u64] = E::$aff::COFACTOR;
type BaseField = $w_base<E::$base, PS::$base_share>;
type Affine = $w_aff<E, PS>;

fn prime_subgroup_generator() -> Self {
todo!()
}
fn batch_normalization(_v: &mut [Self]) {
todo!()
}
fn is_normalized(&self) -> bool {
todo!()
}
fn double_in_place(&mut self) -> &mut Self {
todo!()
}
fn add_assign_mixed(&mut self, _other: &Self::Affine) {
todo!()
}
}
};
}

Expand Down

0 comments on commit 889e438

Please sign in to comment.