referrerpolicy=no-referrer-when-downgrade

sp_application_crypto/
ecdsa_bls381.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//! ECDSA and BLS12-381 paired crypto applications.
19
20use crate::{KeyTypeId, RuntimePublic};
21use alloc::vec::Vec;
22
23pub use sp_core::paired_crypto::ecdsa_bls381::*;
24use sp_core::{
25	bls381,
26	crypto::CryptoType,
27	ecdsa, ecdsa_bls381,
28	proof_of_possession::{NonAggregatable, ProofOfPossessionVerifier},
29};
30
31mod app {
32	crate::app_crypto!(super, sp_core::testing::ECDSA_BLS381);
33}
34
35#[cfg(feature = "full_crypto")]
36pub use app::Pair as AppPair;
37pub use app::{
38	ProofOfPossession as AppProofOfPossession, Public as AppPublic, Signature as AppSignature,
39};
40
41impl RuntimePublic for Public {
42	type Signature = Signature;
43	type ProofOfPossession = ecdsa_bls381::ProofOfPossession;
44
45	/// Dummy implementation. Returns an empty vector.
46	fn all(_key_type: KeyTypeId) -> Vec<Self> {
47		Vec::new()
48	}
49
50	fn generate_pair(key_type: KeyTypeId, seed: Option<Vec<u8>>) -> Self {
51		sp_io::crypto::ecdsa_bls381_generate(key_type, seed)
52	}
53
54	/// Dummy implementation. Returns `None`.
55	fn sign<M: AsRef<[u8]>>(&self, _key_type: KeyTypeId, _msg: &M) -> Option<Self::Signature> {
56		None
57	}
58
59	/// Dummy implementation. Returns `false`.
60	fn verify<M: AsRef<[u8]>>(&self, _msg: &M, _signature: &Self::Signature) -> bool {
61		false
62	}
63
64	fn generate_proof_of_possession(
65		&mut self,
66		key_type: KeyTypeId,
67		owner: &[u8],
68	) -> Option<Self::ProofOfPossession> {
69		let pub_key_as_bytes = self.to_raw_vec();
70		let (ecdsa_pub_as_bytes, bls381_pub_as_bytes) = split_pub_key_bytes(&pub_key_as_bytes)?;
71		let ecdsa_proof_of_possession =
72			generate_ecdsa_proof_of_possession(key_type, ecdsa_pub_as_bytes, owner)?;
73		let bls381_proof_of_possession =
74			generate_bls381_proof_of_possession(key_type, bls381_pub_as_bytes, owner)?;
75		let combined_proof_of_possession_raw =
76			combine_proof_of_possession(&ecdsa_proof_of_possession, &bls381_proof_of_possession)?;
77		Some(Self::ProofOfPossession::from_raw(combined_proof_of_possession_raw))
78	}
79
80	fn verify_proof_of_possession(
81		&self,
82		owner: &[u8],
83		proof_of_possession: &Self::ProofOfPossession,
84	) -> bool {
85		let pub_key = AppPublic::from(*self);
86		<AppPublic as CryptoType>::Pair::verify_proof_of_possession(
87			owner,
88			&proof_of_possession,
89			&pub_key,
90		)
91	}
92
93	fn to_raw_vec(&self) -> Vec<u8> {
94		sp_core::crypto::ByteArray::to_raw_vec(self)
95	}
96}
97
98/// Helper: Split public key bytes into ECDSA and BLS381 parts
99fn split_pub_key_bytes(
100	pub_key_as_bytes: &[u8],
101) -> Option<([u8; ecdsa::PUBLIC_KEY_SERIALIZED_SIZE], [u8; bls381::PUBLIC_KEY_SERIALIZED_SIZE])> {
102	let ecdsa_pub_as_bytes =
103		pub_key_as_bytes[..ecdsa::PUBLIC_KEY_SERIALIZED_SIZE].try_into().ok()?;
104	let bls381_pub_as_bytes =
105		pub_key_as_bytes[ecdsa::PUBLIC_KEY_SERIALIZED_SIZE..].try_into().ok()?;
106	Some((ecdsa_pub_as_bytes, bls381_pub_as_bytes))
107}
108
109/// Helper: Generate ECDSA proof of possession
110fn generate_ecdsa_proof_of_possession(
111	key_type: KeyTypeId,
112	ecdsa_pub_as_bytes: [u8; ecdsa::PUBLIC_KEY_SERIALIZED_SIZE],
113	owner: &[u8],
114) -> Option<ecdsa::Signature> {
115	let ecdsa_pub = ecdsa::Public::from_raw(ecdsa_pub_as_bytes);
116	let proof_of_possession_statement = ecdsa::Pair::proof_of_possession_statement(owner);
117	sp_io::crypto::ecdsa_sign(key_type, &ecdsa_pub, &proof_of_possession_statement)
118}
119
120/// Helper: Generate BLS381 proof of possession
121fn generate_bls381_proof_of_possession(
122	key_type: KeyTypeId,
123	bls381_pub_as_bytes: [u8; bls381::PUBLIC_KEY_SERIALIZED_SIZE],
124	owner: &[u8],
125) -> Option<bls381::ProofOfPossession> {
126	let bls381_pub = bls381::Public::from_raw(bls381_pub_as_bytes);
127	sp_io::crypto::bls381_generate_proof_of_possession(key_type, &bls381_pub, owner)
128}
129
130/// Helper: Combine ECDSA and BLS381 proof_of_possessions into a single raw proof_of_possession
131fn combine_proof_of_possession(
132	ecdsa_proof_of_possession: &ecdsa::Signature,
133	bls381_proof_of_possession: &bls381::ProofOfPossession,
134) -> Option<[u8; ecdsa_bls381::POP_LEN]> {
135	let mut combined_proof_of_possession_raw = [0u8; ecdsa_bls381::POP_LEN];
136	combined_proof_of_possession_raw[..ecdsa::SIGNATURE_SERIALIZED_SIZE]
137		.copy_from_slice(ecdsa_proof_of_possession.as_ref());
138	combined_proof_of_possession_raw[ecdsa::SIGNATURE_SERIALIZED_SIZE..]
139		.copy_from_slice(bls381_proof_of_possession.as_ref());
140	Some(combined_proof_of_possession_raw)
141}
142
143#[cfg(test)]
144mod tests {
145	use super::*;
146	use sp_core::{bls381, crypto::Pair, ecdsa};
147
148	/// Helper function to generate test public keys for ECDSA and BLS381
149	fn generate_test_keys(
150	) -> ([u8; ecdsa::PUBLIC_KEY_SERIALIZED_SIZE], [u8; bls381::PUBLIC_KEY_SERIALIZED_SIZE]) {
151		let ecdsa_pair = ecdsa::Pair::generate().0;
152		let bls381_pair = bls381::Pair::generate().0;
153
154		let ecdsa_pub = ecdsa_pair.public();
155		let bls381_pub = bls381_pair.public();
156
157		(ecdsa_pub.to_raw_vec().try_into().unwrap(), bls381_pub.to_raw_vec().try_into().unwrap())
158	}
159
160	#[test]
161	fn test_split_pub_key_bytes() {
162		let (ecdsa_pub, bls381_pub) = generate_test_keys();
163		let mut combined_pub_key = Vec::new();
164		combined_pub_key.extend_from_slice(&ecdsa_pub);
165		combined_pub_key.extend_from_slice(&bls381_pub);
166
167		let result = split_pub_key_bytes(&combined_pub_key).unwrap();
168		assert_eq!(result.0, ecdsa_pub, "ECDSA public key does not match");
169		assert_eq!(result.1, bls381_pub, "BLS381 public key does not match");
170	}
171}