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;
21use alloc::vec::Vec;
22use ark_ec::CurveConfig;
23use ark_ed_on_bls12_377_ext::CurveHooks;
24use sp_runtime_interface::{
25 pass_by::{AllocateAndReturnByCodec, PassFatPointerAndRead},
26 runtime_interface,
27};
28
29/// Curve hooks jumping into [`host_calls`] host functions.
30#[derive(Copy, Clone)]
31pub struct HostHooks;
32
33/// Group configuration.
34pub type EdwardsConfig = ark_ed_on_bls12_377_ext::EdwardsConfig<HostHooks>;
35/// Twisted Edwards form point affine representation.
36pub type EdwardsAffine = ark_ed_on_bls12_377_ext::EdwardsAffine<HostHooks>;
37/// Twisted Edwards form point projective representation.
38pub type EdwardsProjective = ark_ed_on_bls12_377_ext::EdwardsProjective<HostHooks>;
39
40impl CurveHooks for HostHooks {
41 fn msm(
42 bases: &[EdwardsAffine],
43 scalars: &[<EdwardsConfig as CurveConfig>::ScalarField],
44 ) -> EdwardsProjective {
45 host_calls::ed_on_bls12_377_te_msm(utils::encode(bases), utils::encode(scalars))
46 .and_then(|res| utils::decode_proj_te(res))
47 .unwrap_or_default()
48 }
49
50 fn mul_projective(base: &EdwardsProjective, scalar: &[u64]) -> EdwardsProjective {
51 host_calls::ed_on_bls12_377_te_mul_projective(
52 utils::encode_proj_te(base),
53 utils::encode(scalar),
54 )
55 .and_then(|res| utils::decode_proj_te(res))
56 .unwrap_or_default()
57 }
58}
59
60/// Interfaces for working with *Arkworks* *Ed-on-BLS12-377* elliptic curve
61/// related types from within the runtime.
62///
63/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
64/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
65///
66/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
67/// and "not-compressed".
68#[runtime_interface]
69pub trait HostCalls {
70 /// Twisted Edwards multi scalar multiplication for *Ed-on-BLS12-377*.
71 ///
72 /// - Receives encoded:
73 /// - `base`: `ArkScaleProjective<EdwardsProjective>`.
74 /// - `scalars`: `ArkScale<Vec<EdwardsConfig::ScalarField>>`.
75 /// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
76 fn ed_on_bls12_377_te_msm(
77 bases: PassFatPointerAndRead<Vec<u8>>,
78 scalars: PassFatPointerAndRead<Vec<u8>>,
79 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
80 utils::msm_te::<ark_ed_on_bls12_377::EdwardsConfig>(bases, scalars)
81 }
82
83 /// Twisted Edwards projective multiplication for *Ed-on-BLS12-377*.
84 ///
85 /// - Receives encoded:
86 /// - `base`: `ArkScaleProjective<EdwardsProjective>`.
87 /// - `scalar`: `ArkScale<Vec<u64>>`.
88 /// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
89 fn ed_on_bls12_377_te_mul_projective(
90 base: PassFatPointerAndRead<Vec<u8>>,
91 scalar: PassFatPointerAndRead<Vec<u8>>,
92 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
93 utils::mul_projective_te::<ark_ed_on_bls12_377::EdwardsConfig>(base, scalar)
94 }
95}