Class: IOTA::Crypto::Signing

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

Constant Summary collapse

HASH_LENGTH =
Kerl::HASH_LENGTH

Class Method Summary collapse

Class Method Details

.address(digests) ⇒ Object


7
8
9
10
11
12
13
14
15
16
# File 'lib/iota/crypto/signing.rb', line 7

def address(digests)
  addressTrits = []

  kerl = Kerl.new

  kerl.absorb(digests, 0, digests.length)
  kerl.squeeze(addressTrits, 0, Kerl::HASH_LENGTH)

  addressTrits
end

.digest(normalizedBundleFragment, signatureFragment) ⇒ Object


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/iota/crypto/signing.rb', line 18

def digest(normalizedBundleFragment, signatureFragment)
  buffer = []
  kerl = Kerl.new

  (0...27).step(1) do |i|
    buffer = signatureFragment.slice(i * HASH_LENGTH, HASH_LENGTH)

    j = normalizedBundleFragment[i] + 13

    while j > 0
      jKerl = Kerl.new
      jKerl.absorb(buffer, 0, buffer.length)
      jKerl.squeeze(buffer, 0, HASH_LENGTH)
      j -= 1
    end

    kerl.absorb(buffer, 0, buffer.length)
  end

  kerl.squeeze(buffer, 0, HASH_LENGTH)
  buffer
end

.signatureFragment(normalizedBundleFragment, keyFragment) ⇒ Object


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

def signatureFragment(normalizedBundleFragment, keyFragment)
  signatureFragment = keyFragment.slice(0, keyFragment.length)
  hash = []

  kerl = Kerl.new

  (0...27).step(1) do |i|
    hash = signatureFragment.slice(i * HASH_LENGTH, HASH_LENGTH)

    (0...13-normalizedBundleFragment[i]).step(1) do |j|
      kerl.reset
      kerl.absorb(hash, 0, hash.length)
      kerl.squeeze(hash, 0, HASH_LENGTH)
    end

    (0...HASH_LENGTH).step(1) do |j|
      signatureFragment[i * HASH_LENGTH + j] = hash[j]
    end
  end

  signatureFragment
end

.validateSignatures(expectedAddress, signatureFragments, bundleHash) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/iota/crypto/signing.rb', line 64

def validateSignatures(expectedAddress, signatureFragments, bundleHash)
  if !bundleHash
    raise StandardError, "Invalid bundle hash provided"
  end

  bundle = Bundle.new

  normalizedBundleFragments = []
  normalizedBundleHash = bundle.normalizedBundle(bundleHash)

  # Split hash into 3 fragments
  (0...3).step(1) do |i|
    normalizedBundleFragments[i] = normalizedBundleHash.slice(i * 27, 27)
  end

  # Get digests
  digests = []
  (0...signatureFragments.length).step(1) do |i|
    digestBuffer = digest(normalizedBundleFragments[i % 3], Converter.trits(signatureFragments[i]))

    (0...HASH_LENGTH).step(1) do |j|
      digests[i * 243 + j] = digestBuffer[j]
    end
  end

  addressTrits = address(digests)
  address = Converter.trytes(addressTrits)

  expectedAddress == address
end