Skip to content

Commit

Permalink
Rename to get_num_winternitz_chains
Browse files Browse the repository at this point in the history
  • Loading branch information
aewag committed Sep 2, 2024
1 parent a12ec31 commit e3f6c32
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 50 deletions.
36 changes: 18 additions & 18 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub const MAX_HASH_BLOCK_SIZE: usize = 64;
pub const PRNG_MAX_LEN: usize = prng_len(MAX_HASH_SIZE);

pub const MAX_NUM_WINTERNITZ_CHAINS: usize =
get_hash_chain_count(MIN_WINTERNITZ_PARAMETER, MAX_HASH_SIZE);
get_num_winternitz_chains(MIN_WINTERNITZ_PARAMETER, MAX_HASH_SIZE);

pub const MAX_LMOTS_SIGNATURE_LENGTH: usize =
lmots_signature_length(MAX_HASH_SIZE, MAX_NUM_WINTERNITZ_CHAINS);
Expand All @@ -73,7 +73,7 @@ pub const MAX_HSS_SIGNATURE_LENGTH: usize = get_hss_signature_length();
/// https://datatracker.ietf.org/doc/html/rfc8554#appendix-B
const HASH_CHAIN_COUNTS: [usize; 12] = [136, 200, 265, 68, 101, 133, 35, 51, 67, 18, 26, 34];

