mirror of
https://github.com/saymrwulf/fips205-source.git
synced 2026-09-03 19:53:49 +00:00
clippy and test clean
This commit is contained in:
parent
569374a055
commit
69f94c55fe
3 changed files with 125 additions and 137 deletions
20
src/algs.rs
20
src/algs.rs
|
|
@ -39,7 +39,7 @@ pub(crate) fn to_int(x: &[u8], n: u32) -> u64 {
|
|||
///
|
||||
/// Input: Integer `x`, string length `n`. <br>
|
||||
/// Output: Byte string of length `n` containing binary representation of `x` in big-endian byte-order.
|
||||
pub(crate) fn to_byte(x: u16, n: u32) -> [u8; ((crate::LEN2 * crate::LGW + 7) / 8) as usize] {
|
||||
pub(crate) fn to_byte(x: u32, n: u32) -> [u8; ((crate::LEN2 * crate::LGW + 7) / 8) as usize] {
|
||||
let mut s = [0u8; ((crate::LEN2 * crate::LGW + 7) / 8) as usize]; // Size fixed across all profiles (2)
|
||||
debug_assert_eq!(n, ((crate::LEN2 * crate::LGW + 7) / 8)); // just in case life changes
|
||||
debug_assert_eq!(n, 2); // optimize: this resolves into a two-byte (be) write!
|
||||
|
|
@ -68,7 +68,7 @@ pub(crate) fn to_byte(x: u16, n: u32) -> [u8; ((crate::LEN2 * crate::LGW + 7) /
|
|||
/// Algorithm 3: `base_2^b(X, b, out_len)` on page 15.
|
||||
/// Compute the base 2^b representation of X.
|
||||
///
|
||||
/// Input: Byte string `X` of length at least ceil(out_len·b/8), integer `b`, output length `out_len`. <br>
|
||||
/// Input: Byte string `X` of length at least `ceil(out_len·b/8)`, integer `b`, output length `out_len`. <br>
|
||||
/// Output: Array of `out_len` integers in the range `[0, . . . , 2^b − 1]`.
|
||||
pub(crate) fn base_2b(x: &[u8], b: u32, out_len: u32, baseb: &mut [u32]) {
|
||||
debug_assert!(x.len() >= (out_len * b).div_ceil(8) as usize);
|
||||
|
|
@ -260,7 +260,7 @@ pub(crate) fn wots_sign<K: ArrayLength, LEN: ArrayLength, M: ArrayLength, N: Arr
|
|||
|
||||
// 10: msg ← msg ∥ base_2^b(toByte(csum, ceil(len2·lgw/8)), lgw, len2) ▷ Convert csum to base w
|
||||
base_2b(
|
||||
&to_byte(csum as u16, (crate::LEN2 * crate::LGW).div_ceil(8)),
|
||||
&to_byte(csum, (crate::LEN2 * crate::LGW).div_ceil(8)),
|
||||
crate::LGW,
|
||||
crate::LEN2,
|
||||
&mut msg[(2 * N::to_usize())..],
|
||||
|
|
@ -312,7 +312,7 @@ pub(crate) fn wots_pk_from_sig<K: ArrayLength, LEN: ArrayLength, M: ArrayLength,
|
|||
let mut tmp: GenericArray<GenericArray<u8, N>, LEN> = GenericArray::default();
|
||||
|
||||
// 1: csum ← 0
|
||||
let mut csum = 0;
|
||||
let mut csum = 0_u32;
|
||||
|
||||
// 2:
|
||||
// 3: msg ← base_2b (M, lgw , len1 ) ▷ Convert message to base w
|
||||
|
|
@ -324,7 +324,7 @@ pub(crate) fn wots_pk_from_sig<K: ArrayLength, LEN: ArrayLength, M: ArrayLength,
|
|||
for item in msg.iter().take(2 * N::to_usize()) {
|
||||
//
|
||||
// 6: csum ← csum + w − 1 − msg[i]
|
||||
csum += u64::from(crate::W) - 1 - *item as u64;
|
||||
csum += crate::W - 1 - item;
|
||||
|
||||
// 7: end for
|
||||
}
|
||||
|
|
@ -335,7 +335,7 @@ pub(crate) fn wots_pk_from_sig<K: ArrayLength, LEN: ArrayLength, M: ArrayLength,
|
|||
|
||||
// 10: msg ← msg ∥ base_2^b(toByte(csum, ceil(len2·lgw/8)), lgw, len2) ▷ Convert csum to base w
|
||||
base_2b(
|
||||
&to_byte(csum as u16, (crate::LEN2 * crate::LGW).div_ceil(8)),
|
||||
&to_byte(csum, (crate::LEN2 * crate::LGW).div_ceil(8)),
|
||||
crate::LGW,
|
||||
crate::LEN2,
|
||||
&mut msg[(2 * N::to_usize())..],
|
||||
|
|
@ -865,7 +865,7 @@ pub(crate) fn fors_sign<
|
|||
sk_seed,
|
||||
pk_seed,
|
||||
adrs,
|
||||
i * 2u32.pow(A::to_u32()) + indices[i as usize] as u32,
|
||||
i * 2u32.pow(A::to_u32()) + indices[i as usize],
|
||||
);
|
||||
|
||||
// 5:
|
||||
|
|
@ -879,7 +879,7 @@ pub(crate) fn fors_sign<
|
|||
sig_fors.auth[i as usize].tree[j as usize] = fors_node::<A, K, LEN, M, N>(
|
||||
hashers,
|
||||
sk_seed,
|
||||
i * 2u32.pow(A::to_u32() - j) + s as u32,
|
||||
i * 2u32.pow(A::to_u32() - j) + s,
|
||||
j,
|
||||
pk_seed,
|
||||
adrs,
|
||||
|
|
@ -933,7 +933,7 @@ pub(crate) fn fors_pk_from_sig<
|
|||
adrs.set_tree_height(0);
|
||||
|
||||
// 5: ADRS.setTreeIndex(i · 2^a + indices[i])
|
||||
adrs.set_tree_index(i * 2u32.pow(A::to_u32()) + indices[i as usize] as u32);
|
||||
adrs.set_tree_index(i * 2u32.pow(A::to_u32()) + indices[i as usize]);
|
||||
|
||||
// 6: node[0] ← F(PK.seed, ADRS, sk)
|
||||
let mut node_0 = (hashers.f)(pk_seed, &adrs, &sk);
|
||||
|
|
@ -1192,7 +1192,7 @@ pub(crate) fn slh_verify<
|
|||
|
||||
// 8:
|
||||
// 9: digest ← Hmsg(R, PK.seed, PK.root, M) ▷ Compute message digest
|
||||
let digest = (hashers.h_msg)(&r, &pk.pk_seed, &pk.pk_root, m);
|
||||
let digest = (hashers.h_msg)(r, &pk.pk_seed, &pk.pk_root, m);
|
||||
|
||||
// 10: md ← digest[0 : ceil(k·a/8)] ▷ first ceil(k·a/8) bytes
|
||||
let index1 = (K::to_usize() * A::to_usize()).div_ceil(8);
|
||||
|
|
|
|||
132
src/hashers.rs
132
src/hashers.rs
|
|
@ -1,6 +1,11 @@
|
|||
use crate::types::Adrs;
|
||||
use generic_array::{ArrayLength, GenericArray};
|
||||
|
||||
// TODO: We can do a bit better extracting common functionality versus parameter sets...
|
||||
|
||||
|
||||
// Holds hasher function references; constructed by each wrapper
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub(crate) struct Hashers<K: ArrayLength, LEN: ArrayLength, M: ArrayLength, N: ArrayLength> {
|
||||
pub(crate) h_msg: fn(&[u8], &[u8], &[u8], &[u8]) -> GenericArray<u8, M>,
|
||||
pub(crate) prf: fn(&[u8], &[u8], &Adrs) -> GenericArray<u8, N>,
|
||||
|
|
@ -29,70 +34,63 @@ pub(crate) mod shake {
|
|||
use sha3::Shake256;
|
||||
|
||||
|
||||
pub fn shake256_a(input: &[&[u8]], out: &mut [u8]) {
|
||||
#[allow(clippy::module_name_repetitions)]
|
||||
pub fn shake256(input: &[&[u8]], out: &mut [u8]) {
|
||||
let mut hasher = Shake256::default();
|
||||
input.iter().for_each(|item| hasher.update(item));
|
||||
let mut reader = hasher.finalize_xof();
|
||||
reader.read(out);
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn h_msg<M: ArrayLength>(
|
||||
r: &[u8], pk_seed: &[u8], pk_root: &[u8], m: &[u8],
|
||||
) -> GenericArray<u8, M> {
|
||||
let mut digest: GenericArray<u8, M> = GenericArray::default();
|
||||
shake256_a(&[&r, &pk_seed, &pk_root, m], &mut digest);
|
||||
shake256(&[&r, &pk_seed, &pk_root, m], &mut digest);
|
||||
digest
|
||||
}
|
||||
|
||||
|
||||
#[allow(clippy::similar_names)] // pk_seed and sk_seed
|
||||
pub(crate) fn prf<N: ArrayLength>(
|
||||
pk_seed: &[u8], sk_seed: &[u8], adrs: &Adrs,
|
||||
) -> GenericArray<u8, N> {
|
||||
let mut digest: GenericArray<u8, N> = GenericArray::default();
|
||||
shake256_a(&[pk_seed, &adrs.to_32_bytes(), sk_seed], &mut digest); // Note that the spec swaps order of last to params
|
||||
shake256(&[pk_seed, &adrs.to_32_bytes(), sk_seed], &mut digest); // Note that the spec swaps order of last to params
|
||||
digest
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn prf_msg<N: ArrayLength>(
|
||||
sk_prf: &[u8], opt_rand: &[u8], m: &[u8],
|
||||
) -> GenericArray<u8, N> {
|
||||
let mut digest: GenericArray<u8, N> = GenericArray::default();
|
||||
shake256_a(&[sk_prf, opt_rand, m], &mut digest);
|
||||
shake256(&[sk_prf, opt_rand, m], &mut digest);
|
||||
digest
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn f<N: ArrayLength>(pk_seed: &[u8], adrs: &Adrs, m1: &[u8]) -> GenericArray<u8, N> {
|
||||
let mut digest: GenericArray<u8, N> = GenericArray::default();
|
||||
shake256_a(&[pk_seed, &adrs.to_32_bytes(), m1], &mut digest);
|
||||
shake256(&[pk_seed, &adrs.to_32_bytes(), m1], &mut digest);
|
||||
digest
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn h<N: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, m1: &[u8], m2: &[u8],
|
||||
) -> GenericArray<u8, N> {
|
||||
let mut digest: GenericArray<u8, N> = GenericArray::default();
|
||||
shake256_a(&[pk_seed, &adrs.to_32_bytes(), m1, m2], &mut digest);
|
||||
shake256(&[pk_seed, &adrs.to_32_bytes(), m1, m2], &mut digest);
|
||||
digest
|
||||
}
|
||||
|
||||
// Until a more elegant way is found to covert ml into list of bytes
|
||||
pub(crate) fn t_l<LEN: ArrayLength, N: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, ml: &GenericArray<GenericArray<u8, N>, LEN>,
|
||||
) -> GenericArray<u8, N> {
|
||||
let mut hasher = Shake256::default();
|
||||
hasher.update(pk_seed);
|
||||
hasher.update(&adrs.to_32_bytes());
|
||||
ml.iter().for_each(|item| hasher.update(item));
|
||||
let mut reader = hasher.finalize_xof();
|
||||
let mut result = GenericArray::default();
|
||||
reader.read(&mut result);
|
||||
result
|
||||
}
|
||||
|
||||
// TODO: Squash K and LEN versions
|
||||
// Until a more elegant way is found to covert ml into list of bytes
|
||||
pub(crate) fn t_len<K: ArrayLength, N: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, ml: &GenericArray<GenericArray<u8, N>, K>,
|
||||
) -> GenericArray<u8, N> {
|
||||
// Perhaps there is a more elegant way to covert ml into list of bytes
|
||||
pub(crate) fn t_l<X: ArrayLength, Y: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, ml: &GenericArray<GenericArray<u8, Y>, X>,
|
||||
) -> GenericArray<u8, Y> {
|
||||
let mut hasher = Shake256::default();
|
||||
hasher.update(pk_seed);
|
||||
hasher.update(&adrs.to_32_bytes());
|
||||
|
|
@ -104,20 +102,27 @@ pub(crate) mod shake {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(any(
|
||||
feature = "slh_dsa_sha2_128f",
|
||||
feature = "slh_dsa_sha2_128s"
|
||||
))]
|
||||
pub(crate) mod sha2_cat_1 {
|
||||
use crate::types::Adrs;
|
||||
use core::cmp::min;
|
||||
use generic_array::{ArrayLength, GenericArray};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
// Requires length of output less than or equal to 32 byte digeest
|
||||
|
||||
pub fn sha2_256(input: &[&[u8]], out: &mut [u8]) {
|
||||
debug_assert!(out.len() <= 32);
|
||||
let mut hasher = Sha256::new();
|
||||
input.iter().for_each(|item| hasher.update(item));
|
||||
let result = hasher.finalize();
|
||||
out.copy_from_slice(&result[0..out.len()]);
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn h_msg<M: ArrayLength>(
|
||||
r: &[u8], pk_seed: &[u8], pk_root: &[u8], m: &[u8],
|
||||
) -> GenericArray<u8, M> {
|
||||
|
|
@ -137,6 +142,8 @@ pub(crate) mod sha2_cat_1 {
|
|||
result
|
||||
}
|
||||
|
||||
|
||||
#[allow(clippy::similar_names)] // pk_seed and sk_seed
|
||||
pub(crate) fn prf<N: ArrayLength>(
|
||||
pk_seed: &[u8], sk_seed: &[u8], adrs: &Adrs,
|
||||
) -> GenericArray<u8, N> {
|
||||
|
|
@ -165,7 +172,7 @@ pub(crate) mod sha2_cat_1 {
|
|||
inner_hasher.update(&padded[..]);
|
||||
inner_hasher.update(a0);
|
||||
inner_hasher.update(b1);
|
||||
for p in padded.iter_mut() {
|
||||
for p in &mut padded {
|
||||
*p ^= 0x6a;
|
||||
}
|
||||
let mut outer_hasher = Sha256::new();
|
||||
|
|
@ -185,6 +192,7 @@ pub(crate) mod sha2_cat_1 {
|
|||
digest
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn f<N: ArrayLength>(pk_seed: &[u8], adrs: &Adrs, m1: &[u8]) -> GenericArray<u8, N> {
|
||||
let mut digest: GenericArray<u8, N> = GenericArray::default();
|
||||
let to_byte = [0u8; 64];
|
||||
|
|
@ -200,6 +208,7 @@ pub(crate) mod sha2_cat_1 {
|
|||
digest
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn h<N: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, m1: &[u8], m2: &[u8],
|
||||
) -> GenericArray<u8, N> {
|
||||
|
|
@ -218,7 +227,8 @@ pub(crate) mod sha2_cat_1 {
|
|||
digest
|
||||
}
|
||||
|
||||
// Until a more elegant way is found to covert ml into list of bytes
|
||||
|
||||
// Perhaps there is a more elegant way to covert ml into list of bytes
|
||||
pub(crate) fn t_l<LEN: ArrayLength, N: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, ml: &GenericArray<GenericArray<u8, N>, LEN>,
|
||||
) -> GenericArray<u8, N> {
|
||||
|
|
@ -227,24 +237,7 @@ pub(crate) mod sha2_cat_1 {
|
|||
let mut hasher = Sha256::new();
|
||||
hasher.update(pk_seed);
|
||||
hasher.update(&to_byte[0..(64 - N::to_usize())]);
|
||||
hasher.update(&adrs.to_22_bytes());
|
||||
ml.iter().for_each(|item| hasher.update(item));
|
||||
let digest = hasher.finalize();
|
||||
result.copy_from_slice(&digest[0..N::to_usize()]);
|
||||
result
|
||||
}
|
||||
|
||||
// TODO: Squash K and LEN versions
|
||||
// Until a more elegant way is found to covert ml into list of bytes
|
||||
pub(crate) fn t_len<K: ArrayLength, N: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, ml: &GenericArray<GenericArray<u8, N>, K>,
|
||||
) -> GenericArray<u8, N> {
|
||||
let mut result = GenericArray::default();
|
||||
let to_byte = [0u8; 64];
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(pk_seed);
|
||||
hasher.update(&to_byte[0..(64 - N::to_usize())]);
|
||||
hasher.update(&adrs.to_22_bytes());
|
||||
hasher.update(adrs.to_22_bytes());
|
||||
ml.iter().for_each(|item| hasher.update(item));
|
||||
let digest = hasher.finalize();
|
||||
result.copy_from_slice(&digest[0..N::to_usize()]);
|
||||
|
|
@ -252,28 +245,38 @@ pub(crate) mod sha2_cat_1 {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(any(
|
||||
feature = "slh_dsa_sha2_192f",
|
||||
feature = "slh_dsa_sha2_192s",
|
||||
feature = "slh_dsa_sha2_256f",
|
||||
feature = "slh_dsa_sha2_256s"
|
||||
))]
|
||||
pub(crate) mod sha2_cat_3_5 {
|
||||
use crate::types::Adrs;
|
||||
use core::cmp::min;
|
||||
use generic_array::{ArrayLength, GenericArray};
|
||||
use sha2::{Digest, Sha256, Sha512};
|
||||
|
||||
// Requires length of output less than or equal to 32 byte digeest
|
||||
|
||||
pub fn sha2_256(input: &[&[u8]], out: &mut [u8]) {
|
||||
debug_assert!(out.len() <= 32);
|
||||
let mut hasher = Sha256::new();
|
||||
input.iter().for_each(|item| hasher.update(item));
|
||||
let result = hasher.finalize();
|
||||
out.copy_from_slice(&result[0..out.len()]);
|
||||
}
|
||||
|
||||
// Requires length of output less than or equal to 32 byte digeest
|
||||
|
||||
pub fn sha2_512(input: &[&[u8]], out: &mut [u8]) {
|
||||
debug_assert!(out.len() <= 64);
|
||||
let mut hasher = Sha512::new();
|
||||
input.iter().for_each(|item| hasher.update(item));
|
||||
let result = hasher.finalize();
|
||||
out.copy_from_slice(&result[0..out.len()]);
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn h_msg<M: ArrayLength>(
|
||||
r: &[u8], pk_seed: &[u8], pk_root: &[u8], m: &[u8],
|
||||
) -> GenericArray<u8, M> {
|
||||
|
|
@ -290,10 +293,11 @@ pub(crate) mod sha2_cat_3_5 {
|
|||
start += 64;
|
||||
counter += 1;
|
||||
}
|
||||
//println!("h_msg: {:?}", &result[0..4]);
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
#[allow(clippy::similar_names)] // pk_seed and sk_seed
|
||||
pub(crate) fn prf<N: ArrayLength>(
|
||||
pk_seed: &[u8], sk_seed: &[u8], adrs: &Adrs,
|
||||
) -> GenericArray<u8, N> {
|
||||
|
|
@ -308,10 +312,10 @@ pub(crate) mod sha2_cat_3_5 {
|
|||
],
|
||||
&mut digest,
|
||||
); // Note that the spec swaps order of last to params
|
||||
//println!("prf: {:?}", &digest[0..4]);
|
||||
digest
|
||||
}
|
||||
|
||||
|
||||
pub fn hmac_sha_512(key: &[u8], a0: &[u8], b1: &[u8]) -> [u8; 64] {
|
||||
let k2 = key;
|
||||
let mut padded = [0x36; 128];
|
||||
|
|
@ -322,7 +326,7 @@ pub(crate) mod sha2_cat_3_5 {
|
|||
inner_hasher.update(&padded[..]);
|
||||
inner_hasher.update(a0);
|
||||
inner_hasher.update(b1);
|
||||
for p in padded.iter_mut() {
|
||||
for p in &mut padded {
|
||||
*p ^= 0x6a;
|
||||
}
|
||||
let mut outer_hasher = Sha512::new();
|
||||
|
|
@ -336,13 +340,12 @@ pub(crate) mod sha2_cat_3_5 {
|
|||
sk_prf: &[u8], opt_rand: &[u8], m: &[u8],
|
||||
) -> GenericArray<u8, N> {
|
||||
let mut digest: GenericArray<u8, N> = GenericArray::default();
|
||||
//sha2_256(&[sk_prf, opt_rand, m], &mut digest);
|
||||
let xxx = hmac_sha_512(sk_prf, opt_rand, m);
|
||||
digest.copy_from_slice(&xxx[0..N::to_usize()]);
|
||||
//println!("prf_msg: {:?}", &digest[0..4]);
|
||||
digest
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn f<N: ArrayLength>(pk_seed: &[u8], adrs: &Adrs, m1: &[u8]) -> GenericArray<u8, N> {
|
||||
let mut digest: GenericArray<u8, N> = GenericArray::default();
|
||||
let to_byte = [0u8; 64];
|
||||
|
|
@ -355,10 +358,10 @@ pub(crate) mod sha2_cat_3_5 {
|
|||
],
|
||||
&mut digest,
|
||||
);
|
||||
//println!("f: {:?}", &digest[0..4]);
|
||||
digest
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn h<N: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, m1: &[u8], m2: &[u8],
|
||||
) -> GenericArray<u8, N> {
|
||||
|
|
@ -374,11 +377,11 @@ pub(crate) mod sha2_cat_3_5 {
|
|||
],
|
||||
&mut digest,
|
||||
);
|
||||
//println!("h: {:?} pkseed {:?} m1 {:?}", &digest[0..4], pk_seed[0], m1[0]);
|
||||
digest
|
||||
}
|
||||
|
||||
// Until a more elegant way is found to covert ml into list of bytes
|
||||
|
||||
// Perhaps there is a more elegant way to covert ml into list of bytes
|
||||
pub(crate) fn t_l<LEN: ArrayLength, N: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, ml: &GenericArray<GenericArray<u8, N>, LEN>,
|
||||
) -> GenericArray<u8, N> {
|
||||
|
|
@ -387,24 +390,7 @@ pub(crate) mod sha2_cat_3_5 {
|
|||
let mut hasher = Sha512::new();
|
||||
hasher.update(pk_seed);
|
||||
hasher.update(&to_byte[0..(128 - N::to_usize())]);
|
||||
hasher.update(&adrs.to_22_bytes());
|
||||
ml.iter().for_each(|item| hasher.update(item));
|
||||
let digest = hasher.finalize();
|
||||
result.copy_from_slice(&digest[0..N::to_usize()]);
|
||||
result
|
||||
}
|
||||
|
||||
// TODO: Squash K and LEN versions
|
||||
// Until a more elegant way is found to covert ml into list of bytes
|
||||
pub(crate) fn t_len<K: ArrayLength, N: ArrayLength>(
|
||||
pk_seed: &[u8], adrs: &Adrs, ml: &GenericArray<GenericArray<u8, N>, K>,
|
||||
) -> GenericArray<u8, N> {
|
||||
let mut result = GenericArray::default();
|
||||
let to_byte = [0u8; 128];
|
||||
let mut hasher = Sha512::new();
|
||||
hasher.update(pk_seed);
|
||||
hasher.update(&to_byte[0..(128 - N::to_usize())]);
|
||||
hasher.update(&adrs.to_22_bytes());
|
||||
hasher.update(adrs.to_22_bytes());
|
||||
ml.iter().for_each(|item| hasher.update(item));
|
||||
let digest = hasher.finalize();
|
||||
result.copy_from_slice(&digest[0..N::to_usize()]);
|
||||
|
|
|
|||
110
src/lib.rs
110
src/lib.rs
|
|
@ -8,8 +8,10 @@
|
|||
// 1. General clean-up
|
||||
// 2. SerDes on keys
|
||||
// 3. Proper traits and non-rng functions
|
||||
// 4. Separate into proper files
|
||||
// 5. Doc, of course!
|
||||
// 4. Adrs as raw bytes
|
||||
// 5. clippy
|
||||
// 6. Separate into proper files
|
||||
// 7. Doc, of course!
|
||||
mod algs;
|
||||
mod hashers;
|
||||
mod test;
|
||||
|
|
@ -33,7 +35,7 @@ macro_rules! functionality {
|
|||
pub fn slh_keygen_with_rng(
|
||||
rng: &mut impl CryptoRngCore,
|
||||
) -> Result<(SlhPrivateKey<N>, SlhPublicKey<N>), &'static str> {
|
||||
crate::algs::slh_keygen_with_rng::<D, H, HP, K, LEN, M, N>(rng, &HASHERS)
|
||||
crate::algs::slh_keygen_with_rng::<D, H, HP, K, Len, M, N>(rng, &HASHERS)
|
||||
}
|
||||
|
||||
/// blah
|
||||
|
|
@ -41,7 +43,7 @@ macro_rules! functionality {
|
|||
pub fn slh_sign_with_rng(
|
||||
rng: &mut impl CryptoRngCore, m: &[u8], sk: &SlhPrivateKey<N>, randomize: bool,
|
||||
) -> Result<[u8; SIG_LEN], &'static str> {
|
||||
let sig = crate::algs::slh_sign_with_rng::<A, D, H, HP, K, LEN, M, N>(
|
||||
let sig = crate::algs::slh_sign_with_rng::<A, D, H, HP, K, Len, M, N>(
|
||||
rng, &HASHERS, &m, &sk, randomize,
|
||||
);
|
||||
sig.map(|s| s.deserialize())
|
||||
|
|
@ -50,8 +52,8 @@ macro_rules! functionality {
|
|||
/// blah
|
||||
#[must_use]
|
||||
pub fn slh_verify(m: &[u8], sig_bytes: &[u8; SIG_LEN], pk: &SlhPublicKey<N>) -> bool {
|
||||
let sig = SlhDsaSig::<A, D, HP, K, LEN, N>::serialize(sig_bytes);
|
||||
crate::algs::slh_verify::<A, D, H, HP, K, LEN, M, N>(&HASHERS, &m, &sig, &pk)
|
||||
let sig = SlhDsaSig::<A, D, HP, K, Len, N>::serialize(sig_bytes);
|
||||
crate::algs::slh_verify::<A, D, H, HP, K, Len, M, N>(&HASHERS, &m, &sig, &pk)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -82,7 +84,7 @@ macro_rules! functionality {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_sha2_128s")]
|
||||
pub mod slh_dsa_sha2_128s {
|
||||
use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U12, U14, U16, U2, U3, U30, U63, U7, U9};
|
||||
|
||||
|
|
@ -93,12 +95,12 @@ pub mod slh_dsa_sha2_128s {
|
|||
type A = U12;
|
||||
type K = U14;
|
||||
type M = U30;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 7856;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -107,7 +109,7 @@ pub mod slh_dsa_sha2_128s {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_shake_128s")]
|
||||
pub mod slh_dsa_shake_128s {
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U12, U14, U16, U2, U3, U30, U63, U7, U9};
|
||||
|
||||
|
|
@ -118,12 +120,12 @@ pub mod slh_dsa_shake_128s {
|
|||
type A = U12;
|
||||
type K = U14;
|
||||
type M = U30;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 7856;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -132,7 +134,7 @@ pub mod slh_dsa_shake_128s {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_sha2_128f")]
|
||||
pub mod slh_dsa_sha2_128f {
|
||||
use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::sha2_cat_1::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U16, U2, U22, U3, U33, U34, U6, U66};
|
||||
|
||||
|
|
@ -143,12 +145,12 @@ pub mod slh_dsa_sha2_128f {
|
|||
type A = U6;
|
||||
type K = U33;
|
||||
type M = U34;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 17088;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -157,7 +159,7 @@ pub mod slh_dsa_sha2_128f {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_shake_128f")]
|
||||
pub mod slh_dsa_shake_128f {
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U16, U2, U22, U3, U33, U34, U6, U66};
|
||||
|
||||
|
|
@ -168,12 +170,12 @@ pub mod slh_dsa_shake_128f {
|
|||
type A = U6;
|
||||
type K = U33;
|
||||
type M = U34;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 17088;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -182,7 +184,7 @@ pub mod slh_dsa_shake_128f {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_sha2_192s")]
|
||||
pub mod slh_dsa_sha2_192s {
|
||||
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U14, U17, U2, U24, U3, U39, U63, U7, U9};
|
||||
|
||||
|
|
@ -193,12 +195,12 @@ pub mod slh_dsa_sha2_192s {
|
|||
type A = U14;
|
||||
type K = U17;
|
||||
type M = U39;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 16224;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -207,7 +209,7 @@ pub mod slh_dsa_sha2_192s {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_shake_192s")]
|
||||
pub mod slh_dsa_shake_192s {
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U14, U17, U2, U24, U3, U39, U63, U7, U9};
|
||||
|
||||
|
|
@ -218,12 +220,12 @@ pub mod slh_dsa_shake_192s {
|
|||
type A = U14;
|
||||
type K = U17;
|
||||
type M = U39;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 16224;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -232,7 +234,7 @@ pub mod slh_dsa_shake_192s {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_sha2_192f")]
|
||||
pub mod slh_dsa_sha2_192f {
|
||||
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U2, U22, U24, U3, U33, U42, U66, U8};
|
||||
|
||||
|
|
@ -243,12 +245,12 @@ pub mod slh_dsa_sha2_192f {
|
|||
type A = U8;
|
||||
type K = U33;
|
||||
type M = U42;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 35664;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -257,7 +259,7 @@ pub mod slh_dsa_sha2_192f {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_shake_192f")]
|
||||
pub mod slh_dsa_shake_192f {
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U2, U22, U24, U3, U33, U42, U66, U8};
|
||||
|
||||
|
|
@ -268,12 +270,12 @@ pub mod slh_dsa_shake_192f {
|
|||
type A = U8;
|
||||
type K = U33;
|
||||
type M = U42;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 35664;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -282,7 +284,7 @@ pub mod slh_dsa_shake_192f {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_sha2_256s")]
|
||||
pub mod slh_dsa_sha2_256s {
|
||||
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U14, U2, U22, U3, U32, U47, U64, U8};
|
||||
|
||||
|
|
@ -293,12 +295,12 @@ pub mod slh_dsa_sha2_256s {
|
|||
type A = U14;
|
||||
type K = U22;
|
||||
type M = U47;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 29792;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -307,7 +309,7 @@ pub mod slh_dsa_sha2_256s {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_shake_256s")]
|
||||
pub mod slh_dsa_shake_256s {
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U14, U2, U22, U3, U32, U47, U64, U8};
|
||||
|
||||
|
|
@ -318,12 +320,12 @@ pub mod slh_dsa_shake_256s {
|
|||
type A = U14;
|
||||
type K = U22;
|
||||
type M = U47;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 29792;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -332,7 +334,7 @@ pub mod slh_dsa_shake_256s {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_sha2_256f")]
|
||||
pub mod slh_dsa_sha2_256f {
|
||||
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::sha2_cat_3_5::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U17, U2, U3, U32, U35, U4, U49, U68, U9};
|
||||
|
||||
|
|
@ -343,12 +345,12 @@ pub mod slh_dsa_sha2_256f {
|
|||
type A = U9;
|
||||
type K = U35;
|
||||
type M = U49;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 49856;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
@ -357,7 +359,7 @@ pub mod slh_dsa_sha2_256f {
|
|||
/// TKTK
|
||||
#[cfg(feature = "slh_dsa_shake_256f")]
|
||||
pub mod slh_dsa_shake_256f {
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l, t_len};
|
||||
use crate::hashers::shake::{f, h, h_msg, prf, prf_msg, t_l};
|
||||
use crate::hashers::Hashers;
|
||||
use generic_array::typenum::{Prod, Sum, U17, U2, U3, U32, U35, U4, U49, U68, U9};
|
||||
|
||||
|
|
@ -368,12 +370,12 @@ pub mod slh_dsa_shake_256f {
|
|||
type A = U9;
|
||||
type K = U35;
|
||||
type M = U49;
|
||||
type LEN = Sum<Prod<U2, N>, U3>;
|
||||
type Len = Sum<Prod<U2, N>, U3>;
|
||||
//const PK_LEN: usize = 32;
|
||||
const SIG_LEN: usize = 49856;
|
||||
//const SK_LEN: usize = 0000;
|
||||
static HASHERS: Hashers<K, LEN, M, N> =
|
||||
Hashers::<K, LEN, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len };
|
||||
static HASHERS: Hashers<K, Len, M, N> =
|
||||
Hashers::<K, Len, M, N> { h_msg, prf, prf_msg, f, h, t_l, t_len: t_l };
|
||||
|
||||
functionality!();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue