Skip to content

Commit a4ce8c8

Browse files
committed
more inline documentation
1 parent a25bd11 commit a4ce8c8

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

contracts/utils/Base58.sol

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ library Base58 {
4040
function _encode(bytes memory data) private pure returns (bytes memory encoded) {
4141
// For reference, solidity implementation
4242
// unchecked {
43-
// uint256 dataCLZ = data.countLeading(0x00);
44-
// uint256 length = dataCLZ + ((data.length - dataCLZ) * 8351) / 6115 + 1;
43+
// uint256 dataLeadingZeros = data.countLeading(0x00);
44+
// uint256 length = dataLeadingZeros + ((data.length - dataLeadingZeros) * 8351) / 6115 + 1;
4545
// encoded = new bytes(length);
4646
// uint256 end = length;
4747
// for (uint256 i = 0; i < data.length; ++i) {
@@ -54,8 +54,8 @@ library Base58 {
5454
// end = ptr;
5555
// }
5656
// uint256 encodedCLZ = encoded.countLeading(0x00);
57-
// length -= encodedCLZ - dataCLZ;
58-
// encoded.splice(encodedCLZ - dataCLZ);
57+
// length -= encodedCLZ - dataLeadingZeros;
58+
// encoded.splice(encodedCLZ - dataLeadingZeros);
5959
// for (uint256 i = 0; i < length; ++i) {
6060
// encoded[i] = _TABLE[uint8(encoded[i])];
6161
// }
@@ -86,11 +86,12 @@ library Base58 {
8686
encoded := mload(0x40)
8787
let dataLength := mload(data)
8888

89-
// Count number of zero bytes at the beginning of `data`
90-
let dataCLZ := clzBytes(add(data, 0x20), dataLength)
89+
// Count number of zero bytes at the beginning of `data`. These are encoded using the same number of '1's
90+
// at then beginning of the encoded string.
91+
let dataLeadingZeros := clzBytes(add(data, 0x20), dataLength)
9192

92-
// Initial encoding
93-
let slotLength := add(add(dataCLZ, div(mul(sub(dataLength, dataCLZ), 8351), 6115)), 1)
93+
// Initial encoding length: 100% of zero bytes (zero prefix) + 138% of non zero bytes + 1
94+
let slotLength := add(add(div(mul(sub(dataLength, dataLeadingZeros), 138), 100), dataLeadingZeros), 1)
9495

9596
// Zero the encoded buffer
9697
for {
@@ -121,17 +122,19 @@ library Base58 {
121122
end := ptr
122123
}
123124

124-
// Count number of zero bytes at the beginning of slots
125-
let slotCLZ := clzBytes(add(encoded, 0x20), slotLength)
125+
// Count number of zero bytes at the beginning of slots. This is a pointer to the first non zero slot that
126+
// contains the base58 data. This base58 data span over `slotLength-slotLeadingZeros` bytes.
127+
let slotLeadingZeros := clzBytes(add(encoded, 0x20), slotLength)
126128

127-
// Update length
128-
let offset := sub(slotCLZ, dataCLZ)
129+
// Update length: `slotLength-slotLeadingZeros` of non-zero data plus `dataLeadingZeros` of zero prefix.
130+
let offset := sub(slotLeadingZeros, dataLeadingZeros)
129131
let encodedLength := sub(slotLength, offset)
130132

131133
// Store the encoding table. This overlaps with the FMP that we are going to reset later anyway.
132134
mstore(0x1f, "123456789ABCDEFGHJKLMNPQRSTUVWXY")
133135
mstore(0x3f, "Zabcdefghijkmnopqrstuvwxyz")
134136

137+
// For each slot, use the table to obtain the corresponding base58 "digit".
135138
for {
136139
let i := 0
137140
} lt(i, encodedLength) {
@@ -140,7 +143,7 @@ library Base58 {
140143
mstore8(add(add(encoded, 0x20), i), mload(shr(248, mload(add(add(encoded, 0x20), add(offset, i))))))
141144
}
142145

143-
// Store length and allocate memory
146+
// Store length and allocate (reserve) memory
144147
mstore(encoded, encodedLength)
145148
mstore(0x40, add(add(encoded, 0x20), encodedLength))
146149
}
@@ -184,9 +187,9 @@ library Base58 {
184187
mask = 4;
185188
}
186189

187-
uint256 dataCLZ = data.countLeading(0x31);
188-
uint256 msb = binu.countConsecutive(dataCLZ, 0x00);
189-
return binu.splice(msb * (dataCLZ + msb < binu.length).toUint(), ptr);
190+
uint256 dataLeadingZeros = data.countLeading(0x31);
191+
uint256 msb = binu.countConsecutive(dataLeadingZeros, 0x00);
192+
return binu.splice(msb * (dataLeadingZeros + msb < binu.length).toUint(), ptr);
190193
}
191194
}
192195

0 commit comments

Comments
 (0)