use ark_ec::AffineRepr;
use ark_ff::{BigInteger, PrimeField};
use num_bigint::BigUint;
pub trait CurveTag {
const NAME: &'static str;
}
impl CurveTag for ark_bn254::Bn254 {
const NAME: &'static str = "bn128";
}
impl CurveTag for ark_bls12_381::Bls12_381 {
const NAME: &'static str = "bls12381";
}
pub trait AsFp2 {
type Base: PrimeField;
fn c0_c1(&self) -> (&Self::Base, &Self::Base);
}
impl<P> AsFp2 for ark_ff::fields::models::QuadExtField<P>
where
P: ark_ff::fields::models::quadratic_extension::QuadExtConfig,
P::BaseField: PrimeField,
{
type Base = P::BaseField;
fn c0_c1(&self) -> (&Self::Base, &Self::Base) {
(&self.c0, &self.c1)
}
}
pub fn f_to_dec<F: PrimeField>(f: &F) -> String {
let bi = f.into_bigint();
BigUint::from_bytes_be(&bi.to_bytes_be()).to_str_radix(10)
}
pub fn g1_xy<G>(p: &G) -> [String; 2]
where
G: AffineRepr,
G::BaseField: PrimeField,
{
let (x, y) = p.xy().expect("G1 point at infinity?");
[f_to_dec(&x), f_to_dec(&y)]
}
pub fn g2_xyxy<G>(p: &G) -> [[String; 2]; 2]
where
G: AffineRepr,
G::BaseField: AsFp2,
{
let (x, y) = p.xy().expect("G2 point at infinity?");
let (x0, x1) = x.c0_c1();
let (y0, y1) = y.c0_c1();
[[f_to_dec(x0), f_to_dec(x1)], [f_to_dec(y0), f_to_dec(y1)]]
}
pub fn g1_xyz<G>(p: &G) -> [String; 3]
where
G: AffineRepr,
G::BaseField: PrimeField,
{
let [x, y] = g1_xy(p);
[x, y, "1".to_string()]
}
pub fn g2_xyxy_z<G>(p: &G) -> [[String; 2]; 3]
where
G: AffineRepr,
G::BaseField: AsFp2,
{
let [x, y] = g2_xyxy(p);
[x, y, ["1".to_string(), "0".to_string()]]
}