pub const fn get_hash_chain_count(winternitz_parameter: usize, output_size: usize) -> usize {
pub const fn get_num_winternitz_chains(winternitz_parameter: usize, output_size: usize) -> usize {
let w_i = match winternitz_parameter {
1 => 0usize,
2 => 1usize,
Expand Down Expand Up @@ -132,7 +132,7 @@ pub const fn get_hss_signature_length() -> usize {
while level > 0 {
length += hss_signed_public_key_length(
MAX_HASH_SIZE,
get_hash_chain_count(WINTERNITZ_PARAMETERS[level], MAX_HASH_SIZE),
get_num_winternitz_chains(WINTERNITZ_PARAMETERS[level], MAX_HASH_SIZE),
TREE_HEIGHTS[level],
);
level -= 1;
Expand All @@ -141,7 +141,7 @@ pub const fn get_hss_signature_length() -> usize {
length
+ lms_signature_length(
MAX_HASH_SIZE,
get_hash_chain_count(WINTERNITZ_PARAMETERS[0], MAX_HASH_SIZE),
get_num_winternitz_chains(WINTERNITZ_PARAMETERS[0], MAX_HASH_SIZE),
TREE_HEIGHTS[0],
)
}
Expand Down Expand Up @@ -170,21 +170,21 @@ pub mod winternitz_chain {

#[cfg(test)]
mod tests {
use crate::constants::get_hash_chain_count;
use crate::constants::get_num_winternitz_chains;

#[test]
fn test_get_hash_chain_count() {
assert_eq!(get_hash_chain_count(1, 32), 265);
assert_eq!(get_hash_chain_count(2, 32), 133);
assert_eq!(get_hash_chain_count(4, 32), 67);
assert_eq!(get_hash_chain_count(8, 32), 34);
assert_eq!(get_hash_chain_count(1, 24), 200);
assert_eq!(get_hash_chain_count(2, 24), 101);
assert_eq!(get_hash_chain_count(4, 24), 51);
assert_eq!(get_hash_chain_count(8, 24), 26);
assert_eq!(get_hash_chain_count(1, 16), 136);
assert_eq!(get_hash_chain_count(2, 16), 68);
assert_eq!(get_hash_chain_count(4, 16), 35);
assert_eq!(get_hash_chain_count(8, 16), 18);
fn test_get_num_winternitz_chains() {
assert_eq!(get_num_winternitz_chains(1, 32), 265);
assert_eq!(get_num_winternitz_chains(2, 32), 133);
assert_eq!(get_num_winternitz_chains(4, 32), 67);
assert_eq!(get_num_winternitz_chains(8, 32), 34);
assert_eq!(get_num_winternitz_chains(1, 24), 200);
assert_eq!(get_num_winternitz_chains(2, 24), 101);
assert_eq!(get_num_winternitz_chains(4, 24), 51);
assert_eq!(get_num_winternitz_chains(8, 24), 26);
assert_eq!(get_num_winternitz_chains(1, 16), 136);
assert_eq!(get_num_winternitz_chains(2, 16), 68);
assert_eq!(get_num_winternitz_chains(4, 16), 35);
assert_eq!(get_num_winternitz_chains(8, 16), 18);
}
}
8 changes: 6 additions & 2 deletions src/hss/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ impl<'a, H: HashChain> InMemoryHssSignedPublicKey<'a, H> {
sig.lmots_signature
.lmots_parameter
.get_hash_function_output_size(),
sig.lmots_signature.lmots_parameter.get_hash_chain_count() as usize,
sig.lmots_signature
.lmots_parameter
.get_num_winternitz_chains() as usize,
sig.lms_parameter.get_tree_height() as usize,
);

Expand All @@ -234,7 +236,9 @@ impl<'a, H: HashChain> InMemoryHssSignedPublicKey<'a, H> {
sig.lmots_signature
.lmots_parameter
.get_hash_function_output_size(),
sig.lmots_signature.lmots_parameter.get_hash_chain_count() as usize,
sig.lmots_signature
.lmots_parameter
.get_num_winternitz_chains() as usize,
sig.lms_parameter.get_tree_height() as usize,
);
let public_key_size =
Expand Down
2 changes: 1 addition & 1 deletion src/lm_ots/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mod tests {
let parameter = $parameter.construct_parameter::<$hash_chain>().unwrap();
assert_eq!(parameter.get_hash_function_output_size(), $n);
assert_eq!(parameter.get_winternitz(), $w);
assert_eq!(parameter.get_hash_chain_count(), $p);
assert_eq!(parameter.get_num_winternitz_chains(), $p);
assert_eq!(parameter.get_checksum_left_shift(), $ls);
assert_eq!(parameter.get_type_id(), $type);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lm_ots/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn generate_private_key<H: HashChain>(

let mut hasher = lmots_parameter.get_hasher();

for index in 0..lmots_parameter.get_hash_chain_count() {
for index in 0..lmots_parameter.get_num_winternitz_chains() {
hasher.update(&lms_tree_identifier);
hasher.update(&lms_leaf_identifier);
hasher.update(&index.to_be_bytes());
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn generate_public_key<H: HashChain>(private_key: &LmotsPrivateKey<H>) -> Lm
let mut public_key_data: ArrayVec<[ArrayVec<[u8; MAX_HASH_SIZE]>; MAX_NUM_WINTERNITZ_CHAINS]> =
ArrayVec::new();

for i in 0..lmots_parameter.get_hash_chain_count() as usize {
for i in 0..lmots_parameter.get_num_winternitz_chains() as usize {
let mut hash_chain_data = H::prepare_hash_chain_data(
&private_key.lms_tree_identifier,
&private_key.lms_leaf_identifier,
Expand Down
16 changes: 8 additions & 8 deletions src/lm_ots/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::marker::PhantomData;

use tinyvec::ArrayVec;

use crate::constants::get_hash_chain_count;
use crate::constants::get_num_winternitz_chains;
use crate::{
constants::{FastVerifyCached, MAX_HASH_SIZE},
hasher::HashChain,
Expand Down Expand Up @@ -45,25 +45,25 @@ impl LmotsAlgorithm {
LmotsAlgorithm::LmotsW1 => Some(LmotsParameter::new(
1,
1,
get_hash_chain_count(1, H::OUTPUT_SIZE as usize) as u16,
get_num_winternitz_chains(1, H::OUTPUT_SIZE as usize) as u16,
7,
)),
LmotsAlgorithm::LmotsW2 => Some(LmotsParameter::new(
2,
2,
get_hash_chain_count(2, H::OUTPUT_SIZE as usize) as u16,
get_num_winternitz_chains(2, H::OUTPUT_SIZE as usize) as u16,
6,
)),
LmotsAlgorithm::LmotsW4 => Some(LmotsParameter::new(
3,
4,
get_hash_chain_count(4, H::OUTPUT_SIZE as usize) as u16,
get_num_winternitz_chains(4, H::OUTPUT_SIZE as usize) as u16,
4,
)),
LmotsAlgorithm::LmotsW8 => Some(LmotsParameter::new(
4,
8,
get_hash_chain_count(8, H::OUTPUT_SIZE as usize) as u16,
get_num_winternitz_chains(8, H::OUTPUT_SIZE as usize) as u16,
0,
)),
}
Expand Down Expand Up @@ -119,7 +119,7 @@ impl<H: HashChain> LmotsParameter<H> {
self.winternitz
}

pub fn get_hash_chain_count(&self) -> u16 {
pub fn get_num_winternitz_chains(&self) -> u16 {
self.hash_chain_count
}

Expand All @@ -138,7 +138,7 @@ impl<H: HashChain> LmotsParameter<H> {
let sum = max * max_word_size;

let mut coef = ArrayVec::new();
for i in 0..self.get_hash_chain_count() {
for i in 0..self.get_num_winternitz_chains() {
coef.push(coef_helper(i, self.get_winternitz()));
}

Expand All @@ -163,7 +163,7 @@ impl<H: HashChain> LmotsParameter<H> {
checksum <<= self.get_checksum_left_shift();
let checksum = [(checksum >> 8 & 0xff) as u8, (checksum & 0xff) as u8];

for i in *max..self.get_hash_chain_count() {
for i in *max..self.get_num_winternitz_chains() {
let (index, shift, mask) = coef[i as usize];
let hash_chain_length = ((checksum[index - 32] as u64 >> shift) & mask) as u16;
total_hash_chain_iterations += hash_chain_length;
Expand Down
10 changes: 5 additions & 5 deletions src/lm_ots/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<H: HashChain> LmotsSignature<H> {

let mut signature_data = ArrayVec::new();

for i in 0..lmots_parameter.get_hash_chain_count() {
for i in 0..lmots_parameter.get_num_winternitz_chains() {
let a = coef(
message_hash_with_checksum.as_slice(),
i,
Expand Down Expand Up @@ -179,7 +179,7 @@ impl<H: HashChain> LmotsSignature<H> {
let signature_data =
LmotsSignature::<H>::calculate_signature(private_key, &message_hash_with_checksum);

let hash_iterations = (0..lmots_parameter.get_hash_chain_count()).fold(0, |sum, i| {
let hash_iterations = (0..lmots_parameter.get_num_winternitz_chains()).fold(0, |sum, i| {
sum + coef(
message_hash_with_checksum.as_slice(),
i,
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<'a, H: HashChain> InMemoryLmotsSignature<'a, H> {

let signature_data = read_and_advance(
data,
(H::OUTPUT_SIZE * lmots_parameter.get_hash_chain_count()) as usize,
(H::OUTPUT_SIZE * lmots_parameter.get_num_winternitz_chains()) as usize,
&mut index,
);

Expand Down Expand Up @@ -363,7 +363,7 @@ mod tests {
signature_randomizer.push(i as u8);
}

for i in 0..lmots_parameter.get_hash_chain_count() as usize {
for i in 0..lmots_parameter.get_num_winternitz_chains() as usize {
signature_data.push(ArrayVec::new());
for j in 0..lmots_parameter.get_hash_function_output_size() as usize {
signature_data[i].push(j as u8);
Expand All @@ -381,7 +381,7 @@ mod tests {

// check signature len
let output_size = lmots_parameter.get_hash_function_output_size() as usize;
let hash_chain_count = lmots_parameter.get_hash_chain_count() as usize;
let hash_chain_count = lmots_parameter.get_num_winternitz_chains() as usize;
assert_eq!(binary_rep.len(), 4 + output_size * (hash_chain_count + 1));

let deserialized_signature = InMemoryLmotsSignature::new(binary_rep.as_slice())
Expand Down
30 changes: 17 additions & 13 deletions src/lm_ots/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ use super::{definitions::LmotsPublicKey, signing::InMemoryLmotsSignature};

#[derive(Default)]
struct HashChainArray<H: HashChain> {
pub array_w1:
Option<ArrayVec<[ArrayVec<[u8; MAX_HASH_SIZE]>; get_hash_chain_count(1, MAX_HASH_SIZE)]>>,
pub array_w2:
Option<ArrayVec<[ArrayVec<[u8; MAX_HASH_SIZE]>; get_hash_chain_count(2, MAX_HASH_SIZE)]>>,
pub array_w4:
Option<ArrayVec<[ArrayVec<[u8; MAX_HASH_SIZE]>; get_hash_chain_count(4, MAX_HASH_SIZE)]>>,
pub array_w8:
Option<ArrayVec<[ArrayVec<[u8; MAX_HASH_SIZE]>; get_hash_chain_count(8, MAX_HASH_SIZE)]>>,
pub array_w1: Option<
ArrayVec<[ArrayVec<[u8; MAX_HASH_SIZE]>; get_num_winternitz_chains(1, MAX_HASH_SIZE)]>,
>,
pub array_w2: Option<
ArrayVec<[ArrayVec<[u8; MAX_HASH_SIZE]>; get_num_winternitz_chains(2, MAX_HASH_SIZE)]>,
>,
pub array_w4: Option<
ArrayVec<[ArrayVec<[u8; MAX_HASH_SIZE]>; get_num_winternitz_chains(4, MAX_HASH_SIZE)]>,
>,
pub array_w8: Option<
ArrayVec<[ArrayVec<[u8; MAX_HASH_SIZE]>; get_num_winternitz_chains(8, MAX_HASH_SIZE)]>,
>,
phantom_data: PhantomData<H>,
}

Expand All @@ -25,19 +29,19 @@ impl<H: HashChain> HashChainArray<H> {
let mut hash_chain_array = HashChainArray::<H>::default();
if LmotsAlgorithm::from(lmots_parameter.get_type_id()) == LmotsAlgorithm::LmotsW8 {
hash_chain_array.array_w8 = Some(ArrayVec::<
[ArrayVec<[u8; MAX_HASH_SIZE]>; get_hash_chain_count(8, MAX_HASH_SIZE)],
[ArrayVec<[u8; MAX_HASH_SIZE]>; get_num_winternitz_chains(8, MAX_HASH_SIZE)],
>::default());
} else if LmotsAlgorithm::from(lmots_parameter.get_type_id()) == LmotsAlgorithm::LmotsW4 {
hash_chain_array.array_w4 = Some(ArrayVec::<
[ArrayVec<[u8; MAX_HASH_SIZE]>; get_hash_chain_count(4, MAX_HASH_SIZE)],
[ArrayVec<[u8; MAX_HASH_SIZE]>; get_num_winternitz_chains(4, MAX_HASH_SIZE)],
>::default());
} else if LmotsAlgorithm::from(lmots_parameter.get_type_id()) == LmotsAlgorithm::LmotsW2 {
hash_chain_array.array_w2 = Some(ArrayVec::<
[ArrayVec<[u8; MAX_HASH_SIZE]>; get_hash_chain_count(2, MAX_HASH_SIZE)],
[ArrayVec<[u8; MAX_HASH_SIZE]>; get_num_winternitz_chains(2, MAX_HASH_SIZE)],
>::default());
} else {
hash_chain_array.array_w1 = Some(ArrayVec::<
[ArrayVec<[u8; MAX_HASH_SIZE]>; get_hash_chain_count(1, MAX_HASH_SIZE)],
[ArrayVec<[u8; MAX_HASH_SIZE]>; get_num_winternitz_chains(1, MAX_HASH_SIZE)],
>::default());
}
hash_chain_array
Expand Down Expand Up @@ -111,7 +115,7 @@ pub fn generate_public_key_candidate<H: HashChain>(
let mut hash_chain_array = HashChainArray::new(&lmots_parameter);
let max_w = 2usize.pow(lmots_parameter.get_winternitz() as u32) - 1;

for i in 0..lmots_parameter.get_hash_chain_count() {
for i in 0..lmots_parameter.get_num_winternitz_chains() {
let a = coef(
message_hash_with_checksum.as_slice(),
i,
Expand Down
2 changes: 1 addition & 1 deletion src/lms/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl<'a, H: HashChain> InMemoryLmsSignature<'a, H> {
.unwrap();
let lmots_signature = lm_ots::signing::InMemoryLmotsSignature::new(read_and_advance(
data,
(4 + H::OUTPUT_SIZE * (1 + lmots_parameter.get_hash_chain_count())) as usize,
(4 + H::OUTPUT_SIZE * (1 + lmots_parameter.get_num_winternitz_chains())) as usize,
&mut index,
))
.unwrap();
Expand Down

0 comments on commit e3f6c32

Please sign in to comment.