Skip to content

fix(ciphers): replace base58.py with a real Base58 implementation - #15638

Open
SakshiYeole wants to merge 1 commit into
TheAlgorithms:masterfrom
SakshiYeole:fix-base58-implementation
Open

SakshiYeole wants to merge 1 commit into
TheAlgorithms:masterfrom
SakshiYeole:fix-base58-implementation

Conversation

@SakshiYeole

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

ciphers/base58.py was a byte-identical copy of ciphers/base64_cipher.py, so the repository had no Base58 implementation (see #15637). This replaces it with a Base58 encoder/decoder using the Bitcoin alphabet.

  • base58_encode(data: bytes) -> bytes
  • base58_decode(encoded_data: str | bytes) -> bytes
  • Each leading zero byte is written as a leading 1 and restored on decode.
  • Wrong input types raise TypeError; characters outside the alphabet (0, O, I, l, ...) raise ValueError.

The signatures mirror base64_cipher.py (bytes out, str or bytes accepted by the decoder), so the base* modules in ciphers/ stay consistent.

Note on the issue's examples: base58_encode(b"\x00\x01") is 12 (one leading zero byte gives one 1, and the value 1 is 2), and 2z decodes to b"s". The doctests use the verified values.

Verification: doctests pass; ruff check, ruff format and pre-commit pass; 12,600 random inputs (including leading zero bytes) match the PyPI base58 package in both directions. I also ran the full test suite locally (headless): the only 2 failures are web_programming/instagram_crawler.py (needs network) and machine_learning/linear_discriminant_analysis.py::gaussian_distribution, which fail identically on a clean upstream/master.

Fixes #15637

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

ciphers/base58.py was a byte-identical copy of ciphers/base64_cipher.py,
so the repository had no Base58 code. Replace it with an encoder and
decoder for the Bitcoin alphabet:

- base58_encode(bytes) -> bytes
- base58_decode(str | bytes) -> bytes
- each leading zero byte maps to a leading "1" and back
- TypeError for wrong input types, ValueError for invalid characters

The signatures mirror base64_cipher.py. Doctests cover empty input,
leading zeros, a round trip, and the error paths.

Fixes TheAlgorithms#15637

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ciphers/base58.py is a copy of base64_cipher.py — no Base58 implementation exists in the file

1 participant