|
1 | | -use kes_summed_ed25519::{ |
2 | | - self as kes, |
3 | | - kes::{Sum6Kes, Sum6KesSig}, |
4 | | - traits::{KesSig, KesSk}, |
5 | | -}; |
| 1 | +use kes_summed_ed25519::{self as kes, kes::Sum6KesSig, traits::KesSig}; |
6 | 2 | use std::{array::TryFromSliceError, ops::Deref}; |
7 | 3 | use thiserror::Error; |
8 | 4 |
|
9 | | -// ------------------------------------------------------------------- SecretKey |
10 | | - |
11 | | -/// KES secret key |
12 | | -pub struct SecretKey<'a>(Sum6Kes<'a>); |
13 | | - |
14 | | -impl SecretKey<'_> { |
15 | | - /// Create a new KES secret key |
16 | | - pub fn from_bytes(sk_bytes: &mut Vec<u8>) -> Result<SecretKey<'_>, Error> { |
17 | | - // TODO: extend() could potentially re-allocate memory to a new location and copy the sk_bytes. |
18 | | - // This would leave the original memory containing the secret key without being wiped. |
19 | | - sk_bytes.extend([0u8; 4]); // default to period = 0 |
20 | | - let sum_6_kes = Sum6Kes::from_bytes(sk_bytes.as_mut_slice())?; |
21 | | - Ok(SecretKey(sum_6_kes)) |
22 | | - } |
23 | | - |
24 | | - /// Get the current period of the KES secret key |
25 | | - pub fn get_period(&self) -> u32 { |
26 | | - self.0.get_period() |
27 | | - } |
28 | | - |
29 | | - /// Update the KES secret key to the next period |
30 | | - pub fn update(&mut self) -> Result<(), Error> { |
31 | | - Ok(self.0.update()?) |
32 | | - } |
33 | | -} |
34 | | - |
35 | 5 | // ------------------------------------------------------------------- PublicKey |
36 | 6 |
|
37 | 7 | /// KES public key |
@@ -61,12 +31,6 @@ impl Deref for PublicKey { |
61 | 31 | } |
62 | 32 | } |
63 | 33 |
|
64 | | -impl From<&SecretKey<'_>> for PublicKey { |
65 | | - fn from(sk: &SecretKey<'_>) -> Self { |
66 | | - PublicKey(sk.0.to_pk()) |
67 | | - } |
68 | | -} |
69 | | - |
70 | 34 | impl From<&[u8; PublicKey::SIZE]> for PublicKey { |
71 | 35 | fn from(bytes: &[u8; PublicKey::SIZE]) -> Self { |
72 | 36 | PublicKey(kes::PublicKey::from_bytes(bytes).unwrap_or_else(|e| { |
@@ -135,6 +99,39 @@ pub enum Error { |
135 | 99 | #[cfg(test)] |
136 | 100 | mod tests { |
137 | 101 | use super::*; |
| 102 | + use kes_summed_ed25519::{kes::Sum6Kes, traits::KesSk}; |
| 103 | + |
| 104 | + // ------------------------------------------------------------------- SecretKey |
| 105 | + |
| 106 | + /// KES secret key |
| 107 | + pub struct SecretKey<'a>(Sum6Kes<'a>); |
| 108 | + |
| 109 | + impl SecretKey<'_> { |
| 110 | + /// Create a new KES secret key |
| 111 | + pub fn from_bytes(sk_bytes: &mut Vec<u8>) -> Result<SecretKey<'_>, Error> { |
| 112 | + // TODO: extend() could potentially re-allocate memory to a new location and copy the sk_bytes. |
| 113 | + // This would leave the original memory containing the secret key without being wiped. |
| 114 | + sk_bytes.extend([0u8; 4]); // default to period = 0 |
| 115 | + let sum_6_kes = Sum6Kes::from_bytes(sk_bytes.as_mut_slice())?; |
| 116 | + Ok(SecretKey(sum_6_kes)) |
| 117 | + } |
| 118 | + |
| 119 | + /// Get the current period of the KES secret key |
| 120 | + pub fn get_period(&self) -> u32 { |
| 121 | + self.0.get_period() |
| 122 | + } |
| 123 | + |
| 124 | + /// Update the KES secret key to the next period |
| 125 | + pub fn update(&mut self) -> Result<(), Error> { |
| 126 | + Ok(self.0.update()?) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + impl From<&SecretKey<'_>> for PublicKey { |
| 131 | + fn from(sk: &SecretKey<'_>) -> Self { |
| 132 | + PublicKey(sk.0.to_pk()) |
| 133 | + } |
| 134 | + } |
138 | 135 |
|
139 | 136 | #[test] |
140 | 137 | fn kes_key_evolution() { |
|
0 commit comments