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)


322
323
324
325
# File 'lib/epithet.rb', line 322

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

Instance Method Details

#s2i(str) ⇒ Object

Decode a fixed-length Base58 string to an Integer. Assumes the input passes #valid?.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/epithet.rb', line 329

def s2i(str)
  # rubocop:disable Style/NumericLiterals, Lint/AmbiguousOperatorPrecedence, Layout
  #
  # By unrolling the chunks against literal coefficients, this is ~1.6x
  # faster under YJIT than the generic chunked Block58#s2i, and ~8x
  # 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)) +
                      58 * lut.getbyte(str.getbyte(20)) +
                    3364 * acc1 +
  1449225352009601191936 * acc0

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