High-level functionality for working with Winternitz signatures. Signatures contain the signature of each digit as well as the digit itself. ## See [`CompactWots`]
| 39 | /// |
| 40 | /// [`CompactWots`] |
| 41 | pub trait Wots { |
| 42 | type Converter: Converter; |
| 43 | type PublicKey: AsRef<[[u8; 20]]> + TryFrom<Vec<[u8; 20]>, Error: std::fmt::Debug>; |
| 44 | type Message: AsRef<[u8]> + TryFrom<Vec<u8>, Error: std::fmt::Debug>; |
| 45 | type Signature: AsRef<[[u8; 21]]> + TryFrom<Vec<[u8; 21]>, Error: std::fmt::Debug>; |
| 46 | |
| 47 | const ALGORITHM: Winternitz<ListpickVerifier, Self::Converter> = Winternitz::new(); |
| 48 | const MSG_BYTE_LEN: u32; |
| 49 | const PARAMETERS: Parameters = Parameters::new_by_bit_length(Self::MSG_BYTE_LEN * 8, LOG2_BASE); |
| 50 | const TOTAL_DIGIT_LEN: u32 = Self::PARAMETERS.total_digit_len(); |
| 51 | |
| 52 | /// Generates a random secret key. |
| 53 | fn generate_secret_key() -> WinternitzSecret { |
| 54 | let mut buffer = [0u8; 20]; |
| 55 | let mut rng = rand::rngs::OsRng; |
| 56 | rand::RngCore::fill_bytes(&mut rng, &mut buffer); |
| 57 | Vec::from(buffer) |
| 58 | } |
| 59 | |
| 60 | /// Creates a secret key from the given `secret` string. |
| 61 | /// |
| 62 | /// ## Warning |
| 63 | /// |
| 64 | /// For backwards compatibility, the original conversion function is used. |
| 65 | /// The `secret` string is converted into ASCII bytes, |
| 66 | /// which are in turn converted into lower hex ASCII bytes. |
| 67 | #[deprecated(note = "It is safer to use Vec<u8> directly")] |
| 68 | fn secret_from_str(secret: &str) -> WinternitzSecret { |
| 69 | secret.as_bytes().to_lower_hex_string().into_bytes() |
| 70 | } |
| 71 | |
| 72 | /// Generates a public key for the given `secret_key`. |
| 73 | fn generate_public_key(secret_key: &WinternitzSecret) -> Self::PublicKey { |
| 74 | let pubkey_vec = winternitz::generate_public_key(&Self::PARAMETERS, secret_key); |
| 75 | match Self::PublicKey::try_from(pubkey_vec) { |
| 76 | Ok(public_key) => public_key, |
| 77 | _ => unreachable!(), |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /// Generates a signature for the given `secret_key` and `message`, |
| 82 | /// in form of a Bitcoin witness. |
| 83 | fn sign_to_raw_witness( |
| 84 | secret_key: &WinternitzSecret, |
| 85 | message: &Self::Message, |
| 86 | ) -> bitcoin::Witness { |
| 87 | let witness = Self::ALGORITHM.sign(&Self::PARAMETERS, secret_key, message.as_ref()); |
| 88 | debug_assert_eq!(witness.len(), 2 * Self::TOTAL_DIGIT_LEN as usize); |
| 89 | witness |
| 90 | } |
| 91 | |
| 92 | /// Generates a signature for the given `secret_key` and `message`. |
| 93 | fn sign(secret_key: &WinternitzSecret, message: &Self::Message) -> Self::Signature { |
| 94 | let witness = Self::sign_to_raw_witness(secret_key, message); |
| 95 | Self::raw_witness_to_signature(&witness) |
| 96 | } |
| 97 | |
| 98 | /// Generates a signature for the given `inputs`. |
no test coverage detected