Class: Epithet::Block58::Unrolled16

Inherits:
Block58
  • Object
show all
Defined in:
lib/epithet.rb

Overview

Specialised decoder for 16-byte blocks (22 digits) with a fully unrolled inner product.

Instance Method Summary collapse

Constructor Details

#initializeUnrolled16

Returns a new instance of Unrolled16.

Raises:

  • (ArgumentError)


335
336
337
338
# File 'lib/epithet.rb', line 335

def initialize(...)
  super
  raise ArgumentError, 'unrolled codec requires a 16-byte block' unless @size == 22
end

Instance Method Details

#s2i(str) ⇒ Object

Decode a 22-digit base58 string to an integer. Assumes the input passes #valid?, behaviour undefined if it doesn't.



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/epithet.rb', line 342

def s2i(str)
  # rubocop:disable Style/NumericLiterals, Lint/AmbiguousOperatorPrecedence, Layout
  #
  # By unrolling the chunks against literal coefficients, this tested with Ruby 4.0
  # at ~1.5x faster under YJIT than the generic chunked Block58#s2i, and ~6x faster
  # than Horner's scheme.
  lut = @lut

  acc0 = lut.getbyte(str.getbyte(0)) * 7427658739644928 +
         lut.getbyte(str.getbyte(1)) * 128063081718016 +
         lut.getbyte(str.getbyte(2)) * 2207984167552 +
         lut.getbyte(str.getbyte(3)) * 38068692544 +
         lut.getbyte(str.getbyte(4)) * 656356768 +
         lut.getbyte(str.getbyte(5)) * 11316496 +
         lut.getbyte(str.getbyte(6)) * 195112 +
         lut.getbyte(str.getbyte(7)) * 3364 +
         lut.getbyte(str.getbyte(8)) * 58 +
         lut.getbyte(str.getbyte(9))

  acc1 = lut.getbyte(str.getbyte(10)) * 7427658739644928 +
         lut.getbyte(str.getbyte(11)) * 128063081718016 +
         lut.getbyte(str.getbyte(12)) * 2207984167552 +
         lut.getbyte(str.getbyte(13)) * 38068692544 +
         lut.getbyte(str.getbyte(14)) * 656356768 +
         lut.getbyte(str.getbyte(15)) * 11316496 +
         lut.getbyte(str.getbyte(16)) * 195112 +
         lut.getbyte(str.getbyte(17)) * 3364 +
         lut.getbyte(str.getbyte(18)) * 58 +
         lut.getbyte(str.getbyte(19))

         lut.getbyte(str.getbyte(21)) +
         lut.getbyte(str.getbyte(20)) * 58 +
                                 acc1 * 3364 +
                                 acc0 * 1449225352009601191936

  # rubocop:enable Style/NumericLiterals, Lint/AmbiguousOperatorPrecedence, Layout
end