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 ed_on_bls12_377_msm(
42 bases: &[EdwardsAffine],
43 scalars: &[<EdwardsConfig as CurveConfig>::ScalarField],
44 ) -> Result<EdwardsProjective, ()> {
45 let bases = utils::encode(bases);
46 let scalars = utils::encode(scalars);
47 let res = host_calls::ed_on_bls12_377_te_msm(bases, scalars).unwrap_or_default();
48 utils::decode_proj_te(res)
49 }
50
51 fn ed_on_bls12_377_mul_projective(
52 base: &EdwardsProjective,
53 scalar: &[u64],
54 ) -> Result<EdwardsProjective, ()> {
55 let base = utils::encode_proj_te(base);
56 let scalar = utils::encode(scalar);
57 let res = host_calls::ed_on_bls12_377_te_mul_projective(base, scalar).unwrap_or_default();
58 utils::decode_proj_te(res)
59 }
60}
61
62/// Interfaces for working with *Arkworks* *Ed-on-BLS12-377* elliptic curve
63/// related types from within the runtime.
64///
65/// All types are (de-)serialized through the wrapper types from the `ark-scale` trait,
66/// with `ark_scale::{ArkScale, ArkScaleProjective}`.
67///
68/// `ArkScale`'s `Usage` generic parameter is expected to be set to "not-validated"
69/// and "not-compressed".
70#[runtime_interface]
71pub trait HostCalls {
72 /// Twisted Edwards multi scalar multiplication for *Ed-on-BLS12-377*.
73 ///
74 /// - Receives encoded:
75 /// - `base`: `ArkScaleProjective<EdwardsProjective>`.
76 /// - `scalars`: `ArkScale<Vec<EdwardsConfig::ScalarField>>`.
77 /// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
78 fn ed_on_bls12_377_te_msm(
79 bases: PassFatPointerAndRead<Vec<u8>>,
80 scalars: PassFatPointerAndRead<Vec<u8>>,
81 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
82 utils::msm_te::<ark_ed_on_bls12_377::EdwardsConfig>(bases, scalars)
83 }
84
85 /// Twisted Edwards projective multiplication for *Ed-on-BLS12-377*.
86 ///
87 /// - Receives encoded:
88 /// - `base`: `ArkScaleProjective<EdwardsProjective>`.
89 /// - `scalar`: `ArkScale<Vec<u64>>`.
90 /// - Returns encoded: `ArkScaleProjective<EdwardsProjective>`.
91 fn ed_on_bls12_377_te_mul_projective(
92 base: PassFatPointerAndRead<Vec<u8>>,
93 scalar: PassFatPointerAndRead<Vec<u8>>,
94 ) -> AllocateAndReturnByCodec<Result<Vec<u8>, ()>> {
95 utils::mul_projective_te::<ark_ed_on_bls12_377::EdwardsConfig>(base, scalar)
96 }
97}