referrerpolicy=no-referrer-when-downgrade

sp_crypto_ec_utils/
bls12_381.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-381* types and host functions.
19
20use crate::utils;
21use alloc::vec::Vec;
22use ark_bls12_381_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_381_ext::g1::{BETA, G1_GENERATOR_X, G1_GENERATOR_Y};
32	/// Group configuration.
33	pub type Config = ark_bls12_381_ext::g1::Config<super::HostHooks>;
34	/// Short Weierstrass form point affine representation.
35	pub type G1Affine = ark_bls12_381_ext::g1::G1Affine<super::HostHooks>;
36	/// Short Weierstrass form point projective representation.
37	pub type G1Projective = ark_bls12_381_ext::g1::G1Projective<super::HostHooks>;
38}
39
40/// Second pairing group definitions.
41pub mod g2 {
42	pub use ark_bls12_381_ext::g2::{
43		G2_GENERATOR_X, G2_GENERATOR_X_C0, G2_GENERATOR_X_C1, G2_GENERATOR_Y, G2_GENERATOR_Y_C0,
44		G2_GENERATOR_Y_C1,
45	};
46	/// Group configuration.
47	pub type Config = ark_bls12_381_ext::g2::Config<super::HostHooks>;
48	/// Short Weierstrass form point affine representation.
49	pub type G2Affine = ark_bls12_381_ext::g2::G2Affine<super::HostHooks>;
50	/// Short Weierstrass form point projective representation.
51	pub type G2Projective = ark_bls12_381_ext::g2::G2Projective<super::HostHooks>;
52}
53
54pub use self::{
55	g1::{Config as G1Config, G1Affine, G1Projective},
56	g2::{Config as G2Config, G2Affine, G2Projective},
57};
58
59/// Curve hooks jumping into [`host_calls`] host functions.
60#[derive(Copy, Clone)]
61pub struct HostHooks;
62
63/// Configuration for *BLS12-381* curve.
64pub type Config = ark_bls12_381_ext::Config<HostHooks>;
65
66/// *BLS12-381* definition.
67///
68/// A generic *BLS12* model specialized with *BLS12-381* configuration.
69pub type Bls12_381 = ark_bls12_381_ext::Bls12_381<HostHooks>;
70
71impl CurveHooks for HostHooks {
72	fn bls12_381_multi_miller_loop(
73		g1: impl Iterator<Item = <Bls12_381 as Pairing>::G1Prepared>,
74		g2: impl Iterator<Item = <Bls12_381 as Pairing>::G2Prepared>,
75	) -> Result<<Bls12_381 as Pairing>::TargetField, ()> {
76		let g1 = utils::encode(g1.collect::<Vec<_>>());
77		let g2 = utils::encode(g2.collect::<Vec<_>>());
78		let res = host_calls::bls12_381_multi_miller_loop(g1, g2).unwrap_or_default();
79		utils::decode(res)
80	}
81
82	fn bls12_381_final_exponentiation(
83		target: <Bls12_381 as Pairing>::TargetField,
84	) -> Result<<Bls12_381 as Pairing>::TargetField, ()> {
85		let target = utils::encode(target);
86		let res = host_calls::bls12_381_final_exponentiation(target).unwrap_or_default();
87		utils::decode(res)
88	}
89
90	fn bls12_381_msm_g1(
91		bases: &[G1Affine],
92		scalars: &[<G1Config as CurveConfig>::ScalarField],
93	) -> Result<G1Projective, ()> {
94		let bases = utils::encode(bases);
95		let scalars = utils::encode(scalars);
96		let res = host_calls::bls12_381_msm_g1(bases, scalars).unwrap_or_default();
97		utils::decode_proj_sw(res)
98	}
99
100	fn bls12_381_msm_g2(
101		bases: &[G2Affine],
102		scalars: &[<G2Config as CurveConfig>::ScalarField],
103	) -> Result<G2Projective, ()> {
104		let bases = utils::encode(bases);
105		let scalars = utils::encode(scalars);
106		let res = host_calls::bls12_381_msm_g2(bases, scalars).unwrap_or_default();
107		utils::decode_proj_sw(res)
108	}
109
110	fn bls12_381_mul_projective_g1(
111		base: &G1Projective,
112		scalar: &[u64],
113	) -> Result<G1Projective, ()> {
114		let base = utils::encode_proj_sw(base);
115		let scalar = utils::encode(scalar);
116		let res = host_calls::bls12_381_mul_projective_g1(base, scalar).unwrap_or_default();
117		utils::decode_proj_sw(res)
118	}
119
120	fn bls12_381_mul_projective_g2(
121		base: &G2Projective,
122		scalar: &[u64],
123	) -> Result<G2Projective, ()> {
124		let base = utils::encode_proj_sw(base);
125		let scalar = utils::encode(scalar);
126		let res = host_calls::bls12_381_mul_projective_g2(base, scalar).unwrap_or_default();
127		utils::decode_proj_sw(res)
128	}
129}
130
131/// Interfaces for working with *Arkworks* *BLS12-381* elliptic curve related types
132/// from within the runtime.
133///
134/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
135/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
136///
137/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
138/// and "not-compressed".
139#[runtime_interface]
140pub trait HostCalls {
141	/// Pairing multi Miller loop for *BLS12-381*.
142	///
143	/// - Receives encoded:
144	///   - `a`: `ArkScale<Vec<G1Affine>>`.
145	///   - `b`: `ArkScale<Vec<G2Affine>>`.
146	/// - Returns encoded: `ArkScale<Bls12_381::TargetField>`.
147	fn bls12_381_multi_miller_loop(
148		a: PassFatPointerAndRead<Vec<u8>>,
149		b: PassFatPointerAndRead<Vec<u8>>,
150	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
151		utils::multi_miller_loop::<ark_bls12_381::Bls12_381>(a, b)
152	}
153
154	/// Pairing final exponentiation for *BLS12-381*.
155	///
156	/// - Receives encoded: `ArkScale<<Bls12_377::TargetField>`.
157	/// - Returns encoded: `ArkScale<<Bls12_377::TargetField>`
158	fn bls12_381_final_exponentiation(
159		f: PassFatPointerAndRead<Vec<u8>>,
160	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
161		utils::final_exponentiation::<ark_bls12_381::Bls12_381>(f)
162	}
163
164	/// Multi scalar multiplication on *G1* for *BLS12-381*
165	///
166	/// - Receives encoded:
167	///   - `bases`: `ArkScale<Vec<G1Affine>>`.
168	///   - `scalars`: `ArkScale<Vec<G1Config::ScalarField>>`.
169	/// - Returns encoded: `ArkScaleProjective<ark_bls12_381::G1Projective>`.
170	fn bls12_381_msm_g1(
171		bases: PassFatPointerAndRead<Vec<u8>>,
172		scalars: PassFatPointerAndRead<Vec<u8>>,
173	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
174		utils::msm_sw::<ark_bls12_381::g1::Config>(bases, scalars)
175	}
176
177	/// Multi scalar multiplication on *G2* for *BLS12-381*
178	///
179	/// - Receives encoded:
180	///   - `bases`: `ArkScale<Vec<G2Affine>>`.
181	///   - `scalars`: `ArkScale<Vec<G2Config::ScalarField>>`.
182	/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
183	fn bls12_381_msm_g2(
184		bases: PassFatPointerAndRead<Vec<u8>>,
185		scalars: PassFatPointerAndRead<Vec<u8>>,
186	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
187		utils::msm_sw::<ark_bls12_381::g2::Config>(bases, scalars)
188	}
189
190	/// Projective multiplication on *G1* for *BLS12-381*.
191	///
192	/// - Receives encoded:
193	///   - `base`: `ArkScaleProjective<G1Projective>`.
194	///   - `scalar`: `ArkScale<Vec<u64>>`.
195	/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
196	fn bls12_381_mul_projective_g1(
197		base: PassFatPointerAndRead<Vec<u8>>,
198		scalar: PassFatPointerAndRead<Vec<u8>>,
199	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
200		utils::mul_projective_sw::<ark_bls12_381::g1::Config>(base, scalar)
201	}
202
203	/// Projective multiplication on *G2* for *BLS12-381*
204	///
205	/// - Receives encoded:
206	///   - `base`: `ArkScaleProjective<G2Projective>`.
207	///   - `scalar`: `ArkScale<Vec<u64>>`.
208	/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
209	fn bls12_381_mul_projective_g2(
210		base: PassFatPointerAndRead<Vec<u8>>,
211		scalar: PassFatPointerAndRead<Vec<u8>>,
212	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
213		utils::mul_projective_sw::<ark_bls12_381::g2::Config>(base, scalar)
214	}
215}