arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/r1cs-std/src/groups/curves/twisted_edwards/mod.rs
r1cs-std/src/groups/curves/twisted_edwards/mod.rsRaw
   1use algebra::{
   2    curves::{
   3        twisted_edwards_extended::{GroupAffine as TEAffine, GroupProjective as TEProjective},
   4        AffineCurve, MontgomeryModelParameters, ProjectiveCurve, TEModelParameters,
   5    },
   6    BigInteger, BitIteratorBE, Field, One, PrimeField, Zero,
   7};
   8
   9use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};
  10
  11use crate::{prelude::*, ToConstraintFieldGadget, Vec};
  12
  13use crate::fields::fp::FpVar;
  14use core::{borrow::Borrow, marker::PhantomData};
  15
  16/// An implementation of arithmetic for Montgomery curves that relies on
  17/// incomplete addition formulae for the affine model, as outlined in the
  18/// [EFD](https://www.hyperelliptic.org/EFD/g1p/auto-montgom.html).
  19///
  20/// This is intended for use primarily for implementing efficient
  21/// multi-scalar-multiplication in the Bowe-Hopwood-Pedersen hash.
  22#[derive(Derivative)]
  23#[derivative(Debug, Clone)]
  24#[must_use]
  25pub struct MontgomeryAffineVar<
  26    P: TEModelParameters,
  27    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
  28> where
  29    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
  30{
  31    /// The x-coordinate.
  32    pub x: F,
  33    /// The y-coordinate.
  34    pub y: F,
  35    #[derivative(Debug = "ignore")]
  36    _params: PhantomData<P>,
  37}
  38
  39mod montgomery_affine_impl {
  40    use super::*;
  41    use algebra::{twisted_edwards_extended::GroupAffine, Field};
  42    use core::ops::Add;
  43
  44    impl<P, F> R1CSVar<<P::BaseField as Field>::BasePrimeField> for MontgomeryAffineVar<P, F>
  45    where
  46        P: TEModelParameters,
  47        F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
  48        for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
  49    {
  50        type Value = (P::BaseField, P::BaseField);
  51
  52        fn cs(&self) -> ConstraintSystemRef<<P::BaseField as Field>::BasePrimeField> {
  53            self.x.cs().or(self.y.cs())
  54        }
  55
  56        fn value(&self) -> Result<Self::Value, SynthesisError> {
  57            let x = self.x.value()?;
  58            let y = self.y.value()?;
  59            Ok((x, y))
  60        }
  61    }
  62
  63    impl<
  64            P: TEModelParameters,
  65            F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
  66        > MontgomeryAffineVar<P, F>
  67    where
  68        for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
  69    {
  70        /// Constructs `Self` from an `(x, y)` coordinate pair.
  71        pub fn new(x: F, y: F) -> Self {
  72            Self {
  73                x,
  74                y,
  75                _params: PhantomData,
  76            }
  77        }
  78
  79        /// Converts a Twisted Edwards curve point to coordinates for the corresponding affine
  80        /// Montgomery curve point.
  81        #[tracing::instrument(target = "r1cs")]
  82        pub fn from_edwards_to_coords(
  83            p: &TEAffine<P>,
  84        ) -> Result<(P::BaseField, P::BaseField), SynthesisError> {
  85            let montgomery_point: GroupAffine<P> = if p.y == P::BaseField::one() {
  86                GroupAffine::zero()
  87            } else if p.x == P::BaseField::zero() {
  88                GroupAffine::new(P::BaseField::zero(), P::BaseField::zero())
  89            } else {
  90                let u =
  91                    (P::BaseField::one() + &p.y) * &(P::BaseField::one() - &p.y).inverse().unwrap();
  92                let v = u * &p.x.inverse().unwrap();
  93                GroupAffine::new(u, v)
  94            };
  95
  96            Ok((montgomery_point.x, montgomery_point.y))
  97        }
  98
  99        /// Converts a Twisted Edwards curve point to coordinates for the corresponding affine
 100        /// Montgomery curve point.
 101        #[tracing::instrument(target = "r1cs")]
 102        pub fn new_witness_from_edwards(
 103            cs: ConstraintSystemRef<<P::BaseField as Field>::BasePrimeField>,
 104            p: &TEAffine<P>,
 105        ) -> Result<Self, SynthesisError> {
 106            let montgomery_coords = Self::from_edwards_to_coords(p)?;
 107            let u = F::new_witness(r1cs_core::ns!(cs, "u"), || Ok(montgomery_coords.0))?;
 108            let v = F::new_witness(r1cs_core::ns!(cs, "v"), || Ok(montgomery_coords.1))?;
 109            Ok(Self::new(u, v))
 110        }
 111
 112        /// Converts `self` into a Twisted Edwards curve point variable.
 113        #[tracing::instrument(target = "r1cs")]
 114        pub fn into_edwards(&self) -> Result<AffineVar<P, F>, SynthesisError> {
 115            let cs = self.cs();
 116            // Compute u = x / y
 117            let u = F::new_witness(r1cs_core::ns!(cs, "u"), || {
 118                let y_inv = self
 119                    .y
 120                    .value()?
 121                    .inverse()
 122                    .ok_or(SynthesisError::DivisionByZero)?;
 123                Ok(self.x.value()? * &y_inv)
 124            })?;
 125
 126            u.mul_equals(&self.y, &self.x)?;
 127
 128            let v = F::new_witness(r1cs_core::ns!(cs, "v"), || {
 129                let mut t0 = self.x.value()?;
 130                let mut t1 = t0;
 131                t0 -= &P::BaseField::one();
 132                t1 += &P::BaseField::one();
 133
 134                Ok(t0 * &t1.inverse().ok_or(SynthesisError::DivisionByZero)?)
 135            })?;
 136
 137            let xplusone = &self.x + P::BaseField::one();
 138            let xminusone = &self.x - P::BaseField::one();
 139            v.mul_equals(&xplusone, &xminusone)?;
 140
 141            Ok(AffineVar::new(u, v))
 142        }
 143    }
 144
 145    impl<'a, P, F> Add<&'a MontgomeryAffineVar<P, F>> for MontgomeryAffineVar<P, F>
 146    where
 147        P: TEModelParameters,
 148        F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
 149        for<'b> &'b F: FieldOpsBounds<'b, P::BaseField, F>,
 150    {
 151        type Output = MontgomeryAffineVar<P, F>;
 152
 153        #[tracing::instrument(target = "r1cs")]
 154        fn add(self, other: &'a Self) -> Self::Output {
 155            let cs = [&self, other].cs();
 156            let mode = if cs.is_none() {
 157                AllocationMode::Constant
 158            } else {
 159                AllocationMode::Witness
 160            };
 161
 162            let coeff_b = P::MontgomeryModelParameters::COEFF_B;
 163            let coeff_a = P::MontgomeryModelParameters::COEFF_A;
 164
 165            let lambda = F::new_variable(
 166                r1cs_core::ns!(cs, "lambda"),
 167                || {
 168                    let n = other.y.value()? - &self.y.value()?;
 169                    let d = other.x.value()? - &self.x.value()?;
 170                    Ok(n * &d.inverse().ok_or(SynthesisError::DivisionByZero)?)
 171                },
 172                mode,
 173            )
 174            .unwrap();
 175            let lambda_n = &other.y - &self.y;
 176            let lambda_d = &other.x - &self.x;
 177            lambda_d.mul_equals(&lambda, &lambda_n).unwrap();
 178
 179            // Compute x'' = B*lambda^2 - A - x - x'
 180            let xprime = F::new_variable(
 181                r1cs_core::ns!(cs, "xprime"),
 182                || {
 183                    Ok(lambda.value()?.square() * &coeff_b
 184                        - &coeff_a
 185                        - &self.x.value()?
 186                        - &other.x.value()?)
 187                },
 188                mode,
 189            )
 190            .unwrap();
 191
 192            let xprime_lc = &self.x + &other.x + &xprime + coeff_a;
 193            // (lambda) * (lambda) = (A + x + x' + x'')
 194            let lambda_b = &lambda * coeff_b;
 195            lambda_b.mul_equals(&lambda, &xprime_lc).unwrap();
 196
 197            let yprime = F::new_variable(
 198                r1cs_core::ns!(cs, "yprime"),
 199                || {
 200                    Ok(-(self.y.value()?
 201                        + &(lambda.value()? * &(xprime.value()? - &self.x.value()?))))
 202                },
 203                mode,
 204            )
 205            .unwrap();
 206
 207            let xres = &self.x - &xprime;
 208            let yres = &self.y + &yprime;
 209            lambda.mul_equals(&xres, &yres).unwrap();
 210            MontgomeryAffineVar::new(xprime, yprime)
 211        }
 212    }
 213}
 214
 215/// An implementation of arithmetic for Twisted Edwards curves that relies on
 216/// the complete formulae for the affine model, as outlined in the
 217/// [EFD](https://www.hyperelliptic.org/EFD/g1p/auto-twisted.html).
 218#[derive(Derivative)]
 219#[derivative(Debug, Clone)]
 220#[must_use]
 221pub struct AffineVar<
 222    P: TEModelParameters,
 223    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
 224> where
 225    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
 226{
 227    /// The x-coordinate.
 228    pub x: F,
 229    /// The y-coordinate.
 230    pub y: F,
 231    #[derivative(Debug = "ignore")]
 232    _params: PhantomData<P>,
 233}
 234
 235impl<P: TEModelParameters, F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>>
 236    AffineVar<P, F>
 237where
 238    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
 239{
 240    /// Constructs `Self` from an `(x, y)` coordinate triple.
 241    pub fn new(x: F, y: F) -> Self {
 242        Self {
 243            x,
 244            y,
 245            _params: PhantomData,
 246        }
 247    }
 248
 249    /// Allocates a new variable without performing an on-curve check, which is
 250    /// useful if the variable is known to be on the curve (eg., if the point
 251    /// is a constant or is a public input).
 252    #[tracing::instrument(target = "r1cs", skip(cs, f))]
 253    pub fn new_variable_omit_on_curve_check<T: Into<TEAffine<P>>>(
 254        cs: impl Into<Namespace<<P::BaseField as Field>::BasePrimeField>>,
 255        f: impl FnOnce() -> Result<T, SynthesisError>,
 256        mode: AllocationMode,
 257    ) -> Result<Self, SynthesisError> {
 258        let ns = cs.into();
 259        let cs = ns.cs();
 260
 261        let (x, y) = match f() {
 262            Ok(ge) => {
 263                let ge: TEAffine<P> = ge.into();
 264                (Ok(ge.x), Ok(ge.y))
 265            }
 266            _ => (
 267                Err(SynthesisError::AssignmentMissing),
 268                Err(SynthesisError::AssignmentMissing),
 269            ),
 270        };
 271
 272        let x = F::new_variable(r1cs_core::ns!(cs, "x"), || x, mode)?;
 273        let y = F::new_variable(r1cs_core::ns!(cs, "y"), || y, mode)?;
 274
 275        Ok(Self::new(x, y))
 276    }
 277}
 278
 279impl<P: TEModelParameters, F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>>
 280    AffineVar<P, F>
 281where
 282    P: TEModelParameters,
 283    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>
 284        + TwoBitLookupGadget<<P::BaseField as Field>::BasePrimeField, TableConstant = P::BaseField>
 285        + ThreeBitCondNegLookupGadget<
 286            <P::BaseField as Field>::BasePrimeField,
 287            TableConstant = P::BaseField,
 288        >,
 289    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
 290{
 291    /// Compute a scalar multiplication of `bases` with respect to `scalars`,
 292    /// where the elements of `scalars` are length-three slices of bits, and which
 293    /// such that the first two bits are use to select one of the bases,
 294    /// while the third bit is used to conditionally negate the selection.
 295    #[tracing::instrument(target = "r1cs", skip(bases, scalars))]
 296    pub fn precomputed_base_3_bit_signed_digit_scalar_mul<J>(
 297        bases: &[impl Borrow<[TEProjective<P>]>],
 298        scalars: &[impl Borrow<[J]>],
 299    ) -> Result<Self, SynthesisError>
 300    where
 301        J: Borrow<[Boolean<<P::BaseField as Field>::BasePrimeField>]>,
 302    {
 303        const CHUNK_SIZE: usize = 3;
 304        let mut ed_result: Option<AffineVar<P, F>> = None;
 305        let mut result: Option<MontgomeryAffineVar<P, F>> = None;
 306
 307        let mut process_segment_result = |result: &MontgomeryAffineVar<P, F>| {
 308            let sgmt_result = result.into_edwards()?;
 309            ed_result = match ed_result.as_ref() {
 310                None => Some(sgmt_result),
 311                Some(r) => Some(sgmt_result + r),
 312            };
 313            Ok::<(), SynthesisError>(())
 314        };
 315
 316        // Compute ∏(h_i^{m_i}) for all i.
 317        for (segment_bits_chunks, segment_powers) in scalars.iter().zip(bases) {
 318            for (bits, base_power) in segment_bits_chunks
 319                .borrow()
 320                .iter()
 321                .zip(segment_powers.borrow())
 322            {
 323                let base_power = base_power.borrow();
 324                let mut acc_power = *base_power;
 325                let mut coords = vec![];
 326                for _ in 0..4 {
 327                    coords.push(acc_power);
 328                    acc_power += base_power;
 329                }
 330
 331                let bits = bits.borrow().to_bits_le()?;
 332                if bits.len() != CHUNK_SIZE {
 333                    return Err(SynthesisError::Unsatisfiable);
 334                }
 335
 336                let coords = coords
 337                    .iter()
 338                    .map(|p| MontgomeryAffineVar::from_edwards_to_coords(&p.into_affine()))
 339                    .collect::<Result<Vec<_>, _>>()?;
 340
 341                let x_coeffs = coords.iter().map(|p| p.0).collect::<Vec<_>>();
 342                let y_coeffs = coords.iter().map(|p| p.1).collect::<Vec<_>>();
 343
 344                let precomp = bits[0].and(&bits[1])?;
 345
 346                let x = F::zero()
 347                    + x_coeffs[0]
 348                    + F::from(bits[0].clone()) * (x_coeffs[1] - &x_coeffs[0])
 349                    + F::from(bits[1].clone()) * (x_coeffs[2] - &x_coeffs[0])
 350                    + F::from(precomp.clone())
 351                        * (x_coeffs[3] - &x_coeffs[2] - &x_coeffs[1] + &x_coeffs[0]);
 352
 353                let y = F::three_bit_cond_neg_lookup(&bits, &precomp, &y_coeffs)?;
 354
 355                let tmp = MontgomeryAffineVar::new(x, y);
 356                result = match result.as_ref() {
 357                    None => Some(tmp),
 358                    Some(r) => Some(tmp + r),
 359                };
 360            }
 361
 362            process_segment_result(&result.unwrap())?;
 363            result = None;
 364        }
 365        if result.is_some() {
 366            process_segment_result(&result.unwrap())?;
 367        }
 368        Ok(ed_result.unwrap())
 369    }
 370}
 371
 372impl<P, F> R1CSVar<<P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
 373where
 374    P: TEModelParameters,
 375    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
 376    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
 377{
 378    type Value = TEProjective<P>;
 379
 380    fn cs(&self) -> ConstraintSystemRef<<P::BaseField as Field>::BasePrimeField> {
 381        self.x.cs().or(self.y.cs())
 382    }
 383
 384    #[inline]
 385    fn value(&self) -> Result<TEProjective<P>, SynthesisError> {
 386        let (x, y) = (self.x.value()?, self.y.value()?);
 387        let result = TEAffine::new(x, y);
 388        Ok(result.into())
 389    }
 390}
 391
 392impl<P, F> CurveVar<TEProjective<P>, <P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
 393where
 394    P: TEModelParameters,
 395    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>
 396        + TwoBitLookupGadget<<P::BaseField as Field>::BasePrimeField, TableConstant = P::BaseField>,
 397    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
 398{
 399    fn constant(g: TEProjective<P>) -> Self {
 400        let cs = ConstraintSystemRef::None;
 401        Self::new_variable_omit_on_curve_check(cs, || Ok(g), AllocationMode::Constant).unwrap()
 402    }
 403
 404    fn zero() -> Self {
 405        Self::new(F::zero(), F::one())
 406    }
 407
 408    fn is_zero(&self) -> Result<Boolean<<P::BaseField as Field>::BasePrimeField>, SynthesisError> {
 409        self.x.is_zero()?.and(&self.x.is_one()?)
 410    }
 411
 412    #[tracing::instrument(target = "r1cs", skip(cs, f))]
 413    fn new_variable_omit_prime_order_check(
 414        cs: impl Into<Namespace<<P::BaseField as Field>::BasePrimeField>>,
 415        f: impl FnOnce() -> Result<TEProjective<P>, SynthesisError>,
 416        mode: AllocationMode,
 417    ) -> Result<Self, SynthesisError> {
 418        let ns = cs.into();
 419        let cs = ns.cs();
 420
 421        let g = Self::new_variable_omit_on_curve_check(cs, f, mode)?;
 422
 423        if mode != AllocationMode::Constant {
 424            let d = P::COEFF_D;
 425            let a = P::COEFF_A;
 426            // Check that ax^2 + y^2 = 1 + dx^2y^2
 427            // We do this by checking that ax^2 - 1 = y^2 * (dx^2 - 1)
 428            let x2 = g.x.square()?;
 429            let y2 = g.y.square()?;
 430
 431            let one = P::BaseField::one();
 432            let d_x2_minus_one = &x2 * d - one;
 433            let a_x2_minus_one = &x2 * a - one;
 434
 435            d_x2_minus_one.mul_equals(&y2, &a_x2_minus_one)?;
 436        }
 437        Ok(g)
 438    }
 439
 440    /// Enforce that `self` is in the prime-order subgroup.
 441    ///
 442    /// Does so by multiplying by the prime order, and checking that the result
 443    /// is unchanged.
 444    #[tracing::instrument(target = "r1cs")]
 445    fn enforce_prime_order(&self) -> Result<(), SynthesisError> {
 446        let r_minus_1 = (-P::ScalarField::one()).into_repr();
 447
 448        let mut result = Self::zero();
 449        for b in BitIteratorBE::without_leading_zeros(r_minus_1) {
 450            result.double_in_place()?;
 451
 452            if b {
 453                result += self;
 454            }
 455        }
 456        self.negate()?.enforce_equal(&result)?;
 457        Ok(())
 458    }
 459
 460    #[inline]
 461    #[tracing::instrument(target = "r1cs")]
 462    fn double_in_place(&mut self) -> Result<(), SynthesisError> {
 463        if self.is_constant() {
 464            let value = self.value()?;
 465            *self = Self::constant(value.double());
 466        } else {
 467            let cs = self.cs();
 468            let a = P::COEFF_A;
 469
 470            // xy
 471            let xy = &self.x * &self.y;
 472            let x2 = self.x.square()?;
 473            let y2 = self.y.square()?;
 474
 475            let a_x2 = &x2 * a;
 476
 477            // Compute x3 = (2xy) / (ax^2 + y^2)
 478            let x3 = F::new_witness(r1cs_core::ns!(cs, "x3"), || {
 479                let t0 = xy.value()?.double();
 480                let t1 = a * &x2.value()? + &y2.value()?;
 481                Ok(t0 * &t1.inverse().ok_or(SynthesisError::DivisionByZero)?)
 482            })?;
 483
 484            let a_x2_plus_y2 = &a_x2 + &y2;
 485            let two_xy = xy.double()?;
 486            x3.mul_equals(&a_x2_plus_y2, &two_xy)?;
 487
 488            // Compute y3 = (y^2 - ax^2) / (2 - ax^2 - y^2)
 489            let two = P::BaseField::one().double();
 490            let y3 = F::new_witness(r1cs_core::ns!(cs, "y3"), || {
 491                let a_x2 = a * &x2.value()?;
 492                let t0 = y2.value()? - &a_x2;
 493                let t1 = two - &a_x2 - &y2.value()?;
 494                Ok(t0 * &t1.inverse().ok_or(SynthesisError::DivisionByZero)?)
 495            })?;
 496            let y2_minus_a_x2 = &y2 - &a_x2;
 497            let two_minus_ax2_minus_y2 = (&a_x2 + &y2).negate()? + two;
 498
 499            y3.mul_equals(&two_minus_ax2_minus_y2, &y2_minus_a_x2)?;
 500            self.x = x3;
 501            self.y = y3;
 502        }
 503        Ok(())
 504    }
 505
 506    #[tracing::instrument(target = "r1cs")]
 507    fn negate(&self) -> Result<Self, SynthesisError> {
 508        Ok(Self::new(self.x.negate()?, self.y.clone()))
 509    }
 510
 511    #[tracing::instrument(target = "r1cs", skip(scalar_bits_with_base_powers))]
 512    fn precomputed_base_scalar_mul_le<'a, I, B>(
 513        &mut self,
 514        scalar_bits_with_base_powers: I,
 515    ) -> Result<(), SynthesisError>
 516    where
 517        I: Iterator<Item = (B, &'a TEProjective<P>)>,
 518        B: Borrow<Boolean<<P::BaseField as Field>::BasePrimeField>>,
 519    {
 520        let scalar_bits_with_base_powers = scalar_bits_with_base_powers
 521            .map(|(bit, base)| (bit.borrow().clone(), (*base).into()))
 522            .collect::<Vec<(_, TEProjective<P>)>>();
 523        let zero = TEProjective::zero();
 524        for bits_base_powers in scalar_bits_with_base_powers.chunks(2) {
 525            if bits_base_powers.len() == 2 {
 526                let bits = [bits_base_powers[0].0.clone(), bits_base_powers[1].0.clone()];
 527                let base_powers = [&bits_base_powers[0].1, &bits_base_powers[1].1];
 528
 529                let mut table = [
 530                    zero,
 531                    *base_powers[0],
 532                    *base_powers[1],
 533                    *base_powers[0] + base_powers[1],
 534                ];
 535
 536                TEProjective::batch_normalization(&mut table);
 537                let x_s = [table[0].x, table[1].x, table[2].x, table[3].x];
 538                let y_s = [table[0].y, table[1].y, table[2].y, table[3].y];
 539
 540                let x = F::two_bit_lookup(&bits, &x_s)?;
 541                let y = F::two_bit_lookup(&bits, &y_s)?;
 542                *self += Self::new(x, y);
 543            } else if bits_base_powers.len() == 1 {
 544                let bit = bits_base_powers[0].0.clone();
 545                let base_power = bits_base_powers[0].1;
 546                let new_encoded = &*self + base_power;
 547                *self = bit.select(&new_encoded, &self)?;
 548            }
 549        }
 550
 551        Ok(())
 552    }
 553}
 554
 555impl<P, F> AllocVar<TEProjective<P>, <P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
 556where
 557    P: TEModelParameters,
 558    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>
 559        + TwoBitLookupGadget<<P::BaseField as Field>::BasePrimeField, TableConstant = P::BaseField>,
 560    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
 561{
 562    #[tracing::instrument(target = "r1cs", skip(cs, f))]
 563    fn new_variable<Point: Borrow<TEProjective<P>>>(
 564        cs: impl Into<Namespace<<P::BaseField as Field>::BasePrimeField>>,
 565        f: impl FnOnce() -> Result<Point, SynthesisError>,
 566        mode: AllocationMode,
 567    ) -> Result<Self, SynthesisError> {
 568        let ns = cs.into();
 569        let cs = ns.cs();
 570        let f = || Ok(*f()?.borrow());
 571        match mode {
 572            AllocationMode::Constant => Self::new_variable_omit_prime_order_check(cs, f, mode),
 573            AllocationMode::Input => Self::new_variable_omit_prime_order_check(cs, f, mode),
 574            AllocationMode::Witness => {
 575                // if cofactor.is_even():
 576                //   divide until you've removed all even factors
 577                // else:
 578                //   just directly use double and add.
 579                let mut power_of_2: u32 = 0;
 580                let mut cofactor = P::COFACTOR.to_vec();
 581                while cofactor[0] % 2 == 0 {
 582                    div2(&mut cofactor);
 583                    power_of_2 += 1;
 584                }
 585
 586                let cofactor_weight = BitIteratorBE::new(cofactor.as_slice())
 587                    .filter(|b| *b)
 588                    .count();
 589                let modulus_minus_1 = (-P::ScalarField::one()).into_repr(); // r - 1
 590                let modulus_minus_1_weight =
 591                    BitIteratorBE::new(modulus_minus_1).filter(|b| *b).count();
 592
 593                // We pick the most efficient method of performing the prime order check:
 594                // If the cofactor has lower hamming weight than the scalar field's modulus,
 595                // we first multiply by the inverse of the cofactor, and then, after allocating,
 596                // multiply by the cofactor. This ensures the resulting point has no cofactors
 597                //
 598                // Else, we multiply by the scalar field's modulus and ensure that the result
 599                // equals the identity.
 600
 601                let (mut ge, iter) = if cofactor_weight < modulus_minus_1_weight {
 602                    let ge = Self::new_variable_omit_prime_order_check(
 603                        r1cs_core::ns!(cs, "Witness without subgroup check with cofactor mul"),
 604                        || f().map(|g| g.borrow().into_affine().mul_by_cofactor_inv().into()),
 605                        mode,
 606                    )?;
 607                    (
 608                        ge,
 609                        BitIteratorBE::without_leading_zeros(cofactor.as_slice()),
 610                    )
 611                } else {
 612                    let ge = Self::new_variable_omit_prime_order_check(
 613                        r1cs_core::ns!(cs, "Witness without subgroup check with `r` check"),
 614                        || {
 615                            f().map(|g| {
 616                                let g = g.into_affine();
 617                                let mut power_of_two = P::ScalarField::one().into_repr();
 618                                power_of_two.muln(power_of_2);
 619                                let power_of_two_inv = P::ScalarField::from_repr(power_of_two)
 620                                    .and_then(|n| n.inverse())
 621                                    .unwrap();
 622                                g.mul(power_of_two_inv)
 623                            })
 624                        },
 625                        mode,
 626                    )?;
 627
 628                    (
 629                        ge,
 630                        BitIteratorBE::without_leading_zeros(modulus_minus_1.as_ref()),
 631                    )
 632                };
 633                // Remove the even part of the cofactor
 634                for _ in 0..power_of_2 {
 635                    ge.double_in_place()?;
 636                }
 637
 638                let mut result = Self::zero();
 639                for b in iter {
 640                    result.double_in_place()?;
 641                    if b {
 642                        result += &ge;
 643                    }
 644                }
 645                if cofactor_weight < modulus_minus_1_weight {
 646                    Ok(result)
 647                } else {
 648                    ge.enforce_equal(&ge)?;
 649                    Ok(ge)
 650                }
 651            }
 652        }
 653    }
 654}
 655
 656impl<P, F> AllocVar<TEAffine<P>, <P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
 657where
 658    P: TEModelParameters,
 659    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>
 660        + TwoBitLookupGadget<<P::BaseField as Field>::BasePrimeField, TableConstant = P::BaseField>,
 661    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
 662{
 663    #[tracing::instrument(target = "r1cs", skip(cs, f))]
 664    fn new_variable<Point: Borrow<TEAffine<P>>>(
 665        cs: impl Into<Namespace<<P::BaseField as Field>::BasePrimeField>>,
 666        f: impl FnOnce() -> Result<Point, SynthesisError>,
 667        mode: AllocationMode,
 668    ) -> Result<Self, SynthesisError> {
 669        Self::new_variable(cs, || f().map(|b| b.borrow().into_projective()), mode)
 670    }
 671}
 672
 673impl<P, F> ToConstraintFieldGadget<<P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
 674where
 675    P: TEModelParameters,
 676    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
 677    for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
 678    F: ToConstraintFieldGadget<<P::BaseField as Field>::BasePrimeField>,
 679{
 680    fn to_constraint_field(
 681        &self,
 682    ) -> Result<Vec<FpVar<<P::BaseField as Field>::BasePrimeField>>, SynthesisError> {
 683        let mut res = Vec::new();
 684
 685        res.extend_from_slice(&self.x.to_constraint_field()?);
 686        res.extend_from_slice(&self.y.to_constraint_field()?);
 687
 688        Ok(res)
 689    }
 690}
 691
 692#[inline]
 693fn div2(limbs: &mut [u64]) {
 694    let mut t = 0;
 695    for i in limbs.iter_mut().rev() {
 696        let t2 = *i << 63;
 697        *i >>= 1;
 698        *i |= t;
 699        t = t2;
 700    }
 701}
 702
 703impl_bounded_ops!(
 704    AffineVar<P, F>,
 705    TEProjective<P>,
 706    Add,
 707    add,
 708    AddAssign,
 709    add_assign,
 710    |this: &'a AffineVar<P, F>, other: &'a AffineVar<P, F>| {
 711
 712        if [this, other].is_constant() {
 713            assert!(this.is_constant() && other.is_constant());
 714            AffineVar::constant(this.value().unwrap() + &other.value().unwrap())
 715        } else {
 716            let cs = [this, other].cs();
 717            let a = P::COEFF_A;
 718            let d = P::COEFF_D;
 719
 720            // Compute U = (x1 + y1) * (x2 + y2)
 721            let u1 = (&this.x * -a) + &this.y;
 722            let u2 = &other.x + &other.y;
 723
 724            let u = u1 *  &u2;
 725
 726            // Compute v0 = x1 * y2
 727            let v0 = &other.y * &this.x;
 728
 729            // Compute v1 = x2 * y1
 730            let v1 = &other.x * &this.y;
 731
 732            // Compute C = d*v0*v1
 733            let v2 = &v0 * &v1 * d;
 734
 735            // Compute x3 = (v0 + v1) / (1 + v2)
 736            let x3 = F::new_witness(r1cs_core::ns!(cs, "x3"), || {
 737                let t0 = v0.value()? + &v1.value()?;
 738                let t1 = P::BaseField::one() + &v2.value()?;
 739                Ok(t0 * &t1.inverse().ok_or(SynthesisError::DivisionByZero)?)
 740            }).unwrap();
 741
 742            let v2_plus_one = &v2 + P::BaseField::one();
 743            let v0_plus_v1 = &v0 + &v1;
 744            x3.mul_equals(&v2_plus_one, &v0_plus_v1).unwrap();
 745
 746            // Compute y3 = (U + a * v0 - v1) / (1 - v2)
 747            let y3 = F::new_witness(r1cs_core::ns!(cs, "y3"), || {
 748                let t0 = u.value()? + &(a * &v0.value()?) - &v1.value()?;
 749                let t1 = P::BaseField::one() - &v2.value()?;
 750                Ok(t0 * &t1.inverse().ok_or(SynthesisError::DivisionByZero)?)
 751            }).unwrap();
 752
 753            let one_minus_v2 = (&v2 - P::BaseField::one()).negate().unwrap();
 754            let a_v0 = &v0 * a;
 755            let u_plus_a_v0_minus_v1 = &u + &a_v0 - &v1;
 756
 757            y3.mul_equals(&one_minus_v2, &u_plus_a_v0_minus_v1).unwrap();
 758
 759            AffineVar::new(x3, y3)
 760        }
 761    },
 762    |this: &'a AffineVar<P, F>, other: TEProjective<P>| this + AffineVar::constant(other),
 763    (
 764        F :FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>
 765            + TwoBitLookupGadget<<P::BaseField as Field>::BasePrimeField, TableConstant = P::BaseField>,
 766        P: TEModelParameters,
 767    ),
 768    for <'b> &'b F: FieldOpsBounds<'b, P::BaseField, F>,
 769);
 770
 771impl_bounded_ops!(
 772    AffineVar<P, F>,
 773    TEProjective<P>,
 774    Sub,
 775    sub,
 776    SubAssign,
 777    sub_assign,
 778    |this: &'a AffineVar<P, F>, other: &'a AffineVar<P, F>| this + other.negate().unwrap(),
 779    |this: &'a AffineVar<P, F>, other: TEProjective<P>| this - AffineVar::constant(other),
 780    (
 781        F :FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>
 782            + TwoBitLookupGadget<<P::BaseField as Field>::BasePrimeField, TableConstant = P::BaseField>,
 783        P: TEModelParameters,
 784    ),
 785    for <'b> &'b F: FieldOpsBounds<'b, P::BaseField, F>
 786);
 787
 788impl<'a, P, F> GroupOpsBounds<'a, TEProjective<P>, AffineVar<P, F>> for AffineVar<P, F>
 789where
 790    P: TEModelParameters,
 791    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>
 792        + TwoBitLookupGadget<<P::BaseField as Field>::BasePrimeField, TableConstant = P::BaseField>,
 793    for<'b> &'b F: FieldOpsBounds<'b, P::BaseField, F>,
 794{
 795}
 796
 797impl<'a, P, F> GroupOpsBounds<'a, TEProjective<P>, AffineVar<P, F>> for &'a AffineVar<P, F>
 798where
 799    P: TEModelParameters,
 800    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>
 801        + TwoBitLookupGadget<<P::BaseField as Field>::BasePrimeField, TableConstant = P::BaseField>,
 802    for<'b> &'b F: FieldOpsBounds<'b, P::BaseField, F>,
 803{
 804}
 805
 806impl<P, F> CondSelectGadget<<P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
 807where
 808    P: TEModelParameters,
 809    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
 810    for<'b> &'b F: FieldOpsBounds<'b, P::BaseField, F>,
 811{
 812    #[inline]
 813    #[tracing::instrument(target = "r1cs")]
 814    fn conditionally_select(
 815        cond: &Boolean<<P::BaseField as Field>::BasePrimeField>,
 816        true_value: &Self,
 817        false_value: &Self,
 818    ) -> Result<Self, SynthesisError> {
 819        let x = cond.select(&true_value.x, &false_value.x)?;
 820        let y = cond.select(&true_value.y, &false_value.y)?;
 821
 822        Ok(Self::new(x, y))
 823    }
 824}
 825
 826impl<P, F> EqGadget<<P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
 827where
 828    P: TEModelParameters,
 829    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
 830    for<'b> &'b F: FieldOpsBounds<'b, P::BaseField, F>,
 831{
 832    #[tracing::instrument(target = "r1cs")]
 833    fn is_eq(
 834        &self,
 835        other: &Self,
 836    ) -> Result<Boolean<<P::BaseField as Field>::BasePrimeField>, SynthesisError> {
 837        let x_equal = self.x.is_eq(&other.x)?;
 838        let y_equal = self.y.is_eq(&other.y)?;
 839        x_equal.and(&y_equal)
 840    }
 841
 842    #[inline]
 843    #[tracing::instrument(target = "r1cs")]
 844    fn conditional_enforce_equal(
 845        &self,
 846        other: &Self,
 847        condition: &Boolean<<P::BaseField as Field>::BasePrimeField>,
 848    ) -> Result<(), SynthesisError> {
 849        self.x.conditional_enforce_equal(&other.x, condition)?;
 850        self.y.conditional_enforce_equal(&other.y, condition)?;
 851        Ok(())
 852    }
 853
 854    #[inline]
 855    #[tracing::instrument(target = "r1cs")]
 856    fn conditional_enforce_not_equal(
 857        &self,
 858        other: &Self,
 859        condition: &Boolean<<P::BaseField as Field>::BasePrimeField>,
 860    ) -> Result<(), SynthesisError> {
 861        self.is_eq(other)?
 862            .and(condition)?
 863            .enforce_equal(&Boolean::Constant(false))
 864    }
 865}
 866
 867impl<P, F> ToBitsGadget<<P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
 868where
 869    P: TEModelParameters,
 870    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
 871    for<'b> &'b F: FieldOpsBounds<'b, P::BaseField, F>,
 872{
 873    #[tracing::instrument(target = "r1cs")]
 874    fn to_bits_le(
 875        &self,
 876    ) -> Result<Vec<Boolean<<P::BaseField as Field>::BasePrimeField>>, SynthesisError> {
 877        let mut x_bits = self.x.to_bits_le()?;
 878        let y_bits = self.y.to_bits_le()?;
 879        x_bits.extend_from_slice(&y_bits);
 880        Ok(x_bits)
 881    }
 882
 883    #[tracing::instrument(target = "r1cs")]
 884    fn to_non_unique_bits_le(
 885        &self,
 886    ) -> Result<Vec<Boolean<<P::BaseField as Field>::BasePrimeField>>, SynthesisError> {
 887        let mut x_bits = self.x.to_non_unique_bits_le()?;
 888        let y_bits = self.y.to_non_unique_bits_le()?;
 889        x_bits.extend_from_slice(&y_bits);
 890
 891        Ok(x_bits)
 892    }
 893}
 894
 895impl<P, F> ToBytesGadget<<P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
 896where
 897    P: TEModelParameters,
 898    F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
 899    for<'b> &'b F: FieldOpsBounds<'b, P::BaseField, F>,
 900{
 901    #[tracing::instrument(target = "r1cs")]
 902    fn to_bytes(
 903        &self,
 904    ) -> Result<Vec<UInt8<<P::BaseField as Field>::BasePrimeField>>, SynthesisError> {
 905        let mut x_bytes = self.x.to_bytes()?;
 906        let y_bytes = self.y.to_bytes()?;
 907        x_bytes.extend_from_slice(&y_bytes);
 908        Ok(x_bytes)
 909    }
 910
 911    #[tracing::instrument(target = "r1cs")]
 912    fn to_non_unique_bytes(
 913        &self,
 914    ) -> Result<Vec<UInt8<<P::BaseField as Field>::BasePrimeField>>, SynthesisError> {
 915        let mut x_bytes = self.x.to_non_unique_bytes()?;
 916        let y_bytes = self.y.to_non_unique_bytes()?;
 917        x_bytes.extend_from_slice(&y_bytes);
 918
 919        Ok(x_bytes)
 920    }
 921}
 922
 923#[cfg(test)]
 924#[allow(dead_code)]
 925pub(crate) fn test<P, GG>() -> Result<(), SynthesisError>
 926where
 927    P: TEModelParameters,
 928    GG: CurveVar<TEProjective<P>, <P::BaseField as Field>::BasePrimeField>,
 929    for<'a> &'a GG: GroupOpsBounds<'a, TEProjective<P>, GG>,
 930{
 931    use crate::prelude::*;
 932    use algebra::{test_rng, BitIteratorLE, Group, UniformRand};
 933    use r1cs_core::ConstraintSystem;
 934
 935    crate::groups::test::group_test::<TEProjective<P>, _, GG>()?;
 936
 937    let mut rng = test_rng();
 938
 939    let cs = ConstraintSystem::<<P::BaseField as Field>::BasePrimeField>::new_ref();
 940
 941    let a = TEProjective::<P>::rand(&mut rng);
 942    let b = TEProjective::<P>::rand(&mut rng);
 943    let a_affine = a.into_affine();
 944    let b_affine = b.into_affine();
 945
 946    println!("Allocating things");
 947    let ns = r1cs_core::ns!(cs, "allocating variables");
 948    let mut gadget_a = GG::new_witness(cs.clone(), || Ok(a))?;
 949    let gadget_b = GG::new_witness(cs.clone(), || Ok(b))?;
 950    drop(ns);
 951    println!("Done Allocating things");
 952    assert_eq!(gadget_a.value()?.into_affine().x, a_affine.x);
 953    assert_eq!(gadget_a.value()?.into_affine().y, a_affine.y);
 954    assert_eq!(gadget_b.value()?.into_affine().x, b_affine.x);
 955    assert_eq!(gadget_b.value()?.into_affine().y, b_affine.y);
 956    assert_eq!(cs.which_is_unsatisfied()?, None);
 957
 958    println!("Checking addition");
 959    // Check addition
 960    let ab = a + &b;
 961    let ab_affine = ab.into_affine();
 962    let gadget_ab = &gadget_a + &gadget_b;
 963    let gadget_ba = &gadget_b + &gadget_a;
 964    gadget_ba.enforce_equal(&gadget_ab)?;
 965
 966    let ab_val = gadget_ab.value()?.into_affine();
 967    assert_eq!(ab_val, ab_affine, "Result of addition is unequal");
 968    assert!(cs.is_satisfied().unwrap());
 969    println!("Done checking addition");
 970
 971    println!("Checking doubling");
 972    // Check doubling
 973    let aa = Group::double(&a);
 974    let aa_affine = aa.into_affine();
 975    gadget_a.double_in_place()?;
 976    let aa_val = gadget_a.value()?.into_affine();
 977    assert_eq!(
 978        aa_val, aa_affine,
 979        "Gadget and native values are unequal after double."
 980    );
 981    assert!(cs.is_satisfied().unwrap());
 982    println!("Done checking doubling");
 983
 984    println!("Checking mul_bits");
 985    // Check mul_bits
 986    let scalar = P::ScalarField::rand(&mut rng);
 987    let native_result = AffineCurve::mul(&aa.into_affine(), scalar);
 988    let native_result = native_result.into_affine();
 989
 990    let scalar: Vec<bool> = BitIteratorLE::new(scalar.into_repr()).collect();
 991    let input: Vec<Boolean<_>> =
 992        Vec::new_witness(r1cs_core::ns!(cs, "bits"), || Ok(scalar)).unwrap();
 993    let result = gadget_a.scalar_mul_le(input.iter())?;
 994    let result_val = result.value()?.into_affine();
 995    assert_eq!(
 996        result_val, native_result,
 997        "gadget & native values are diff. after scalar mul"
 998    );
 999    assert!(cs.is_satisfied().unwrap());
1000    println!("Done checking mul_bits");
1001
1002    if !cs.is_satisfied().unwrap() {
1003        println!("Not satisfied");
1004        println!("{:?}", cs.which_is_unsatisfied().unwrap());
1005    }
1006
1007    assert!(cs.is_satisfied().unwrap());
1008    Ok(())
1009}