-
Notifications
You must be signed in to change notification settings - Fork 2
/
interoperability.rs
195 lines (168 loc) · 7.37 KB
/
interoperability.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//! Tests to check for interoperability with Haskell function. To generate the
//! test data, available in ./data, we ran the following script using the `cardano-base`
//! implementation. Run the same code with `SumXCompactKES` to generate the data of
//! the compact version.
//!
//! ```haskell
//! import Cardano.Crypto.Seed
//! import Cardano.Crypto.DSIGN
//! import Cardano.Crypto.Hash
//! import Cardano.Crypto.KES
//!
//! import Data.Maybe (fromJust)
//! import qualified Data.ByteString.Char8 as Bytechar
//! import qualified Data.ByteString as B
//!
//!
//! main :: IO()
//! main = let
//! seed = mkSeedFromBytes $ Bytechar.pack "test string of 32 byte of lenght"
//! kesSk0 = genKeyKES @(Sum0KES Ed25519DSIGN) seed
//! kesSk1 = genKeyKES @(Sum1KES Ed25519DSIGN Blake2b_256) seed
//! kesSk = genKeyKES @(Sum6KES Ed25519DSIGN Blake2b_256) seed
//! kesSignature = signKES () 0 (Bytechar.pack "test message") kesSk
//! kesSkTwoUpdate = fromJust (updateKES () kesSkOneUpdate 1)
//! kesSkThreeUpdate = fromJust (updateKES () kesSkTwoUpdate 2)
//! kesSkFourUpdate = fromJust (updateKES () kesSkThreeUpdate 3)
//! kesSkFiveUpdate = fromJust (updateKES () kesSkFourUpdate 4)
//! kesSignatureFive = signKES () 5 (Bytechar.pack "test message") kesSkFiveUpdate
//!
//! in do
//! B.writeFile "<PATH>/key1.bin" (rawSerialiseSignKeyKES kesSk1)
//! B.writeFile "<PATH>/key6.bin" (rawSerialiseSignKeyKES kesSk)
//! B.writeFile "<PATH>/key6Sig.bin" (rawSerialiseSigKES kesSignature)
//! B.writeFile "<PATH>/key6update1.bin" (rawSerialiseSignKeyKES kesSkOneUpdate)
//! B.writeFile "<PATH>/key6update5.bin" (rawSerialiseSignKeyKES kesSkFiveUpdate)
//! B.writeFile "<PATH>/key6Sig5.bin" (rawSerialiseSigKES kesSignatureFive)
//! ```
//!
use kes_summed_ed25519::kes::*;
use kes_summed_ed25519::traits::KesSk;
// The following function is required for parsing a Cardano Shelley formatted key
// into a secret key in this library. Mainly, it appends the period = 0 formatted
// as a u32.
fn from_shelley_format<'a, KES: KesSk<'a>>(key: &'a mut Vec<u8>) -> KES {
assert_eq!(key.len(), KES::SIZE, "Invalid input size");
key.extend_from_slice(&[0u8; 4]);
KES::from_bytes(key).expect("Invalid format")
}
#[test]
fn haskel_depth_1() {
// haskell generated key
let mut h_key = include_bytes!("data/key1.bin").to_vec();
let parsed_h_key: Sum1Kes = from_shelley_format(&mut h_key);
let mut key_buffer = [0u8; Sum1Kes::SIZE + 4];
let mut seed = [
116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103, 32, 111, 102, 32, 51, 50, 32, 98,
121, 116, 101, 32, 111, 102, 32, 108, 101, 110, 103, 104, 116,
];
let (skey, _) = Sum1Kes::keygen(&mut key_buffer, &mut seed);
assert_eq!(skey.as_bytes(), parsed_h_key.as_bytes());
}
#[test]
fn haskell_depth_6() {
let mut h_key = include_bytes!("data/key6.bin").to_vec();
let parsed_h_key: Sum6Kes = from_shelley_format(&mut h_key);
let mut key_buffer = [0u8; Sum6Kes::SIZE + 4];
let mut seed = [
116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103, 32, 111, 102, 32, 51, 50, 32, 98,
121, 116, 101, 32, 111, 102, 32, 108, 101, 110, 103, 104, 116,
];
let (mut skey, _) = Sum6Kes::keygen(&mut key_buffer, &mut seed);
assert_eq!(skey.as_bytes(), parsed_h_key.as_bytes());
let h_1update_key: &[u8; 608] = include_bytes!("data/key6update1.bin");
skey.update().unwrap();
assert_eq!(skey.as_bytes()[..Sum6Kes::SIZE], h_1update_key[..]);
}
#[test]
fn haskell_signature_6() {
// haskell generated signature
let h_signature: &[u8; 448] = include_bytes!("data/key6Sig.bin");
let mut key_buffer = [0u8; Sum6Kes::SIZE + 4];
let mut seed = [
116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103, 32, 111, 102, 32, 51, 50, 32, 98,
121, 116, 101, 32, 111, 102, 32, 108, 101, 110, 103, 104, 116,
];
let message = b"test message";
let (skey, _) = Sum6Kes::keygen(&mut key_buffer, &mut seed);
let signature = skey.sign(message);
assert_eq!(&signature.to_bytes(), h_signature);
}
#[test]
fn haskell_signature_6_update_5() {
let h_signature: &[u8; 448] = include_bytes!("data/key6Sig5.bin");
let mut key_buffer = [0u8; Sum6Kes::SIZE + 4];
let mut seed = [
116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103, 32, 111, 102, 32, 51, 50, 32, 98,
121, 116, 101, 32, 111, 102, 32, 108, 101, 110, 103, 104, 116,
];
let message = b"test message";
let (mut skey, _) = Sum6Kes::keygen(&mut key_buffer, &mut seed);
skey.update().unwrap();
skey.update().unwrap();
skey.update().unwrap();
skey.update().unwrap();
skey.update().unwrap();
let signature = skey.sign(message);
assert_eq!(&signature.to_bytes(), h_signature);
}
#[test]
fn haskel_compact_depth_1() {
// haskell generated key
let mut h_key = include_bytes!("data/compactkey1.bin").to_vec();
let parsed_h_key: Sum1CompactKes = from_shelley_format(&mut h_key);
let mut key_buffer = [0u8; Sum1CompactKes::SIZE + 4];
let mut seed = [
116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103, 32, 111, 102, 32, 51, 50, 32, 98,
121, 116, 101, 32, 111, 102, 32, 108, 101, 110, 103, 104, 116,
];
let (skey, _) = Sum1CompactKes::keygen(&mut key_buffer, &mut seed);
assert_eq!(skey.as_bytes(), parsed_h_key.as_bytes());
}
#[test]
fn haskell_compact_depth_6() {
let mut h_key = include_bytes!("data/compactkey6.bin").to_vec();
let parsed_h_key: Sum6CompactKes = from_shelley_format(&mut h_key);
let mut key_buffer = [0u8; Sum6CompactKes::SIZE + 4];
let mut seed = [
116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103, 32, 111, 102, 32, 51, 50, 32, 98,
121, 116, 101, 32, 111, 102, 32, 108, 101, 110, 103, 104, 116,
];
let (mut skey, _) = Sum6CompactKes::keygen(&mut key_buffer, &mut seed);
assert_eq!(skey.as_bytes(), parsed_h_key.as_bytes());
let h_1update_key: &[u8; 608] = include_bytes!("data/compactkey6update1.bin");
skey.update().unwrap();
assert_eq!(skey.as_bytes()[..Sum6CompactKes::SIZE], h_1update_key[..]);
}
#[test]
fn haskell_compact_signature_6() {
// haskell generated signature
let h_signature: &[u8; 288] = include_bytes!("data/compactkey6Sig.bin");
let mut key_buffer = [0u8; Sum6CompactKes::SIZE + 4];
let mut seed = [
116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103, 32, 111, 102, 32, 51, 50, 32, 98,
121, 116, 101, 32, 111, 102, 32, 108, 101, 110, 103, 104, 116,
];
let message = b"test message";
let (skey, _) = Sum6CompactKes::keygen(&mut key_buffer, &mut seed);
let signature = skey.sign(message);
assert_eq!(&signature.to_bytes(), h_signature);
}
#[test]
fn haskell_compact_signature_6_update_5() {
let h_signature: &[u8; 288] = include_bytes!("data/compactkey6Sig5.bin");
let mut key_buffer = [0u8; Sum6CompactKes::SIZE + 4];
let mut seed = [
116, 101, 115, 116, 32, 115, 116, 114, 105, 110, 103, 32, 111, 102, 32, 51, 50, 32, 98,
121, 116, 101, 32, 111, 102, 32, 108, 101, 110, 103, 104, 116,
];
let message = b"test message";
let (mut skey, _) = Sum6CompactKes::keygen(&mut key_buffer, &mut seed);
skey.update().unwrap();
skey.update().unwrap();
skey.update().unwrap();
skey.update().unwrap();
skey.update().unwrap();
let signature = skey.sign(message);
assert_eq!(&signature.to_bytes(), h_signature);
}