Skip to content

Commit

Permalink
adding bandersnatch host function and application crypto impls
Browse files Browse the repository at this point in the history
  • Loading branch information
coax1d committed Nov 22, 2024
1 parent c4dfeb7 commit 2e545b7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
22 changes: 14 additions & 8 deletions substrate/primitives/application-crypto/src/bandersnatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use crate::{KeyTypeId, RuntimePublic};
use alloc::vec::Vec;
pub use sp_core::bandersnatch::*;

use sp_core::crypto::ProofOfPossessionVerifier;
use sp_core::{
crypto::{ProofOfPossessionVerifier, POP_CONTEXT_TAG},
Pair as TraitPair,
};

mod app {
crate::app_crypto!(super, sp_core::testing::BANDERSNATCH);
Expand All @@ -43,19 +46,22 @@ impl RuntimePublic for Public {
sp_io::crypto::bandersnatch_generate(key_type, seed)
}

/// Dummy implementation. Returns `None`.
fn sign<M: AsRef<[u8]>>(&self, _key_type: KeyTypeId, _msg: &M) -> Option<Self::Signature> {
None
fn sign<M: AsRef<[u8]>>(&self, key_type: KeyTypeId, msg: &M) -> Option<Self::Signature> {
sp_io::crypto::bandersnatch_sign(key_type, self, msg.as_ref())
}

/// Dummy implementation. Returns `false`.
fn verify<M: AsRef<[u8]>>(&self, _msg: &M, _signature: &Self::Signature) -> bool {
false
fn verify<M: AsRef<[u8]>>(&self, msg: &M, signature: &Self::Signature) -> bool {
let sig = AppSignature::from(signature.clone());
let pub_key = AppPublic::from(self.clone());
AppPair::verify(&sig, msg.as_ref(), &pub_key)
}

/// Dummy implementation. Returns 'None'.
fn generate_pop(&mut self, _key_type: KeyTypeId) -> Option<Self::Signature> {
None
fn generate_pop(&mut self, key_type: KeyTypeId) -> Option<Self::Signature> {
let pub_key_as_bytes = self.to_raw_vec();
let pop_statement = [POP_CONTEXT_TAG, pub_key_as_bytes.as_slice()].concat();
sp_io::crypto::bandersnatch_sign(key_type, self, pop_statement.as_slice())
}

fn verify_pop(&self, pop: &Self::Signature) -> bool {
Expand Down
10 changes: 5 additions & 5 deletions substrate/primitives/core/src/bandersnatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
#[cfg(feature = "full_crypto")]
use crate::crypto::VrfSecret;
use crate::crypto::{
ByteArray, CryptoType, CryptoTypeId, DeriveError, DeriveJunction, Pair as TraitPair,
ProofOfPossessionGenerator, ProofOfPossessionVerifier, PublicBytes, SecretStringError,
SignatureBytes, UncheckedFrom, VrfPublic,
ByteArray, CryptoType, CryptoTypeId, DeriveError, DeriveJunction, NonAggregatable,
Pair as TraitPair, PublicBytes, SecretStringError, SignatureBytes, UncheckedFrom, VrfPublic,
};
use sp_crypto_pubkeycrypto_proc_macro::ProofOfPossession;

use bandersnatch_vrfs::{CanonicalSerialize, SecretKey};
use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
Expand Down Expand Up @@ -77,7 +75,7 @@ impl CryptoType for Signature {
type Seed = [u8; SEED_SERIALIZED_SIZE];

/// Bandersnatch secret key.
#[derive(Clone, ProofOfPossession)]
#[derive(Clone)]
pub struct Pair {
secret: SecretKey,
seed: Seed,
Expand Down Expand Up @@ -169,6 +167,8 @@ impl CryptoType for Pair {
type Pair = Pair;
}

impl NonAggregatable for Pair {}

/// Bandersnatch VRF types and operations.
pub mod vrf {
use super::*;
Expand Down
18 changes: 18 additions & 0 deletions substrate/primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,24 @@ pub trait Crypto {
.bandersnatch_generate_new(id, seed)
.expect("`bandernatch_generate` failed")
}

/// Sign the given `msg` with the `bandersnatch` key that corresponds to the given public key
/// and key type in the keystore.
///
/// Returns the signature.
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_sign(
&mut self,
id: KeyTypeId,
pub_key: &bandersnatch::Public,
msg: &[u8],
) -> Option<bandersnatch::Signature> {
self.extension::<KeystoreExt>()
.expect("No `keystore` associated for the current context!")
.bandersnatch_sign(id, pub_key, msg)
.ok()
.flatten()
}
}

/// Interface that provides functions for hashing with different algorithms.
Expand Down

0 comments on commit 2e545b7

Please sign in to comment.