Skip to content

Commit 697b10e

Browse files
authored
Merge pull request #674 from input-output-hk/iquerejeta/update-kes
Update kes dependency
2 parents cab17fa + b592b22 commit 697b10e

File tree

6 files changed

+85
-21
lines changed

6 files changed

+85
-21
lines changed

Cargo.lock

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-common/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-common"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
authors = { workspace = true }
55
edition = { workspace = true }
66
documentation = { workspace = true }
@@ -27,7 +27,7 @@ glob = "0.3"
2727
hex = "0.4.3"
2828
http = "0.2.6"
2929
jsonschema = "0.16.0"
30-
kes-summed-ed25519 = { git = "https://github.com/input-output-hk/kes", rev = "1418efa", features = ["serde_enabled"] }
30+
kes-summed-ed25519 = { version = "0.1.1", features = ["serde_enabled"] }
3131
mockall = "0.11.0"
3232
nom = "7.1"
3333
rand-chacha-dalek-compat = { package = "rand_chacha", version = "0.2" }

mithril-common/src/crypto_helper/cardano/codec.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
5555
/// The description of the Cardano key
5656
const DESCRIPTION: &'static str;
5757

58-
/// Deserialize a Cardano key from file
58+
/// Deserialize a type `T: Serialize + DeserializeOwned` from file following Cardano
59+
/// Shelley file format.
5960
fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, ParseError> {
6061
let data = fs::read_to_string(path)?;
6162
let file: ShelleyFileFormat = serde_json::from_str(&data)?;
@@ -65,7 +66,8 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
6566
Ok(a)
6667
}
6768

