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 bw6_761_multi_miller_loop(
70 g1: impl Iterator<Item = <BW6_761 as Pairing>::G1Prepared>,
71 g2: impl Iterator<Item = <BW6_761 as Pairing>::G2Prepared>,
72 ) -> Result<<BW6_761 as Pairing>::TargetField, ()> {
73 let g1 = utils::encode(g1.collect::<Vec<_>>());
74 let g2 = utils::encode(g2.collect::<Vec<_>>());
75 let res = host_calls::bw6_761_multi_miller_loop(g1, g2).unwrap_or_default();
76 utils::decode(res)
77 }
78
79 fn bw6_761_final_exponentiation(
80 target: <BW6_761 as Pairing>::TargetField,
81 ) -> Result<<BW6_761 as Pairing>::TargetField, ()> {
82 let target = utils::encode(target);
83 let res = host_calls::bw6_761_final_exponentiation(target).unwrap_or_default();
84 utils::decode(res)
85 }
86
87 fn bw6_761_msm_g1(
88 bases: &[G1Affine],
89 scalars: &[<G1Config as CurveConfig>::ScalarField],
90 ) -> Result<G1Projective, ()> {
91 let bases = utils::encode(bases);
92 let scalars = utils::encode(scalars);
93 let res = host_calls::bw6_761_msm_g1(bases, scalars).unwrap_or_default();
94 utils::decode_proj_sw(res)
95 }
96
97 fn bw6_761_msm_g2(
98 bases: &[G2Affine],
99 scalars: &[<G2Config as CurveConfig>::ScalarField],
100 ) -> Result<G2Projective, ()> {
101 let bases = utils::encode(bases);
102 let scalars = utils::encode(scalars);
103 let res = host_calls::bw6_761_msm_g2(bases, scalars).unwrap_or_default();
104 utils::decode_proj_sw(res)
105 }
106
107 fn bw6_761_mul_projective_g1(base: &G1Projective, scalar: &[u64]) -> Result<G1Projective, ()> {
108 let base = utils::encode_proj_sw(base);
109 let scalar = utils::encode(scalar);
110 let res = host_calls::bw6_761_mul_projective_g1(base, scalar).unwrap_or_default();
111 utils::decode_proj_sw(res)
112 }
113
114 fn bw6_761_mul_projective_g2(base: &G2Projective, scalar: &[u64]) -> Result<G2Projective, ()> {
115 let base = utils::encode_proj_sw(base);
116 let scalar = utils::encode(scalar);
117 let res = host_calls::bw6_761_mul_projective_g2(base, scalar).unwrap_or_default();
118 utils::decode_proj_sw(res)
119 }
120}
121
122#[runtime_interface]
131pub trait HostCalls {
132 fn bw6_761_multi_miller_loop(
139 a: PassFatPointerAndRead<Vec<u8>>,
140 b: PassFatPointerAndRead<Vec<u8>>,
141 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
142 utils::multi_miller_loop::<ark_bw6_761::BW6_761>(a, b)
143 }
144
145 fn bw6_761_final_exponentiation(
150 f: PassFatPointerAndRead<Vec<u8>>,
151 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
152 utils::final_exponentiation::<ark_bw6_761::BW6_761>(f)
153 }
154
155 fn bw6_761_msm_g1(
162 bases: PassFatPointerAndRead<Vec<u8>>,
163 scalars: PassFatPointerAndRead<Vec<u8>>,
164 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
165 utils::msm_sw::<ark_bw6_761::g1::Config>(bases, scalars)
166 }
167
168 fn bw6_761_msm_g2(
175 bases: PassFatPointerAndRead<Vec<u8>>,
176 scalars: PassFatPointerAndRead<Vec<u8>>,
177 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
178 utils::msm_sw::<ark_bw6_761::g2::Config>(bases, scalars)
179 }
180
181 fn bw6_761_mul_projective_g1(
188 base: PassFatPointerAndRead<Vec<u8>>,
189 scalar: PassFatPointerAndRead<Vec<u8>>,
190 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
191 utils::mul_projective_sw::<ark_bw6_761::g1::Config>(base, scalar)
192 }
193
194 fn bw6_761_mul_projective_g2(
201 base: PassFatPointerAndRead<Vec<u8>>,
202 scalar: PassFatPointerAndRead<Vec<u8>>,
203 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
204 utils::mul_projective_sw::<ark_bw6_761::g2::Config>(base, scalar)
205 }
206}