Class: SSSA::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prime = 115792089237316195423570985008687907853269984665640564039457584007913129639747) ⇒ Utils

Returns a new instance of Utils.



8
9
10
# File 'lib/utils.rb', line 8

def initialize(prime=115792089237316195423570985008687907853269984665640564039457584007913129639747)
    @prime = prime
end

Instance Attribute Details

#primeObject

Returns the value of attribute prime.



6
7
8
# File 'lib/utils.rb', line 6

def prime
  @prime
end

Instance Method Details

#evaluate_polynomial(coefficients, value) ⇒ Object

This evaluates a polynomial with reversed coefficients at a given value. Namely, given the array [a, b, c, d], and x=value, the equation is: a + bx + cx^2 + dx^3



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/utils.rb', line 49

def evaluate_polynomial(coefficients, value)
    result = 0
    coefficients.each_with_index do |coefficient, exponent|
        expmod = 1
        (0...exponent).each do
            expmod = (expmod * value) % @prime
        end
        result += coefficient * expmod
        result = result % @prime
    end

    return result
end

#from_base64(number) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/utils.rb', line 72

def from_base64(number)
    segment = Base64.urlsafe_decode64(number).split('').map do |x|
        if x.ord > 15
            x.ord.to_s(16)
        else
            "0" + x.ord.to_s(16)
        end
    end
    return (segment+["00"]*(32-segment.size)).join.hex
end

#gcd(a, b) ⇒ Object

Uses extended Euclidian algorithm to compute the GCD of a pair of numbers, and returns [gcd, x, y], such that gcd = ax+ by.

Note: computing the GCD over a finite field with a = @prime means that GCD will always return 1.



88
89
90
91
92
93
94
95
96
97
# File 'lib/utils.rb', line 88

def gcd(a, b)
    if b == 0
        return [a, 1, 0]
    else
        n = (a*1.0/b).floor
        c = a % b
        r = gcd(b, c)
        return [r[0], r[2], r[1]-r[2]*n]
    end
end

#merge_ints(secrets) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/utils.rb', line 32

def merge_ints(secrets)
    result = []

    secrets.each { |int|
        data = int.to_s(16)
        ("0"*(64-data.size) + data).scan(/../) { |segment|
            result.push segment.hex
        }
    }

    return result.pack('C*').force_encoding("utf-8").gsub(/\u0000*$/, '')
end

#mod_inverse(number) ⇒ Object

Computes the multiplicitive inverse of the given number on the finite field. Note: number should never be less than zero; however, if it is, the inverse is inverted



102
103
104
105
106
107
108
# File 'lib/utils.rb', line 102

def mod_inverse(number)
    remainder = gcd(@prime, number % @prime)[2]
    if (number < 0)
        remainder *= -1
    end
    return (@prime + remainder) % @prime
end

#randomObject

Returns a random number on 0 to @prime-1, inclusive.



13
14
15
# File 'lib/utils.rb', line 13

def random()
    return SecureRandom.random_number(@prime)
end

#split_ints(secret) ⇒ Object

split_ints and merge_ints converts between string and integer array, where the integer is right-padded until it fits a 256 bit integer.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/utils.rb', line 19

def split_ints(secret)
    result = []

    secret.split('').map { |x|
        data = x.unpack("H*")[0]
        "0"*(data.size % 2) + data
    }.join("").scan(/.{1,64}/) { |segment|
        result.push (segment+"0"*(64-segment.size)).hex
    }

    return result
end

#to_base64(number) ⇒ Object

The to_base64 and from_base64 converts between base 10 and base 64 integers, with a left-zero-padded, fixed-size hex representation. This is cross-compatible with the go implementation, and by changing base versus encoding as a string, it reduces the size of the representation. Note: the output is always 44 characters.



68
69
70
# File 'lib/utils.rb', line 68

def to_base64(number)
    return Base64.urlsafe_encode64(("0"*(64-number.to_s(16).size) + number.to_s(16)).scan(/../).map{|x| x.hex.chr}.join)
end