multiformats / rust-multihash

multihash implementation in Rust
https://github.com/multiformats/multihash
Other
150 stars 60 forks source link

Add Sha3 SHAKE* #311

Closed DougAnderson444 closed 1 year ago

DougAnderson444 commented 1 year ago

References

Rust-multihash almost got it here: https://github.com/multiformats/rust-multihash/pull/6 JS-multiformats has it here: https://github.com/multiformats/js-sha3/tree/ab7efd0fb1da9458c80977c5252cf941e7fe62eb#usage Crates.io has it available here: https://docs.rs/sha3/0.10.8/sha3/

Drivers

Needed for BLS12-381 hash-to-curve and interop with JS implementers.

vmx commented 1 year ago

The currently released rust-multihash version, is kind of "catch all" for all sorts of hash function. Current master has already split things into smaller pieces. In theory it should be possible to implement other hash functions outside of this crate an integrate it in your own custom code table.

If anyone wants to give it a try, that would be great. If there are then issues, I'm happy to help.

DougAnderson444 commented 1 year ago

I can give that a try, but would that mean I need to also assign a code for Sha3 SHAKE128 and Sha3 SHAKE256? What would that do for interoperability if there are multiple implementations... with potentially different codes?

Also, if I do need to pick a code, should I just keep going with the Sha3 sequence (0x18, 0x19...)

thomaseizinger commented 1 year ago

I can give that a try, but would that mean I need to also assign a code for Sha3 SHAKE128 and Sha3 SHAKE256? What would that do for interoperability if there are multiple implementations... with potentially different codes?

Also, if I do need to pick a code, should I just keep going with the Sha3 sequence (0x18, 0x19...)

Code assignment should happen in the multicodec repository: https://github.com/multiformats/multicodec

DougAnderson444 commented 1 year ago

@thomaseizinger absolutely of course. Plus, they already exist!

https://github.com/multiformats/multicodec/blob/df81972d764f30da4ad32e1e5b778d8b619de477/table.csv#L15-L16

thomaseizinger commented 1 year ago

@thomaseizinger absolutely of course. Plus, they already exist!

multiformats/multicodec@df81972/table.csv#L15-L16

Then all that is needed is an implementation :)

In the simplest case, you can construct a Multihash with the correct code and the hashed digest. See https://github.com/multiformats/rust-multihash/pull/289/files#diff-b3696301e6450e80cb6418ed8cf625a5db0a2cd4acf9e4d2e446b00fef092b72R20 for example.

Note that we have heavily restructured this repository to make the base data structure nearly dependency free. This is not released yet but hopefully soon.

DougAnderson444 commented 1 year ago

Sounds amazing! The goods news is for Shake, no additional dependencies are required we already have sha3. Just need a slightly modified macro, which I've already built 🚀. Happy to request a pull if you want to take on another one of my PRs...

thomaseizinger commented 1 year ago

Sounds amazing! The goods news is for Shake, no additional dependencies are required we already have sha3. Just need a slightly modified macro, which I've already built 🚀. Happy to request a pull if you want to take on another one of my PRs...

Would you like to see it in multihash-codetable? I am ambivalent :)

DougAnderson444 commented 1 year ago

That is what I was originally thinking, yes. But perhaps it's unnecessary. Here's what I've been able to do outside the library without any changes:

https://github.com/DougAnderson444/shake-multihash

The Shake derive macro I wrote is pretty close to derive_rustcrypto_hasher, but the thing about SHAKE is the user is supposed to be able to pick their digest length, so the library needs to export the macro to be able to support that.

Maybe just leave it for now, if the crowds want it integrated badly it's straightforward to upgrade.

vmx commented 1 year ago

@DougAnderson444 oh wow, this is nice! I actually would prefer keeping it outside of multihsah-codetable as it nicely shows, how people can add their own hash functions to the ecosystem. I'll probably keep people pointing to that repo, if anyone requests adding a new hash function.

It's so great to see the library being used as imagined. Thanks!

thomaseizinger commented 1 year ago

@DougAnderson444 Relating to https://github.com/DougAnderson444/shake-multihash/blob/9cbd3b09272f5053d241afa9ea775692048f5bfe/src/lib.rs#L19-L20, in case you didn't know, this also works:

pub const SHAKE_128_HASH_CODE: u64 = 0x18;
thomaseizinger commented 1 year ago

I think we can close this then? :)

DougAnderson444 commented 1 year ago

Yep! Thanks for the feedback gents, this helped a lot.