Skip to content

HashMap parameters are checked for size, not for contents #479

@hdevalence

Description

@hdevalence

Several parts of the DKG API use HashMaps to pass a map that indexes some data by an Identifier. Surprisingly, these methods require that the map has a specific size, not just that it contains all of the relevant data. This makes test code much more verbose. For instance, we ended up writing

    // Round 1 is a broadcast, so it's enough to copy all the round 1 packages.
    let mut round2_secrets = HashMap::new();
    let mut round2_packages = HashMap::new();

    for id in &ids {
        let round1_secret = round1_secrets.remove(id).unwrap();

        let mut round1_packages_except_us = round1_packages.clone();
        round1_packages_except_us.remove(id);

        let (secret, packages) =
            frost::keys::dkg::part2(round1_secret, &round1_packages_except_us)?;
        round2_secrets.insert(*id, secret);
        round2_packages.insert(*id, packages);
    }

    // Round 2 is point-to-point (but we're faking it), so we need to
    // build a map of messages received by each participant.
    let mut shares = HashMap::new();
    let mut public_key_packages = HashMap::new();

    for id in &ids {
        let mut recvd_packages = HashMap::new();
        for (other_id, its_packages) in &round2_packages {
            if other_id == id {
                continue;
            }
            recvd_packages.insert(*other_id, its_packages.get(id).unwrap().clone());
        }

        let mut round1_packages_except_us = round1_packages.clone();
        round1_packages_except_us.remove(id);

        let round2_secret = round2_secrets.remove(id).unwrap();
        let (key_package, public_key_package) =
            frost::keys::dkg::part3(&round2_secret, &round1_packages_except_us, &recvd_packages)?;

        shares.insert(id, key_package);
        public_key_packages.insert(*id, public_key_package);
    }

While in a real application this might not be a problem, it's awkward to have to carefully remove elements from the HashMap in test code.

Instead, the methods could check that all of the required keys are present, and ignore any additional keys that it wouldn't be using.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Product Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions