Giter Club home page Giter Club logo

Comments (5)

vmx avatar vmx commented on June 12, 2024 1

Thanks @mriise for a fix and @hummingly for being patient and explaining things in even more depths. It's funny that I even opened an issue about it and didn't recall.

from rust-multihash.

mriise avatar mriise commented on June 12, 2024 1

quick note for transparency, this was a bit more broken than this issue mentions, since the digest returned by blake3 was only 32 bytes, anything bigger would've been filled with 0s

...fun

from rust-multihash.

vmx avatar vmx commented on June 12, 2024

I don't think it would panic, but I might miss something.

So let's say S is 16 and digest_bytes.len() is 32. Then digest_bytes.len().max(S) would be 32.

So Blake3Hasher.finalize() makes sure that the returned value has at least S bytes, but maybe more. Then later, when the hash is used (that mostly happens in the multihash-derive crate), the result goes through Multihash::wrap(), which will error (not panic) in case the has is too long:

if input_digest.len() > S {
return Err(Error::InvalidSize(input_digest.len() as _));
}

from rust-multihash.

hummingly avatar hummingly commented on June 12, 2024

My code looks like that where DIGEST_SIZE is 32 right now.

pub struct ContentHasher(Blake3Hasher<DIGEST_SIZE>);

impl ContentHasher {
    pub fn new() -> ContentHasher {
        ContentHasher(Blake3Hasher::default())
    }

    pub fn write(&mut self, input: &[u8]) {
        self.0.update(input);
    }

    pub fn finish(&mut self) -> ContentHash {
        let hash = multihash::Code::Blake3_256.wrap(self.0.finalize()).unwrap();
        hash.resize::<DIGEST_SIZE>().unwrap()
    }

    pub fn reset(&mut self) {
        self.0.reset();
    }
}

However, if I were to set it to something smaller than the default digest size (32), it will panic. The reason is an out of bounds indexing error due the array digest in the Blake3Hasher struct as I explained in the previous comment.

pub struct Blake3Hasher<const S: usize> {
hasher: ::blake3::Hasher,
digest: [u8; S],
}

So Blake3Hasher.finalize() makes sure that the returned value has at least S bytes, but maybe more. Then later, when the hash is used (that mostly happens in the multihash-derive crate), the result goes through Multihash::wrap(), which will error (not panic) in case the has is too long:

It is the other way around. The length will always be S or smaller because Blake3Hasher.finalize() returns a slice to the array digest in Blake3Hasher.

Do you consider it a user error to use an S smaller than the default size of the hash?

from rust-multihash.

mriise avatar mriise commented on June 12, 2024

Blake3Hasher was originally intended to be flexible like this, but some implementation details had kept me away at the time but has since been fixed. see #130
The Code table being generic over Blake Digest sizes is still something to be solved, either through more proc macro magic or new const generic things.

from rust-multihash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.