Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fixed_result_into to prevent copy #86

Closed
bwesterb opened this issue Jul 7, 2019 · 2 comments · Fixed by RustCrypto/traits#180
Closed

Add fixed_result_into to prevent copy #86

bwesterb opened this issue Jul 7, 2019 · 2 comments · Fixed by RustCrypto/traits#180

Comments

@bwesterb
Copy link

bwesterb commented Jul 7, 2019

Rust does not (yet) implement named return value optimisation, so fixed_result forces a copy.

How would you feel about adding a

fn fixed_result_into(mut self, out : &mut GenericArray<u8, Self::OutputSize>)

function to the FixedOutput trait so that the copy can be avoided. I'm happy to create a PR.

(Background: I'm implementing the hash-based signature XMSSMT in rust, where lots of hashes are computed on on very short inputs. These copies have a significant overhead.)

@tarcieri
Copy link
Member

tarcieri commented Jul 7, 2019

It seems like with const generics on the horizon there should (possibly) be a way to do this in terms of FromIterator soon? e.g.

https://internals.rust-lang.org/t/collecting-iterators-into-arrays/10330

@newpavlov
Copy link
Member

newpavlov commented Jul 8, 2019

I was thinking about doing the following re-design in the next digest version:

pub trait FixedOutput {
    type OutputSize: ArrayLength<u8>;

    /// Private method not intended to be used directly
    fn _fixed_result_core(&mut self, out: &mut GenericArray<u8, Self::OutputSize>, reset: bool);

    #[inline]
    fn fixed_result(self) -> GenericArray<u8, Self::OutputSize> {
        let mut buf = Default::default();
        self._fixed_result_core(&mut buf, false);
        buf
    }

    #[inline]
    fn fixed_result_reset(&mut self) -> GenericArray<u8, Self::OutputSize> { .. }
    fn fixed_result_into(self, out: &mut GenericArray<u8, Self::OutputSize>) { .. }
    fn fixed_result_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>) { .. }
}

I think #[inline] should help with needless copies.

tarcieri added a commit to RustCrypto/traits that referenced this issue Jun 9, 2020
Closes RustCrypto/hashes#86

Replaces the `Reset` trait with a set of `_reset` methods on all of the
various traits, and uses these as the default impl for the methods which
consume hashers.

Also adds a set of methods to `FixedOutput` (`finalize_fixed_into*`)
which eliminate copies by accepting an existing buffer as an argument.
tarcieri added a commit to RustCrypto/traits that referenced this issue Jun 9, 2020
Closes RustCrypto/hashes#86

Replaces the `Reset` trait with a set of `_reset` methods on all of the
various traits, and uses these as the default impl for the methods which
consume hashers.

Also adds a set of methods to `FixedOutput` (`finalize_fixed_into*`)
which eliminate copies by accepting an existing buffer as an argument.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants