hash_based_signatures/signature/winternitz/
d.rs1use anyhow::{anyhow, Error, Result};
2
3#[derive(Clone, Copy)]
5pub struct D {
6 pub d: u64,
7 log_log_d_plus_1: usize,
8}
9
10impl D {
11 pub fn new(d: u64) -> Self {
17 D::try_from(d).unwrap()
18 }
19
20 pub fn bits_to_combine(&self) -> usize {
22 1 << self.log_log_d_plus_1
23 }
24
25 pub fn bits_c(&self) -> usize {
27 let bits_c = self.bits_to_combine() + 8 - self.log_log_d_plus_1;
34
35 let bits_c = (((bits_c as f32) / (self.bits_to_combine() as f32)).ceil() as usize)
37 * self.bits_to_combine();
38 bits_c
39 }
40
41 pub fn signature_and_key_size(&self) -> usize {
43 (256 + self.bits_c()) / self.bits_to_combine()
44 }
45}
46
47impl TryFrom<u64> for D {
48 type Error = Error;
49
50 fn try_from(d: u64) -> Result<D> {
51 let log_log_d_plus_1 = ((d + 1) as f64).log2().log2() as usize;
52 if d + 1 != (1 << (1 << log_log_d_plus_1)) {
53 Err(anyhow!(
54 "d is not of the form 2^(2^x) - 1! Try one of 1, 3, 15, or 255."
55 ))
56 } else {
57 Ok(D {
58 d,
59 log_log_d_plus_1,
60 })
61 }
62 }
63}