Skip to content

Commit ee0776f

Browse files
committed
newtypes support
1 parent 137cda7 commit ee0776f

File tree

22 files changed

+179
-139
lines changed

22 files changed

+179
-139
lines changed

Cargo.lock

+5-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,7 @@ members = [
2727

2828
[profile.dev]
2929
opt-level = 2
30+
31+
[patch.crates-io]
32+
#digest = { path = "../traits/digest" }
33+
digest = { git = "https://github.com/baloo/traits.git", branch = "baloo/digest/new-type" }

ascon-hash/src/lib.rs

+61-48
Original file line numberDiff line numberDiff line change
@@ -381,55 +381,68 @@ impl AlgorithmName for AsconAXofCore {
381381
}
382382
}
383383

384-
/// Ascon hash
385-
///
386-
/// ```
387-
/// use ascon_hash::{AsconHash, Digest};
388-
///
389-
/// let mut hasher = AsconHash::new();
390-
/// hasher.update(b"some bytes");
391-
/// let digest = hasher.finalize();
392-
/// assert_eq!(&digest[..], b"\xb7\x42\xca\x75\xe5\x70\x38\x75\x70\x59\xcc\xcc\x68\x74\x71\x4f\x9d\xbd\x7f\xc5\x92\x4a\x7d\xf4\xe3\x16\x59\x4f\xd1\x42\x6c\xa8");
393-
/// ```
394-
pub type AsconHash = CoreWrapper<AsconCore>;
395-
/// AsconA hash
396-
///
397-
/// ```
398-
/// use ascon_hash::{AsconAHash, Digest};
399-
///
400-
/// let mut hasher = AsconAHash::new();
401-
/// hasher.update(b"some bytes");
402-
/// let digest = hasher.finalize();
403-
/// assert_eq!(&digest[..], b"\x1d\x1a\xc8\x74\x4a\x4a\x05\x81\x33\x7d\x5a\xf2\x78\xc2\x55\x88\xe1\xa3\xdd\x2d\x86\x73\x07\x64\x26\x53\xdc\xa4\x45\xf5\x5c\x2a");
404-
/// ```
405-
pub type AsconAHash = CoreWrapper<AsconACore>;
406-
/// AsconXof
407-
///
408-
/// ```
409-
/// use ascon_hash::{AsconXof, ExtendableOutput, Update, XofReader};
410-
///
411-
/// let mut xof = AsconXof::default();
412-
/// xof.update(b"some bytes");
413-
/// let mut reader = xof.finalize_xof();
414-
/// let mut dst = [0u8; 5];
415-
/// reader.read(&mut dst);
416-
/// assert_eq!(&dst, b"\xc2\x19\x72\xfd\xe9");
417-
/// ```
418-
pub type AsconXof = CoreWrapper<AsconXofCore>;
384+
digest::newtype!(
385+
r#"Ascon hash
386+
387+
```
388+
use ascon_hash::{AsconHash, Digest};
389+
390+
let mut hasher = AsconHash::new();
391+
hasher.update(b"some bytes");
392+
let digest = hasher.finalize();
393+
assert_eq!(&digest[..], b"\xb7\x42\xca\x75\xe5\x70\x38\x75\x70\x59\xcc\xcc\x68\x74\x71\x4f\x9d\xbd\x7f\xc5\x92\x4a\x7d\xf4\xe3\x16\x59\x4f\xd1\x42\x6c\xa8");
394+
```"#,
395+
AsconHash = CoreWrapper<AsconCore>
396+
);
397+
398+
digest::newtype!(
399+
r#"AsconA hash
400+
401+
```
402+
use ascon_hash::{AsconAHash, Digest};
403+
404+
let mut hasher = AsconAHash::new();
405+
hasher.update(b"some bytes");
406+
let digest = hasher.finalize();
407+
assert_eq!(&digest[..], b"\x1d\x1a\xc8\x74\x4a\x4a\x05\x81\x33\x7d\x5a\xf2\x78\xc2\x55\x88\xe1\xa3\xdd\x2d\x86\x73\x07\x64\x26\x53\xdc\xa4\x45\xf5\x5c\x2a");
408+
```"#,
409+
AsconAHash = CoreWrapper<AsconACore>
410+
);
411+
412+
digest::newtype!(
413+
r#"AsconXof
414+
415+
```
416+
use ascon_hash::{AsconXof, ExtendableOutput, Update, XofReader};
417+
418+
let mut xof = AsconXof::default();
419+
xof.update(b"some bytes");
420+
let mut reader = xof.finalize_xof();
421+
let mut dst = [0u8; 5];
422+
reader.read(&mut dst);
423+
assert_eq!(&dst, b"\xc2\x19\x72\xfd\xe9");
424+
```"#,
425+
AsconXof = CoreWrapper<AsconXofCore>
426+
);
427+
419428
/// Reader for AsconXof output
420429
pub type AsconXofReader = XofReaderCoreWrapper<AsconXofReaderCore>;
421-
/// AsconAXof
422-
///
423-
/// ```
424-
/// use ascon_hash::{AsconAXof, ExtendableOutput, Update, XofReader};
425-
///
426-
/// let mut xof = AsconAXof::default();
427-
/// xof.update(b"some bytes");
428-
/// let mut reader = xof.finalize_xof();
429-
/// let mut dst = [0u8; 5];
430-
/// reader.read(&mut dst);
431-
/// assert_eq!(&dst, b"\xb8\xd6\xbd\xf0\xa7");
432-
/// ```
433-
pub type AsconAXof = CoreWrapper<AsconAXofCore>;
430+
431+
digest::newtype!(
432+
r#"AsconAXof
433+
434+
```
435+
use ascon_hash::{AsconAXof, ExtendableOutput, Update, XofReader};
436+
437+
let mut xof = AsconAXof::default();
438+
xof.update(b"some bytes");
439+
let mut reader = xof.finalize_xof();
440+
let mut dst = [0u8; 5];
441+
reader.read(&mut dst);
442+
assert_eq!(&dst, b"\xb8\xd6\xbd\xf0\xa7");
443+
```"#,
444+
AsconAXof = CoreWrapper<AsconAXofCore>
445+
);
446+
434447
/// Reader for AsconAXof output
435448
pub type AsconAXofReader = XofReaderCoreWrapper<AsconAXofReaderCore>;

