sp_crypto_ec_utils/
bls12_377.rs1use crate::utils;
21use alloc::vec::Vec;
22use ark_bls12_377_ext::CurveHooks;
23use ark_ec::{pairing::Pairing, CurveConfig};
24use sp_runtime_interface::{
25 pass_by::{AllocateAndReturnByCodec, PassFatPointerAndRead},
26 runtime_interface,
27};
28
29pub mod g1 {
31 pub use ark_bls12_377_ext::g1::{
32 G1_GENERATOR_X, G1_GENERATOR_Y, TE_GENERATOR_X, TE_GENERATOR_Y,
33 };
34 pub type Config = ark_bls12_377_ext::g1::Config<super::HostHooks>;
36 pub type G1Affine = ark_bls12_377_ext::g1::G1Affine<super::HostHooks>;
38 pub type G1Projective = ark_bls12_377_ext::g1::G1Projective<super::HostHooks>;
40 pub type G1SWAffine = ark_bls12_377_ext::g1::G1SWAffine<super::HostHooks>;
42 pub type G1SWProjective = ark_bls12_377_ext::g1::G1SWProjective<super::HostHooks>;
44 pub type G1TEAffine = ark_bls12_377_ext::g1::G1TEAffine<super::HostHooks>;
46 pub type G1TEProjective = ark_bls12_377_ext::g1::G1TEProjective<super::HostHooks>;
48}
49
50pub mod g2 {
52 pub use ark_bls12_377_ext::g2::{
53 G2_GENERATOR_X, G2_GENERATOR_X_C0, G2_GENERATOR_X_C1, G2_GENERATOR_Y, G2_GENERATOR_Y_C0,
54 G2_GENERATOR_Y_C1,
55 };
56 pub type Config = ark_bls12_377_ext::g2::Config<super::HostHooks>;
58 pub type G2Affine = ark_bls12_377_ext::g2::G2Affine<super::HostHooks>;
60 pub type G2Projective = ark_bls12_377_ext::g2::G2Projective<super::HostHooks>;
62}
63
64pub use self::{
65 g1::{Config as G1Config, G1Affine, G1Projective},
66 g2::{Config as G2Config, G2Affine, G2Projective},
67};
68
69#[derive(Copy, Clone)]
71pub struct HostHooks;
72
73pub type Config = ark_bls12_377_ext::Config<HostHooks>;
75
76pub type Bls12_377 = ark_bls12_377_ext::Bls12_377<HostHooks>;
80
81impl CurveHooks for HostHooks {
82 fn multi_miller_loop(
83 g1: impl Iterator<Item = <Bls12_377 as Pairing>::G1Prepared>,
84 g2: impl Iterator<Item = <Bls12_377 as Pairing>::G2Prepared>,
85 ) -> <Bls12_377 as Pairing>::TargetField {
86 host_calls::bls12_377_multi_miller_loop(utils::encode_iter(g1), utils::encode_iter(g2))
87 .and_then(|res| utils::decode(res))
88 .unwrap_or_default()
89 }
90
91 fn final_exponentiation(
92 target: <Bls12_377 as Pairing>::TargetField,
93 ) -> <Bls12_377 as Pairing>::TargetField {
94 host_calls::bls12_377_final_exponentiation(utils::encode(target))
95 .and_then(|res| utils::decode(res))
96 .unwrap_or_default()
97 }
98
99 fn msm_g1(
100 bases: &[G1Affine],
101 scalars: &[<G1Config as CurveConfig>::ScalarField],
102 ) -> G1Projective {
103 host_calls::bls12_377_msm_g1(utils::encode(bases), utils::encode(scalars))
104 .and_then(|res| utils::decode_proj_sw(res))
105 .unwrap_or_default()
106 }
107
108 fn msm_g2(
109 bases: &[G2Affine],
110 scalars: &[<G2Config as CurveConfig>::ScalarField],
111 ) -> G2Projective {
112 host_calls::bls12_377_msm_g2(utils::encode(bases), utils::encode(scalars))
113 .and_then(|res| utils::decode_proj_sw(res))
114 .unwrap_or_default()
115 }
116
117 fn mul_projective_g1(base: &G1Projective, scalar: &[u64]) -> G1Projective {
118 host_calls::bls12_377_mul_projective_g1(utils::encode_proj_sw(base), utils::encode(scalar))
119 .and_then(|res| utils::decode_proj_sw(res))
120 .unwrap_or_default()
121 }
122
123 fn mul_projective_g2(base: &G2Projective, scalar: &[u64]) -> G2Projective {
124 host_calls::bls12_377_mul_projective_g2(utils::encode_proj_sw(base), utils::encode(scalar))
125 .and_then(|res| utils::decode_proj_sw(res))
126 .unwrap_or_default()
127 }
128}
129
130#[runtime_interface]
139pub trait HostCalls {
140 fn bls12_377_multi_miller_loop(
147 a: PassFatPointerAndRead<Vec<u8>>,
148 b: PassFatPointerAndRead<Vec<u8>>,
149 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
150 utils::multi_miller_loop::<ark_bls12_377::Bls12_377>(a, b)
151 }
152
153 fn bls12_377_final_exponentiation(
158 f: PassFatPointerAndRead<Vec<u8>>,
159 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
160 utils::final_exponentiation::<ark_bls12_377::Bls12_377>(f)
161 }
162
163 fn bls12_377_msm_g1(
170 bases: PassFatPointerAndRead<Vec<u8>>,
171 scalars: PassFatPointerAndRead<Vec<u8>>,
172 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
173 utils::msm_sw::<ark_bls12_377::g1::Config>(bases, scalars)
174 }
175
176 fn bls12_377_msm_g2(
183 bases: PassFatPointerAndRead<Vec<u8>>,
184 scalars: PassFatPointerAndRead<Vec<u8>>,
185 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
186 utils::msm_sw::<ark_bls12_377::g2::Config>(bases, scalars)
187 }
188
189 fn bls12_377_mul_projective_g1(
196 base: PassFatPointerAndRead<Vec<u8>>,
197 scalar: PassFatPointerAndRead<Vec<u8>>,
198 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
199 utils::mul_projective_sw::<ark_bls12_377::g1::Config>(base, scalar)
200 }
201
202 fn bls12_377_mul_projective_g2(
209 base: PassFatPointerAndRead<Vec<u8>>,
210 scalar: PassFatPointerAndRead<Vec<u8>>,
211 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
212 utils::mul_projective_sw::<ark_bls12_377::g2::Config>(base, scalar)
213 }
214}