class Stribog::CompressionFunc

Compression

Class implements compression function of GOST R 34.11-2012 algorithm. @author WildDima

Public Class Methods

new(n, message, hash_vector) click to toggle source
# File lib/stribog/compression_func.rb, line 9
def initialize(n, message, hash_vector)
  @n = n
  @message = message
  @hash_vector = hash_vector
end

Public Instance Methods

call() click to toggle source
# File lib/stribog/compression_func.rb, line 15
def call
  vector = lpsx_func @n, @hash_vector
  vector = func_e vector, @message
  vector = vector ^ @hash_vector
  vector ^ @message
end

Private Instance Methods

func_e(first_vector, second_vector) click to toggle source

rubocop:disable Style/EachWithObject

# File lib/stribog/compression_func.rb, line 66
def func_e(first_vector, second_vector)
  vectors = CONSTANTS_C
            .inject(v1: first_vector.dup,
                    v2: second_vector.dup) do |vs, const|
    vs[:v2] = lpsx_func(vs[:v1], vs[:v2])
    vs[:v1] = lpsx_func(vs[:v1], ByteVector.convert(const))
    vs
  end
  vectors[:v1] ^ vectors[:v2]
end
linear_transformation(vector) click to toggle source

rubocop:enable Style/EachWithObject

# File lib/stribog/compression_func.rb, line 49
def linear_transformation(vector)
  ByteVector.new(
    vector.bit64.map do |byte8|
      small_linear_transformation(byte8)
    end.flatten
  )
end
lpsx_func(first_vector, second_vector) click to toggle source
# File lib/stribog/compression_func.rb, line 24
def lpsx_func(first_vector, second_vector)
  linear_transformation(
    permutation_t(
      replacement_pi(
        first_vector ^ second_vector
      )
    )
  )
end
not_zeros_indexes(vector) click to toggle source

rubocop:enable Style/EachWithObject

# File lib/stribog/compression_func.rb, line 78
def not_zeros_indexes(vector)
  vector.chars.map.with_index do |bit, index|
    next if bit == '0'
    index
  end.compact
end
permutation_t(vector) click to toggle source

rubocop:disable Style/EachWithObject

# File lib/stribog/compression_func.rb, line 39
def permutation_t(vector)
  ByteVector.new(
    vector.each.with_index.inject([]) do |b_arr, (byte, index)|
      b_arr[T[index]] = byte
      b_arr
    end.map(&:to_i)
  )
end
replacement_pi(vector) click to toggle source
# File lib/stribog/compression_func.rb, line 34
def replacement_pi(vector)
  ByteVector.new vector.map { |byte| PI[byte] }
end
small_linear_transformation(vector) click to toggle source
# File lib/stribog/compression_func.rb, line 57
def small_linear_transformation(vector)
  # REFACTOR
  ByteVector.convert(
    not_zeros_indexes(vector)
      .inject(0) { |acc, elem| acc ^ MATRIX_A[elem] }
  ).to_a
end