module CryptBufferConcern::Arithmetic
Public Instance Methods
add(n, mod: 256, offset: 0)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/crypto-toolbox/crypt_buffer/concerns/arithmetic.rb, line 16 def sub(n) CryptBuffer( bytes.map{|byte| byte -n } ) end
Private Instance Methods
hamming_distance(other)
click to toggle source
# File lib/crypto-toolbox/crypt_buffer/concerns/arithmetic.rb, line 40 def hamming_distance(other) (self ^ other).bits.join.count("1") end
sanitize_modulus(mod)
click to toggle source
# File lib/crypto-toolbox/crypt_buffer/concerns/arithmetic.rb, line 44 def sanitize_modulus(mod) (mod > 0) ? mod : 256 end