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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! Random number generator based on root VRF key and Merlin transcripts.
use std::cell::RefCell;

use anyhow::anyhow;
use merlin::{Transcript, TranscriptRng};
use rand_core::{CryptoRng, OsRng, RngCore};
use schnorrkel::keys::{ExpansionMode, Keypair, MiniSecretKey};

use oasis_core_runtime::common::crypto::hash::Hash;

use crate::{
    context::Context, dispatcher, keymanager::KeyManagerError, modules::core::Error, state::Mode,
};

/// RNG domain separation context.
const RNG_CONTEXT: &[u8] = b"oasis-runtime-sdk/crypto: rng v1";
/// Per-block root VRF key domain separation context.
const VRF_KEY_CONTEXT: &[u8] = b"oasis-runtime-sdk/crypto: root vrf key v1";

/// A root RNG that can be used to derive domain-separated leaf RNGs.
pub struct RootRng {
    inner: RefCell<Inner>,
    mode: Mode,
    valid: bool,
}

struct Inner {
    /// Merlin transcript for initializing the RNG.
    transcript: Transcript,
    /// A transcript-based RNG (when initialized).
    rng: Option<TranscriptRng>,
}

impl RootRng {
    /// Create a new root RNG.
    pub fn new(mode: Mode) -> Self {
        Self {
            inner: RefCell::new(Inner {
                transcript: Transcript::new(RNG_CONTEXT),
                rng: None,
            }),
            mode,
            valid: true,
        }
    }

    /// Create an invalid root RNG which will fail when any leaf RNGs are requested.
    pub fn invalid() -> Self {
        Self {
            inner: RefCell::new(Inner {
                transcript: Transcript::new(&[]),
                rng: None,
            }),
            mode: Mode::Simulate, // Use a "safe" mode even though it will never be used.
            valid: false,
        }
    }

    fn derive_root_vrf_key<C: Context + ?Sized>(ctx: &C, mode: Mode) -> Result<Keypair, Error> {
        let km = ctx
            .key_manager()
            .ok_or(Error::Abort(dispatcher::Error::KeyManagerFailure(
                KeyManagerError::NotInitialized,
            )))?;
        let round_header_hash = ctx.runtime_header().encoded_hash();
        let key_id = crate::keymanager::get_key_pair_id([
            VRF_KEY_CONTEXT,
            &[mode as u8],
            round_header_hash.as_ref(),
        ]);
        let km_kp = km
            .get_or_create_ephemeral_keys(key_id, ctx.epoch())
            .map_err(|err| Error::Abort(dispatcher::Error::KeyManagerFailure(err)))?
            .input_keypair;
        // The KM returns an ed25519 key, but it needs to be in "expanded" form to use with
        // schnorrkel. Please refer to [`schnorrkel::keys::MiniSecretKey`] for further details.
        let kp = MiniSecretKey::from_bytes(km_kp.sk.0.as_ref())
            .map_err(|err| {
                Error::Abort(dispatcher::Error::KeyManagerFailure(
                    KeyManagerError::Other(anyhow::anyhow!("{}", err)),
                ))
            })?
            .expand_to_keypair(ExpansionMode::Uniform);

        Ok(kp)
    }

    /// Append local entropy to the root RNG.
    ///
    /// # Non-determinism
    ///
    /// Using this method will result in the RNG being non-deterministic.
    pub fn append_local_entropy(&self) {
        if !self.valid {
            return;
        }

        let mut bytes = [0u8; 32];
        OsRng.fill_bytes(&mut bytes);

        let mut inner = self.inner.borrow_mut();
        inner.transcript.append_message(b"local-rng", &bytes);
    }

    /// Append an observed transaction hash to RNG transcript.
    pub fn append_tx(&self, tx_hash: Hash) {
        if !self.valid {
            return;
        }

        let mut inner = self.inner.borrow_mut();
        inner.transcript.append_message(b"tx", tx_hash.as_ref());
    }

    /// Append an observed subcontext to RNG transcript.
    pub fn append_subcontext(&self) {
        if !self.valid {
            return;
        }

        let mut inner = self.inner.borrow_mut();
        inner.transcript.append_message(b"subctx", &[]);
    }

    /// Create an independent leaf RNG using this RNG as its parent.
    pub fn fork<C: Context + ?Sized>(&self, ctx: &C, pers: &[u8]) -> Result<LeafRng, Error> {
        if !self.valid {
            return Err(Error::InvalidArgument(anyhow!("rng is not available")));
        }

        let mut inner = self.inner.borrow_mut();

        // Ensure the RNG is initialized and initialize it if not.
        if inner.rng.is_none() {
            // Derive the root VRF key for the current block.
            let root_vrf_key = Self::derive_root_vrf_key(ctx, self.mode)?;

            // Initialize the root RNG.
            let rng = root_vrf_key
                .vrf_create_hash(&mut inner.transcript)
                .make_merlin_rng(&[]);
            inner.rng = Some(rng);
        }

        // Generate the leaf RNG.
        inner.transcript.append_message(b"fork", pers);

        let rng_builder = inner.transcript.build_rng();
        let parent_rng = inner.rng.as_mut().expect("rng must be initialized");
        let rng = rng_builder.finalize(parent_rng);

        Ok(LeafRng(rng))
    }
}

/// A leaf RNG.
pub struct LeafRng(TranscriptRng);

impl RngCore for LeafRng {
    fn next_u32(&mut self) -> u32 {
        self.0.next_u32()
    }

    fn next_u64(&mut self) -> u64 {
        self.0.next_u64()
    }

    fn fill_bytes(&mut self, dest: &mut [u8]) {
        self.0.fill_bytes(dest)
    }

    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
        self.0.try_fill_bytes(dest)
    }
}

