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 multi_miller_loop(
70		g1: impl Iterator<Item = <BW6_761 as Pairing>::G1Prepared>,
71		g2: impl Iterator<Item = <BW6_761 as Pairing>::G2Prepared>,
72	) -> <BW6_761 as Pairing>::TargetField {
73		host_calls::bw6_761_multi_miller_loop(utils::encode_iter(g1), utils::encode_iter(g2))
74			.and_then(|res| utils::decode(res))
75			.unwrap_or_default()
76	}
77
78	fn final_exponentiation(
79		target: <BW6_761 as Pairing>::TargetField,
80	) -> <BW6_761 as Pairing>::TargetField {
81		host_calls::bw6_761_final_exponentiation(utils::encode(target))
82			.and_then(|res| utils::decode(res))
83			.unwrap_or_default()
84	}
85
86	fn msm_g1(
87		bases: &[G1Affine],
88		scalars: &[<G1Config as CurveConfig>::ScalarField],
89	) -> G1Projective {
90		host_calls::bw6_761_msm_g1(utils::encode(bases), utils::encode(scalars))
91			.and_then(|res| utils::decode_proj_sw(res))
92			.unwrap_or_default()
93	}
94
95	fn msm_g2(
96		bases: &[G2Affine],
97		scalars: &[<G2Config as CurveConfig>::ScalarField],
98	) -> G2Projective {
99		host_calls::bw6_761_msm_g2(utils::encode(bases), utils::encode(scalars))
100			.and_then(|res| utils::decode_proj_sw(res))
101			.unwrap_or_default()
102	}
103
104	fn mul_projective_g1(base: &G1Projective, scalar: &[u64]) -> G1Projective {
105		host_calls::bw6_761_mul_projective_g1(utils::encode_proj_sw(base), utils::encode(scalar))
106			.and_then(|res| utils::decode_proj_sw(res))
107			.unwrap_or_default()
108	}
109
110	fn mul_projective_g2(base: &G2Projective, scalar: &[u64]) -> G2Projective {
111		host_calls::bw6_761_mul_projective_g2(utils::encode_proj_sw(base), utils::encode(scalar))
112			.and_then(|res| utils::decode_proj_sw(res))
113			.unwrap_or_default()
114	}
115}
116
117/// Interfaces for working with *Arkworks* *BW6-761* elliptic curve related types
118/// from within the runtime.
119///
120/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
121/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
122///
123/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
124/// and "not-compressed".
125#[runtime_interface]
126pub trait HostCalls {
127	/// Pairing multi Miller loop for *BW6-761*.
128	///
129	/// - Receives encoded:
130	///   - `a: ArkScale<Vec<G1Affine>>`.
131	///   - `b: ArkScale<Vec<G2Affine>>`.
132	/// - Returns encoded: `ArkScale<BW6_761;:TargetField>`.
133	fn bw6_761_multi_miller_loop(
134		a: PassFatPointerAndRead<Vec<u8>>,
135		b: PassFatPointerAndRead<Vec<u8>>,
136	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
137		utils::multi_miller_loop::<ark_bw6_761::BW6_761>(a, b)
138	}
139
140	/// Pairing final exponentiation for *BW6-761*.
141	///
142	/// - Receives encoded: `ArkScale<BW6_761::TargetField>`.
143	/// - Returns encoded: `ArkScale<BW6_761::TargetField>`.
144	fn bw6_761_final_exponentiation(
145		f: PassFatPointerAndRead<Vec<u8>>,
146	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
147		utils::final_exponentiation::<ark_bw6_761::BW6_761>(f)
148	}
149
150	/// Multi scalar multiplication on *G1* for *BW6-761*.
151	///
152	/// - Receives encoded:
153	///   - `bases`: `ArkScale<Vec<G1Affine>>`.
154	///   - `scalars`: `ArkScale<G1Config::ScalarField>`.
155	/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
156	fn bw6_761_msm_g1(
157		bases: PassFatPointerAndRead<Vec<u8>>,
158		scalars: PassFatPointerAndRead<Vec<u8>>,
159	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
160		utils::msm_sw::<ark_bw6_761::g1::Config>(bases, scalars)
161	}
162
163	/// Multi scalar multiplication on *G2* for *BW6-761*.
164	///
165	/// - Receives encoded:
166	///   - `bases`: `ArkScale<Vec<G2Affine>>`.
167	///   - `scalars`: `ArkScale<Vec<G2Config::ScalarField>>`.
168	/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
169	fn bw6_761_msm_g2(
170		bases: PassFatPointerAndRead<Vec<u8>>,
171		scalars: PassFatPointerAndRead<Vec<u8>>,
172	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
173		utils::msm_sw::<ark_bw6_761::g2::Config>(bases, scalars)
174	}
175
176	/// Projective multiplication on *G1* for *BW6-761*.
177	///
178	/// - Receives encoded:
179	///   - `base`: `ArkScaleProjective<G1Projective>`.
180	///   - `scalar`: `ArkScale<Vec<u64>>`.
181	/// - Returns encoded: `ArkScaleProjective<G1Projective>`.
182	fn bw6_761_mul_projective_g1(
183		base: PassFatPointerAndRead<Vec<u8>>,
184		scalar: PassFatPointerAndRead<Vec<u8>>,
185	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
186		utils::mul_projective_sw::<ark_bw6_761::g1::Config>(base, scalar)
187	}
188
189	/// Projective multiplication on *G2* for *BW6-761*.
190	///
191	/// - Receives encoded:
192	///   - `base`: `ArkScaleProjective<G2Projective>`.
193	///   - `scalar`: `ArkScale<Vec<u64>>`.
194	/// - Returns encoded: `ArkScaleProjective<G2Projective>`.
195	fn bw6_761_mul_projective_g2(
196		base: PassFatPointerAndRead<Vec<u8>>,
197		scalar: PassFatPointerAndRead<Vec<u8>>,
198	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
199		utils::mul_projective_sw::<ark_bw6_761::g2::Config>(base, scalar)
200	}
201}