Repository navigation
Closed
Description
Claimed by @gsgsingh93
Moved from #1:
I'm having some trouble with the types for Grostl. The issue is that the BlockSize depends on the OutputSize, and so I can't figure out to define the impl for Digest. If the Output size is 256 bits or less, then 512 is used as the block size. If the output size is greater than 256 bytes, then 1024 is used as the block size. Here's what I have right now, with the incorrectly hardcoded BlockSize as U512
extern crate digest;
extern crate generic_array;
use std::marker::PhantomData;
use digest::Digest;
use generic_array::{ArrayLength, GenericArray};
use generic_array::typenum::U512;
// TODO: This could also be U1024
type BlockSize = U512;
pub struct Grostl<OutputSize: ArrayLength<u8>> {
phantom: PhantomData<OutputSize>,
}
impl<OutputSize: ArrayLength<u8>> Grostl<OutputSize> {
fn new() -> Grostl<OutputSize> {
Grostl { phantom: PhantomData }
}
}
impl<OutputSize: ArrayLength<u8>> Default for Grostl<OutputSize> {
fn default() -> Self { Self::new() }
}
impl<OutputSize: ArrayLength<u8>> Digest for Grostl<OutputSize> {
type OutputSize = OutputSize;
type BlockSize = BlockSize;
fn input(&mut self, input: &[u8]) {
}
fn result(mut self) -> GenericArray<u8, Self::OutputSize> {
GenericArray::default()
}
}
Note that OutputSize is parameterized here because Grostl can output hashes between 1 and 64 bytes.
EDIT: I guess if there's no fancy solution with the generics that could get this to work, I could always just override block_bytes and block_bits in the trait, and just set a dummy BlockSize and not use it.
Metadata
Metadata
Assignees
Labels
No labels
Activity
newpavlov commentedon Jan 11, 2017
Overriding
block_bytesandblock bitswill not work, asBlockSizeused in theHMACimplementation to create key arrays.Probably the easiest way to implement this will be to define two separate types with appropriate bounds on OutputSize. It will look approximately like this:
Grostlwill be private and user will only have access toGrostlBigandGrostlSmall(and other aliases based on them, e.g.Grostl256,Grostl384,Grostl512, etc.). After that you can implementDigestgenerically overOutputSizeandBlockSizewithout worrying about incorrect combinations.This approach is a bit hacky, but I couldn't get with a better one. Probably with specialization it will be possible to write it in a more general manner, but unfortunately I am not that familiar with it.
gsingh93 commentedon Jan 12, 2017
Thanks, I'll do that. I have some questions though.
T1you meanOutputSize?Cmp<U512>bound?GrostlBighave be bounded by something likeCompare<OutputSize, U513>: Same<Less>as well?gsingh93 commentedon Jan 12, 2017
Another point:
With that code, I'd like to not make the
Grostlstruct public. Then the user is forced to use a type alias with the correctBlockSize; they can't mess that up. But I'm hinting the warning E0446 (rust-lang/rust#34537) about exporting a private type in a public API, which will soon be an error. I'll go ahead an make it public for now, but I'm wondering if you have any ideas on the issue.newpavlov commentedon Jan 12, 2017
Yes, it was leftover from testing code.
It allows us to write condition which uses
Comparetype operator. You can think about it as oftypenum's implementation detail.Yes, forgot to add upper bound.
I believe warning also gives a note with a way around it. You just need to place
Grostlin the different module. I've used the same aproach forStreeboghash function.newpavlov commentedon Jan 22, 2017
Closed in the #10.
Merge pull request RustCrypto#8 from AnarchistHoneybun/no_std