Class: IOTA::Crypto::Converter

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

Constant Summary collapse

RADIX =
3
RADIX_BYTES =
256
MAX_TRIT_VALUE =
1
MIN_TRIT_VALUE =
-1
BYTE_HASH_LENGTH =
48
HASH_LENGTH =
Curl::HASH_LENGTH
TRYTES_ALPHABET =

All possible tryte values

"9ABCDEFGHIJKLMNOPQRSTUVWXYZ"
TRYTE_TRITS =
[
  [ 0,  0,  0],
  [ 1,  0,  0],
  [-1,  1,  0],
  [ 0,  1,  0],
  [ 1,  1,  0],
  [-1, -1,  1],
  [ 0, -1,  1],
  [ 1, -1,  1],
  [-1,  0,  1],
  [ 0,  0,  1],
  [ 1,  0,  1],
  [-1,  1,  1],
  [ 0,  1,  1],
  [ 1,  1,  1],
  [-1, -1, -1],
  [ 0, -1, -1],
  [ 1, -1, -1],
  [-1,  0, -1],
  [ 0,  0, -1],
  [ 1,  0, -1],
  [-1,  1, -1],
  [ 0,  1, -1],
  [ 1,  1, -1],
  [-1, -1,  0],
  [ 0, -1,  0],
  [ 1, -1,  0],
  [-1,  0,  0]
]

Class Method Summary collapse

Class Method Details

.convertBaseToBigInt(array, base) ⇒ Object


153
154
155
156
157
158
159
# File 'lib/iota/crypto/converter.rb', line 153

def convertBaseToBigInt(array, base)
  bigint = 0
  (0...array.length).step(1) do |i|
    bigint += array[i] * (base ** i)
  end
  bigint
end

.convertBigIntToBase(bigInt, base, length) ⇒ Object


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/iota/crypto/converter.rb', line 161

def convertBigIntToBase(bigInt, base, length)
  result = []

  isNegative = bigInt < 0
  quotient = bigInt.abs

  max, _ = (isNegative ? base : base-1).divmod(2)

  length.times do
    quotient, remainder = quotient.divmod(base)

    if remainder > max      # Lend 1 to the next place so we can make this digit negative.

      quotient += 1
      remainder -= base
    end

    remainder = -remainder if isNegative

    result << remainder
  end

  result
end

.convertBigIntToBytes(big) ⇒ Object


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/iota/crypto/converter.rb', line 186

def convertBigIntToBytes(big)
  bytesArrayTemp = []

  (0...48).step(1) do |pos|
    bytesArrayTemp << (big.abs >> pos * 8) % (1 << 8)
  end

  # big endian and balanced
  bytesArray = bytesArrayTemp.reverse.map { |x| x <= 0x7F ? x : x - 0x100 }

  if big < 0    # 1-compliment

    bytesArray = bytesArray.map { |x| ~x }

    # add1
    (0...bytesArray.length).reverse_each do |pos|
      add = (bytesArray[pos] & 0xFF) + 1
      bytesArray[pos] = add <= 0x7F ? add : add - 0x100
      break if bytesArray[pos] != 0
    end
  end

  bytesArray
end

.convertBytesToBigInt(array) ⇒ Object


211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/iota/crypto/converter.rb', line 211

def convertBytesToBigInt(array)
  # copy of array
  bytesArray = array.map { |x| x }

  # number sign in MSB
  signum = bytesArray[0] >= 0 ? 1 : -1

  if signum == -1    # sub1

    (0...bytesArray.length).reverse_each do |pos|
      sub = (bytesArray[pos] & 0xFF) - 1
      bytesArray[pos] = sub <= 0x7F ? sub : sub - 0x100
      break if bytesArray[pos] != -1
    end

    # 1-compliment
    bytesArray = bytesArray.map { |x| ~x }
  end

  # sum magnitudes and set sign
  sum = 0
  bytesArray.reverse.each_with_index do |v, pos|
    sum += (v & 0xFF) << pos * 8
  end

  sum * signum
end

.convertSign(byte) ⇒ Object

Convert between signed and unsigned bytes


144
145
146
147
148
149
150
151
# File 'lib/iota/crypto/converter.rb', line 144

def convertSign(byte)
  if byte < 0
    return 256 + byte
  elsif byte > 127
    return -256 + byte
  end
  byte
end

.convertToBytes(trits) ⇒ Object

ADOPTED FROM PYTHON LIBRARY Word to tryte & trytes to words conversion


131
132
133
134
135
# File 'lib/iota/crypto/converter.rb', line 131

def convertToBytes(trits)
  bigInt = convertBaseToBigInt(trits, 3)
  bytes_k = convertBigIntToBytes(bigInt)
  bytes_k
end

.convertToTrits(bytes) ⇒ Object


137
138
139
140
141
# File 'lib/iota/crypto/converter.rb', line 137

def convertToTrits(bytes)
  bigInt = convertBytesToBigInt(bytes)
  trits = convertBigIntToBase(bigInt, 3, HASH_LENGTH)
  trits
end

.fromValue(value) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/iota/crypto/converter.rb', line 102

def fromValue(value)
  destination = []
  absoluteValue = value < 0 ? -value : value
  i = 0

  while absoluteValue > 0
    remainder = absoluteValue % RADIX
    absoluteValue = (absoluteValue / RADIX).floor

    if remainder > MAX_TRIT_VALUE
      remainder = MIN_TRIT_VALUE
      absoluteValue += 1
    end
    destination[i] = remainder
    i += 1
  end

  if value < 0
    (0...destination.length).step(1) do |j|
      # switch values
      destination[j] = destination[j] == 0 ? 0 : -destination[j]
    end
  end

  destination
end

.trits(input, state = []) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/iota/crypto/converter.rb', line 45

def trits(input, state = [])
  trits = state

  if input.is_a? Integer
    absoluteValue = input < 0 ? -input : input

    while absoluteValue > 0
      remainder = absoluteValue % 3
      absoluteValue = (absoluteValue / 3).floor

      if remainder > 1
        remainder = -1
        absoluteValue += 1
      end

      trits[trits.length] = remainder
    end

    if input < 0
      (0... trits.length).step(1) do |i|
        trits[i] = -trits[i]
      end
    end
  else
    (0... input.length).step(1) do |i|
      index = TRYTES_ALPHABET.index(input[i])
      tmp = i * 3
      trits[tmp...tmp+3] = TRYTE_TRITS[index]
    end
  end

  trits
end

.trytes(trits) ⇒ Object


79
80
81
82
83
84
85
86
87
88
# File 'lib/iota/crypto/converter.rb', line 79

def trytes(trits)
  trytes = ""

  (0...trits.length).step(3) do |i|
    chunk = trits[i...i+3]
    trytes += TRYTES_ALPHABET[TRYTE_TRITS.index(chunk)]
  end

  trytes
end

.value(trits) ⇒ Object


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/iota/crypto/converter.rb', line 90

def value(trits)
  returnValue = 0

  range = (trits.length..0)

  range.first.downto(range.last).each do |i|
    returnValue = returnValue * 3 + trits[i].to_i
  end

  returnValue
end