Skip to content

feat: support bytes codec in ProtocolKey #2557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jun 18, 2025

Conversation

jpraynaud
Copy link
Member

@jpraynaud jpraynaud commented Jun 6, 2025

Content

This PR includes the support for bytes codec in ProtocolKey.

Serialization and Deserialization Enhancements:

  • Binary Serialization Traits: Added TryToBytes and TryFromBytes traits to standardize binary serialization and deserialization across multiple cryptographic structures. (mithril-common/src/crypto_helper/codec/binary.rs, mithril-common/src/crypto_helper/codec/binary.rsR1-R261)
  • CBOR Serialization Utilities: Introduced methods like to_cbor_bytes, to_cbor_hex, from_cbor_bytes, and from_cbor_hex for CBOR encoding and decoding in the SerDeShelleyFileFormat trait. (mithril-common/src/crypto_helper/cardano/codec.rs, [1] [2]
  • Bech32 Encoding: Added a utility function encode_bech32 for Bech32 encoding with test coverage. (mithril-common/src/crypto_helper/codec/bech32.rs, mithril-common/src/crypto_helper/codec/bech32.rsR1-R29)

Modularization:

  • Codec Utilities: Refactored codec-related functionalities into separate modules (bech32, binary, json_hex) for improved modularity and maintainability. (mithril-common/src/crypto_helper/codec/mod.rs, mithril-common/src/crypto_helper/codec/mod.rsR1-R7)
  • File Renaming: Renamed mithril-common/src/crypto_helper/codec.rs to mithril-common/src/crypto_helper/codec/json_hex.rs and removed Bech32-related code from this file. (mithril-common/src/crypto_helper/codec/json_hex.rs, [1] [2] [3]

Cryptographic Structures Updates:

  • StmInitializerWrapper: Enhanced safety in byte slicing operations using get() to avoid panics. (mithril-common/src/crypto_helper/cardano/key_certification.rs, mithril-common/src/crypto_helper/cardano/key_certification.rsL233-R239)
  • MKMapProof and MKProof: Added serialization and deserialization methods for Merkle proofs, with corresponding tests to ensure correctness. (mithril-common/src/crypto_helper/merkle_map.rs, [1] [2]; mithril-common/src/crypto_helper/merkle_tree.rs, [3]

Dependency Updates:

  • Bincode Library: Added bincode dependency with serde feature for efficient binary serialization. (mithril-common/Cargo.toml, mithril-common/Cargo.tomlR49)

Test Coverage:

Pre-submit checklist

  • Branch
    • Tests are provided (if possible)
    • Crates versions are updated (if relevant)
    • CHANGELOG file is updated (if relevant)
    • Commit sequence broadly makes sense
    • Key commits have useful messages
  • PR
    • All check jobs of the CI have succeeded
    • Self-reviewed the diff
    • Useful pull request description
    • Reviewer requested
  • Documentation
    • No new TODOs introduced

Issue(s)

Closes #2536

@jpraynaud jpraynaud self-assigned this Jun 6, 2025
@jpraynaud jpraynaud changed the base branch from main to jpraynaud/2536-fix-bytes-codec-stm June 6, 2025 14:56
@jpraynaud jpraynaud requested a review from Copilot June 6, 2025 14:56
Copilot

This comment was marked as outdated.

Copy link

github-actions bot commented Jun 6, 2025

Test Results

    3 files  ± 0     88 suites  ±0   13m 55s ⏱️ +34s
2 022 tests + 8  2 022 ✅ + 8  0 💤 ±0  0 ❌ ±0 
3 563 runs  +15  3 563 ✅ +15  0 💤 ±0  0 ❌ ±0 

Results for commit 873cdb8. ± Comparison against base commit 90439c9.

This pull request removes 5 and adds 13 tests. Note that renamed tests count towards both.
mithril-common ‑ crypto_helper::codec::tests::test_bech32_encode
mithril-common ‑ crypto_helper::codec::tests::test_key_decode_hex_with_leading_and_trailing_whitespace
mithril-common ‑ crypto_helper::codec::tests::test_key_encode_decode_hex
mithril-common ‑ crypto_helper::types::protocol_key::test::can_deserialize_a_struct_containing_a_verification_key
mithril-common ‑ messages::certificate::tests::test_current_json_deserialized_into_current_message
mithril-aggregator ‑ message_adapters::from_register_signature::tests::golden_protocol_key_encodings::restorations_from_json_hex_and_bytes_hex_give_same_signature
mithril-common ‑ crypto_helper::codec::bech32::tests::test_bech32_encode
mithril-common ‑ crypto_helper::codec::json_hex::tests::test_key_decode_hex_with_leading_and_trailing_whitespace
mithril-common ‑ crypto_helper::codec::json_hex::tests::test_key_encode_decode_hex
mithril-common ‑ crypto_helper::merkle_map::tests::test_mk_map_should_serialize_deserialize_proof
mithril-common ‑ crypto_helper::merkle_tree::tests::test_should_serialize_deserialize_proof
mithril-common ‑ crypto_helper::types::protocol_key::test::can_deserialize_a_struct_containing_a_bytes_hex_verification_key
mithril-common ‑ crypto_helper::types::protocol_key::test::can_deserialize_a_struct_containing_a_json_hex_verification_key
mithril-common ‑ messages::certificate::tests::golden_json_serialization::test_current_json_deserialized_into_current_message
mithril-common ‑ messages::certificate::tests::golden_protocol_key_encodings::genesis_certificate::restorations_from_bytes_hex_succeeds
…

♻️ This comment has been updated with latest results.

@jpraynaud jpraynaud temporarily deployed to testing-preview June 6, 2025 15:21 — with GitHub Actions Inactive
@jpraynaud jpraynaud force-pushed the jpraynaud/2536-fix-bytes-codec-stm branch from 96b5396 to dce8f68 Compare June 10, 2025 16:19
@jpraynaud jpraynaud force-pushed the jpraynaud/2536-support-bytes-encoding branch from b79692a to c49bdec Compare June 10, 2025 16:47
@jpraynaud jpraynaud force-pushed the jpraynaud/2536-support-bytes-encoding branch from c49bdec to e9ebeb2 Compare June 11, 2025 09:51
@jpraynaud jpraynaud temporarily deployed to testing-preview June 11, 2025 10:02 — with GitHub Actions Inactive
@jpraynaud jpraynaud force-pushed the jpraynaud/2536-fix-bytes-codec-stm branch 2 times, most recently from 41c907d to 919b73d Compare June 11, 2025 16:14
Base automatically changed from jpraynaud/2536-fix-bytes-codec-stm to main June 12, 2025 07:35
@jpraynaud jpraynaud force-pushed the jpraynaud/2536-support-bytes-encoding branch from e9ebeb2 to 0745bb3 Compare June 12, 2025 12:51
@jpraynaud jpraynaud temporarily deployed to testing-preview June 12, 2025 13:02 — with GitHub Actions Inactive
@jpraynaud jpraynaud force-pushed the jpraynaud/2536-support-bytes-encoding branch from 0745bb3 to 09ad81c Compare June 12, 2025 14:34
@jpraynaud jpraynaud temporarily deployed to testing-preview June 12, 2025 14:43 — with GitHub Actions Inactive
@jpraynaud jpraynaud force-pushed the jpraynaud/2536-support-bytes-encoding branch from 09ad81c to ac4adcc Compare June 13, 2025 11:05
@jpraynaud jpraynaud temporarily deployed to testing-preview June 13, 2025 11:14 — with GitHub Actions Inactive
@jpraynaud jpraynaud force-pushed the jpraynaud/2536-support-bytes-encoding branch from ac4adcc to 4a54f95 Compare June 17, 2025 13:55
@jpraynaud jpraynaud temporarily deployed to testing-preview June 17, 2025 14:04 — with GitHub Actions Inactive
@jpraynaud jpraynaud requested a review from Copilot June 17, 2025 14:09
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements bytes codec support for ProtocolKey along with broad enhancements to binary encoding/decoding, error handling improvements, and code refactoring across cryptographic utilities and related modules. Key changes include:

  • Introduction and integration of TryToBytes/TryFromBytes traits for binary serialization across multiple cryptographic types.
  • Enhanced error context and safer slicing for deserialization in functions like from_bytes.
  • Refactoring of codec modules (bech32, binary, json_hex) and updates to Debug implementations and test helpers.

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
mithril-common/src/test_utils/fake_keys.rs Added trait bounds to assert conversions in test helpers.
mithril-common/src/messages/message_parts/signer.rs Adjusted Debug trait implementations for Signer and SignerWithStake types.
mithril-common/src/messages/certificate.rs Propagated error context in genesis signature encoding.
mithril-common/src/entities/certificate.rs Updated ProtocolKey usage with unwrap in byte encoding conversion.
mithril-common/src/crypto_helper/types/protocol_key.rs Extended trait bounds to support binary codec implementations.
mithril-common/src/crypto_helper/merkle_tree.rs & merkle_map.rs Introduced to_bytes and from_bytes functions for proof serialization.
mithril-common/src/crypto_helper/cardano/key_certification.rs Modified slicing for safety when reading initializer bytes.
mithril-common/src/crypto_helper/cardano/codec.rs Revised CBOR serialization/deserialization with hex encoding.
Cargo.toml Added bincode dependency with serde support.
mithril-aggregator/src/database/record/certificate.rs Updated conversion of genesis signature to bytes hex using unwrap.
mithril-aggregator/src/message_adapters/from_register_signature.rs Added golden tests to verify consistency between json hex and bytes hex encodings.
Comments suppressed due to low confidence (1)

mithril-common/src/messages/message_parts/signer.rs:276

  • [nitpick] Verify that the Debug trait implementations for 'SignerMessagePart' and 'SignerWithStakeMessagePart' are correctly assigned to avoid naming inconsistencies that could lead to confusing debug output.
impl Debug for SignerMessagePart {

@jpraynaud jpraynaud marked this pull request as ready for review June 17, 2025 14:30
Copy link
Collaborator

@Alenar Alenar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@dlachaume dlachaume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

jpraynaud added 19 commits June 18, 2025 12:01
And add support for failure of the conversion.
To make sure that both json hex and bytes hex encoding are supported.
To make sure that both json hex and bytes hex encoding are supported.
To make sure that both json hex and bytes hex encoding are supported.
* mithril-aggregator from `0.7.62` to `0.7.63`
* mithril-common from `0.5.39` to `0.5.40`
@jpraynaud jpraynaud force-pushed the jpraynaud/2536-support-bytes-encoding branch from 4a54f95 to 873cdb8 Compare June 18, 2025 10:01
@jpraynaud jpraynaud temporarily deployed to testing-preview June 18, 2025 10:09 — with GitHub Actions Inactive
Copy link
Collaborator

@turmelclem turmelclem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jpraynaud jpraynaud merged commit 95ee801 into main Jun 18, 2025
38 checks passed
@jpraynaud jpraynaud deleted the jpraynaud/2536-support-bytes-encoding branch June 18, 2025 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Switch messages encoding to binary hex - Phase 1
4 participants