referrerpolicy=no-referrer-when-downgrade

sp_crypto_ec_utils/
ed_on_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//! *Ed-on-BLS12-377* types and host functions.
19
20use crate::utils::{self, HostcallResult, FAIL_MSG};
21use alloc::vec::Vec;
22use ark_ec::{AffineRepr, CurveConfig, CurveGroup};
23use ark_ed_on_bls12_377_ext::CurveHooks;
24use sp_runtime_interface::{
25	pass_by::{PassFatPointerAndRead, PassFatPointerAndWrite},
26	runtime_interface,
27};
28
29/// Group configuration.
30pub type EdwardsConfig = ark_ed_on_bls12_377_ext::EdwardsConfig<HostHooks>;
31/// Twisted Edwards form point affine representation.
32pub type EdwardsAffine = ark_ed_on_bls12_377_ext::EdwardsAffine<HostHooks>;
33/// Twisted Edwards form point projective representation.
34pub type EdwardsProjective = ark_ed_on_bls12_377_ext::EdwardsProjective<HostHooks>;
35
36/// Group scalar field (Fr).
37pub type ScalarField = <EdwardsConfig as CurveConfig>::ScalarField;
38
39/// Curve hooks jumping into [`host_calls`] host functions.
40#[derive(Copy, Clone)]
41pub struct HostHooks;
42
43impl CurveHooks for HostHooks {
44	fn msm(bases: &[EdwardsAffine], scalars: &[ScalarField]) -> EdwardsProjective {
45		let mut out = utils::buffer_for::<EdwardsAffine>();
46		host_calls::ed_on_bls12_377_msm(&utils::encode(bases), &utils::encode(scalars), &mut out)
47			.and_then(|_| utils::decode::<EdwardsAffine>(&out))
48			.expect(FAIL_MSG)
49			.into_group()
50	}
51
52	fn mul_projective(base: &EdwardsProjective, scalar: &[u64]) -> EdwardsProjective {
53		let mut out = utils::buffer_for::<EdwardsAffine>();
54		host_calls::ed_on_bls12_377_mul(
55			&utils::encode(base.into_affine()),
56			&utils::encode(scalar),
57			&mut out,
58		)
59		.and_then(|_| utils::decode::<EdwardsAffine>(&out))
60		.expect(FAIL_MSG)
61		.into_group()
62	}
63}
64
65/// Interfaces for working with *Arkworks* *Ed-on-BLS12-377* elliptic curve related types
66/// from within the runtime.
67///
68/// All types are (de-)serialized through the wrapper types from `ark-scale`.
69///
70/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
71/// and "not-compressed".
72#[runtime_interface]
73pub trait HostCalls {
74	/// Twisted Edwards multi scalar multiplication for *Ed-on-BLS12-377*.
75	///
76	/// Receives encoded:
77	/// - `bases`: `Vec<EdwardsAffine>`.
78	/// - `scalars`: `Vec<ScalarField>`.
79	/// Writes encoded `EdwardsAffine` to `out`.
80	fn ed_on_bls12_377_msm(
81		bases: PassFatPointerAndRead<&[u8]>,
82		scalars: PassFatPointerAndRead<&[u8]>,
83		out: PassFatPointerAndWrite<&mut [u8]>,
84	) -> HostcallResult {
85		utils::msm_te::<ark_ed_on_bls12_377::EdwardsConfig>(bases, scalars, out)
86	}
87
88	/// Twisted Edwards affine multiplication for *Ed-on-BLS12-377*.
89	///
90	/// Receives encoded:
91	/// - `base`: `EdwardsAffine`.
92	/// - `scalar`: `BigInteger`.
93	/// Writes encoded `EdwardsAffine` to `out`.
94	fn ed_on_bls12_377_mul(
95		base: PassFatPointerAndRead<&[u8]>,
96		scalar: PassFatPointerAndRead<&[u8]>,
97		out: PassFatPointerAndWrite<&mut [u8]>,
98	) -> HostcallResult {
99		utils::mul_te::<ark_ed_on_bls12_377::EdwardsConfig>(base, scalar, out)
100	}
101}
102
103#[cfg(test)]
104mod tests {
105	use super::*;
106	use crate::utils::testing::*;
107
108	#[test]
109	fn mul_works() {
110		mul_te_test::<EdwardsAffine, ark_ed_on_bls12_377::EdwardsAffine>();
111	}
112
113	#[test]
114	fn msm_works() {
115		msm_te_test::<EdwardsAffine, ark_ed_on_bls12_377::EdwardsAffine>();
116	}
117}