Class: IOTA::Crypto::RubyCurl

Inherits:
Object
  • Object
show all
Defined in:
lib/iota/crypto/curl_ruby.rb

Constant Summary collapse

NUMBER_OF_ROUNDS =
81
HASH_LENGTH =
243
STATE_LENGTH =
3 * HASH_LENGTH
TRUTH_TABLE =
[1, 0, -1, 1, -1, 0, -1, 1, 0]

Instance Method Summary collapse

Constructor Details

#initialize(rounds = nil) ⇒ RubyCurl

Returns a new instance of RubyCurl.


9
10
11
12
# File 'lib/iota/crypto/curl_ruby.rb', line 9

def initialize(rounds = nil)
  @rounds = rounds || NUMBER_OF_ROUNDS
  reset
end

Instance Method Details

#absorb(trits) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/iota/crypto/curl_ruby.rb', line 18

def absorb(trits)
  length  = trits.length
  offset  = 0

  while offset < length
    start = offset
    stop  = [start + HASH_LENGTH, length].min

    @state[0...stop-start] = trits.slice(start, stop-start)
    transform

    offset += HASH_LENGTH
  end
end

#resetObject


14
15
16
# File 'lib/iota/crypto/curl_ruby.rb', line 14

def reset
  @state = [0] * STATE_LENGTH
end

#squeeze(trits) ⇒ Object


33
34
35
36
# File 'lib/iota/crypto/curl_ruby.rb', line 33

def squeeze(trits)
  trits[0...HASH_LENGTH] = @state.slice(0, HASH_LENGTH)
  transform
end

#transformObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/iota/crypto/curl_ruby.rb', line 38

def transform
  previousState  = @state.slice(0, @state.length)
  newState   = @state.slice(0, @state.length)

  index = 0
  round = 0
  while round < @rounds
    previousTrit = previousState[index].to_i

    pos = 0
    while true
      index += (index < 365) ? 364 : -365
      newTrit = previousState[index].to_i
      newState[pos] = TRUTH_TABLE[previousTrit + (3 * newTrit) + 4]
      previousTrit = newTrit
      pos += 1
      break if pos >= STATE_LENGTH
    end

    previousState = newState
    newState = newState.slice(0, newState.length)
    round += 1
  end

  @state = newState
end

#versionObject


65
66
67
# File 'lib/iota/crypto/curl_ruby.rb', line 65

def version
  "Ruby"
end