Module: CryptBufferConcern::Arithmetic

Included in:
CryptBuffer
Defined in:
lib/crypto-toolbox/crypt_buffer/concerns/arithmetic.rb

Instance Method Summary collapse

Instance Method Details

#add(n, mod: 256, offset: 0) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/crypto-toolbox/crypt_buffer/concerns/arithmetic.rb', line 20

def add(n, mod: 256, offset: 0)
  real_mod = [256,mod].min

  tmp = bytes.map do |b|
    val = (b + n) % real_mod
    val >= offset ? val : val+offset
  end
  CryptBuffer(tmp)
end

#hdist(other, normalize: false) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/crypto-toolbox/crypt_buffer/concerns/arithmetic.rb', line 30

def hdist(other,normalize: false)
  if normalize
    hamming_distance(other) / length.to_f
  else
    hamming_distance(other)
  end
end

#mod_sub(n, mod: 256) ⇒ Object



9
10
11
12
13
14
# File 'lib/crypto-toolbox/crypt_buffer/concerns/arithmetic.rb', line 9

def mod_sub(n,mod: 256)
  tmp = bytes.map do |byte|
    val = byte.to_bn.mod_sub(n,mod).to_i
  end
  CryptBuffer(tmp)
end

#modulus(mod) ⇒ Object



4
5
6
7
# File 'lib/crypto-toolbox/crypt_buffer/concerns/arithmetic.rb', line 4

def modulus(mod)
  real_mod = sanitize_modulus(mod)
  CryptBuffer( bytes.map{|b| b % real_mod } )
end

#sub(n) ⇒ Object



16
17
18
# File 'lib/crypto-toolbox/crypt_buffer/concerns/arithmetic.rb', line 16

def sub(n)
  CryptBuffer( bytes.map{|byte| byte -n } )
end