OSDN Git Service

test (#52)
[bytom/vapor.git] / mining / tensority / go_algorithm / seed.go
1 package go_algorithm
2
3 import (
4         "encoding/binary"
5         "unsafe"
6
7         "github.com/vapor/crypto/scrypt"
8         "github.com/vapor/crypto/sha3pool"
9         "github.com/vapor/protocol/bc"
10 )
11
12 func calcSeed(blockHashs []*bc.Hash) []byte {
13         data := []byte{}
14         for _, blockHash := range blockHashs {
15                 data = append(data, blockHash.Bytes()...)
16         }
17         var s [32]byte
18         sha3pool.Sum256(s[:], data)
19
20         return s[:]
21 }
22
23 func extendBytes(seed []byte, round int) []byte {
24         extSeed := make([]byte, len(seed)*(round+1))
25         copy(extSeed, seed)
26
27         for i := 0; i < round; i++ {
28                 var h [32]byte
29                 sha3pool.Sum256(h[:], extSeed[i*32:(i+1)*32])
30                 copy(extSeed[(i+1)*32:(i+2)*32], h[:])
31         }
32
33         return extSeed
34 }
35
36 func calcSeedCache(seed []byte) (cache []uint32) {
37         extSeed := extendBytes(seed, 3)
38         v := make([]uint32, 32*1024)
39
40         // Swap the byte order on big endian systems
41         if !isLittleEndian() {
42                 swap(extSeed)
43         }
44
45         for i := 0; i < 128; i++ {
46                 scrypt.Smix(extSeed, v)
47                 cache = append(cache, v...)
48         }
49
50         return cache
51 }
52
53 // isLittleEndian returns whether the local system is running in little or big
54 // endian byte order.
55 func isLittleEndian() bool {
56         n := uint32(0x01020304)
57         return *(*byte)(unsafe.Pointer(&n)) == 0x04
58 }
59
60 // swap changes the byte order of the buffer assuming a uint32 representation.
61 func swap(buffer []byte) {
62         for i := 0; i < len(buffer); i += 4 {
63                 binary.BigEndian.PutUint32(buffer[i:], binary.LittleEndian.Uint32(buffer[i:]))
64         }
65 }