Module: Litecoin::Scrypt

Extended by:
Scrypt
Included in:
Scrypt
Defined in:
lib/bitcoin/litecoin.rb

Instance Method Summary collapse

Instance Method Details

#pbkdf2_sha256(pass, salt, c = 1, dk_len = 128) ⇒ Object



27
28
29
30
31
# File 'lib/bitcoin/litecoin.rb', line 27

def pbkdf2_sha256(pass, salt, c=1, dk_len=128)
  raise "pbkdf2_sha256: wrong length." if pass.bytesize != 80 or ![80,128].include?(salt.bytesize)
  raise "pbkdf2_sha256: wrong dk length." if ![128,32].include?(dk_len)
  OpenSSL::PKCS5.pbkdf2_hmac(pass, salt, iter=c, dk_len, OpenSSL::Digest::SHA256.new)
end

#rotl(a, b) ⇒ Object



33
34
35
# File 'lib/bitcoin/litecoin.rb', line 33

def rotl(a, b)
  a &= 0xffffffff; ((a << b) | (a >> (32 - b))) & 0xffffffff
end

#scrypt_1024_1_1_256_sp(input, scratchpad = []) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bitcoin/litecoin.rb', line 6

def scrypt_1024_1_1_256_sp(input, scratchpad=[])
  b = pbkdf2_sha256(input, input, 1, 128)
  x = b.unpack("V*")
  v = scratchpad

  1024.times{|i|
    v[(i*32)...((i*32)+32)] = x.dup
    xor_salsa8(x, x, 0, 16)
    xor_salsa8(x, x, 16, 0)
  }

  1024.times{|i|
    j = 32 * (x[16] & 1023)
    32.times{|k| x[k] ^= v[j+k] }
    xor_salsa8(x, x, 0, 16)
    xor_salsa8(x, x, 16, 0)
  }

  pbkdf2_sha256(input, x.pack("V*"), 1, 32)
end

#xor_salsa8(a, b, a_offset, b_offset) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bitcoin/litecoin.rb', line 37

def xor_salsa8(a, b, a_offset, b_offset)
  x = 16.times.map{|n| a[a_offset+n] ^= b[b_offset+n] }

  4.times{
    [
      [4, 0, 12, 7], [9, 5, 1, 7],  [14, 10, 6, 7], [3, 15, 11, 7],
      [8, 4, 0, 9], [13, 9, 5, 9],  [2, 14, 10, 9], [7, 3, 15, 9],
      [12, 8, 4, 13], [1, 13, 9, 13],  [6, 2, 14, 13], [11, 7, 3, 13],
      [0, 12, 8, 18], [5, 1, 13, 18],  [10, 6, 2, 18], [15, 11, 7, 18],

      [1, 0, 3, 7], [6, 5, 4, 7],  [11, 10, 9, 7], [12, 15, 14, 7],
      [2, 1, 0, 9], [7, 6, 5, 9],  [8, 11, 10, 9], [13, 12, 15, 9],
      [3, 2, 1, 13], [4, 7, 6, 13],  [9, 8, 11, 13], [14, 13, 12, 13],
      [0, 3, 2, 18], [5, 4, 7, 18],  [10, 9, 8, 18], [15, 14, 13, 18]
    ].each{|i|
      x[ i[0] ] ^= rotl(x[ i[1] ] + x[ i[2] ], i[3])
    }
  }

  16.times{|n| a[a_offset+n] = (a[a_offset+n] + x[n]) & 0xffffffff }
  true
end