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

Ascertain degree of output length determinism for base58 encoding #95

Closed
wyc opened this issue Feb 21, 2021 · 2 comments
Closed

Ascertain degree of output length determinism for base58 encoding #95

wyc opened this issue Feb 21, 2021 · 2 comments

Comments

@wyc
Copy link
Contributor

wyc commented Feb 21, 2021

We should have the answer to this following riddle to ensure that our libraries are correct in all cases, not just the common ones. This is currently hard-coded in places like did-key's resolution fn.

When an arbitrary bytearray of length n is encoded as base58check, what is the length of bytearray output?

@clehner
Copy link
Contributor

clehner commented Feb 22, 2021

That length check refers to the length of the decoded data - the bytes before encoding with multibase/base58btc.
did:key doesn't place limits on the size of the encoded string or the inner bytes. But some key types have specific-sized public keys. I've responded about the length of secp256k1 keys in #93 (comment).

The length of a byte string encoded into base58 would be: Math.ceil(byteString.length * 8 / Math.log2(58))

For example, a secp256k public key of 33 bytes, plus the 2-byte multicodec prefix is 35 bytes:
Math.ceil(35 * 8 / Math.log2(58)) = 48.
This corresponds to the length of "Q3shokFTS3brHcDQrn82RUDfCZESWL1ZdCEJwekUDPQiYBme" in the example did:key. Then adding the prefix "did:key:z" (z in multicodec indicating base58btc) gives the complete DID which is 57 characters in length.

@wyc
Copy link
Contributor Author

wyc commented Feb 22, 2021

I see we consider variable length base58 encoding here:

let method_specific_id = &did[8..];

Thanks!

@wyc wyc closed this as completed Feb 22, 2021
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

No branches or pull requests

2 participants