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 bls12_377_multi_miller_loop(
83		g1: impl Iterator<Item = <Bls12_377 as Pairing>::G1Prepared>,
84		g2: impl Iterator<Item = <Bls12_377 as Pairing>::G2Prepared>,
85	) -> Result<<Bls12_377 as Pairing>::TargetField, ()> {
86		let g1 = utils::encode(g1.collect::<Vec<_>>());
87		let g2 = utils::encode(g2.collect::<Vec<_>>());
88		let res = host_calls::bls12_377_multi_miller_loop(g1, g2).unwrap_or_default();
89		utils::decode(res)
90	}
91
92	fn bls12_377_final_exponentiation(
93		target: <Bls12_377 as Pairing>::TargetField,
94	) -> Result<<Bls12_377 as Pairing>::TargetField, ()> {
95		let target = utils::encode(target);
96		let res = host_calls::bls12_377_final_exponentiation(target).unwrap_or_default();
97		utils::decode(res)
98	}
99
100	fn bls12_377_msm_g1(
101		bases: &[G1Affine],
102		scalars: &[<G1Config as CurveConfig>::ScalarField],
103	) -> Result<G1Projective, ()> {
104		let bases = utils::encode(bases);
105		let scalars = utils::encode(scalars);
106		let res = host_calls::bls12_377_msm_g1(bases, scalars).unwrap_or_default();
107		utils::decode_proj_sw(res)
108	}
109
110	fn bls12_377_msm_g2(
111		bases: &[G2Affine],
112		scalars: &[<G2Config as CurveConfig>::ScalarField],
113	) -> Result<G2Projective, ()> {
114		let bases = utils::encode(bases);
115		let scalars = utils::encode(scalars);
116		let res = host_calls::bls12_377_msm_g2(bases, scalars).unwrap_or_default();
117		utils::decode_proj_sw(res)
118	}
119
120	fn bls12_377_mul_projective_g1(
121		base: &G1Projective,
122		scalar: &[u64],
123	) -> Result<G1Projective, ()> {
124		let base = utils::encode_proj_sw(base);
125		let scalar = utils::encode(scalar);
126		let res = host_calls::bls12_377_mul_projective_g1(base, scalar).unwrap_or_default();
127		utils::decode_proj_sw(res)
128	}
129
130	fn bls12_377_mul_projective_g2(
131		base: &G2Projective,
132		scalar: &[u64],
133	) -> Result<G2Projective, ()> {
134		let base = utils::encode_proj_sw(base);
135		let scalar = utils::encode(scalar);
136		let res = host_calls::bls12_377_mul_projective_g2(base, scalar).unwrap_or_default();
137		utils::decode_proj_sw(res)
138	}
139}
140
141/// Interfaces for working with *Arkworks* *BLS12-377* elliptic curve related types
142/// from within the runtime.
143///
144/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
145/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
146///
147/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
148/// and "not-compressed".
149#[runtime_interface]
150pub trait HostCalls {
151	/// Pairing multi Miller loop for *BLS12-377*.
152	///
153	/// - Receives encoded:
154	///   - `a`: `ArkScale<Vec<G1Affine>>`.
155	///   - `b`: `ArkScale<Vec<G2Affine>>`.
156	/// - Returns encoded:  `ArkScale<Bls12_377::TargetField>`.
157	fn bls12_377_multi_miller_loop(
158		a: PassFatPointerAndRead<Vec<u8>>,
159		b: PassFatPointerAndRead<Vec<u8>>,
160	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
161		utils::multi_miller_loop::<ark_bls12_377::Bls12_377>(a, b)
162	}
163
164	/// Pairing final exponentiation for *BLS12-377.*
165	///
166	/// - Receives encoded: `ArkScale<Bls12_377::TargetField>`.
167	/// - Returns encoded:  `ArkScale<Bls12_377::TargetField>`.
168	fn bls12_377_final_exponentiation(
169		f: PassFatPointerAndRead<Vec<u8>>,
170	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
171		utils::final_exponentiation::<ark_bls12_377::Bls12_377>(f)
172	}
173
174	/// Multi scalar multiplication on *G1* for *BLS12-377*.
175	///
176	/// - Receives encoded:
177	///   - `bases`: `ArkScale<Vec<G1Affine>>`.
178	///   - `scalars`: `ArkScale<Vec<G1Config::ScalarField>>`.
179	/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
180	fn bls12_377_msm_g1(
181		bases: PassFatPointerAndRead<Vec<u8>>,
182		scalars: PassFatPointerAndRead<Vec<u8>>,
183	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
184		utils::msm_sw::<ark_bls12_377::g1::Config>(bases, scalars)
185	}
186
187	/// Multi scalar multiplication on *G2* for *BLS12-377*.
188	///
189	/// - Receives encoded:
190	///   - `bases`: `ArkScale<Vec<G2Affine>>`.
191	///   - `scalars`: `ArkScale<Vec<G2Config::ScalarField>>`.
192	/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
193	fn bls12_377_msm_g2(
194		bases: PassFatPointerAndRead<Vec<u8>>,
195		scalars: PassFatPointerAndRead<Vec<u8>>,
196	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
197		utils::msm_sw::<ark_bls12_377::g2::Config>(bases, scalars)
198	}
199
200	/// Projective multiplication on *G1* for *BLS12-377*.
201	///
202	/// - Receives encoded:
203	///   - `base`: `ArkScaleProjective<G1Projective>`.
204	///   - `scalar`: `ArkScale<Vec<u64>>`.
205	/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
206	fn bls12_377_mul_projective_g1(
207		base: PassFatPointerAndRead<Vec<u8>>,
208		scalar: PassFatPointerAndRead<Vec<u8>>,
209	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
210		utils::mul_projective_sw::<ark_bls12_377::g1::Config>(base, scalar)
211	}
212
213	/// Projective multiplication on *G2* for *BLS12-377*.
214	///
215	/// - Receives encoded:
216	///   - `base`: `ArkScaleProjective<G2Projective>`.
217	///   - `scalar`: `ArkScale<Vec<u64>>`.
218	/// - Returns encoded: `ArkScaleProjective<ark_bls12_377::G2Projective>`.
219	fn bls12_377_mul_projective_g2(
220		base: PassFatPointerAndRead<Vec<u8>>,
221		scalar: PassFatPointerAndRead<Vec<u8>>,
222	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
223		utils::mul_projective_sw::<ark_bls12_377::g2::Config>(base, scalar)
224	}
225}