miden-objects 0.17.0-rc.3

Canonical Protobuf representations for Miden protocol objects
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use alloc::collections::BTreeMap;
use alloc::format;
use alloc::vec::Vec;

use miden_protocol::crypto::dsa::ecdsa_k256_keccak::{PublicKey, Signature};
use miden_protocol::crypto::merkle::InnerNodeInfo;
use miden_protocol::crypto::merkle::store::MerkleStore;
use miden_protocol::utils::serde::{Deserializable, Serializable};
use miden_protocol::vm::{AdviceInputs, AdviceMap, AdviceStack, ExecutionProof};
use miden_protocol::{Felt, MastForest, Word};

use super::{MessageDecodeExt, required};
use crate::{ConversionError, ConversionResultExt, proto};

const WORD_SERIALIZED_SIZE: usize = Word::SERIALIZED_SIZE;

fn ensure_exact_length(
    encoded: &[u8],
    expected: usize,
    field: &'static str,
) -> Result<(), ConversionError> {
    if encoded.len() != expected {
        return Err(ConversionError::message(format!(
            "expected exactly {expected} bytes, got {}",
            encoded.len()
        ))
        .context(field));
    }
    Ok(())
}

// FELT
// ================================================================================================

impl From<Felt> for proto::primitives::Felt {
    fn from(value: Felt) -> Self {
        Self { value: value.as_canonical_u64() }
    }
}

impl From<&Felt> for proto::primitives::Felt {
    fn from(value: &Felt) -> Self {
        Self { value: value.as_canonical_u64() }
    }
}

impl TryFrom<proto::primitives::Felt> for Felt {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::Felt) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl TryFrom<&proto::primitives::Felt> for Felt {
    type Error = ConversionError;

    fn try_from(value: &proto::primitives::Felt) -> Result<Self, Self::Error> {
        Self::try_from(value.value).map_err(ConversionError::new).context("felt.value")
    }
}

// WORD
// ================================================================================================

impl From<Word> for proto::primitives::Word {
    fn from(value: Word) -> Self {
        Self { encoded: value.to_bytes() }
    }
}

impl From<&Word> for proto::primitives::Word {
    fn from(value: &Word) -> Self {
        Self { encoded: value.to_bytes() }
    }
}

impl TryFrom<proto::primitives::Word> for Word {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::Word) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl TryFrom<&proto::primitives::Word> for Word {
    type Error = ConversionError;

    fn try_from(value: &proto::primitives::Word) -> Result<Self, Self::Error> {
        ensure_exact_length(&value.encoded, WORD_SERIALIZED_SIZE, "word.encoded")?;
        Self::read_from_bytes(&value.encoded)
            .map_err(|error| ConversionError::deserialization("word.encoded", error))
    }
}

// EXECUTION PROOF
// ================================================================================================

impl From<&ExecutionProof> for proto::primitives::ExecutionProof {
    fn from(value: &ExecutionProof) -> Self {
        Self { encoded: value.to_bytes() }
    }
}

impl From<ExecutionProof> for proto::primitives::ExecutionProof {
    fn from(value: ExecutionProof) -> Self {
        (&value).into()
    }
}

impl TryFrom<proto::primitives::ExecutionProof> for ExecutionProof {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::ExecutionProof) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl TryFrom<&proto::primitives::ExecutionProof> for ExecutionProof {
    type Error = ConversionError;

    fn try_from(value: &proto::primitives::ExecutionProof) -> Result<Self, Self::Error> {
        Self::read_from_bytes(&value.encoded)
            .map_err(|error| ConversionError::deserialization("ExecutionProof", error))
            .map_err(|error| error.context("encoded"))
    }
}

// MAST FOREST
// ================================================================================================

impl From<&MastForest> for proto::primitives::MastForest {
    fn from(value: &MastForest) -> Self {
        Self { encoded: value.to_bytes() }
    }
}

impl From<MastForest> for proto::primitives::MastForest {
    fn from(value: MastForest) -> Self {
        (&value).into()
    }
}

impl TryFrom<proto::primitives::MastForest> for MastForest {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::MastForest) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl TryFrom<&proto::primitives::MastForest> for MastForest {
    type Error = ConversionError;

    fn try_from(value: &proto::primitives::MastForest) -> Result<Self, Self::Error> {
        Self::read_from_bytes(&value.encoded)
            .map_err(|error| ConversionError::deserialization("MastForest", error))
            .map_err(|error| error.context("encoded"))
    }
}

