referrerpolicy=no-referrer-when-downgrade

sp_crypto_ec_utils/
ed_on_bls12_381_bandersnatch.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//! Elliptic Curves host functions to handle some of the *Arkworks* *Ed-on-BLS12-381-Bandersnatch*
19//! computationally expensive operations.
20
21use crate::utils;
22use alloc::vec::Vec;
23use ark_ec::CurveConfig;
24use ark_ed_on_bls12_381_bandersnatch_ext::CurveHooks;
25use sp_runtime_interface::{
26	pass_by::{AllocateAndReturnByCodec, PassFatPointerAndRead},
27	runtime_interface,
28};
29
30/// Curve hooks jumping into [`host_calls`] host functions.
31#[derive(Copy, Clone)]
32pub struct HostHooks;
33
34/// Group configuration.
35pub type BandersnatchConfig = ark_ed_on_bls12_381_bandersnatch_ext::BandersnatchConfig<HostHooks>;
36/// Group configuration for Twisted Edwards form (equal to [`BandersnatchConfig`]).
37pub type EdwardsConfig = ark_ed_on_bls12_381_bandersnatch_ext::EdwardsConfig<HostHooks>;
38/// Twisted Edwards form point affine representation.
39pub type EdwardsAffine = ark_ed_on_bls12_381_bandersnatch_ext::EdwardsAffine<HostHooks>;
40/// Twisted Edwards form point projective representation.
41pub type EdwardsProjective = ark_ed_on_bls12_381_bandersnatch_ext::EdwardsProjective<HostHooks>;
42/// Group configuration for Short Weierstrass form (equal to [`BandersnatchConfig`]).
43pub type SWConfig = ark_ed_on_bls12_381_bandersnatch_ext::SWConfig<HostHooks>;
44/// Short Weierstrass form point affine representation.
45pub type SWAffine = ark_ed_on_bls12_381_bandersnatch_ext::SWAffine<HostHooks>;
46/// Short Weierstrass form point projective representation.
47pub type SWProjective = ark_ed_on_bls12_381_bandersnatch_ext::SWProjective<HostHooks>;
48
49impl CurveHooks for HostHooks {
50	fn msm_te(
51		bases: &[EdwardsAffine],
52		scalars: &[<EdwardsConfig as CurveConfig>::ScalarField],
53	) -> EdwardsProjective {
54		host_calls::ed_on_bls12_381_bandersnatch_te_msm(
55			utils::encode(bases),
56			utils::encode(scalars),
57		)
58		.and_then(|res| utils::decode_proj_te(res))
59		.unwrap_or_default()
60	}
61
62	fn mul_projective_te(base: &EdwardsProjective, scalar: &[u64]) -> EdwardsProjective {
63		host_calls::ed_on_bls12_381_bandersnatch_te_mul_projective(
64			utils::encode_proj_te(base),
65			utils::encode(scalar),
66		)
67		.and_then(|res| utils::decode_proj_te(res))
68		.unwrap_or_default()
69	}
70
71	fn msm_sw(
72		bases: &[SWAffine],
73		scalars: &[<SWConfig as CurveConfig>::ScalarField],
74	) -> SWProjective {
75		host_calls::ed_on_bls12_381_bandersnatch_sw_msm(
76			utils::encode(bases),
77			utils::encode(scalars),
78		)
79		.and_then(|res| utils::decode_proj_sw(res))
80		.unwrap_or_default()
81	}
82
83	fn mul_projective_sw(base: &SWProjective, scalar: &[u64]) -> SWProjective {
84		host_calls::ed_on_bls12_381_bandersnatch_sw_mul_projective(
85			utils::encode_proj_sw(base),
86			utils::encode(scalar),
87		)
88		.and_then(|res| utils::decode_proj_sw(res))
89		.unwrap_or_default()
90	}
91}
92
93/// Interfaces for working with *Arkworks* *Ed-on-BLS12-381-Bandersnatch* elliptic curve
94/// related types from within the runtime.
95///
96/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
97/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
98///
99/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
100/// and "not-compressed".
101#[runtime_interface]
102pub trait HostCalls {
103	/// Twisted Edwards multi scalar multiplication for *Ed-on-BLS12-381-Bandersnatch*.
104	///
105	/// - Receives encoded:
106	///   - `base`: `ArkScaleProjective<EdwardsProjective>`.
107	///   - `scalars`: `ArkScale<Vec<EdwardsConfig::ScalarField>>`.
108	/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
109	fn ed_on_bls12_381_bandersnatch_te_msm(
110		bases: PassFatPointerAndRead<Vec<u8>>,
111		scalars: PassFatPointerAndRead<Vec<u8>>,
112	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
113		utils::msm_te::<ark_ed_on_bls12_381_bandersnatch::EdwardsConfig>(bases, scalars)
114	}
115
116	/// Twisted Edwards projective multiplication for *Ed-on-BLS12-381-Bandersnatch*.
117	///
118	/// - Receives encoded:
119	///   - `base`: `ArkScaleProjective<EdwardsProjective>`.
120	///   - `scalar`: `ArkScale<Vec<u64>>`.
121	/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
122	fn ed_on_bls12_381_bandersnatch_te_mul_projective(
123		base: PassFatPointerAndRead<Vec<u8>>,
124		scalar: PassFatPointerAndRead<Vec<u8>>,
125	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
126		utils::mul_projective_te::<ark_ed_on_bls12_381_bandersnatch::EdwardsConfig>(base, scalar)
127	}
128
129	/// Short Weierstrass multi scalar multiplication for *Ed-on-BLS12-381-Bandersnatch*.
130	///
131	/// - Receives encoded:
132	///   - `bases`: `ArkScale<Vec<SWAffine>>`.
133	///   - `scalars`: `ArkScale<Vec<SWConfig::ScalarField>>`.
134	/// - Returns encoded: `ArkScaleProjective<SWProjective>`.
135	fn ed_on_bls12_381_bandersnatch_sw_msm(
136		bases: PassFatPointerAndRead<Vec<u8>>,
137		scalars: PassFatPointerAndRead<Vec<u8>>,
138	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
139		utils::msm_sw::<ark_ed_on_bls12_381_bandersnatch::SWConfig>(bases, scalars)
140	}
141
142	/// Short Weierstrass projective multiplication for *Ed-on-BLS12-381-Bandersnatch*.
143	///
144	/// - Receives encoded:
145	///   - `base`: `ArkScaleProjective<SWProjective>`.
146	///   - `scalar`: `ArkScale<Vec<u64>>`.
147	/// - Returns encoded: `ArkScaleProjective<SWProjective>`.
148	fn ed_on_bls12_381_bandersnatch_sw_mul_projective(
149		base: PassFatPointerAndRead<Vec<u8>>,
150		scalar: PassFatPointerAndRead<Vec<u8>>,
151	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
152		utils::mul_projective_sw::<ark_ed_on_bls12_381_bandersnatch::SWConfig>(base, scalar)
153	}
154}