belt-hash/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ pub struct BeltHashCore {
4040
h: [u32; 8],
4141
}
4242

43-
/// BelT hasher state.
44-
pub type BeltHash = CoreWrapper<BeltHashCore>;
43+
digest::newtype!("BelT hasher hasher", BeltHash = CoreWrapper<BeltHashCore>);
4544

4645
impl BeltHashCore {
4746
fn compress_block(&mut self, block: &Block<Self>) {

blake2/src/lib.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,10 @@ pub type Blake2bVar = RtVariableCoreWrapper<Blake2bVarCore>;
6969
pub type Blake2bCore<OutSize> = CtVariableCoreWrapper<Blake2bVarCore, OutSize>;
7070
/// BLAKE2b generic over output size.
7171
pub type Blake2b<OutSize> = CoreWrapper<Blake2bCore<OutSize>>;
72-
/// BLAKE2b-128 hasher state.
73-
pub type Blake2b128 = Blake2b<U16>;
74-
/// BLAKE2b-256 hasher state.
75-
pub type Blake2b256 = Blake2b<U32>;
76-
/// BLAKE2b-512 hasher state.
77-
pub type Blake2b512 = Blake2b<U64>;
72+
73+
digest::newtype!("BLAKE2b-128 hasher state.", Blake2b128 = Blake2b<U16>);
74+
digest::newtype!("BLAKE2b-256 hasher state.", Blake2b256 = Blake2b<U32>);
75+
digest::newtype!("BLAKE2b-512 hasher state.", Blake2b512 = Blake2b<U64>);
7876

7977
blake2_mac_impl!(Blake2bMac, Blake2bVarCore, U64, "Blake2b MAC function");
8078

fsb/src/macros.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ macro_rules! fsb_impl {
1212
state: [u8; $r / 8],
1313
}
1414

15-
#[doc=$full_doc]
16-
pub type $full_state = CoreWrapper<$state>;
15+
digest::newtype!($full_doc, $full_state = CoreWrapper<$state>);
1716

1817
impl HashMarker for $state {}
1918

gost94/src/lib.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ impl AssociatedOid for Gost94Core<params::GOST28147UAParam> {
3434
const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.804.2.1.1.1.1.2.1");
3535
}
3636

37-
/// GOST94 hash function with CryptoPro parameters.
38-
pub type Gost94CryptoPro = CoreWrapper<Gost94Core<params::CryptoProParam>>;
39-
/// GOST94 hash function with S-box defined in GOST R 34.12-2015.
40-
pub type Gost94s2015 = CoreWrapper<Gost94Core<params::S2015Param>>;
41-
/// GOST94 hash function with test parameters.
42-
pub type Gost94Test = CoreWrapper<Gost94Core<params::TestParam>>;
43-
/// GOST94 hash function with UAPKI GOST 34.311-95 parameters
44-
/// (1.2.804.2.1.1.1.1.2.1 OID).
45-
pub type Gost94UA = CoreWrapper<Gost94Core<params::GOST28147UAParam>>;
37+
digest::newtype!("GOST94 hash function with CryptoPro parameters.", Gost94CryptoPro = CoreWrapper<Gost94Core<params::CryptoProParam>>);
38+
39+
digest::newtype!("GOST94 hash function with S-box defined in GOST R 34.12-2015.", Gost94s2015 = CoreWrapper<Gost94Core<params::S2015Param>>);
40+
41+
digest::newtype!("GOST94 hash function with test parameters.", Gost94Test = CoreWrapper<Gost94Core<params::TestParam>>);
42+
43+
digest::newtype!(
44+
r#"GOST94 hash function with UAPKI GOST 34.311-95 parameters
45+
(1.2.804.2.1.1.1.1.2.1 OID)."#,
46+
Gost94UA = CoreWrapper<Gost94Core<params::GOST28147UAParam>>
47+
);

groestl/src/lib.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@ pub type GroestlShortCore<OutSize> = CtVariableCoreWrapper<GroestlShortVarCore,
4444
/// Hasher state of the short Groestl variant generic over output size.
4545
pub type GroestlShort<OutSize> = CoreWrapper<GroestlShortCore<OutSize>>;
4646

47-
/// Groestl-224 hasher state.
48-
pub type Groestl224 = CoreWrapper<GroestlShortCore<U28>>;
49-
/// Groestl-256 hasher state.
50-
pub type Groestl256 = CoreWrapper<GroestlShortCore<U32>>;
47+
digest::newtype!("Groestl-224 hasher state.", Groestl224 = CoreWrapper<GroestlShortCore<U28>>);
48+
digest::newtype!("Groestl-256 hasher state.", Groestl256 = CoreWrapper<GroestlShortCore<U32>>);
5149

5250
impl HashMarker for GroestlShortVarCore {}
5351

@@ -175,10 +173,8 @@ pub type GroestlLongCore<OutSize> = CtVariableCoreWrapper<GroestlLongVarCore, Ou
175173
/// Hasher state of the long Groestl variant generic over output size.
176174
pub type GroestlLong<OutSize> = CoreWrapper<GroestlLongCore<OutSize>>;
177175

178-
/// Groestl-384 hasher state.
179-
pub type Groestl384 = CoreWrapper<GroestlLongCore<U48>>;
180-
/// Groestl-512 hasher state.
181-
pub type Groestl512 = CoreWrapper<GroestlLongCore<U64>>;
176+
digest::newtype!("Groestl-384 hasher state.", Groestl384 = CoreWrapper<GroestlLongCore<U48>>);
177+
digest::newtype!("Groestl-512 hasher state.", Groestl512 = CoreWrapper<GroestlLongCore<U64>>);
182178

183179
impl HashMarker for GroestlLongVarCore {}
184180

jh/src/lib.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,22 @@ pub struct JhCore {
3838
block_len: u64,
3939
}
4040

41-
/// Jh-224 hasher state
42-
pub type Jh224 = CoreWrapper<CtVariableCoreWrapper<JhCore, U28>>;
43-
/// Jh-256 hasher state
44-
pub type Jh256 = CoreWrapper<CtVariableCoreWrapper<JhCore, U32>>;
45-
/// Jh-384 hasher state
46-
pub type Jh384 = CoreWrapper<CtVariableCoreWrapper<JhCore, U48>>;
47-
/// Jh-512 hasher state
48-
pub type Jh512 = CoreWrapper<CtVariableCoreWrapper<JhCore, U64>>;
41+
digest::newtype!(
42+
"Jh-224 hasher state",
43+
Jh224 = CoreWrapper<CtVariableCoreWrapper<JhCore, U28>>
44+
);
45+
digest::newtype!(
46+
"Jh-256 hasher state",
47+
Jh256 = CoreWrapper<CtVariableCoreWrapper<JhCore, U32>>
48+
);
49+
digest::newtype!(
50+
"Jh-384 hasher state",
51+
Jh384 = CoreWrapper<CtVariableCoreWrapper<JhCore, U48>>
52+
);
53+
digest::newtype!(
54+
"Jh-512 hasher state",
55+
Jh512 = CoreWrapper<CtVariableCoreWrapper<JhCore, U64>>
56+
);
4957

5058
impl HashMarker for JhCore {}
5159

md2/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ pub struct Md2Core {
3737
checksum: Block<Self>,
3838
}
3939

40-
/// MD2 hasher state.
41-
pub type Md2 = CoreWrapper<Md2Core>;
40+
digest::newtype!("MD2 hasher state.", Md2 = CoreWrapper<Md2Core>);
4241

4342
const STATE_LEN: usize = 48;
4443

md4/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ pub struct Md4Core {
4444
state: [Wu32; STATE_LEN],
4545
}
4646

47-
/// MD4 hasher state
48-
pub type Md4 = CoreWrapper<Md4Core>;
47+
digest::newtype!("MD4 hasher state", Md4 = CoreWrapper<Md4Core>);
4948

5049
const STATE_LEN: usize = 4;
5150

md5/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ pub struct Md5Core {
3737
state: [u32; STATE_LEN],
3838
}
3939

40-
/// MD5 hasher state.
41-
pub type Md5 = CoreWrapper<Md5Core>;
40+
digest::newtype!("MD5 hasher", Md5 = CoreWrapper<Md5Core>);
4241

4342
const STATE_LEN: usize = 4;
4443

ripemd/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ macro_rules! impl_ripemd {
4646
block_len: u64,
4747
}
4848

49-
#[doc = $doc_name]
50-
#[doc = " hasher."]
51-
pub type $wrapped_name = CoreWrapper<$name>;
49+
digest::newtype!(
50+
concat!($doc_name, " hasher state."),
51+
$wrapped_name = CoreWrapper<$name>
52+
);
5253

5354
impl HashMarker for $name {}
5455

sha1/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ pub struct Sha1Core {
4444
block_len: u64,
4545
}
4646

47-
/// SHA-1 hasher state.
48-
pub type Sha1 = CoreWrapper<Sha1Core>;
47+
digest::newtype!("SHA-1 hasher", Sha1 = CoreWrapper<Sha1Core>);
4948

5049
impl HashMarker for Sha1Core {}
5150

sha2/src/lib.rs

+29-12
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,32 @@ impl_oid_carrier!(OidSha224, "2.16.840.1.101.3.4.2.4");
4646
impl_oid_carrier!(OidSha512_224, "2.16.840.1.101.3.4.2.5");
4747
impl_oid_carrier!(OidSha512_256, "2.16.840.1.101.3.4.2.6");
4848

49-
/// SHA-224 hasher.
50-
pub type Sha224 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U28, OidSha224>>;
51-
/// SHA-256 hasher.
52-
pub type Sha256 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U32, OidSha256>>;
53-
/// SHA-512/224 hasher.
54-
pub type Sha512_224 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U28, OidSha512_224>>;
55-
/// SHA-512/256 hasher.
56-
pub type Sha512_256 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U32, OidSha512_256>>;
57-
/// SHA-384 hasher.
58-
pub type Sha384 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U48, OidSha384>>;
59-
/// SHA-512 hasher.
60-
pub type Sha512 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U64, OidSha512>>;
49+
digest::newtype!(
50+
"SHA-224 hasher",
51+
Sha224 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U28, OidSha224>>
52+
);
53+
54+
digest::newtype!(
55+
"SHA-256 hasher",
56+
Sha256 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U32, OidSha256>>
57+
);
58+
59+
digest::newtype!(
60+
"SHA-512/224 hasher",
61+
Sha512_224 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U28, OidSha512_224>>
62+
);
63+
64+
digest::newtype!(
65+
"SHA-512/256 hasher",
66+
Sha512_256 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U32, OidSha512_256>>
67+
);
68+
69+
digest::newtype!(
70+
"SHA-384 hasher",
71+
Sha384 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U48, OidSha384>>
72+
);
73+
74+
digest::newtype!(
75+
"SHA-512 hasher",
76+
Sha512 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U64, OidSha512>>
77+
);

sha3/src/macros.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -464,9 +464,10 @@ macro_rules! impl_cshake {
464464
padding: u8,
465465
}
466466

467-
#[doc = $alg_name]
468-
#[doc = " hasher state."]
469-
pub type $full_name = CoreWrapper<$name>;
467+
digest::newtype!(
468+
concat!($alg_name, " hasher state."),
469+
$full_name = CoreWrapper<$name>
470+
);
470471

471472
impl $name {
472473
/// Creates a new CSHAKE instance with the given function name and customization.

0 commit comments

Comments
 (0)