sp_crypto_ec_utils/
bw6_761.rs1use crate::utils;
21use alloc::vec::Vec;
22use ark_bw6_761_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_bw6_761_ext::g1::{G1_GENERATOR_X, G1_GENERATOR_Y};
32 pub type Config = ark_bw6_761_ext::g1::Config<super::HostHooks>;
34 pub type G1Affine = ark_bw6_761_ext::g1::G1Affine<super::HostHooks>;
36 pub type G1Projective = ark_bw6_761_ext::g1::G1Projective<super::HostHooks>;
38}
39
40pub mod g2 {
42 pub use ark_bw6_761_ext::g2::{G2_GENERATOR_X, G2_GENERATOR_Y};
43 pub type Config = ark_bw6_761_ext::g2::Config<super::HostHooks>;
45 pub type G2Affine = ark_bw6_761_ext::g2::G2Affine<super::HostHooks>;
47 pub type G2Projective = ark_bw6_761_ext::g2::G2Projective<super::HostHooks>;
49}
50
51pub use self::{
52 g1::{Config as G1Config, G1Affine, G1Projective},
53 g2::{Config as G2Config, G2Affine, G2Projective},
54};
55
56#[derive(Copy, Clone)]
58pub struct HostHooks;
59
60pub type Config = ark_bw6_761_ext::Config<HostHooks>;
62
63pub type BW6_761 = ark_bw6_761_ext::BW6_761<HostHooks>;
67
68impl CurveHooks for HostHooks {
69 fn multi_miller_loop(
70 g1: impl Iterator<Item = <BW6_761 as Pairing>::G1Prepared>,
71 g2: impl Iterator<Item = <BW6_761 as Pairing>::G2Prepared>,
72 ) -> <BW6_761 as Pairing>::TargetField {
73 host_calls::bw6_761_multi_miller_loop(utils::encode_iter(g1), utils::encode_iter(g2))
74 .and_then(|res| utils::decode(res))
75 .unwrap_or_default()
76 }
77
78 fn final_exponentiation(
79 target: <BW6_761 as Pairing>::TargetField,
80 ) -> <BW6_761 as Pairing>::TargetField {
81 host_calls::bw6_761_final_exponentiation(utils::encode(target))
82 .and_then(|res| utils::decode(res))
83 .unwrap_or_default()
84 }
85
86 fn msm_g1(
87 bases: &[G1Affine],
88 scalars: &[<G1Config as CurveConfig>::ScalarField],
89 ) -> G1Projective {
90 host_calls::bw6_761_msm_g1(utils::encode(bases), utils::encode(scalars))
91 .and_then(|res| utils::decode_proj_sw(res))
92 .unwrap_or_default()
93 }
94
95 fn msm_g2(
96 bases: &[G2Affine],
97 scalars: &[<G2Config as CurveConfig>::ScalarField],
98 ) -> G2Projective {
99 host_calls::bw6_761_msm_g2(utils::encode(bases), utils::encode(scalars))
100 .and_then(|res| utils::decode_proj_sw(res))
101 .unwrap_or_default()
102 }
103
104 fn mul_projective_g1(base: &G1Projective, scalar: &[u64]) -> G1Projective {
105 host_calls::bw6_761_mul_projective_g1(utils::encode_proj_sw(base), utils::encode(scalar))
106 .and_then(|res| utils::decode_proj_sw(res))
107 .unwrap_or_default()
108 }
109
110 fn mul_projective_g2(base: &G2Projective, scalar: &[u64]) -> G2Projective {
111 host_calls::bw6_761_mul_projective_g2(utils::encode_proj_sw(base), utils::encode(scalar))
112 .and_then(|res| utils::decode_proj_sw(res))
113 .unwrap_or_default()
114 }
115}
116
117#[runtime_interface]
126pub trait HostCalls {
127 fn bw6_761_multi_miller_loop(
134 a: PassFatPointerAndRead<Vec<u8>>,
135 b: PassFatPointerAndRead<Vec<u8>>,
136 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
137 utils::multi_miller_loop::<ark_bw6_761::BW6_761>(a, b)
138 }
139
140 fn bw6_761_final_exponentiation(
145 f: PassFatPointerAndRead<Vec<u8>>,
146 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
147 utils::final_exponentiation::<ark_bw6_761::BW6_761>(f)
148 }
149
150 fn bw6_761_msm_g1(
157 bases: PassFatPointerAndRead<Vec<u8>>,
158 scalars: PassFatPointerAndRead<Vec<u8>>,
159 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
160 utils::msm_sw::<ark_bw6_761::g1::Config>(bases, scalars)
161 }
162
163 fn bw6_761_msm_g2(
170 bases: PassFatPointerAndRead<Vec<u8>>,
171 scalars: PassFatPointerAndRead<Vec<u8>>,
172 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
173 utils::msm_sw::<ark_bw6_761::g2::Config>(bases, scalars)
174 }
175
176 fn bw6_761_mul_projective_g1(
183 base: PassFatPointerAndRead<Vec<u8>>,
184 scalar: PassFatPointerAndRead<Vec<u8>>,
185 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
186 utils::mul_projective_sw::<ark_bw6_761::g1::Config>(base, scalar)
187 }
188
189 fn bw6_761_mul_projective_g2(
196 base: PassFatPointerAndRead<Vec<u8>>,
197 scalar: PassFatPointerAndRead<Vec<u8>>,
198 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
199 utils::mul_projective_sw::<ark_bw6_761::g2::Config>(base, scalar)
200 }
201}