GithubHelp home page GithubHelp logo

base-x-rs's People

Contributors

0mp avatar davide125 avatar dignifiedquire avatar eugene2k avatar fasterthanlime avatar kornelski avatar maciejhirsz avatar orkon avatar peamaeq avatar whalelephant avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

base-x-rs's Issues

No_alloc compatibility

No_alloc support seems to be possible, but would require a change in the API. Instead of constructing Strings or Vec, just use mutable slice passed in. This change would either require a seperate section of the API dedicated to no_alloc, or causing breaking changes in the current one.

Proposal: Limit the encoder input

Considering the nature of base-x, it might be worth considering to limit the encoder input and return an error if limit is reached (yes, I know I just had a PR merged that removed Result type from encode, bare with me).

The way encoder works now is continously reallocating a Vec as new digits are pushed onto it (Rust will grow the vec exponentially in this case, so it's not super terrible), then reverse the Vec to obtain the encoded value. Since what we are trying to do is, effectively, treat the input byte buffer as a single integer and modulo/divide it by base to obtain digits, it might be worth considering an optimization usually done when stringifying integers:

  • Instead of a heap allocated Vec, create a reasonably big byte array buffer on stack. When stringifying u64 to decimal the buffer would need to be 20 bytes long since that's how many decimal digits u64 can produce. In our case a buffer of 1024 bytes could be enough for virtually all use-cases. The buffer can be uninitialized via unsafe { mem::uinitialized() }, at which point it really doesn't matter how big it is.
  • Instead writing digits from the beginning and reversing, write the incoming digits from the end (I know, right).
  • Once done, take a slice of the buffer from the end equal to the number of digits produces and convert to a Vec.

Pros:

  • No expensive reversing.
  • Only a single heap allocation performed during the whole process, and only allocating the precise amount of bytes necessary.
  • Operating on stack is generally faster since you don't have to worry so much about going out of CPU caches.

Cons:

  • Can only produce outputs no longer than the buffer (1024 bytes). Since we know the size of the input and the base of the alphabet, we can predict an upper bound for which the buffer would be overflowed and return a meaningful error should that be the case.

The readme should warn about this library being unsuitable for standards-compliant base encoding

Per https://github.com/cryptocoinjs/base-x

WARNING: This module is NOT RFC3548 compliant, it cannot be used for base16 (hex), base32, or base64 encoding in a standards compliant manner.

This is because standards-compliant base64 aligns the start of processing with the start of the data (hence implicitly padding the bottom with zeroes), whereas base-x aligns with the end (therefore padding the top) - it will work sometimes, if the number of bits in the data is divisible by 6 (for base64) as there are no bits to pad. I found https://www.lucidchart.com/techblog/2017/10/23/base64-encoding-a-visual-explanation/ useful to help my understanding)

(as it happens, I think it might be ok for base16 because bytes are always divisible into 4 bits, but 5 bits with base32 surely has the same problem)

Concretely, encoding 'A' with a standards compliant base64 encoder results in QQ, but results in BB with base-x.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.