w3c-ccg / did-method-key

DID (Decentralized Identifier) did:key method for embedding keys in DID urls
https://w3c-ccg.github.io/did-method-key
Other
18 stars 12 forks source link

P256, P384, P512 multicodec uvarint encodings #29

Closed troyronda closed 3 years ago

troyronda commented 3 years ago

To clarify, I think I see most of the encodings in this spec using the Multicodec uvarint encoding. This turns out to mean concatenating 0x01 after the code for most of the curves.

The NIST curves examples also seem to be concatenating 0x01 after the code, but uvarint has a different pattern for those curve's codes.

troyronda commented 3 years ago

@OR13 @msporny Applying uvarint to the P curve codes, the examples would look like the following:

P-256 examples:

P-384 examples:

P-521 examples:

OR13 commented 3 years ago

@troyronda AFAIK, nobody has checked my work on these yet :)

I am trying to follow: https://github.com/multiformats/multicodec/blob/master/table.csv#L123

https://github.com/transmute-industries/did-key.js/blob/master/packages/did-key-web-crypto/src/constants/index.ts#L21

troyronda commented 3 years ago

@OR13 - I took the values from that table and then apply uvarint to it with the results listed above.

OR13 commented 3 years ago

Seems like the spec tests vectors are incorrect, glad we caught this.

OR13 commented 3 years ago

@dlongley @dmitrizagidulin I don't think you guys support the NIST curves yet... but looks like the test vectors I added are not correct.

msporny commented 3 years ago

@dlongley @dmitrizagidulin I don't think you guys support the NIST curves yet... but looks like the test vectors I added are not correct.

Ed25519 is an accepted NIST curve now. :P

But yes, we don't support the legacy P-* NIST curves.

We don't have an ETA on when we would given their legacy nature.

msporny commented 3 years ago

This turns out to mean concatenating 0x01 after the code for most of the curves.

This is a common mistake and has bitten me many times. You can't assume "oh, I just concatenate" anything with the varint stuff... you have to always go through the bitwise encoding. In other words, I hope you're using a varint library to do this and not just concatenating bytes. :)

dlongley commented 3 years ago

The "simple" rule is: if the multicodec for the hash/key/whatever is >= 128 (>= 0x80), then you need to append 0x01 when encoding as a varint ... of course, if the value is even larger than 255 you need to do more: https://github.com/multiformats/unsigned-varint#format

troyronda commented 3 years ago

if the value is even larger than 255 you need to do more

Yup. Hence the update to the P curve examples and test vectors. PR #30 @dlongley