// ADVICE INPUTS
// ================================================================================================

impl From<&AdviceStack> for proto::primitives::AdviceStack {
    fn from(value: &AdviceStack) -> Self {
        Self {
            values: value.iter().map(Into::into).collect(),
        }
    }
}

impl TryFrom<proto::primitives::AdviceStack> for AdviceStack {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::AdviceStack) -> Result<Self, Self::Error> {
        value
            .values
            .into_iter()
            .enumerate()
            .map(|(index, value)| Felt::try_from(value).context(format!("values[{index}]")))
            .collect::<Result<AdviceStack, _>>()
    }
}

impl From<&AdviceMap> for proto::primitives::AdviceMap {
    fn from(value: &AdviceMap) -> Self {
        Self {
            entries: value
                .iter()
                .map(|(key, values)| proto::primitives::AdviceMapEntry {
                    key: Some(key.into()),
                    values: values.iter().map(Into::into).collect(),
                })
                .collect(),
        }
    }
}

impl TryFrom<proto::primitives::AdviceMap> for AdviceMap {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::AdviceMap) -> Result<Self, Self::Error> {
        let mut entries = BTreeMap::new();
        for (index, entry) in value.entries.into_iter().enumerate() {
            let decoder = entry.decoder();
            let entry_context = format!("entries[{index}]");
            let key = required!(decoder, entry.key).context(&entry_context)?;
            let values = entry
                .values
                .into_iter()
                .enumerate()
                .map(|(value_index, value)| {
                    Felt::try_from(value).context(format!("{entry_context}.values[{value_index}]"))
                })
                .collect::<Result<Vec<_>, _>>()?;
            if entries.insert(key, values).is_some() {
                return Err(ConversionError::message("duplicate advice map key")
                    .context(format!("{entry_context}.key")));
            }
        }

        Ok(entries.into())
    }
}

impl From<&MerkleStore> for proto::primitives::MerkleStore {
    fn from(value: &MerkleStore) -> Self {
        let default_nodes = MerkleStore::new()
            .inner_nodes()
            .map(|node| (node.value, (node.left, node.right)))
            .collect::<BTreeMap<_, _>>();
        let mut nodes = value
            .inner_nodes()
            .filter(|node| default_nodes.get(&node.value) != Some(&(node.left, node.right)))
            .collect::<Vec<_>>();
        nodes.sort_by_key(|node| node.value);

        Self {
            nodes: nodes
                .into_iter()
                .map(|node| proto::primitives::MerkleStoreNode {
                    value: Some(node.value.into()),
                    left: Some(node.left.into()),
                    right: Some(node.right.into()),
                })
                .collect(),
        }
    }
}

impl TryFrom<proto::primitives::MerkleStore> for MerkleStore {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::MerkleStore) -> Result<Self, Self::Error> {
        let mut nodes = BTreeMap::new();
        for (index, node) in value.nodes.into_iter().enumerate() {
            let decoder = node.decoder();
            let node_context = format!("nodes[{index}]");
            let parent = required!(decoder, node.value).context(&node_context)?;
            let left = required!(decoder, node.left).context(&node_context)?;
            let right = required!(decoder, node.right).context(&node_context)?;
            if nodes.insert(parent, (left, right)).is_some() {
                return Err(ConversionError::message("duplicate Merkle store parent")
                    .context(format!("{node_context}.value")));
            }
        }

        let mut store = MerkleStore::new();
        store.extend(nodes.into_iter().map(|(value, (left, right))| InnerNodeInfo {
            value,
            left,
            right,
        }));
        Ok(store)
    }
}

impl From<&AdviceInputs> for proto::primitives::AdviceInputs {
    fn from(value: &AdviceInputs) -> Self {
        Self {
            advice_stack: Some((&value.stack()).into()),
            advice_map: Some(value.map().into()),
            merkle_store: Some(value.store().into()),
        }
    }
}

impl TryFrom<proto::primitives::AdviceInputs> for AdviceInputs {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::AdviceInputs) -> Result<Self, Self::Error> {
        let decoder = value.decoder();
        let advice_stack = required!(decoder, value.advice_stack)?;
        let advice_map: AdviceMap = required!(decoder, value.advice_map)?;
        let merkle_store: MerkleStore = required!(decoder, value.merkle_store)?;

        Ok(AdviceInputs::new(advice_stack, advice_map, merkle_store))
    }
}

