referrerpolicy=no-referrer-when-downgrade

sp_application_crypto/
bandersnatch.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//! Bandersnatch VRF application crypto types.
19
20use crate::{KeyTypeId, RuntimePublic};
21use alloc::vec::Vec;
22pub use sp_core::bandersnatch::*;
23
24use sp_core::{
25	crypto::CryptoType,
26	proof_of_possession::{NonAggregatable, ProofOfPossessionVerifier},
27	Pair as TraitPair,
28};
29
30mod app {
31	crate::app_crypto!(super, sp_core::testing::BANDERSNATCH);
32}
33
34#[cfg(feature = "full_crypto")]
35pub use app::Pair as AppPair;
36pub use app::{
37	ProofOfPossession as AppProofOfPossession, Public as AppPublic, Signature as AppSignature,
38};
39
40impl RuntimePublic for Public {
41	type Signature = Signature;
42	type ProofOfPossession = Signature;
43
44	/// Dummy implementation. Returns an empty vector.
45	fn all(_key_type: KeyTypeId) -> Vec<Self> {
46		Vec::new()
47	}
48
49	fn generate_pair(key_type: KeyTypeId, seed: Option<Vec<u8>>) -> Self {
50		sp_io::crypto::bandersnatch_generate(key_type, seed)
51	}
52
53	fn sign<M: AsRef<[u8]>>(&self, key_type: KeyTypeId, msg: &M) -> Option<Self::Signature> {
54		sp_io::crypto::bandersnatch_sign(key_type, self, msg.as_ref())
55	}
56
57	fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
58		let sig = AppSignature::from(*signature);
59		let pub_key = AppPublic::from(*self);
60		<AppPublic as CryptoType>::Pair::verify(&sig, msg.as_ref(), &pub_key)
61	}
62
63	fn generate_proof_of_possession(
64		&mut self,
65		key_type: KeyTypeId,
66		owner: &[u8],
67	) -> Option<Self::ProofOfPossession> {
68		let proof_of_possession_statement = Pair::proof_of_possession_statement(owner);
69		sp_io::crypto::bandersnatch_sign(key_type, self, &proof_of_possession_statement)
70	}
71
72	fn verify_proof_of_possession(
73		&self,
74		owner: &[u8],
75		proof_of_possession: &Self::Signature,
76	) -> bool {
77		let pub_key = AppPublic::from(*self);
78		<AppPublic as CryptoType>::Pair::verify_proof_of_possession(
79			owner,
80			&proof_of_possession,
81			&pub_key,
82		)
83	}
84
85	fn to_raw_vec(&self) -> Vec<u8> {
86		sp_core::crypto::ByteArray::to_raw_vec(self)
87	}
88}