68-
/// Serialize a Cardano Key to file
69+
/// Serialize a type `T: Serialize + DeserializeOwned` to file following Cardano
70+
/// Shelley file format.
6971
fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), ParseError> {
7072
let cbor_string = hex::encode(serde_cbor::to_vec(&self)?);
7173

@@ -86,4 +88,54 @@ pub trait SerDeShelleyFileFormat: Serialize + DeserializeOwned {
8688
impl SerDeShelleyFileFormat for Sum6Kes {
8789
const TYPE: &'static str = "KesSigningKey_ed25519_kes_2^6";
8890
const DESCRIPTION: &'static str = "KES Signing Key";
91+
92+
/// Deserialize a Cardano key from file. Cardano KES key Shelley format does not
93+
/// contain the period (it is always zero). Therefore we need to include it in the
94+
/// deserialisation.
95+
fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, ParseError> {
96+
let data = fs::read_to_string(path)?;
97+
let file: ShelleyFileFormat = serde_json::from_str(&data)?;
98+
let mut hex_vector = Vec::from_hex(file.cbor_hex)?;
99+
100+
// We check whether the serialisation was performed by the haskell library or the rust library
101+
if (hex_vector[2] & 4u8) == 0 {
102+
// First we need to change the cbor format to notify about the extra 4 bytes:
103+
hex_vector[2] |= 4u8;
104+
// Then we append the bytes representing the period = 0
105+
hex_vector.extend_from_slice(&[0u8; 4]);
106+
}
107+
108+
let a: Self = serde_cbor::from_slice(&hex_vector)?;
109+
Ok(a)
110+
}
111+
}
112+
113+
#[cfg(all(test))]
114+
mod test {
115+
use super::*;
116+
117+
#[test]
118+
fn compat_with_shelly_format() {
119+
let temp_dir = std::env::temp_dir().join("testing");
120+
fs::create_dir_all(&temp_dir).expect("temp dir creation should not fail");
121+
let sk_dir = temp_dir.join("dummy.skey");
122+
let cbor_string = "590260fe77acdfa56281e4b05198f5136018057a65f425411f0990cac4aca0f2917aa00a3d51e191f6f425d870aca3c6a2a41833621f5729d7bc0e3dfc3ae77d057e5e1253b71def7a54157b9f98973ca3c49edd9f311e5f4b23ac268b56a6ac040c14c6d2217925492e42f00dc89a2a01ff363571df0ca0db5ba37001cee56790cc01cd69c6aa760fca55a65a110305ea3c11da0a27be345a589329a584ebfc499c43c55e8c6db5d9c0b014692533ee78abd7ac1e79f7ec9335c7551d31668369b4d5111db78072f010043e35e5ca7f11acc3c05b26b9c7fe56f02aa41544f00cb7685e87f34c73b617260ade3c7b8d8c4df46693694998f85ad80d2cbab0b575b6ccd65d90574e84368169578bff57f751bc94f7eec5c0d7055ec88891a69545eedbfbd3c5f1b1c1fe09c14099f6b052aa215efdc5cb6cdc84aa810db41dbe8cb7d28f7c4beb75cc53915d3ac75fc9d0bf1c734a46e401e15150c147d013a938b7e07cc4f25a582b914e94783d15896530409b8acbe31ef471de8a1988ac78dfb7510729eff008084885f07df870b65e4f382ca15908e1dcda77384b5c724350de90cec22b1dcbb1cdaed88da08bb4772a82266ec154f5887f89860d0920dba705c45957ef6d93e42f6c9509c966277d368dd0eefa67c8147aa15d40a222f7953a4f34616500b310d00aa1b5b73eb237dc4f76c0c16813d321b2fc5ac97039be25b22509d1201d61f4ccc11cd4ff40fffe39f0e937b4722074d8e073a775d7283b715d46f79ce128e3f1362f35615fa72364d20b6db841193d96e58d9d8e86b516bbd1f05e45b39823a93f6e9f29d9e01acf2c12c072d1c64e0afbbabf6903ef542e".to_string();
123+
124+
let file_format = ShelleyFileFormat {
125+
file_type: Sum6Kes::TYPE.to_string(),
126+
description: Sum6Kes::DESCRIPTION.to_string(),
127+
cbor_hex: cbor_string,
128+
};
129+
130+
let mut file =
131+
fs::File::create(sk_dir.clone()).expect("Unexpected error with file creation.");
132+
let json_str =
133+
serde_json::to_string(&file_format).expect("Unexpected error with serialisation.");
134+
135+
write!(file, "{}", json_str).expect("Unexpected error writing to file.");
136+
137+
let kes_sk = Sum6Kes::from_file(&sk_dir);
138+
139+
assert!(kes_sk.is_ok(), "Failure parsing Shelley file format.");
140+
}
89141
}

mithril-common/src/crypto_helper/cardano/key_certification.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use thiserror::Error;
2929
type D = Blake2b<U32>;
3030

3131
/// The KES period that is used to check if the KES keys is expired
32-
pub type KESPeriod = usize;
32+
pub type KESPeriod = u32;
3333

3434
/// New registration error
3535
#[derive(Error, Debug, PartialEq, Eq)]
@@ -53,7 +53,7 @@ pub enum ProtocolRegistrationErrorWrapper {
5353

5454
/// Error raised when a KES Signature verification fails
5555
#[error("KES signature verification error: CurrentKesPeriod={0}, StartKesPeriod={1}")]
56-
KesSignatureInvalid(usize, u64),
56+
KesSignatureInvalid(u32, u64),
5757

5858
/// Error raised when a KES Signature is needed but not provided
5959
#[error("missing KES signature")]
@@ -82,6 +82,10 @@ pub enum ProtocolInitializerErrorWrapper {
8282
/// Error raised when a KES update error occurs
8383
#[error("KES key cannot be updated for period {0}")]
8484
KesUpdate(KESPeriod),
85+
86+
/// Period of key file does not match with period provided by user
87+
#[error("Period of key file, {0}, does not match with period provided by user, {1}")]
88+
KesMismatch(KESPeriod, KESPeriod),
8589
}
8690
/// Wrapper structure for [MithrilStm:StmInitializer](mithril_stm::stm::StmInitializer).
8791
/// It now obtains a KES signature over the Mithril key. This allows the signers prove
@@ -119,17 +123,23 @@ impl StmInitializerWrapper {
119123
let kes_signature = if let Some(kes_sk_path) = kes_sk_path {
120124
let mut kes_sk: Sum6Kes = Sum6Kes::from_file(kes_sk_path)?;
121125

122-
// We need to perform the evolutions, as the key is stored in evolution 0 in `kes.skey`
123-
for period in 0..kes_period.unwrap_or_default() {
126+
let kes_sk_period = kes_sk.get_period();
127+
let provided_period = kes_period.unwrap_or_default();
128+
if kes_sk_period > provided_period {
129+
return Err(ProtocolInitializerErrorWrapper::KesMismatch(
130+
kes_sk_period,
131+
provided_period,
132+
));
133+
}
134+
135+
// We need to perform the evolutions
136+
for period in kes_sk_period..provided_period {
124137
kes_sk
125-
.update(period)
138+
.update()
126139
.map_err(|_| ProtocolInitializerErrorWrapper::KesUpdate(period))?;
127140
}
128141

129-
Some(kes_sk.sign(
130-
kes_period.unwrap_or_default(),
131-
&stm_initializer.verification_key().to_bytes(),
132-
))
142+
Some(kes_sk.sign(&stm_initializer.verification_key().to_bytes()))
133143
} else {
134144
println!("WARNING: Non certified signer registration by providing only a Pool Id is decommissionned and must be used for tests only!");
135145
None
@@ -148,7 +158,7 @@ impl StmInitializerWrapper {
148158

149159
/// Extract the verification key signature.
150160
pub fn verification_key_signature(&self) -> Option<ProtocolSignerVerificationKeySignature> {
151-
self.kes_signature.clone()
161+
self.kes_signature
152162
}
153163

154164
/// Extract the stake of the party

mithril-common/src/crypto_helper/cardano/opcert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use blake2::{digest::consts::U28, Blake2b, Digest};
99
#[cfg(any(test, feature = "test_only"))]
1010
use ed25519_dalek::{Keypair as EdKeypair, Signer};
1111
use ed25519_dalek::{PublicKey as EdPublicKey, Signature as EdSignature, Verifier};
12-
use kes_summed_ed25519::common::PublicKey as KesPublicKey;
12+
use kes_summed_ed25519::PublicKey as KesPublicKey;
1313
use serde::de::Error;
1414
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1515
use sha2::Sha256;

mithril-signer/src/single_signer.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use std::path::PathBuf;
44
use thiserror::Error;
55

66
use mithril_common::crypto_helper::{
7-
key_decode_hex, key_encode_hex, ProtocolClerk, ProtocolInitializer, ProtocolKeyRegistration,
8-
ProtocolPartyId, ProtocolRegistrationError, ProtocolSigner, ProtocolStakeDistribution,
7+
key_decode_hex, key_encode_hex, KESPeriod, ProtocolClerk, ProtocolInitializer,
8+
ProtocolKeyRegistration, ProtocolPartyId, ProtocolRegistrationError, ProtocolSigner,
9+
ProtocolStakeDistribution,
910
};
1011
use mithril_common::entities::{
1112
PartyId, ProtocolMessage, ProtocolParameters, SignerWithStake, SingleSignatures, Stake,
@@ -38,7 +39,7 @@ impl MithrilProtocolInitializerBuilder {
3839
stake: &Stake,
3940
protocol_parameters: &ProtocolParameters,
4041
kes_secret_key_path: Option<PathBuf>,
41-
kes_period: Option<usize>,
42+
kes_period: Option<KESPeriod>,
4243
) -> Result<ProtocolInitializer, MithrilProtocolInitializerBuilderError> {
4344
let mut rng = rand_core::OsRng;
4445
let protocol_initializer = ProtocolInitializer::setup(

0 commit comments

Comments
 (0)