h2c-rust-ref icon indicating copy to clipboard operation
h2c-rust-ref copied to clipboard

Remove alloc dependancy

Open burdges opened this issue 2 years ago • 0 comments

A priori, hasher code like this should really run without an alloc or std dependency, so in particular Vec should not be used anywhere. It may be unavoidable if the standard is bad of course, but things like an XoF mode clearly never need Vec. There is also a lot of redundant hashing.

An expander trait could look like this for example:

pub trait Expander {
    fn construct_dst_prime(&self) -> ArrayVec::<[u8; { MAX_DST_LENGTH +1 }]>;
    fn expand<const LENGTH: usize>(&self, msg: &[u8]) -> [u8; LENGTH];
}

As this trait is internal, construct_dst_prime should really be some setup method, thus avoiding the AtomicRefCell, so maybe:

pub trait Expander {
    fn set_dst(&mut self, dst: &[u8]);
    fn expand<const LENGTH: usize>(&self, msg: &[u8]) -> [u8; LENGTH];
}

pub(super) struct ExpanderXof<T: Update + Clone + ExtendableOutput> {
    pub(super) xofer: T,
    pub(super) dst_prime: ArrayVec::<[u8; { MAX_DST_LENGTH +1 }]>,
    pub(super) k: usize,
}

pub(super) struct ExpanderXmd<T: DynDigest + Clone> {
    pub(super) hasher: T,
    pub(super) dst_prime: ArrayVec::<[u8; { MAX_DST_LENGTH +1 }]>,
    pub(super) block_size: usize,
}

Also, MAX_DST_LENGTH = 256 is enforced by the "I2OSP(len(DST), 1)" in the standard, but where does this DST shortening logic? I'm only seeing "ABORT .. if len(DST) > 256" in https://datatracker.ietf.org/doc/draft-irtf-cfrg-hash-to-curve/16/

burdges avatar Feb 12 '23 08:02 burdges