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

Hashing Structures helpers #86

Closed
gakonst opened this issue Feb 28, 2020 · 0 comments · Fixed by #265
Closed

Hashing Structures helpers #86

gakonst opened this issue Feb 28, 2020 · 0 comments · Fixed by #265
Labels
D-easy Difficulty: easy T-feature Type: new features

Comments

@gakonst
Copy link

gakonst commented Feb 28, 2020

Depends on arkworks-rs/snark#104

It'd be nice to have a helper method as an extension trait over CanonicalSerialize which calls a cryptographically secure hash function (maybe make it take a Digest?) on the serialized input, and returns the fixed size hash of the provided data.

It'd be even better if it would also be available on structs composing such elements.

Example:

use blake2::Blake2b;
use generic_array::GenericArray;
use zexe::algebra::*;
use digest::Digest;

trait Hasher: CanonicalSerialize {
   fn hash<H: Digest>(&self) -> GenericArray<u8, H::OutputSize> {
       let buffer = self.serialize();
       let mut hasher = H::new();
       hasher.input(buffer);
       hasher.result()
   }
}

struct X<E: PairingEngine> {
    a: E::G1Affine,
    b: E::G2Affine,
    c: Vec<E::G1Affine>,
}

x = X::<E> {
  a: E::G1Affine::zero(),
  b: E::G2Affine::zero(),
  c: vec![],
}
let hash = x.hash::<Blake2b>();

This would serialize all elements (compressed or uncompressed? i don't think it matters) to a buffer, and then take the hash of the buffer.

It becomes a bit trickier I believe if the struct ends up having non-CanonicalSerialize elements.

e.g.

struct X<E: PairingEngine> {
    a: E::G1Affine,
    b: E::G2Affine,
    c: Vec<E::G1Affine>,
    transcript_hash: [u8; 64],
} 
@Pratyush Pratyush transferred this issue from arkworks-rs/snark Nov 20, 2020
@Pratyush Pratyush added T-feature Type: new features D-easy Difficulty: easy labels Nov 20, 2020
huitseeker added a commit to huitseeker/algebra that referenced this issue Apr 21, 2021
…lize

- this saves an alllocation w.r.t the suggested approach by implementing io::Write on the input instance of digest::Digest,
- note that most instances of digest::Digest [already](https://gist.github.com/huitseeker/e827161413063e347ce5a496b66ff287) have an [io::Write instance](https://github.com/rustcrypto/hashes#hashing-readable-objects), but CanonicalSerialize consuming its io::Write argument prevents its usage,
- this hence implements io::Write on a [cheap newtype wrapper](https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html)

Fixes arkworks-rs#86
huitseeker added a commit to huitseeker/algebra that referenced this issue Apr 21, 2021
…alize`

- this saves an alllocation w.r.t the suggested approach by implementing `io::Write` on the input instance of `digest::Digest`,
- note that most instances of `digest::Digest` [already](https://gist.github.com/huitseeker/e827161413063e347ce5a496b66ff287) have an [`io::Write` instance](https://github.com/rustcrypto/hashes#hashing-readable-objects), but `CanonicalSerialize` consuming its `io::Write` argument prevents its usage,
- this hence implements `io::Write` on a [cheap newtype wrapper](https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html)

Fixes arkworks-rs#86
huitseeker added a commit to huitseeker/algebra that referenced this issue Apr 21, 2021
…alize`

- this saves an alllocation w.r.t the suggested approach by implementing `io::Write` on the input instance of `digest::Digest`,
- note that most instances of `digest::Digest` [already](https://gist.github.com/huitseeker/e827161413063e347ce5a496b66ff287) have an [`io::Write` instance](https://github.com/rustcrypto/hashes#hashing-readable-objects), but `CanonicalSerialize` consuming its `io::Write` argument prevents its usage,
- this hence implements `io::Write` on a [cheap newtype wrapper](https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html)

Fixes arkworks-rs#86
huitseeker added a commit to huitseeker/algebra that referenced this issue Apr 21, 2021
…alize`

- this saves an alllocation w.r.t the suggested approach by implementing `io::Write` on the input instance of `digest::Digest`,
- note that most instances of `digest::Digest` [already](https://gist.github.com/huitseeker/e827161413063e347ce5a496b66ff287) have an [`io::Write` instance](https://github.com/rustcrypto/hashes#hashing-readable-objects), but `CanonicalSerialize` consuming its `io::Write` argument prevents its usage,
- this hence implements `io::Write` on a [cheap newtype wrapper](https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html)

Fixes arkworks-rs#86
huitseeker added a commit to huitseeker/algebra that referenced this issue Apr 21, 2021
…alize`

- this saves an alllocation w.r.t the suggested approach by implementing `io::Write` on the input instance of `digest::Digest`,
- note that most instances of `digest::Digest` [already](https://gist.github.com/huitseeker/e827161413063e347ce5a496b66ff287) have an [`io::Write` instance](https://github.com/rustcrypto/hashes#hashing-readable-objects), but `CanonicalSerialize` consuming its `io::Write` argument prevents its usage,
- this hence implements `io::Write` on a [cheap newtype wrapper](https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html)

Fixes arkworks-rs#86
weikengchen added a commit that referenced this issue Apr 23, 2021
…lize` (#265)

* Implements hashing as a blanket trait for instances of `CanonicalSerialize`

- this saves an alllocation w.r.t the suggested approach by implementing `io::Write` on the input instance of `digest::Digest`,
- note that most instances of `digest::Digest` [already](https://gist.github.com/huitseeker/e827161413063e347ce5a496b66ff287) have an [`io::Write` instance](https://github.com/rustcrypto/hashes#hashing-readable-objects), but `CanonicalSerialize` consuming its `io::Write` argument prevents its usage,
- this hence implements `io::Write` on a [cheap newtype wrapper](https://rust-unofficial.github.io/patterns/patterns/behavioural/newtype.html)

Fixes #86

* Adjust post review

- rename Hash -> CanonicalSerialize Ext according to [extension trait best practices](https://rust-lang.github.io/rfcs/0445-extension-trait-conventions.html#the-convention)
- add the same structure for hashing uncompressed bytes

* Changelog entries for #263, #265

* Update CHANGELOG.md

Co-authored-by: Weikeng Chen <w.k@berkeley.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
D-easy Difficulty: easy T-feature Type: new features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants