subsoil 0.2.0

Soil primitives foundation crate
Documentation
// This file is part of Soil.

// Copyright (C) Soil contributors.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0

//! Bandersnatch VRF application crypto types.

use super::{KeyTypeId, RuntimePublic};
pub use crate::core::bandersnatch::*;
use alloc::vec::Vec;

use crate::core::{
	crypto::CryptoType,
	proof_of_possession::{NonAggregatable, ProofOfPossessionVerifier},
	Pair as TraitPair,
};

mod app {
	crate::app_crypto!(super, crate::core::testing::BANDERSNATCH);
}

#[cfg(feature = "full_crypto")]
pub use app::Pair as AppPair;
pub use app::{
	ProofOfPossession as AppProofOfPossession, Public as AppPublic, Signature as AppSignature,
};

impl RuntimePublic for Public {
	type Signature = Signature;
	type ProofOfPossession = Signature;

	/// Dummy implementation. Returns an empty vector.
	fn all(_key_type: KeyTypeId) -> Vec<Self> {
		Vec::new()
	}

	fn generate_pair(key_type: KeyTypeId, seed: Option<Vec<u8>>) -> Self {
		crate::io::crypto::bandersnatch_generate(key_type, seed)
	}

	fn sign<M: AsRef<[u8]>>(&self, key_type: KeyTypeId, msg: &M) -> Option<Self::Signature> {
		crate::io::crypto::bandersnatch_sign(key_type, self, msg.as_ref())
	}

	fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
		let sig = AppSignature::from(*signature);
		let pub_key = AppPublic::from(*self);
		<AppPublic as CryptoType>::Pair::verify(&sig, msg.as_ref(), &pub_key)
	}

	fn generate_proof_of_possession(
		&mut self,
		key_type: KeyTypeId,
		owner: &[u8],
	) -> Option<Self::ProofOfPossession> {
		let proof_of_possession_statement = Pair::proof_of_possession_statement(owner);
		crate::io::crypto::bandersnatch_sign(key_type, self, &proof_of_possession_statement)
	}

	fn verify_proof_of_possession(
		&self,
		owner: &[u8],
		proof_of_possession: &Self::Signature,
	) -> bool {
		let pub_key = AppPublic::from(*self);
		<AppPublic as CryptoType>::Pair::verify_proof_of_possession(
			owner,
			&proof_of_possession,
			&pub_key,
		)
	}

	fn to_raw_vec(&self) -> Vec<u8> {
		crate::core::crypto::ByteArray::to_raw_vec(self)
	}
}