use crate::utils;
use alloc::vec::Vec;
use ark_bls12_377_ext::CurveHooks;
use ark_ec::{pairing::Pairing, CurveConfig};
use pezsp_runtime_interface::{
pass_by::{AllocateAndReturnByCodec, PassFatPointerAndRead},
runtime_interface,
};
pub mod g1 {
pub use ark_bls12_377_ext::g1::{
G1_GENERATOR_X, G1_GENERATOR_Y, TE_GENERATOR_X, TE_GENERATOR_Y,
};
pub type Config = ark_bls12_377_ext::g1::Config<super::HostHooks>;
pub type G1Affine = ark_bls12_377_ext::g1::G1Affine<super::HostHooks>;
pub type G1Projective = ark_bls12_377_ext::g1::G1Projective<super::HostHooks>;
pub type G1SWAffine = ark_bls12_377_ext::g1::G1SWAffine<super::HostHooks>;
pub type G1SWProjective = ark_bls12_377_ext::g1::G1SWProjective<super::HostHooks>;
pub type G1TEAffine = ark_bls12_377_ext::g1::G1TEAffine<super::HostHooks>;
pub type G1TEProjective = ark_bls12_377_ext::g1::G1TEProjective<super::HostHooks>;
}
pub mod g2 {
pub use ark_bls12_377_ext::g2::{
G2_GENERATOR_X, G2_GENERATOR_X_C0, G2_GENERATOR_X_C1, G2_GENERATOR_Y, G2_GENERATOR_Y_C0,
G2_GENERATOR_Y_C1,
};
pub type Config = ark_bls12_377_ext::g2::Config<super::HostHooks>;
pub type G2Affine = ark_bls12_377_ext::g2::G2Affine<super::HostHooks>;
pub type G2Projective = ark_bls12_377_ext::g2::G2Projective<super::HostHooks>;
}
pub use self::{
g1::{Config as G1Config, G1Affine, G1Projective},
g2::{Config as G2Config, G2Affine, G2Projective},
};
#[derive(Copy, Clone)]
pub struct HostHooks;
pub type Config = ark_bls12_377_ext::Config<HostHooks>;
pub type Bls12_377 = ark_bls12_377_ext::Bls12_377<HostHooks>;
impl CurveHooks for HostHooks {
fn multi_miller_loop(
g1: impl Iterator<Item = <Bls12_377 as Pairing>::G1Prepared>,
g2: impl Iterator<Item = <Bls12_377 as Pairing>::G2Prepared>,
) -> <Bls12_377 as Pairing>::TargetField {
host_calls::bls12_377_multi_miller_loop(utils::encode_iter(g1), utils::encode_iter(g2))
.and_then(|res| utils::decode(res))
.unwrap_or_default()
}
fn final_exponentiation(
target: <Bls12_377 as Pairing>::TargetField,
) -> <Bls12_377 as Pairing>::TargetField {
host_calls::bls12_377_final_exponentiation(utils::encode(target))
.and_then(|res| utils::decode(res))
.unwrap_or_default()
}
fn msm_g1(
bases: &[G1Affine],
scalars: &[<G1Config as CurveConfig>::ScalarField],
) -> G1Projective {
host_calls::bls12_377_msm_g1(utils::encode(bases), utils::encode(scalars))
.and_then(|res| utils::decode_proj_sw(res))
.unwrap_or_default()
}
fn msm_g2(
bases: &[G2Affine],
scalars: &[<G2Config as CurveConfig>::ScalarField],
) -> G2Projective {
host_calls::bls12_377_msm_g2(utils::encode(bases), utils::encode(scalars))
.and_then(|res| utils::decode_proj_sw(res))
.unwrap_or_default()
}
fn mul_projective_g1(base: &G1Projective, scalar: &[u64]) -> G1Projective {
host_calls::bls12_377_mul_projective_g1(utils::encode_proj_sw(base), utils::encode(scalar))
.and_then(|res| utils::decode_proj_sw(res))
.unwrap_or_default()
}
fn mul_projective_g2(base: &G2Projective, scalar: &[u64]) -> G2Projective {
host_calls::bls12_377_mul_projective_g2(utils::encode_proj_sw(base), utils::encode(scalar))
.and_then(|res| utils::decode_proj_sw(res))
.unwrap_or_default()
}
}
#[runtime_interface]
pub trait HostCalls {
fn bls12_377_multi_miller_loop(
a: PassFatPointerAndRead<Vec<u8>>,
b: PassFatPointerAndRead<Vec<u8>>,
) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
utils::multi_miller_loop::<ark_bls12_377::Bls12_377>(a, b)
}
fn bls12_377_final_exponentiation(
f: PassFatPointerAndRead<Vec<u8>>,
) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
utils::final_exponentiation::<ark_bls12_377::Bls12_377>(f)
}
fn bls12_377_msm_g1(
bases: PassFatPointerAndRead<Vec<u8>>,
scalars: PassFatPointerAndRead<Vec<u8>>,
) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
utils::msm_sw::<ark_bls12_377::g1::Config>(bases, scalars)
}
fn bls12_377_msm_g2(
bases: PassFatPointerAndRead<Vec<u8>>,
scalars: PassFatPointerAndRead<Vec<u8>>,
) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
utils::msm_sw::<ark_bls12_377::g2::Config>(bases, scalars)
}
fn bls12_377_mul_projective_g1(
base: PassFatPointerAndRead<Vec<u8>>,
scalar: PassFatPointerAndRead<Vec<u8>>,
) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
utils::mul_projective_sw::<ark_bls12_377::g1::Config>(base, scalar)
}
fn bls12_377_mul_projective_g2(
base: PassFatPointerAndRead<Vec<u8>>,
scalar: PassFatPointerAndRead<Vec<u8>>,
) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
utils::mul_projective_sw::<ark_bls12_377::g2::Config>(base, scalar)
}
}