decentralized-identity / did-key.rs

Rust implementation of the did:key method
Apache License 2.0
47 stars 24 forks source link

BLS test vectors don't resolve correctly #30

Open vdods opened 2 years ago

vdods commented 2 years ago

More test cases here: https://github.com/LedgerDomain/did-key.rs/blob/main/src/lib.rs

In particular, the BLS key type test vectors don't resolve correctly. There's a difference between the did-key crate generated BLS keys, which start with did:key:z5T (or maybe did:key:z5Tc), whereas the ones in the spec https://w3c-ccg.github.io/did-method-key/#bls-12381 which start with did:key:zQ3s.

vdods commented 2 years ago

There's a mismatch between Bls12381KeyPairs::get_fingerprint_g2

    fn get_fingerprint_g2(&self) -> String {
        let codec: &[u8] = &[0xeb, 0x1];
        let data = [codec, &self.pk_g2.to_bytes()[..]].concat().to_vec();
        format!("z{}", bs58::encode(data).into_string())
    }

vs impl TryFrom<&str> for KeyPair with case [0xee, 0x1] => KeyPair::Bls12381G1G2(Bls12381KeyPairs::from_public_key(&pub_key[2..])),

As a reference, the ssi crate uses a fingerprint of [0xeb, 0x01] (which matches the did-key spec test vectors; see https://github.com/spruceid/ssi/blob/13b5f601eada99eb2ce9d354dd9eb81dea19cdbc/did-key/src/lib.rs#L23 ).

tmarkovski commented 2 years ago

This problem stems from the combined use of G1/G2 in a single key. Initially, did:key defined: G1 keys, G2, and a combination of G1/G2 with its own multicodec value, starting with z5TC. Here are some JS test vectors that use this. They correspond to the bls12_381-g1g2-pub type in multicodec. I'm not sure if this key type is still supported, it seems it's not documented enough in the spec, but one can imply it's supported since it's defined in multicodec.

Having said that, the single group bls12_381-g1-pub and bls12_381-g2-pub keys are not actually supported by this library - I kinda ran out of time initially, so I only added the combined version.

vdods commented 2 years ago

Ah, gotcha!