referrerpolicy=no-referrer-when-downgrade

sp_crypto_ec_utils/
utils.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//! Generic executions of the operations for *Arkworks* elliptic curves.
19
20// As not all functions are used by each elliptic curve and some elliptic
21// curve may be excluded by the build we resort to `#[allow(unused)]` to
22// suppress the expected warning.
23
24use alloc::vec::Vec;
25use ark_ec::{
26	pairing::{MillerLoopOutput, Pairing},
27	short_weierstrass::{Affine as SWAffine, Projective as SWProjective, SWCurveConfig},
28	twisted_edwards::{Affine as TEAffine, Projective as TEProjective, TECurveConfig},
29	CurveConfig, VariableBaseMSM,
30};
31use ark_scale::{
32	ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Compress, Validate},
33	scale::{Decode, Encode},
34};
35
36// SCALE encoding parameters shared by all the enabled modules
37const SCALE_USAGE: u8 = ark_scale::make_usage(Compress::No, Validate::No);
38type ArkScale<T> = ark_scale::ArkScale<T, SCALE_USAGE>;
39type ArkScaleProjective<T> = ark_scale::hazmat::ArkScaleProjective<T>;
40
41#[inline(always)]
42pub fn encode<T: CanonicalSerialize>(val: T) -> Vec<u8> {
43	ArkScale::from(val).encode()
44}
45
46#[inline(always)]
47pub fn decode<T: CanonicalDeserialize>(buf: Vec<u8>) -> Result<T, ()> {
48	ArkScale::<T>::decode(&mut &buf[..]).map_err(|_| ()).map(|v| v.0)
49}
50
51#[inline(always)]
52pub fn encode_proj_sw<T: SWCurveConfig>(val: &SWProjective<T>) -> Vec<u8> {
53	ArkScaleProjective::from(val).encode()
54}
55
56#[inline(always)]
57pub fn decode_proj_sw<T: SWCurveConfig>(buf: Vec<u8>) -> Result<SWProjective<T>, ()> {
58	ArkScaleProjective::decode(&mut &buf[..]).map_err(|_| ()).map(|v| v.0)
59}
60
61#[inline(always)]
62pub fn encode_proj_te<T: TECurveConfig>(val: &TEProjective<T>) -> Vec<u8> {
63	ArkScaleProjective::from(val).encode()
64}
65
66#[inline(always)]
67pub fn decode_proj_te<T: TECurveConfig>(buf: Vec<u8>) -> Result<TEProjective<T>, ()> {
68	ArkScaleProjective::decode(&mut &buf[..]).map_err(|_| ()).map(|v| v.0)
69}
70
71#[allow(unused)]
72pub fn multi_miller_loop<T: Pairing>(g1: Vec<u8>, g2: Vec<u8>) -> Result<Vec<u8>, ()> {
73	let g1 = decode::<Vec<<T as Pairing>::G1Affine>>(g1)?;
74	let g2 = decode::<Vec<<T as Pairing>::G2Affine>>(g2)?;
75	let res = T::multi_miller_loop(g1, g2);
76	Ok(encode(res.0))
77}
78
79#[allow(unused)]
80pub fn final_exponentiation<T: Pairing>(target: Vec<u8>) -> Result<Vec<u8>, ()> {
81	let target = decode::<<T as Pairing>::TargetField>(target)?;
82	let res = T::final_exponentiation(MillerLoopOutput(target)).ok_or(())?;
83	Ok(encode(res.0))
84}
85
86#[allow(unused)]
87pub fn msm_sw<T: SWCurveConfig>(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
88	let bases = decode::<Vec<SWAffine<T>>>(bases)?;
89	let scalars = decode::<Vec<<T as CurveConfig>::ScalarField>>(scalars)?;
90	let res = <SWProjective<T> as VariableBaseMSM>::msm(&bases, &scalars).map_err(|_| ())?;
91	Ok(encode_proj_sw(&res))
92}
93
94#[allow(unused)]
95pub fn msm_te<T: TECurveConfig>(bases: Vec<u8>, scalars: Vec<u8>) -> Result<Vec<u8>, ()> {
96	let bases = decode::<Vec<TEAffine<T>>>(bases)?;
97	let scalars = decode::<Vec<<T as CurveConfig>::ScalarField>>(scalars)?;
98	let res = <TEProjective<T> as VariableBaseMSM>::msm(&bases, &scalars).map_err(|_| ())?;
99	Ok(encode_proj_te(&res))
100}
101
102#[allow(unused)]
103pub fn mul_projective_sw<T: SWCurveConfig>(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
104	let base = decode_proj_sw::<T>(base)?;
105	let scalar = decode::<Vec<u64>>(scalar)?;
106	let res = <T as SWCurveConfig>::mul_projective(&base, &scalar);
107	Ok(encode_proj_sw(&res))
108}
109
110#[allow(unused)]
111pub fn mul_projective_te<T: TECurveConfig>(base: Vec<u8>, scalar: Vec<u8>) -> Result<Vec<u8>, ()> {
112	let base = decode_proj_te::<T>(base)?;
113	let scalar = decode::<Vec<u64>>(scalar)?;
114	let res = <T as TECurveConfig>::mul_projective(&base, &scalar);
115	Ok(encode_proj_te(&res))
116}