// PUBLIC KEY
// ================================================================================================

fn decode_public_key_variant(variant: i32) -> Result<(), ConversionError> {
    match proto::primitives::PublicKeyVariant::try_from(variant) {
        Ok(proto::primitives::PublicKeyVariant::EcdsaK256Keccak) => Ok(()),
        Ok(proto::primitives::PublicKeyVariant::Unspecified) => {
            Err(ConversionError::message("public key variant is unspecified"))
        },
        Err(error) => Err(ConversionError::with_source(
            format!("unknown public key variant {variant}"),
            error,
        )),
    }
}

impl From<&PublicKey> for proto::primitives::PublicKey {
    fn from(value: &PublicKey) -> Self {
        Self {
            variant: proto::primitives::PublicKeyVariant::EcdsaK256Keccak as i32,
            encoded: value.to_bytes(),
        }
    }
}

impl From<PublicKey> for proto::primitives::PublicKey {
    fn from(value: PublicKey) -> Self {
        (&value).into()
    }
}

impl TryFrom<proto::primitives::PublicKey> for PublicKey {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::PublicKey) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl TryFrom<&proto::primitives::PublicKey> for PublicKey {
    type Error = ConversionError;

    fn try_from(value: &proto::primitives::PublicKey) -> Result<Self, Self::Error> {
        decode_public_key_variant(value.variant).context("variant")?;
        Self::read_from_bytes(&value.encoded)
            .map_err(|error| ConversionError::deserialization("PublicKey", error))
            .map_err(|error| error.context("encoded"))
    }
}

// SIGNATURE
// ================================================================================================

fn decode_signature_variant(variant: i32) -> Result<(), ConversionError> {
    match proto::primitives::SignatureVariant::try_from(variant) {
        Ok(proto::primitives::SignatureVariant::EcdsaK256Keccak) => Ok(()),
        Ok(proto::primitives::SignatureVariant::Unspecified) => {
            Err(ConversionError::message("signature variant is unspecified"))
        },
        Err(error) => Err(ConversionError::with_source(
            format!("unknown signature variant {variant}"),
            error,
        )),
    }
}

impl From<&Signature> for proto::primitives::Signature {
    fn from(value: &Signature) -> Self {
        Self {
            variant: proto::primitives::SignatureVariant::EcdsaK256Keccak as i32,
            encoded: value.to_bytes(),
        }
    }
}

impl From<Signature> for proto::primitives::Signature {
    fn from(value: Signature) -> Self {
        (&value).into()
    }
}

impl TryFrom<proto::primitives::Signature> for Signature {
    type Error = ConversionError;

    fn try_from(value: proto::primitives::Signature) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl TryFrom<&proto::primitives::Signature> for Signature {
    type Error = ConversionError;

    fn try_from(value: &proto::primitives::Signature) -> Result<Self, Self::Error> {
        decode_signature_variant(value.variant).context("variant")?;
        Self::read_from_bytes(&value.encoded)
            .map_err(|error| ConversionError::deserialization("Signature", error))
            .map_err(|error| error.context("encoded"))
    }
}

#[cfg(test)]
mod tests {
    use alloc::string::ToString;
    use alloc::vec;
    use core::error::Error;

    use assert_matches::assert_matches;
    use miden_protocol::testing::dummy_execution_proof;
    use miden_protocol::testing::random_secret_key::random_secret_key;
    use miden_protocol::utils::serde::DeserializationError;

    use super::*;

    #[test]
    fn felt_roundtrips_zero_and_rejects_the_field_order() {
        for felt in [Felt::ZERO, Felt::from(42_u32)] {
            let encoded = proto::primitives::Felt::from(felt);
            assert_eq!(encoded.value, felt.as_canonical_u64());
            assert_eq!(Felt::try_from(encoded).unwrap(), felt);
        }

        let error = Felt::try_from(proto::primitives::Felt { value: Felt::ORDER }).unwrap_err();
        assert_matches!(
            error
                .source()
                .and_then(|source| source.downcast_ref::<<Felt as TryFrom<u64>>::Error>()),
            Some(source) if source.as_u64() == Felt::ORDER
        );
    }

