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 ed_on_bls12_381_bandersnatch_te_msm(
51		bases: &[EdwardsAffine],
52		scalars: &[<EdwardsConfig as CurveConfig>::ScalarField],
53	) -> Result<EdwardsProjective, ()> {
54		let bases = utils::encode(bases);
55		let scalars = utils::encode(scalars);
56		let res =
57			host_calls::ed_on_bls12_381_bandersnatch_te_msm(bases, scalars).unwrap_or_default();
58		utils::decode_proj_te(res)
59	}
60
61	fn ed_on_bls12_381_bandersnatch_te_mul_projective(
62		base: &EdwardsProjective,
63		scalar: &[u64],
64	) -> Result<EdwardsProjective, ()> {
65		let base = utils::encode_proj_te(base);
66		let scalar = utils::encode(scalar);
67		let res = host_calls::ed_on_bls12_381_bandersnatch_te_mul_projective(base, scalar)
68			.unwrap_or_default();
69		utils::decode_proj_te(res)
70	}
71
72	fn ed_on_bls12_381_bandersnatch_sw_msm(
73		bases: &[SWAffine],
74		scalars: &[<SWConfig as CurveConfig>::ScalarField],
75	) -> Result<SWProjective, ()> {
76		let bases = utils::encode(bases);
77		let scalars = utils::encode(scalars);
78		let res =
79			host_calls::ed_on_bls12_381_bandersnatch_sw_msm(bases, scalars).unwrap_or_default();
80		utils::decode_proj_sw(res)
81	}
82
83	fn ed_on_bls12_381_bandersnatch_sw_mul_projective(
84		base: &SWProjective,
85		scalar: &[u64],
86	) -> Result<SWProjective, ()> {
87		let base = utils::encode_proj_sw(base);
88		let scalar = utils::encode(scalar);
89		let res = host_calls::ed_on_bls12_381_bandersnatch_sw_mul_projective(base, scalar)
90			.unwrap_or_default();
91		utils::decode_proj_sw(res)
92	}
93}
94
95/// Interfaces for working with *Arkworks* *Ed-on-BLS12-381-Bandersnatch* elliptic curve
96/// related types from within the runtime.
97///
98/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
99/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
100///
101/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
102/// and "not-compressed".
103#[runtime_interface]
104pub trait HostCalls {
105	/// Twisted Edwards multi scalar multiplication for *Ed-on-BLS12-381-Bandersnatch*.
106	///
107	/// - Receives encoded:
108	///   - `base`: `ArkScaleProjective<EdwardsProjective>`.
109	///   - `scalars`: `ArkScale<Vec<EdwardsConfig::ScalarField>>`.
110	/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
111	fn ed_on_bls12_381_bandersnatch_te_msm(
112		bases: PassFatPointerAndRead<Vec<u8>>,
113		scalars: PassFatPointerAndRead<Vec<u8>>,
114	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
115		utils::msm_te::<ark_ed_on_bls12_381_bandersnatch::EdwardsConfig>(bases, scalars)
116	}
117
118	/// Twisted Edwards projective multiplication for *Ed-on-BLS12-381-Bandersnatch*.
119	///
120	/// - Receives encoded:
121	///   - `base`: `ArkScaleProjective<EdwardsProjective>`.
122	///   - `scalar`: `ArkScale<Vec<u64>>`.
123	/// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
124	fn ed_on_bls12_381_bandersnatch_te_mul_projective(
125		base: PassFatPointerAndRead<Vec<u8>>,
126		scalar: PassFatPointerAndRead<Vec<u8>>,
127	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
128		utils::mul_projective_te::<ark_ed_on_bls12_381_bandersnatch::EdwardsConfig>(base, scalar)
129	}
130
131	/// Short Weierstrass multi scalar multiplication for *Ed-on-BLS12-381-Bandersnatch*.
132	///
133	/// - Receives encoded:
134	///   - `bases`: `ArkScale<Vec<SWAffine>>`.
135	///   - `scalars`: `ArkScale<Vec<SWConfig::ScalarField>>`.
136	/// - Returns encoded: `ArkScaleProjective<SWProjective>`.
137	fn ed_on_bls12_381_bandersnatch_sw_msm(
138		bases: PassFatPointerAndRead<Vec<u8>>,
139		scalars: PassFatPointerAndRead<Vec<u8>>,
140	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
141		utils::msm_sw::<ark_ed_on_bls12_381_bandersnatch::SWConfig>(bases, scalars)
142	}
143
144	/// Short Weierstrass projective multiplication for *Ed-on-BLS12-381-Bandersnatch*.
145	///
146	/// - Receives encoded:
147	///   - `base`: `ArkScaleProjective<SWProjective>`.
148	///   - `scalar`: `ArkScale<Vec<u64>>`.
149	/// - Returns encoded: `ArkScaleProjective<SWProjective>`.
150	fn ed_on_bls12_381_bandersnatch_sw_mul_projective(
151		base: PassFatPointerAndRead<Vec<u8>>,
152		scalar: PassFatPointerAndRead<Vec<u8>>,
153	) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
154		utils::mul_projective_sw::<ark_ed_on_bls12_381_bandersnatch::SWConfig>(base, scalar)
155	}
156}