Skip to main content

LengthBounds

Struct LengthBounds 

Source
pub struct LengthBounds;
Expand description

Type that is used to bound the implementations to valid key and output lengths.

We implement SupportsKeyLen and SupportsOutLen for Blake2s<LengthBounds> and Blake2b<LengthBounds> with the appropriate lengths. This can be used by callers to bound their own implementations to only accept valid lengths.

§Example

use libcrux_blake2::{Blake2b, Blake2bBuilder, LengthBounds, SupportsKeyLen, SupportsOutLen};

// A function that does a keyed Blake2b and only accepts valid key lengths
fn keyed_hash<const KEY_LEN: usize>(key: &[u8; KEY_LEN], msg: &[u8]) -> [u8; 32]
where
    Blake2b<LengthBounds>: SupportsKeyLen<KEY_LEN>,
{
    let mut hasher = Blake2bBuilder::new_keyed_const(key)
        .build_const_digest_len::<32>();
    hasher.update(msg);
    let mut output = [0u8; 32];
    hasher.finalize(&mut output);
    output
}

// This compiles because Blake2b supports 32 byte keys
let key = [0; 32]; // this should actually be random
let msg = b"a test message";
let result = keyed_hash(&key, msg);

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Classify for T

Source§

type Classified = T

Source§

fn classify(self) -> T

Source§

impl<T> Declassify for T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.