pub fn multi_scalar_mul_gadget(
    base: &EdwardsAffineVar,
    scalar_1: &[Boolean<Fq>],
    endor_base: &EdwardsAffineVar,
    scalar_2: &[Boolean<Fq>],
    scalar_2_sign_var: &Boolean<Fq>,
) -> Result<EdwardsAffineVar, SynthesisError> {
    let length = scalar_1.len();
    assert_eq!(
        length,
        scalar_2.len(),
        "the input length do not equal: {} vs {}",
        length,
        scalar_2.len()
    );

    let zero = EdwardsAffineVar::zero();

    let endor_base =
        scalar_2_sign_var.select(&endor_base.negate()?, endor_base)?;

    let sum = base.clone() + endor_base.clone();

    let mut res = EdwardsAffineVar::zero();
    for i in 0..length {
        res = res.double()?;

        let add = scalar_1[length - i - 1].select(
            // both bits are 1 => add the sum to self
            // first bit is 1 and second bit is 0 => add the base to self
            &scalar_2[length - i - 1].select(&sum, base)?,
            // first bit is 0 and second bit is 1 => add the endor_base to self
            // bot bits are 0 => ignore
            &scalar_2[length - i - 1].select(&endor_base, &zero)?,
        )?;
        res += add;
    }

    Ok(res)
}
/// The circuit for 2 base scalar multiplication.
The circuit for 2 base scalar multiplication.
The circuit for 2 base scalar multiplication .
pub fn multi_scalar_mul_gadget(
    base: &EdwardsAffineVar,
    scalar_1: &[Boolean<Fq>],
    endor_base: &EdwardsAffineVar,
    scalar_2: &[Boolean<Fq>],
    scalar_2_sign_var: &Boolean<Fq>,
) -> Result<EdwardsAffineVar, SynthesisError> {
    let length = scalar_1.len();
    assert_eq!(
        length,
        scalar_2.len(),
        "the input length do not equal: {} vs {}",
        length,
        scalar_2.len()
    );
    let zero = EdwardsAffineVar::zero();
    let endor_base =
        scalar_2_sign_var.select(&endor_base.negate()?, endor_base)?;
    let sum = base.clone() + endor_base.clone();
    let mut res = EdwardsAffineVar::zero();
    for i in 0..length {
        res = res.double()?;
        let add = scalar_1[length - i - 1].select(
            // both bits are 1 => add the sum to self
            // first bit is 1 and second bit is 0 => add the base to self
            &scalar_2[length - i - 1].select(&sum, base)?,
            // first bit is 0 and second bit is 1 => add the endor_base to self
            // bot bits are 0 => ignore
            &scalar_2[length - i - 1].select(&endor_base, &zero)?,
        )?;
        res += add;
    }
    Ok(res)
}
pub fn multi_scalar_mul_gadget ( base : & EdwardsAffineVar , scalar_1 : & [ Boolean < Fq > ] , endor_base : & EdwardsAffineVar , scalar_2 : & [ Boolean < Fq > ] , scalar_2_sign_var : & Boolean < Fq > , ) -> Result < EdwardsAffineVar , SynthesisError > { let length = scalar_1 . len ( ) ; assert_eq ! ( length scalar_2 len ( ) " " length scalar_2 len ( ) ) ; let zero = EdwardsAffineVar :: zero ( ) ; let endor_base = scalar_2_sign_var . select ( & endor_base . negate ( ) ? , endor_base ) ? ; let sum = base . clone ( ) + endor_base . clone ( ) ; let mut res = EdwardsAffineVar :: zero ( ) ; for i in 0 .. length { res = res . double ( ) ? ; let add = scalar_1 [ length - i - 1 ] . select ( // both bits are 1 => add the sum to self // first bit is 1 and second bit is 0 => add the base to self & scalar_2 [ length - i - 1 ] . select ( & sum , base ) ? , // first bit is 0 and second bit is 1 => add the endor_base to self // bot bits are 0 => ignore & scalar_2 [ length - i - 1 ] . select ( & endor_base , & zero ) ? , ) ? ; res += add ; } Ok ( res ) }
The circuit for 2 base scalar multiplication.
The circuit for 2 base scalar multiplication .
base
&EdwardsAffineVar
scalar_1
&[Boolean<Fq>]
endor_base
&EdwardsAffineVar
scalar_2
&[Boolean<Fq>]
scalar_2_sign_var
&Boolean<Fq>
base
&EdwardsAffineVar
scalar_1
&[Boolean<Fq>]
endor_base
&EdwardsAffineVar
scalar_2
&[Boolean<Fq>]
scalar_2_sign_var
&Boolean<Fq>