Skip to content
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

Protocol writing shortcuts #81

Open
fjarri opened this issue Jan 4, 2025 · 0 comments
Open

Protocol writing shortcuts #81

fjarri opened this issue Jan 4, 2025 · 0 comments
Labels
code quality Making things simpler
Milestone

Comments

@fjarri
Copy link
Member

fjarri commented Jan 4, 2025

During the work on entropyxyz/synedrion#170, a number of repeating patterns in the protocols and evidence verification came up. These can be wrapped in helper methods/traits somehow, to make protocols easier to write and read.

  1. BTreeMap access in the protocols. You would want to return LocalError if the key is not there. The repeating pattern is

    some_map.get(key).ok_or_else(|| LocalError::new(format!("{key} is not found in `some_map`")))?

    Updates for CGGMP'24 synedrion#170 introduces a trait that provides a safe_get() method for maps that returns a LocalError with the provided error message. This flattens the code with multiple map accesses (which are pretty common).

  2. BTreeMap access in evidence verification. The pattern is the same, except here if the key is not found it is almost always InvalidEvidence, not a LocalError, so it will require a separate method.

  3. Downcasting a BTreeMap<Id, Payload> (or Artifact) to a map of typed payloads, needed in pretty much every finalize(). synedrion has a trait providing downcast_all() for maps.

  4. Getting the data for a specific round from previous_messages and combined_echos in verify_messages_constitute_error(), returning InvalidEvidence if the corresponding key is not present. While it's not that much to write, it repeats so many times in the evidence verification code that it's worth simplifying. This also makes the code flatter and easier to read.

  5. Deserializing all echos from combined_echos and returning InvalidEvidence on error.

  6. Flattening error checks in evidence verification. For example, instead of

    if r2_message.data.hash(&sid_hash, guilty_party) != r1_message.cap_v {
       Ok(())
    } else {
       Err(ProtocolValidationError::InvalidEvidence(
          "The received hash is valid".into(),
       ))
    }

    (a pattern that repeats for every error, and there can be a dozen of them) it would be nice to write

    verify_that(r2_message.data.hash(&sid_hash, guilty_party) != r1_message.cap_v)

    Note that we don't really need to provide an error message here because there's only one error like that per variant of the error enum, which is already documented.

    Alternatively, this can be a method on bool instead of a function.

  7. Declaring the required messages for evidence verification. Can we make the following example simpler?

    RequiredMessages::new(
        RequiredMessageParts::normal_broadcast_only(),
        Some([(RoundId::new(1), RequiredMessageParts::echo_broadcast_only())].into()),
        None,
    )
  8. In particular, should we add RequiredMessageParts::without_normal_broadcast() etc, to cover all the remaining combinations of (echo_broadcast, normal_broadcast, direct_message)?

Currently these are all implemented in entropyxyz/synedrion#170, but I wonder if it is worth moving these traits to manul. An alternative to traits is custom types, but protocol already exports a huge number of types, so I don't know if that would be a good idea.

@fjarri fjarri added the code quality Making things simpler label Jan 4, 2025
@fjarri fjarri added this to the v0.2.0 milestone Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality Making things simpler
Projects
None yet
Development

No branches or pull requests

1 participant