base58_native 0.1.0-arm64-darwin
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +189 -0
- data/lib/base58_native/3.3/base58_native.bundle +0 -0
- data/lib/base58_native/3.4/base58_native.bundle +0 -0
- data/lib/base58_native/4.0/base58_native.bundle +0 -0
- data/lib/base58_native/fallback.rb +67 -0
- data/lib/base58_native/parity.rb +155 -0
- data/lib/base58_native/version.rb +5 -0
- data/lib/base58_native.rb +53 -0
- metadata +60 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 4c999d52c84a1735eeecafcc4e051e93659b5dae7e3f937cd028ed158077b3ca
|
|
4
|
+
data.tar.gz: '09a2e513f4342fbcfd0166df01ba5548c83d02209726207b2e15d1d47365723a'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8c4755722d4f3a6348651d2108523e37f401167d8a7593204fc9d3699d9e710a0601f5afc8e7b583b8e3be8f0b2beb03976ed3e1b1de2f6c399b22a79e74cc73
|
|
7
|
+
data.tar.gz: 7ae63c943072d15b7e7b974141ed0019eb78354c9b3b5ef291c68e103e55b065e2d7ef34b3862c00186371802cac57080d99135e9081f179dc0b4d8159c81cfd
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pocketflows
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
# base58_native
|
|
2
|
+
|
|
3
|
+
Fast Base58 encoding of UUIDs, hex strings and integers for Ruby, implemented
|
|
4
|
+
in safe Rust. It is a behaviour-identical replacement for Pocketflows'
|
|
5
|
+
pure-Ruby `Base58` (the short public ids used across the app), and is
|
|
6
|
+
**~40–60x faster** on the UUID paths under YJIT.
|
|
7
|
+
|
|
8
|
+
```ruby
|
|
9
|
+
# Gemfile
|
|
10
|
+
gem "base58_native"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Precompiled gems are built for `x86_64-linux`, `aarch64-linux` (glibc) and
|
|
14
|
+
`arm64-darwin` (Apple Silicon). Each one bundles a binary for Ruby 3.3, 3.4
|
|
15
|
+
and 4.0, so installing on those platforms does **not** need a Rust
|
|
16
|
+
toolchain. Other platforms (Alpine/musl, Intel Macs, Windows) fall back to the
|
|
17
|
+
source gem, which compiles with `cargo`.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
require "base58_native"
|
|
23
|
+
|
|
24
|
+
Base58Native.encode_from_uuid("550e8400-e29b-41d4-a716-446655440000") # => "BWBeN28Vb7cMEx7Ym8AUzs"
|
|
25
|
+
Base58Native.decode_to_uuid("BWBeN28Vb7cMEx7Ym8AUzs") # => "550e8400-e29b-41d4-a716-446655440000"
|
|
26
|
+
|
|
27
|
+
Base58Native.encode_from_hex("ff") # => "5Q"
|
|
28
|
+
Base58Native.decode_to_hex("5Q") # => "ff"
|
|
29
|
+
Base58Native.encode_from_base10(255) # => "5Q"
|
|
30
|
+
Base58Native.decode_to_base10("5Q") # => 255
|
|
31
|
+
|
|
32
|
+
Base58Native.decode_to_uuid("not0valid") # raises Base58Native::DecodeError
|
|
33
|
+
Base58Native.decode_to_uuid("z" * 22) # raises Base58Native::InvalidUUIDError
|
|
34
|
+
Base58Native.native? # => true when the Rust extension is loaded
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`Base58Native::DecodeError` and `Base58Native::InvalidUUIDError` both inherit
|
|
38
|
+
from `Base58Native::Error < StandardError`. To use them where code rescues the
|
|
39
|
+
original `Base58::DecodeError` / `Base58::InvalidUUIDError`, alias the
|
|
40
|
+
constants:
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
class Base58
|
|
44
|
+
DecodeError = Base58Native::DecodeError
|
|
45
|
+
InvalidUUIDError = Base58Native::InvalidUUIDError
|
|
46
|
+
end
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Parity with the original Ruby `Base58`
|
|
50
|
+
|
|
51
|
+
Results and errors match the original implementation for **every** input,
|
|
52
|
+
including odd ones. That covers return values, `String#encoding`
|
|
53
|
+
(`decode_to_hex` returns US-ASCII like `Integer#to_s`, everything else is
|
|
54
|
+
UTF-8), the exception class and message, and the lenient parsing inherited
|
|
55
|
+
from `Integer(hex, 16)` (`"0x1f"`, `" 1f "`, `"-1f"`, `"1_f"`). Here is how:
|
|
56
|
+
|
|
57
|
+
* The Rust fast path accepts only the strict, common shapes: canonical or
|
|
58
|
+
dash-free hex UUIDs, `[0-9A-Fa-f]+`, strings from the Base58 alphabet, and
|
|
59
|
+
non-negative Integers, all for values below 2**128 and in UTF-8, US-ASCII
|
|
60
|
+
or ASCII-8BIT strings.
|
|
61
|
+
* Anything else goes, unchanged, to `Base58Native::Fallback`. That is a
|
|
62
|
+
verbatim port of the original Ruby code, so it produces exactly the
|
|
63
|
+
original result or exception. This covers malformed input, values wider
|
|
64
|
+
than 128 bits, other encodings and other argument types.
|
|
65
|
+
|
|
66
|
+
`require "base58_native/parity"` exposes the differential harness used by the
|
|
67
|
+
test suite. Point it at your own reference implementation:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
cases = Base58Native::Parity.cases # ~25k typical and adversarial inputs
|
|
71
|
+
Base58Native::Parity.each_mismatch(Base58, Base58Native, cases) do |method, arg, expected, actual|
|
|
72
|
+
raise "#{method}(#{arg.inspect}): #{expected.inspect} != #{actual.inspect}"
|
|
73
|
+
end
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`test/reference/base58.rb` is a verbatim copy of the original
|
|
77
|
+
`lib/base58.rb`. The tests diff the native extension and the fallback against
|
|
78
|
+
it, including under `GC.stress`, `GC.compact` and threads.
|
|
79
|
+
|
|
80
|
+
## Safety
|
|
81
|
+
|
|
82
|
+
The extension is designed so it cannot crash the Ruby process:
|
|
83
|
+
|
|
84
|
+
* **No `unsafe`.** `unsafe_code = "forbid"` is set for both crates.
|
|
85
|
+
* **No panicking constructs.** `unwrap`, `expect`, `panic!`, `unreachable!`
|
|
86
|
+
and slice indexing are all denied by clippy. Table lookups use `.get()`,
|
|
87
|
+
and arithmetic is either provably in range or `checked_*`.
|
|
88
|
+
* **Belt and braces.** Every entry point runs under `catch_unwind`. A
|
|
89
|
+
hypothetical panic would surface as a rescuable `Base58Native::Error`,
|
|
90
|
+
never magnus's process-ending `fatal`.
|
|
91
|
+
* **Avoids panicking or slow magnus helpers.** `Integer::to_u128` and
|
|
92
|
+
`integer_from_u128` go through decimal strings, and the latter `unwrap`s
|
|
93
|
+
internally. The extension uses bignum arithmetic and hex formatting instead.
|
|
94
|
+
* **Fallback when the native library can't load.** If it fails to load (for
|
|
95
|
+
example on an unsupported platform without Rust), the gem warns and serves
|
|
96
|
+
every call from the pure-Ruby fallback, so behaviour is still correct.
|
|
97
|
+
|
|
98
|
+
The pure codec lives in its own crate (`ext/base58_codec`) and has no Ruby
|
|
99
|
+
dependency. Its unit tests check it against `std` formatting and naive
|
|
100
|
+
reference algorithms in debug builds (with overflow checks) and in release
|
|
101
|
+
builds.
|
|
102
|
+
|
|
103
|
+
## Performance
|
|
104
|
+
|
|
105
|
+
These numbers compare the original Ruby implementation, run with Ruby 3.4.13
|
|
106
|
+
and YJIT, against this gem (`bundle exec rake bench`). There are two
|
|
107
|
+
environments: macOS on Apple Silicon, and Linux aarch64 (the precompiled gem
|
|
108
|
+
in `ruby:3.4.13-slim` under Docker on the same machine). Runs vary by about
|
|
109
|
+
±15%.
|
|
110
|
+
|
|
111
|
+
| method | Ruby (YJIT) | native | speedup (macOS) | speedup (Linux) |
|
|
112
|
+
| --- | ---: | ---: | ---: | ---: |
|
|
113
|
+
| `encode_from_uuid` | ~2.7 µs | ~65 ns | **~40x** | **~50x** |
|
|
114
|
+
| `decode_to_uuid` | ~3.7 µs | ~60 ns | **~55–65x** | **~85x** |
|
|
115
|
+
| `encode_from_hex` | ~2.3 µs | ~70 ns | **~30–35x** | **~40x** |
|
|
116
|
+
| `decode_to_hex` | ~3.4 µs | ~65 ns | **~50x** | **~80x** |
|
|
117
|
+
| `decode_to_base10` | ~3.4 µs | ~115 ns | **~30x** | **~28x** |
|
|
118
|
+
| `encode_from_base10` (128-bit) | ~2.2 µs | ~170 ns | **~12x** | **~17x** |
|
|
119
|
+
| `encode_from_base10` (small int) | ~320 ns | ~27 ns | **~12x** | **~13x** |
|
|
120
|
+
|
|
121
|
+
Roughly 45 ns of a UUID call is Ruby-boundary cost: about 5 ns of method
|
|
122
|
+
dispatch, about 15 ns to copy the argument's bytes (magnus has no safe
|
|
123
|
+
borrowed view of a Ruby string), and about 21 ns to allocate the result
|
|
124
|
+
String. The Base58 arithmetic itself is about 8 ns. See
|
|
125
|
+
`ext/base58_codec/examples/micro.rs`:
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
cargo run --release --example micro -p base58_codec
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Development
|
|
132
|
+
|
|
133
|
+
`test/reference/base58.rb` is a verbatim copy of the original Ruby
|
|
134
|
+
implementation. Tests, `rake parity` and `rake bench` all run against it, so
|
|
135
|
+
the gem can be iterated on by itself. Set `REFERENCE=/path/to/base58.rb` to
|
|
136
|
+
compare against a different copy, such as code-3's live file.
|
|
137
|
+
|
|
138
|
+
```sh
|
|
139
|
+
bundle install
|
|
140
|
+
bundle exec rake compile # build the extension into lib/
|
|
141
|
+
bundle exec rake cargo # Rust unit tests (debug + release) and clippy
|
|
142
|
+
bundle exec rake test # full Ruby parity suite (GC stress, compaction, threads)
|
|
143
|
+
bundle exec rake parity # quick differential check vs the vendored reference
|
|
144
|
+
bundle exec rake bench # native vs the vendored reference, YJIT
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Precompiled gems
|
|
148
|
+
|
|
149
|
+
```sh
|
|
150
|
+
bundle exec rake gem:native # Docker + rb-sys-dock: pkg/base58_native-<v>-{x86_64-linux,aarch64-linux,arm64-darwin}.gem
|
|
151
|
+
bundle exec rake gem:verify # each gem has a correct-arch binary for Ruby 3.3/3.4/4.0 and no runtime deps
|
|
152
|
+
bundle exec rake gem:smoke # install the Linux gems on stock ruby:*-slim images (no Rust) and run test/smoke.rb
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Use `PLATFORMS=arm64-darwin bundle exec rake gem:native` to build a subset.
|
|
156
|
+
|
|
157
|
+
### Releasing
|
|
158
|
+
|
|
159
|
+
Releases go through Bundler's standard `rake release`, run from a
|
|
160
|
+
maintainer's machine. You need Docker, Rust (for the source-gem check) and
|
|
161
|
+
`gem signin` with access to `base58_native`.
|
|
162
|
+
|
|
163
|
+
```sh
|
|
164
|
+
# 1. bump lib/base58_native/version.rb, commit on main
|
|
165
|
+
git commit -am "Bump version to 0.1.1"
|
|
166
|
+
|
|
167
|
+
# 2. optional dry run: build, verify and smoke-test every gem; publishes nothing
|
|
168
|
+
bundle exec rake release:native
|
|
169
|
+
|
|
170
|
+
# 3. release
|
|
171
|
+
bundle exec rake release
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
`rake release` runs these steps in order:
|
|
175
|
+
|
|
176
|
+
1. Builds the source gem and checks that the tree is clean.
|
|
177
|
+
2. Runs `release:native`: builds the three platform gems, checks them with
|
|
178
|
+
`gem:verify`, and smoke-tests the Linux gems on stock Ruby images with
|
|
179
|
+
`gem:smoke`. It also installs the Mac gem and the source gem into clean gem
|
|
180
|
+
homes and tests them.
|
|
181
|
+
3. Tags `v0.1.1`, then pushes `main` and the tag.
|
|
182
|
+
4. Runs `gem push` on the platform gems first, then the source gem.
|
|
183
|
+
|
|
184
|
+
If step 2 fails, nothing has been tagged or pushed. `gem_push=no bundle exec
|
|
185
|
+
rake release` tags and pushes git without publishing to RubyGems.
|
|
186
|
+
|
|
187
|
+
## License
|
|
188
|
+
|
|
189
|
+
MIT
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Base58Native
|
|
4
|
+
# Reference implementation, kept byte-for-byte equivalent to Pocketflows'
|
|
5
|
+
# original pure-Ruby Base58 (code-3 lib/base58.rb). The native extension
|
|
6
|
+
# handles the common strict inputs itself and forwards everything else here,
|
|
7
|
+
# so results and exceptions match the reference for every input.
|
|
8
|
+
#
|
|
9
|
+
# Do not "improve" this module: its quirks are the spec.
|
|
10
|
+
module Fallback
|
|
11
|
+
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
|
12
|
+
INVERSE_MAP = ALPHABET.each_char.with_index.map { |char, i| [char, i] }.to_h.freeze
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
def encode_from_uuid(uuid)
|
|
16
|
+
no_dash = uuid.gsub("-", "")
|
|
17
|
+
encode_from_hex(no_dash)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def decode_to_uuid(uuid)
|
|
21
|
+
no_dash = decode_to_hex(uuid)
|
|
22
|
+
padded = no_dash.rjust(32, "0")
|
|
23
|
+
|
|
24
|
+
raise InvalidUUIDError, "decoded to invalid uuid" if padded.length > 32
|
|
25
|
+
|
|
26
|
+
"#{padded[0...8]}-#{padded[8...12]}-#{padded[12...16]}-#{padded[16...20]}-#{padded[20...]}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def encode_from_hex(hex)
|
|
30
|
+
int_value = Integer(hex, 16)
|
|
31
|
+
encode_from_base10(int_value)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def decode_to_hex(hex)
|
|
35
|
+
base10 = decode_to_base10(hex)
|
|
36
|
+
base10.to_s(16)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def encode_from_base10(int_value)
|
|
40
|
+
digits = []
|
|
41
|
+
|
|
42
|
+
while int_value > 0
|
|
43
|
+
int_value, remainder = int_value.divmod(58)
|
|
44
|
+
digits << ALPHABET[remainder]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
digits << ALPHABET[0] if digits.empty?
|
|
48
|
+
|
|
49
|
+
digits.reverse.join
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def decode_to_base10(s)
|
|
53
|
+
int_value = 0
|
|
54
|
+
|
|
55
|
+
base = 1
|
|
56
|
+
s.chars.reverse.each.with_index do |c, _i|
|
|
57
|
+
raise DecodeError, "Invalid character #{c} in base58 string" if !INVERSE_MAP.key?(c)
|
|
58
|
+
|
|
59
|
+
int_value += base * INVERSE_MAP[c]
|
|
60
|
+
base *= 58
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
int_value
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
require "base58_native"
|
|
6
|
+
|
|
7
|
+
# Differential parity harness (opt-in: `require "base58_native/parity"`).
|
|
8
|
+
#
|
|
9
|
+
# Runs the same call against a reference implementation and Base58Native and
|
|
10
|
+
# compares result value, class, encoding and frozenness, or exception class
|
|
11
|
+
# (unqualified name) and message. Used by this gem's tests and by consumers
|
|
12
|
+
# to check parity against their own pure-Ruby Base58:
|
|
13
|
+
#
|
|
14
|
+
# cases = Base58Native::Parity.cases
|
|
15
|
+
# Base58Native::Parity.each_mismatch(Base58, Base58Native, cases) { |m, arg, ref, nat| ... }
|
|
16
|
+
module Base58Native::Parity
|
|
17
|
+
STRING_METHODS = %i[encode_from_uuid decode_to_uuid encode_from_hex decode_to_hex decode_to_base10].freeze
|
|
18
|
+
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
|
19
|
+
HEX = "0123456789abcdefABCDEF"
|
|
20
|
+
|
|
21
|
+
module_function
|
|
22
|
+
|
|
23
|
+
def outcome(impl, method, arg)
|
|
24
|
+
value = impl.public_send(method, arg)
|
|
25
|
+
[:ok, value, value.class, value.respond_to?(:encoding) ? value.encoding : nil, value.frozen?]
|
|
26
|
+
rescue StandardError => e
|
|
27
|
+
# Error classes live under different namespaces (Base58:: vs Base58Native::),
|
|
28
|
+
# so compare the unqualified class name.
|
|
29
|
+
[:raise, e.class.name.split("::").last, e.message]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Yields [method, arg, reference_outcome, native_outcome] for mismatches.
|
|
33
|
+
def each_mismatch(reference, native, cases)
|
|
34
|
+
cases.each do |method, arg|
|
|
35
|
+
ref = outcome(reference, method, dup_arg(arg))
|
|
36
|
+
nat = outcome(native, method, dup_arg(arg))
|
|
37
|
+
yield method, arg, ref, nat unless ref == nat
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def dup_arg(arg)
|
|
42
|
+
arg.is_a?(String) ? arg.dup : arg
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def rand_str(rng, chars, len)
|
|
46
|
+
Array.new(len) { chars[rng.rand(chars.size)] }.join
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def uuid_from_int(n)
|
|
50
|
+
h = n.to_s(16).rjust(32, "0")
|
|
51
|
+
"#{h[0, 8]}-#{h[8, 4]}-#{h[12, 4]}-#{h[16, 4]}-#{h[20, 12]}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Deterministic (seeded) mix of typical and adversarial inputs.
|
|
55
|
+
def cases(seed: 1234, random: 20_000)
|
|
56
|
+
rng = Random.new(seed)
|
|
57
|
+
out = []
|
|
58
|
+
add = ->(m, a) { out << [m, a] }
|
|
59
|
+
max128 = 2**128 - 1
|
|
60
|
+
|
|
61
|
+
# --- integers / boundaries -------------------------------------------
|
|
62
|
+
ints = [0, 1, 57, 58, 59, max128, max128 + 1, 2**64 - 1, 2**64, 2**200, -1, -58, -(2**130)]
|
|
63
|
+
(1..23).each { |k| ints.push(58**k - 1, 58**k, 58**k + 1) }
|
|
64
|
+
128.times { |b| ints.push(2**b, 2**b - 1) }
|
|
65
|
+
ints.each do |i|
|
|
66
|
+
add[:encode_from_base10, i]
|
|
67
|
+
next if i.negative?
|
|
68
|
+
|
|
69
|
+
hex = i.to_s(16)
|
|
70
|
+
add[:encode_from_hex, hex]
|
|
71
|
+
add[:encode_from_hex, hex.upcase]
|
|
72
|
+
add[:encode_from_hex, "000#{hex}"]
|
|
73
|
+
b58 = Base58Native::Fallback.encode_from_base10(i)
|
|
74
|
+
%i[decode_to_uuid decode_to_hex decode_to_base10].each do |m|
|
|
75
|
+
add[m, b58]
|
|
76
|
+
add[m, "111#{b58}"]
|
|
77
|
+
end
|
|
78
|
+
add[:encode_from_uuid, uuid_from_int(i)] if i <= max128
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# --- canonical-ish UUIDs ---------------------------------------------
|
|
82
|
+
special = [0, max128, 1, 2**127, 2**64]
|
|
83
|
+
(special + Array.new(random) { rng.rand(2**128) } + Array.new(200) { rng.rand(2**(rng.rand(128) + 1)) }).each do |n|
|
|
84
|
+
u = uuid_from_int(n)
|
|
85
|
+
add[:encode_from_uuid, u]
|
|
86
|
+
add[:encode_from_uuid, u.upcase] if rng.rand(4).zero?
|
|
87
|
+
add[:encode_from_uuid, u.delete("-")] if rng.rand(8).zero?
|
|
88
|
+
add[:decode_to_uuid, Base58Native::Fallback.encode_from_uuid(u)]
|
|
89
|
+
end
|
|
90
|
+
200.times { add[:encode_from_uuid, SecureRandom.uuid] }
|
|
91
|
+
|
|
92
|
+
# --- hex oddities (Integer(hex, 16) leniency) ------------------------
|
|
93
|
+
hex_odd = ["", "-", "----", "0x", "0x1f", "0X1F", "0b1", "0o7", "0d9", "+1f", "-1f", " 1f", "1f ", "\n1f\n",
|
|
94
|
+
"1_f", "_1f", "1f_", "1__f", "g", "1g", "ff\0", "é", "1", "0x_ab", "-0", "+0", "00", "0"]
|
|
95
|
+
hex_odd.each do |h|
|
|
96
|
+
add[:encode_from_hex, h]
|
|
97
|
+
add[:encode_from_uuid, h]
|
|
98
|
+
add[:encode_from_uuid, "#{h}-#{h}"]
|
|
99
|
+
end
|
|
100
|
+
["550e8400-e29b-41d4-a716-44665544000", "550e8400-e29b-41d4-a716-4466554400000",
|
|
101
|
+
"550e8400e29b-41d4-a716-446655440000-", "-550e8400-e29b-41d4-a716-446655440000",
|
|
102
|
+
"550e8400--e29b-41d4-a716-446655440000", "550e8400-e29b-41d4-a716-44665544000g",
|
|
103
|
+
"{550e8400-e29b-41d4-a716-446655440000}", "550e8400-e29b-41d4-a716-446655440000\n",
|
|
104
|
+
" 550e8400-e29b-41d4-a716-446655440000", "550E8400-E29B-41D4-A716-446655440000",
|
|
105
|
+
"f" * 32, "f" * 33, "0" * 40 + "1", "-" * 36].each { |u| add[:encode_from_uuid, u]; add[:encode_from_hex, u] }
|
|
106
|
+
|
|
107
|
+
# --- random strings over mixed alphabets -----------------------------
|
|
108
|
+
pools = [ALPHABET, "#{ALPHABET}0OIl+/ ", HEX, "#{HEX}-", "#{HEX}-_xX +", "1", "z"]
|
|
109
|
+
3000.times do
|
|
110
|
+
pool = pools[rng.rand(pools.size)]
|
|
111
|
+
s = rand_str(rng, pool, rng.rand(45))
|
|
112
|
+
STRING_METHODS.each { |m| add[m, s] }
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# --- every single byte, as UTF-8 and binary --------------------------
|
|
116
|
+
256.times do |b|
|
|
117
|
+
s = b.chr
|
|
118
|
+
STRING_METHODS.each do |m|
|
|
119
|
+
add[m, s.dup.force_encoding(Encoding::UTF_8)]
|
|
120
|
+
add[m, s.dup.force_encoding(Encoding::BINARY)]
|
|
121
|
+
add[m, "Ab#{s}cd".force_encoding(Encoding::BINARY)]
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# --- encodings, multibyte, invalid UTF-8 -----------------------------
|
|
126
|
+
good = "BWBeN28Vb7cMEx7Ym8AUzs"
|
|
127
|
+
uuid = "550e8400-e29b-41d4-a716-446655440000"
|
|
128
|
+
[
|
|
129
|
+
good.encode(Encoding::US_ASCII), good.b, good.encode(Encoding::UTF_16LE), good.encode(Encoding::UTF_16BE),
|
|
130
|
+
good.encode(Encoding::UTF_32LE), good.encode(Encoding::ISO_8859_1), good.encode(Encoding::Shift_JIS),
|
|
131
|
+
"AB".encode(Encoding::UTF_16LE), "AB".b.force_encoding(Encoding::UTF_16LE), "\xFF\xFE".b,
|
|
132
|
+
"Bé", "é", "日本", "ab ", "\xC3".dup.force_encoding("UTF-8"), "a\xFFb".dup.force_encoding("UTF-8"),
|
|
133
|
+
"#{good}\u0000", "\u{1F600}", good.dup.freeze
|
|
134
|
+
].each do |s|
|
|
135
|
+
STRING_METHODS.each { |m| add[m, s] }
|
|
136
|
+
end
|
|
137
|
+
[uuid.encode(Encoding::UTF_16LE), uuid.b, uuid.encode(Encoding::US_ASCII), uuid.encode(Encoding::ISO_8859_1)].each do |s|
|
|
138
|
+
add[:encode_from_uuid, s]
|
|
139
|
+
add[:encode_from_hex, s.encode("UTF-8").delete("-").encode(s.encoding)]
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# --- wrong types -----------------------------------------------------
|
|
143
|
+
[nil, 123, 1.5, -2.5, 2**130, :abc, :"550e8400-e29b-41d4-a716-446655440000", [], {}, Object.new, true].each do |v|
|
|
144
|
+
STRING_METHODS.each { |m| add[m, v] }
|
|
145
|
+
end
|
|
146
|
+
[nil, 1.5, 58.9, -0.5, "123", :a, Rational(7, 2), true, Object.new].each { |v| add[:encode_from_base10, v] }
|
|
147
|
+
|
|
148
|
+
# String subclass
|
|
149
|
+
sub = Class.new(String)
|
|
150
|
+
add[:decode_to_uuid, sub.new(good)]
|
|
151
|
+
add[:encode_from_uuid, sub.new(uuid)]
|
|
152
|
+
|
|
153
|
+
out
|
|
154
|
+
end
|
|
155
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "base58_native/version"
|
|
4
|
+
|
|
5
|
+
# Fast Base58 encoding/decoding of UUIDs, hex strings and integers, backed by
|
|
6
|
+
# a Rust extension. API-compatible with Pocketflows' original Base58 class:
|
|
7
|
+
#
|
|
8
|
+
# Base58Native.encode_from_uuid("550e8400-e29b-41d4-a716-446655440000") # => "BWBeN28Vb7cMEx7Ym8AUzs"
|
|
9
|
+
# Base58Native.decode_to_uuid("BWBeN28Vb7cMEx7Ym8AUzs")
|
|
10
|
+
# Base58Native.encode_from_hex / decode_to_hex
|
|
11
|
+
# Base58Native.encode_from_base10 / decode_to_base10
|
|
12
|
+
#
|
|
13
|
+
# Errors: Base58Native::DecodeError, Base58Native::InvalidUUIDError (both
|
|
14
|
+
# subclasses of Base58Native::Error < StandardError), plus whatever the
|
|
15
|
+
# reference implementation raises for malformed arguments (e.g. ArgumentError
|
|
16
|
+
# from Integer()).
|
|
17
|
+
module Base58Native
|
|
18
|
+
class Error < StandardError; end
|
|
19
|
+
class DecodeError < Error; end
|
|
20
|
+
class InvalidUUIDError < Error; end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
require_relative "base58_native/fallback"
|
|
24
|
+
|
|
25
|
+
module Base58Native
|
|
26
|
+
METHODS = %i[
|
|
27
|
+
encode_from_uuid decode_to_uuid
|
|
28
|
+
encode_from_hex decode_to_hex
|
|
29
|
+
encode_from_base10 decode_to_base10
|
|
30
|
+
].freeze
|
|
31
|
+
|
|
32
|
+
begin
|
|
33
|
+
# Precompiled (fat) gems ship one binary per Ruby minor version.
|
|
34
|
+
ruby_minor = RUBY_VERSION[/\A\d+\.\d+/]
|
|
35
|
+
begin
|
|
36
|
+
require_relative "base58_native/#{ruby_minor}/base58_native"
|
|
37
|
+
rescue LoadError
|
|
38
|
+
require_relative "base58_native/base58_native"
|
|
39
|
+
end
|
|
40
|
+
NATIVE = true
|
|
41
|
+
rescue LoadError => e
|
|
42
|
+
warn "[base58_native] native extension unavailable (#{e.message}); using pure-Ruby fallback"
|
|
43
|
+
NATIVE = false
|
|
44
|
+
METHODS.each do |name|
|
|
45
|
+
define_singleton_method(name) { |arg| Fallback.public_send(name, arg) }
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# True when the Rust extension is loaded.
|
|
50
|
+
def self.native?
|
|
51
|
+
NATIVE
|
|
52
|
+
end
|
|
53
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: base58_native
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: arm64-darwin
|
|
6
|
+
authors:
|
|
7
|
+
- Pocketflows
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: |
|
|
14
|
+
Drop-in, behaviour-identical native replacement for Pocketflows' pure-Ruby
|
|
15
|
+
Base58 (UUID <-> Base58 short ids). Written in safe Rust (no unsafe) with a
|
|
16
|
+
reference Ruby fallback for edge cases. Precompiled for common platforms,
|
|
17
|
+
so no Rust toolchain is needed to install.
|
|
18
|
+
email:
|
|
19
|
+
executables: []
|
|
20
|
+
extensions: []
|
|
21
|
+
extra_rdoc_files: []
|
|
22
|
+
files:
|
|
23
|
+
- LICENSE.txt
|
|
24
|
+
- README.md
|
|
25
|
+
- lib/base58_native.rb
|
|
26
|
+
- lib/base58_native/3.3/base58_native.bundle
|
|
27
|
+
- lib/base58_native/3.4/base58_native.bundle
|
|
28
|
+
- lib/base58_native/4.0/base58_native.bundle
|
|
29
|
+
- lib/base58_native/fallback.rb
|
|
30
|
+
- lib/base58_native/parity.rb
|
|
31
|
+
- lib/base58_native/version.rb
|
|
32
|
+
homepage: https://github.com/pocketflows/base58_native
|
|
33
|
+
licenses:
|
|
34
|
+
- MIT
|
|
35
|
+
metadata:
|
|
36
|
+
source_code_uri: https://github.com/pocketflows/base58_native
|
|
37
|
+
rubygems_mfa_required: 'true'
|
|
38
|
+
post_install_message:
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
require_paths:
|
|
41
|
+
- lib
|
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '3.3'
|
|
47
|
+
- - "<"
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: 4.1.dev
|
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
requirements: []
|
|
56
|
+
rubygems_version: 3.5.23
|
|
57
|
+
signing_key:
|
|
58
|
+
specification_version: 4
|
|
59
|
+
summary: Fast Base58 encoding of UUIDs, hex and integers via a Rust extension
|
|
60
|
+
test_files: []
|