fluter
fluter

Reputation: 13786

Size of buffer to hold base58 encoded data

When trying to understand how base58check works, in the referenced implementation by bitcoin, when calculating the size needed to hold a base58 encoded string, it used following formula:

// https://github.com/bitcoin/libbase58/blob/master/base58.c#L155
size = (binsz - zcount) * 138 / 100 + 1;

where binsz is the size of the input buffer to encode, and zcount is the number of leading zeros in the buffer. What is 138 and 100 coming from and why?

Upvotes: 2

Views: 2218

Answers (1)

sleepdeficit
sleepdeficit

Reputation: 46

tl;dr It’s a formula to approximate the output size during base58 <-> base256 conversion.
i.e. the encoding/decoding parts where you’re multiplying and mod’ing by 256 and 58

Encoding output is ~138% of the input size (+1/rounded up):

n * log(256) / log(58) + 1  
(n * 138 / 100 + 1)

Decoding output is ~73% of the input size (+1/rounded up):

n * log(58) / log(256) + 1  
( n * 733 /1000 + 1)

Upvotes: 3

Related Questions