referrerpolicy=no-referrer-when-downgrade

sp_crypto_ec_utils/
bw6_761.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//! *BW6-761* types and host functions.
19
20use 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
29/// First pairing group definitions.
30pub mod g1 {
31	pub use ark_bw6_761_ext::g1::{G1_GENERATOR_X, G1_GENERATOR_Y};
32	/// Group configuration.
33	pub type Config = ark_bw6_761_ext::g1::Config<super::HostHooks>;
34	/// Short Weierstrass form point affine representation.
35	pub type G1Affine = ark_bw6_761_ext::g1::G1Affine<super::HostHooks>;
36	/// Short Weierstrass form point projective representation.
37	pub type G1Projective = ark_bw6_761_ext::g1::G1Projective<super::HostHooks>;
38}
39
40/// Second pairing group definitions.
41pub mod g2 {
42	pub use ark_bw6_761_ext::g2::{G2_GENERATOR_X, G2_GENERATOR_Y};
43	/// Group configuration.
44	pub type Config = ark_bw6_761_ext::g2::Config<super::HostHooks>;
45	/// Short Weierstrass form point affine representation.
46	pub type G2Affine = ark_bw6_761_ext::g2::G2Affine<super::HostHooks>;
47	/// Short Weierstrass form point projective representation.
48	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/// Curve hooks jumping into [`host_calls`] host functions.
57#[derive(Copy, Clone)]
58pub struct HostHooks;
59
60/// Configuration for *BW6-361* curve.
61pub type Config = ark_bw6_761_ext::Config<HostHooks>;
62
63/// *BW6-361* definition.
64///
65/// A generic *BW6* model specialized with *BW6-761* configuration.
66pub 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/// Interfaces for working with *Arkworks* *BW6-761* elliptic curve related types
123/// from within the runtime.
124///
125/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
126/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
127///
128/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
129/// and "not-compressed".
130#[runtime_interface]
131pub trait HostCalls {
132	/// Pairing multi Miller loop for *BW6-761*.
133	///
134	/// - Receives encoded:
135	///   - `a: ArkScale<Vec<G1Affine>>`.
136	///   - `b: ArkScale<Vec<G2Affine>>`.
137	/// - Returns encoded: `ArkScale<BW6_761;:TargetField>`.
138	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	/// Pairing final exponentiation for *BW6-761*.
146	///
147	/// - Receives encoded: `ArkScale<BW6_761::TargetField>`.
148	/// - Returns encoded: `ArkScale<BW6_761::TargetField>`.
149	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	/// Multi scalar multiplication on *G1* for *BW6-761*.
156	///
157	/// - Receives encoded:
158	///   - `bases`: `ArkScale<Vec<G1Affine>>`.
159	///   - `scalars`: `ArkScale<G1Config::ScalarField>`.
160	/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
161	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	/// Multi scalar multiplication on *G2* for *BW6-761*.
169	///
170	/// - Receives encoded:
171	///   - `bases`: `ArkScale<Vec<G2Affine>>`.
172	///   - `scalars`: `ArkScale<Vec<G2Config::ScalarField>>`.
173	/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
174	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	/// Projective multiplication on *G1* for *BW6-761*.
182	///
183	/// - Receives encoded:
184	///   - `base`: `ArkScaleProjective<G1Projective>`.
185	///   - `scalar`: `ArkScale<Vec<u64>>`.
186	/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
187	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	/// Projective multiplication on *G2* for *BW6-761*.
195	///
196	/// - Receives encoded:
197	///   - `base`: `ArkScaleProjective<G2Projective>`.
198	///   - `scalar`: `ArkScale<Vec<u64>>`.
199	/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
200	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}