    #[test]
    fn word_roundtrips_and_rejects_invalid_lengths() {
        let felt = Felt::from(42_u32);

        let word = Word::new([felt, Felt::ZERO, Felt::ONE, Felt::new_unchecked(7)]);
        assert_eq!(Word::try_from(proto::primitives::Word::from(word)).unwrap(), word);

        let error = Word::try_from(proto::primitives::Word { encoded: vec![0; 31] }).unwrap_err();
        assert_eq!(error.to_string(), "word.encoded: expected exactly 32 bytes, got 31");
    }

    #[test]
    fn public_key_and_signature_roundtrip_with_ecdsa_k256_keccak_variants() {
        let signing_key = random_secret_key();
        let public_key = signing_key.public_key();
        let signature = signing_key.sign(Word::empty());

        let encoded_public_key = proto::primitives::PublicKey::from(&public_key);
        assert_eq!(
            encoded_public_key.variant,
            proto::primitives::PublicKeyVariant::EcdsaK256Keccak as i32
        );
        assert_eq!(PublicKey::try_from(encoded_public_key).unwrap(), public_key);

        let encoded_signature = proto::primitives::Signature::from(&signature);
        assert_eq!(
            encoded_signature.variant,
            proto::primitives::SignatureVariant::EcdsaK256Keccak as i32
        );
        assert_eq!(Signature::try_from(encoded_signature).unwrap(), signature);
    }

    #[test]
    fn public_key_and_signature_reject_malformed_encodings() {
        let public_key_error = PublicKey::try_from(proto::primitives::PublicKey {
            variant: proto::primitives::PublicKeyVariant::EcdsaK256Keccak as i32,
            encoded: vec![],
        })
        .unwrap_err();
        assert_matches!(
            public_key_error
                .source()
                .and_then(Error::source)
                .and_then(|source| source.downcast_ref::<DeserializationError>()),
            Some(DeserializationError::UnexpectedEOF)
        );

        let signature_error = Signature::try_from(proto::primitives::Signature {
            variant: proto::primitives::SignatureVariant::EcdsaK256Keccak as i32,
            encoded: vec![],
        })
        .unwrap_err();
        assert_matches!(
            signature_error
                .source()
                .and_then(Error::source)
                .and_then(|source| source.downcast_ref::<DeserializationError>()),
            Some(DeserializationError::UnexpectedEOF)
        );
    }

    #[test]
    fn public_key_and_signature_reject_unspecified_variants_before_decoding_bytes() {
        let public_key_error =
            PublicKey::try_from(proto::primitives::PublicKey { variant: 0, encoded: vec![] })
                .unwrap_err();
        assert_eq!(public_key_error.to_string(), "variant: public key variant is unspecified");

        let signature_error =
            Signature::try_from(proto::primitives::Signature { variant: 0, encoded: vec![] })
                .unwrap_err();
        assert_eq!(signature_error.to_string(), "variant: signature variant is unspecified");
    }

    #[test]
    fn public_key_and_signature_reject_unknown_variants_before_decoding_bytes() {
        let public_key_error = PublicKey::try_from(proto::primitives::PublicKey {
            variant: i32::MAX,
            encoded: vec![],
        })
        .unwrap_err();
        assert_eq!(public_key_error.to_string(), "variant: unknown public key variant 2147483647");

        let signature_error = Signature::try_from(proto::primitives::Signature {
            variant: i32::MAX,
            encoded: vec![],
        })
        .unwrap_err();
        assert_eq!(signature_error.to_string(), "variant: unknown signature variant 2147483647");
    }

    #[test]
    fn execution_proof_roundtrips() {
        let proof = dummy_execution_proof();
        let encoded = proto::primitives::ExecutionProof::from(&proof);
        assert_eq!(ExecutionProof::try_from(encoded).unwrap(), proof);
    }

    #[test]
    fn execution_proof_rejects_unversioned_wire_bytes() {
        let proof = dummy_execution_proof();
        let compatibility = proof.compatibility();
        let compatibility_len = 1
            + compatibility.vm_verifier_roots().to_vec().to_bytes().len()
            + compatibility.pvm_verifier_roots().to_vec().to_bytes().len();
        let unversioned = proof.to_bytes()[compatibility_len..].to_vec();

        let error =
            ExecutionProof::try_from(proto::primitives::ExecutionProof { encoded: unversioned })
                .unwrap_err();

        assert!(error.to_string().starts_with("encoded:"));
    }

    #[test]
    fn mast_forest_roundtrips() {
        let mast = MastForest::new();
        let encoded = proto::primitives::MastForest::from(&mast);
        assert_eq!(MastForest::try_from(encoded).unwrap(), mast);
    }
}