impl CryptoRng for LeafRng {}

#[cfg(test)]
mod test {
    use super::*;

    use crate::{state::Mode, testing::mock};

    #[test]
    fn test_rng_basic() {
        let mut mock = mock::Mock::default();
        let ctx = mock.create_ctx_for_runtime::<mock::EmptyRuntime>(true);

        // Create first root RNG.
        let root_rng = RootRng::new(Mode::Execute);

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes1 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes1);

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes1_1 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes1_1);

        assert_ne!(bytes1, bytes1_1, "rng should apply domain separation");

        // Create second root RNG using the same context so the ephemeral key is shared.
        let root_rng = RootRng::new(Mode::Execute);

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes2 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes2);

        assert_eq!(bytes1, bytes2, "rng should be deterministic");

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes2_1 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes2_1);

        assert_ne!(bytes2, bytes2_1, "rng should apply domain separation");
        assert_eq!(bytes1_1, bytes2_1, "rng should be deterministic");

        // Create third root RNG using the same context, but with different personalization.
        let root_rng = RootRng::new(Mode::Execute);

        let mut leaf_rng = root_rng
            .fork(&ctx, b"domsep")
            .expect("rng fork should work");
        let mut bytes3 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes3);

        assert_ne!(bytes2, bytes3, "rng should apply domain separation");

        // Create another root RNG using the same context, but with different history.
        let root_rng = RootRng::new(Mode::Execute);
        root_rng
            .append_tx("0000000000000000000000000000000000000000000000000000000000000001".into());

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes4 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes4);

        assert_ne!(bytes2, bytes4, "rng should apply domain separation");

        // Create another root RNG using the same context, but with different history.
        let root_rng = RootRng::new(Mode::Execute);
        root_rng
            .append_tx("0000000000000000000000000000000000000000000000000000000000000002".into());

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes5 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes5);

        assert_ne!(bytes4, bytes5, "rng should apply domain separation");

        // Create another root RNG using the same context, but with same history as four.
        let root_rng = RootRng::new(Mode::Execute);
        root_rng
            .append_tx("0000000000000000000000000000000000000000000000000000000000000001".into());

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes6 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes6);

        assert_eq!(bytes4, bytes6, "rng should be deterministic");

        // Create another root RNG using the same context, but with different history.
        let root_rng = RootRng::new(Mode::Execute);
        root_rng
            .append_tx("0000000000000000000000000000000000000000000000000000000000000001".into());
        root_rng
            .append_tx("0000000000000000000000000000000000000000000000000000000000000002".into());

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes7 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes7);

        assert_ne!(bytes4, bytes7, "rng should apply domain separation");

        // Create another root RNG using the same context, but with different init point.
        let root_rng = RootRng::new(Mode::Execute);
        root_rng
            .append_tx("0000000000000000000000000000000000000000000000000000000000000001".into());
        let _ = root_rng.fork(&ctx, &[]).expect("rng fork should work"); // Force init.
        root_rng
            .append_tx("0000000000000000000000000000000000000000000000000000000000000002".into());

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes8 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes8);

        assert_ne!(bytes7, bytes8, "rng should apply domain separation");
        assert_ne!(bytes6, bytes8, "rng should apply domain separation");
    }

    #[test]
    fn test_rng_fail_nonconfidential() {
        let mut mock = mock::Mock::default();
        let ctx = mock.create_ctx_for_runtime::<mock::EmptyRuntime>(false);

        let root_rng = RootRng::new(Mode::Execute);
        assert!(
            root_rng.fork(&ctx, &[]).is_err(),
            "rng fork should fail on non-confidential runtimes"
        );
    }

    #[test]
    fn test_rng_local_entropy() {
        let mut mock = mock::Mock::default();
        let ctx = mock.create_ctx_for_runtime::<mock::EmptyRuntime>(true);

        // Create first root RNG.
        let root_rng = RootRng::new(Mode::Execute);

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes1 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes1);

        // Create second root RNG using the same context, but mix in local entropy.
        let root_rng = RootRng::new(Mode::Execute);
        root_rng.append_local_entropy();

        let mut leaf_rng = root_rng.fork(&ctx, &[]).expect("rng fork should work");
        let mut bytes2 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes2);

        assert_ne!(bytes1, bytes2, "rng should apply domain separation");
    }

    #[test]
    fn test_rng_parent_fork_propagation() {
        let mut mock = mock::Mock::default();
        let ctx = mock.create_ctx_for_runtime::<mock::EmptyRuntime>(true);

        // Create first root RNG.
        let root_rng = RootRng::new(Mode::Execute);

        let mut leaf_rng = root_rng.fork(&ctx, b"a").expect("rng fork should work");
        let mut bytes1 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes1);

        let mut leaf_rng = root_rng.fork(&ctx, b"a").expect("rng fork should work");
        let mut bytes1_1 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes1_1);

        // Create second root RNG.
        let root_rng = RootRng::new(Mode::Execute);

        let mut leaf_rng = root_rng.fork(&ctx, b"b").expect("rng fork should work");
        let mut bytes2 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes2);

        let mut leaf_rng = root_rng.fork(&ctx, b"a").expect("rng fork should work");
        let mut bytes2_1 = [0u8; 32];
        leaf_rng.fill_bytes(&mut bytes2_1);

        assert_ne!(
            bytes1_1, bytes2_1,
            "forks should propagate domain separator to parent"
        );
    }

    #[test]
    fn test_rng_invalid() {
        let mut mock = mock::Mock::default();
        let ctx = mock.create_ctx_for_runtime::<mock::EmptyRuntime>(true);

        let root_rng = RootRng::invalid();
        assert!(
            root_rng.fork(&ctx, b"a").is_err(),
            "rng fork should fail for invalid rng"
        );
    }
}