ark_algebra_test_templates/
glv.rsuse ark_ec::{
scalar_mul::{glv::GLVConfig, sw_double_and_add_affine, sw_double_and_add_projective},
short_weierstrass::{Affine, Projective},
AffineRepr, CurveGroup, PrimeGroup,
};
use ark_ff::{BigInteger, PrimeField};
use ark_std::{ops::Mul, UniformRand};
pub fn glv_scalar_decomposition<P: GLVConfig>() {
let mut rng = ark_std::test_rng();
for _i in 0..100 {
let k = P::ScalarField::rand(&mut rng);
let ((is_k1_positive, k1), (is_k2_positive, k2)) =
<P as GLVConfig>::scalar_decomposition(k);
if is_k1_positive && is_k2_positive {
assert_eq!(k1 + k2 * P::LAMBDA, k);
}
if is_k1_positive && !is_k2_positive {
assert_eq!(k1 - k2 * P::LAMBDA, k);
}
if !is_k1_positive && is_k2_positive {
assert_eq!(-k1 + k2 * P::LAMBDA, k);
}
if !is_k1_positive && !is_k2_positive {
assert_eq!(-k1 - k2 * P::LAMBDA, k);
}
let expected_max_bits = (P::ScalarField::MODULUS_BIT_SIZE + 1) / 2;
assert!(
k1.into_bigint().num_bits() <= expected_max_bits,
"k1 has {} bits",
k1.into_bigint().num_bits()
);
assert!(
k2.into_bigint().num_bits() <= expected_max_bits,
"k2 has {} bits",
k2.into_bigint().num_bits()
);
}
}
pub fn glv_endomorphism_eigenvalue<P: GLVConfig>() {
let g = Projective::<P>::generator();
let endo_g = <P as GLVConfig>::endomorphism(&g);
assert_eq!(endo_g, g.mul(P::LAMBDA));
}
pub fn glv_projective<P: GLVConfig>() {
let mut rng = ark_std::test_rng();
let g = Projective::<P>::generator();
for _i in 0..100 {
let k = P::ScalarField::rand(&mut rng);
let k_g = <P as GLVConfig>::glv_mul_projective(g, k);
let k_g_2 = sw_double_and_add_projective(&g, &k.into_bigint());
assert_eq!(k_g, k_g_2);
}
}
pub fn glv_affine<P: GLVConfig>() {
let mut rng = ark_std::test_rng();
let g = Affine::<P>::generator();
for _i in 0..100 {
let k = P::ScalarField::rand(&mut rng);
let k_g = <P as GLVConfig>::glv_mul_affine(g, k);
let k_g_2 = sw_double_and_add_affine(&g, &k.into_bigint()).into_affine();
assert_eq!(k_g, k_g_2);
}
}