Skip to content

proposal: math/big: base62 and/or arbitrary alphabet and base support #21558

@anitgandhi

Description

@anitgandhi

Introduction

In the process of developing fpe, I used math/big for it's useful big number equivalents of itoa and atoi with base/radix support. However, as of right now, math/big only supports a maximum base/radix of 36: https://github.com/golang/go/blob/master/src/math/big/natconv.go#L18-L24

I believe this can be expanded to allow at least base62 strings and possibly arbitrary alphabets and radices.

Proposal

I'm proposing two separate but related changes such that math/big can add support for:

  1. base62 strings. As of right now, interpreting a big.Int as base62 and base36 strings using Text() and SetString() results in the same string. In the context of FPE, that's effectively a collision in my opinion. See Base62 support capitalone/fpe#1 for details including a small 4-line diff that introduces base62 support
  2. Arbitrary alphabet and base support (Some context: Arbitrary alphabet and radix support capitalone/fpe#2) . This would allow users to define their own alphabets and use their own choice of base to use a subset of said alphabet. This would expand the current alphabet which is base36 (0-9, a-z) and MaxBase=36 even more.
    I believe this may entail:
    1. In natconv.go, change digits and MaxBase consts to vars.
    2. Addition of SetDigits and/or SetMaxBase function to math/big

Admittedly I don't know how many use cases there are for the 2nd point other than what I'm doing on fpe, however the flexibility of having arbitrary alphabets and bases could be useful in other cases.

Notes

CC: @griesemer

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions