Class: Nuckle::Internals::Blake3::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/nuckle/internals/blake3.rb

Overview

Deferred final compression. Supports variable-length output via XOF.

Instance Method Summary collapse

Constructor Details

#initialize(input_cv, block_words, counter, block_len, flags) ⇒ Output

Returns a new instance of Output.



111
112
113
114
115
116
117
118
119
# File 'lib/nuckle/internals/blake3.rb', line 111

def initialize(input_cv, block_words, counter, block_len, flags)
  @cv        = input_cv
  @block     = block_words
  @counter   = counter
  @block_len = block_len
  @flags     = flags
  @xof_ctr   = 0
  @buffer    = String.new(encoding: Encoding::BINARY)
end

Instance Method Details

#chaining_valueObject

8-word CV (non-root). Used when folding intermediate parents.



122
123
124
# File 'lib/nuckle/internals/blake3.rb', line 122

def chaining_value
  Blake3.compress(@cv, @block, @counter, @block_len, @flags)[0, 8]
end

#read(n) ⇒ Object

Read the next n bytes from the root XOF stream.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/nuckle/internals/blake3.rb', line 127

def read(n)
  out = String.new(capacity: n, encoding: Encoding::BINARY)
  while out.bytesize < n
    if @buffer.empty?
      words = Blake3.compress(@cv, @block, @xof_ctr, @block_len, @flags | ROOT)
      @buffer = words.pack("V16")
      @xof_ctr += 1
    end
    take     = [@buffer.bytesize, n - out.bytesize].min
    out     << @buffer.byteslice(0, take)
    @buffer  = @buffer.byteslice(take..) || "".b
  end
  out
end