Skip to content

Commit

Permalink
feat(crypto): Add extra types and conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewWestberg committed Oct 3, 2024
1 parent de88df1 commit 7fa8938
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
6 changes: 6 additions & 0 deletions pallas-codec/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,12 @@ impl From<Bytes> for Vec<u8> {
}
}

impl<const N: usize> From<Bytes> for [u8; N] {
fn from(b: Bytes) -> Self {
b.0.as_slice()[..N].try_into().expect("Infallible")
}
}

impl Deref for Bytes {
type Target = Vec<u8>;

Expand Down
20 changes: 13 additions & 7 deletions pallas-crypto/src/vrf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ pub const VRF_PUBLIC_KEY_SIZE: usize = 32;
pub const VRF_SECRET_KEY_SIZE: usize = 32;
pub const VRF_PROOF_HASH_SIZE: usize = 64;

pub type VrfSeedBytes = [u8; VRF_SEED_SIZE];
pub type VrfProofBytes = [u8; VRF_PROOF_SIZE];
pub type VrfPublicKeyBytes = [u8; VRF_PUBLIC_KEY_SIZE];
pub type VrfSecretKeyBytes = [u8; VRF_SECRET_KEY_SIZE];
pub type VrfProofHashBytes = [u8; VRF_PROOF_HASH_SIZE];

// Wrapper for VRF secret key
pub struct VrfSecretKey {
secret_key_03: SecretKey03,
Expand All @@ -35,28 +41,28 @@ pub struct VrfProof {
}

// Create a VrfSecretKey from a slice
impl From<&[u8; VRF_SECRET_KEY_SIZE]> for VrfSecretKey {
fn from(slice: &[u8; VRF_SECRET_KEY_SIZE]) -> Self {
impl From<&VrfSecretKeyBytes> for VrfSecretKey {
fn from(slice: &VrfSecretKeyBytes) -> Self {
VrfSecretKey {
secret_key_03: SecretKey03::from_bytes(slice),
}
}
}

// Create a VrfPublicKey from a slice
impl From<&[u8; VRF_PUBLIC_KEY_SIZE]> for VrfPublicKey {
fn from(slice: &[u8; VRF_PUBLIC_KEY_SIZE]) -> Self {
impl From<&VrfPublicKeyBytes> for VrfPublicKey {
fn from(slice: &VrfPublicKeyBytes) -> Self {
VrfPublicKey {
public_key_03: PublicKey03::from_bytes(slice),
}
}
}

// Create a VrfProof from a slice
impl From<&[u8; VRF_PROOF_SIZE]> for VrfProof {
fn from(slice: &[u8; VRF_PROOF_SIZE]) -> Self {
impl From<&VrfProofBytes> for VrfProof {
fn from(slice: &VrfProofBytes) -> Self {
VrfProof {
proof_03: VrfProof03::from_bytes(slice).unwrap(),
proof_03: VrfProof03::from_bytes(slice).expect("Infallible"),
}
}
}
Expand Down
30 changes: 29 additions & 1 deletion pallas-primitives/src/babbage/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use serde::{Deserialize, Serialize};

use pallas_codec::minicbor::{Decode, Encode};
use pallas_crypto::hash::Hash;
use pallas_crypto::hash::{Hash, Hasher};

use pallas_codec::utils::{Bytes, CborWrap, KeepRaw, KeyValuePairs, MaybeIndefArray, Nullable};

Expand Down Expand Up @@ -313,6 +313,34 @@ impl<'a> From<MintedTransactionBody<'a>> for TransactionBody {
}
}

pub enum VrfDerivation {
Leader,
Nonce,
}

pub fn derive_tagged_vrf_output(
block_vrf_output_bytes: &[u8],
derivation: VrfDerivation,
) -> Vec<u8> {
let mut tagged_vrf: Vec<u8> = match derivation {
VrfDerivation::Leader => vec![0x4C_u8], /* "L" */
VrfDerivation::Nonce => vec![0x4E_u8], /* "N" */
};

tagged_vrf.extend(block_vrf_output_bytes);
Hasher::<256>::hash(&tagged_vrf).to_vec()
}

impl HeaderBody {
pub fn leader_vrf_output(&self) -> Vec<u8> {
derive_tagged_vrf_output(&self.vrf_result.0, VrfDerivation::Leader)
}

pub fn nonce_vrf_output(&self) -> Vec<u8> {
derive_tagged_vrf_output(&self.vrf_result.0, VrfDerivation::Nonce)
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum PseudoTransactionOutput<T> {
Legacy(LegacyTransactionOutput),
Expand Down
14 changes: 3 additions & 11 deletions pallas-traverse/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::ops::Deref;

use pallas_codec::minicbor;
use pallas_crypto::hash::{Hash, Hasher};
use pallas_crypto::hash::Hash;
use pallas_primitives::{alonzo, babbage, byron};

use crate::{wellknown::GenesisValues, Era, Error, MultiEraHeader, OriginalHash};
Expand Down Expand Up @@ -109,11 +109,7 @@ impl<'b> MultiEraHeader<'b> {
match self {
MultiEraHeader::EpochBoundary(_) => Err(Error::InvalidEra(Era::Byron)),
MultiEraHeader::ShelleyCompatible(x) => Ok(x.header_body.leader_vrf.0.to_vec()),
MultiEraHeader::BabbageCompatible(x) => {
let mut leader_tagged_vrf: Vec<u8> = vec![0x4C_u8]; /* "L" */
leader_tagged_vrf.extend(&*x.header_body.vrf_result.0);
Ok(Hasher::<256>::hash(&leader_tagged_vrf).to_vec())
}
MultiEraHeader::BabbageCompatible(x) => Ok(x.header_body.leader_vrf_output()),
MultiEraHeader::Byron(_) => Err(Error::InvalidEra(Era::Byron)),
}
}
Expand All @@ -122,11 +118,7 @@ impl<'b> MultiEraHeader<'b> {
match self {
MultiEraHeader::EpochBoundary(_) => Err(Error::InvalidEra(Era::Byron)),
MultiEraHeader::ShelleyCompatible(x) => Ok(x.header_body.nonce_vrf.0.to_vec()),
MultiEraHeader::BabbageCompatible(x) => {
let mut nonce_tagged_vrf: Vec<u8> = vec![0x4E_u8]; /* "N" */
nonce_tagged_vrf.extend(&*x.header_body.vrf_result.0);
Ok(Hasher::<256>::hash(&nonce_tagged_vrf).to_vec())
}
MultiEraHeader::BabbageCompatible(x) => Ok(x.header_body.nonce_vrf_output()),
MultiEraHeader::Byron(_) => Err(Error::InvalidEra(Era::Byron)),
}
}
Expand Down

0 comments on commit 7fa8938

Please sign in to comment.