referrerpolicy=no-referrer-when-downgrade

sp_crypto_ec_utils/
bls12_377.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! *BLS12-377* types and host functions.
19
20use 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
29/// First pairing group definitions.
30pub 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	/// Group configuration.
35	pub type Config = ark_bls12_377_ext::g1::Config<super::HostHooks>;
36	/// Short Weierstrass form point affine representation.
37	pub type G1Affine = ark_bls12_377_ext::g1::G1Affine<super::HostHooks>;
38	/// Short Weierstrass form point projective representation.
39	pub type G1Projective = ark_bls12_377_ext::g1::G1Projective<super::HostHooks>;
40	/// Short Weierstrass form point affine representation.
41	pub type G1SWAffine = ark_bls12_377_ext::g1::G1SWAffine<super::HostHooks>;
42	/// Short Weierstrass form point projective representation.
43	pub type G1SWProjective = ark_bls12_377_ext::g1::G1SWProjective<super::HostHooks>;
44	/// Twisted Edwards form point affine representation.
45	pub type G1TEAffine = ark_bls12_377_ext::g1::G1TEAffine<super::HostHooks>;
46	/// Twisted Edwards form point projective representation.
47	pub type G1TEProjective = ark_bls12_377_ext::g1::G1TEProjective<super::HostHooks>;
48}
49
50/// Second pairing group definitions.
51pub 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	/// Group configuration.
57	pub type Config = ark_bls12_377_ext::g2::Config<super::HostHooks>;
58	/// Short Weierstrass form point affine representation.
59	pub type G2Affine = ark_bls12_377_ext::g2::G2Affine<super::HostHooks>;
60	/// Short Weierstrass form point projective representation.
61	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/// Curve hooks jumping into [`host_calls`] host functions.
70#[derive(Copy, Clone)]
71pub struct HostHooks;
72
73/// Configuration for *BLS12-377* curve.
74pub type Config = ark_bls12_377_ext::Config<HostHooks>;
75
76/// *BLS12-377* definition.
77///
78/// A generic *BLS12* model specialized with *BLS12-377* configuration.
79pub 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/// Interfaces for working with *Arkworks* *BLS12-377* elliptic curve related types
131/// from within the runtime.
132///
133/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
134/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
135///
136/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
137/// and "not-compressed".
138#[runtime_interface]
139pub trait HostCalls {
140	/// Pairing multi Miller loop for *BLS12-377*.
141	///
142	/// - Receives encoded:
143	///   - `a`: `ArkScale<Vec<G1Affine>>`.
144	///   - `b`: `ArkScale<Vec<G2Affine>>`.
145	/// - Returns encoded:  `ArkScale<Bls12_377::TargetField>`.
146	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	/// Pairing final exponentiation for *BLS12-377.*
154	///
155	/// - Receives encoded: `ArkScale<Bls12_377::TargetField>`.
156	/// - Returns encoded:  `ArkScale<Bls12_377::TargetField>`.
157	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	/// Multi scalar multiplication on *G1* for *BLS12-377*.
164	///
165	/// - Receives encoded:
166	///   - `bases`: `ArkScale<Vec<G1Affine>>`.
167	///   - `scalars`: `ArkScale<Vec<G1Config::ScalarField>>`.
168	/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
169	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	/// Multi scalar multiplication on *G2* for *BLS12-377*.
177	///
178	/// - Receives encoded:
179	///   - `bases`: `ArkScale<Vec<G2Affine>>`.
180	///   - `scalars`: `ArkScale<Vec<G2Config::ScalarField>>`.
181	/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
182	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	/// Projective multiplication on *G1* for *BLS12-377*.
190	///
191	/// - Receives encoded:
192	///   - `base`: `ArkScaleProjective<G1Projective>`.
193	///   - `scalar`: `ArkScale<Vec<u64>>`.
194	/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
195	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	/// Projective multiplication on *G2* for *BLS12-377*.
203	///
204	/// - Receives encoded:
205	///   - `base`: `ArkScaleProjective<G2Projective>`.
206	///   - `scalar`: `ArkScale<Vec<u64>>`.
207	/// - Returns encoded: `ArkScaleProjective<ark_bls12_377::G2Projective>`.
208	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}