OSDN Git Service

Dev mining (#390)
authorChengcheng Zhang <zcc0721@foxmail.com>
Sat, 10 Mar 2018 01:49:10 +0000 (09:49 +0800)
committerPaladz <yzhu101@uottawa.ca>
Sat, 10 Mar 2018 01:49:10 +0000 (09:49 +0800)
* update

* update

* delete comment

* update tensority, same with c++

* add smix.go

* tensority is right

* add TestHash()

* add TestCalcSeedCache

* add TestCalcSeedCache

* Update scrypt.go

* mining/tensority: Time & space opt for dataIdentity[] init in mulMatrix()

* clean up code

* clean up codes

* remove hexutil files

* update tensority test

cmd/miner/miner [new file with mode: 0755]
crypto/scrypt/scrypt.go
crypto/scrypt/smix.go [new file with mode: 0644]
mining/tensority/matrix.go
mining/tensority/seed.go
mining/tensority/tensority.go
mining/tensority/tensority_test.go [new file with mode: 0644]

diff --git a/cmd/miner/miner b/cmd/miner/miner
new file mode 100755 (executable)
index 0000000..bb09fe4
Binary files /dev/null and b/cmd/miner/miner differ
index 9023319..cf3dd31 100644 (file)
@@ -210,10 +210,6 @@ func smix(b []byte, r, N int, v, xy []uint32) {
        }
 }
 
-func Smix(b []byte, r, N int, v, xy []uint32) {
-       smix(b, r, N, v, xy)
-}
-
 // Key derives a key from the password, salt, and cost parameters, returning
 // a byte slice of length keyLen that can be used as cryptographic key.
 //
diff --git a/crypto/scrypt/smix.go b/crypto/scrypt/smix.go
new file mode 100644 (file)
index 0000000..5376d89
--- /dev/null
@@ -0,0 +1,8 @@
+package scrypt
+
+// Smix create tensority cache
+// Some value is fixed: r = 1, N = 1024.
+func Smix(b []byte, v []uint32) {
+       xy := make([]uint32, 64)
+       smix(b, 1, 1024, v, xy)
+}
index c25f589..a2f63f1 100644 (file)
@@ -5,7 +5,6 @@ import (
        "unsafe"
 
        "gonum.org/v1/gonum/mat"
-
        "github.com/bytom/crypto/sha3pool"
        "github.com/bytom/protocol/bc"
 )
@@ -13,54 +12,82 @@ import (
 const (
        matSize     = 1 << 8 // Size of matrix
        matNum      = 1 << 8 // Number of matrix
-       epochLength = 1 << 7 // Blocks per epoch
-       mulRounds   = 1 << 8 // Number of rounds in mulmatrix
 )
 
 func mulMatrix(headerhash []byte, cache []uint32) []uint8 {
-       // Convert our destination slice to a byte buffer
-       header := *(*reflect.SliceHeader)(unsafe.Pointer(&cache))
+       ui32data := make([]uint32, matNum*matSize*matSize/4)
+       for i := 0; i < 128; i++ {
+               start := i * 1024 * 32
+               for j := 0; j < 512; j++ {
+                       copy(ui32data[start+j*32:start+j*32+32], cache[start+j*64:start+j*64+32])
+                       copy(ui32data[start+512*32+j*32:start+512*32+j*32+32], cache[start+j*64+32:start+j*64+64])
+               }
+       }
+
+       // Convert our destination slice to a int8 buffer
+       header := *(*reflect.SliceHeader)(unsafe.Pointer(&ui32data))
        header.Len *= 4
        header.Cap *= 4
-       cacheInt8 := *(*[]int8)(unsafe.Pointer(&header))
+       i8data := *(*[]int8)(unsafe.Pointer(&header))
 
-       data := make([]float64, matNum*matSize*matSize)
-       for i := 0; i < len(cacheInt8); i++ {
-               data[i] = float64(cacheInt8[i])
+       f64data := make([]float64, matNum*matSize*matSize)
+       for i := 0; i < matNum*matSize*matSize; i++ {
+               f64data[i] = float64(i8data[i])
        }
 
-       exthash := extendBytes(headerhash, 7)
+       tmp := mat.NewDense(matSize, matSize, make([]float64, matSize*matSize))
+
 
-       mb := mat.NewDense(matSize, matSize, data[:matSize*matSize])
-       mc := mat.NewDense(matSize, matSize, make([]float64, matSize*matSize))
+       dataIdentity := make([]float64, matSize*matSize)
        for i := 0; i < 256; i++ {
-               index := int(exthash[i])
-               ma := mat.NewDense(matSize, matSize, data[index*matSize*matSize:(index+1)*matSize*matSize])
-               mc.Mul(ma, mb)
+               dataIdentity[i*257] = float64(1)
+       }
 
-               for j := 0; j < matSize; j++ {
-                       for k := 0; k < matSize; k++ {
-                               i32v := int32(mc.At(j, k))
-                               i8v := int8((i32v & 0xff) +
-                                       ((i32v >> 8) & 0xff))
-                               mc.Set(j, k, float64(i8v))
+       for i := 0; i < 4; i++ {
+               ma := mat.NewDense(matSize, matSize, dataIdentity)
+               mc := mat.NewDense(matSize, matSize, make([]float64, matSize*matSize))
+
+               var sequence [32]byte
+               sha3pool.Sum256(sequence[:], headerhash[i*8:(i+1)*8])
+
+               for j := 0; j < 2; j++ {
+                       for k := 0; k < 32; k++ {
+                               index := int(sequence[k])
+                               mb := mat.NewDense(matSize, matSize, f64data[index*matSize*matSize:(index+1)*matSize*matSize])
+                               mc.Mul(ma, mb.T())
+                               for row := 0; row < matSize; row++ {
+                                       for col := 0; col < matSize; col++ {
+                                               i32v := int32(mc.At(row, col))
+                                               i8v := int8((i32v & 0xff) +
+                                                       ((i32v >> 8) & 0xff))
+                                               mc.Set(row, col, float64(i8v))
+                                       }
+                               }
+                               ma = mc
                        }
                }
 
-               mb = mc
+               for row := 0; row < matSize; row++ {
+                       for col := 0; col < matSize; col++ {
+                               i32vtmp := int32(tmp.At(row, col))
+                               i32vma := int32(ma.At(row, col))
+                               i8v := int8(int32(i32vtmp+i32vma) & 0xff)
+                               tmp.Set(row, col, float64(i8v))
+                       }
+               }
        }
 
        result := make([]uint8, 0)
        for i := 0; i < matSize; i++ {
                for j := 0; j < matSize; j++ {
-                       result = append(result, uint8(mc.At(i, j)))
+                       result = append(result, uint8(tmp.At(i, j)))
                }
        }
 
        return result
 }
 
-// TODO: check why
+// hashMatrix hash result of mulMatrix
 func hashMatrix(result []uint8) *bc.Hash {
        var mat8 [matSize][matSize]uint8
        for i := 0; i < matSize; i++ {
@@ -70,7 +97,7 @@ func hashMatrix(result []uint8) *bc.Hash {
        }
 
        var mat32 [matSize][matSize / 4]uint32
-       // ATTENTION !!!!!!! C++ is different!!!
+
        for i := 0; i < matSize; i++ {
                for j := 0; j < matSize/4; j++ {
                        mat32[i][j] = ((uint32(mat8[i][j+192])) << 24) |
@@ -80,7 +107,6 @@ func hashMatrix(result []uint8) *bc.Hash {
                }
        }
 
-       data := make([]uint32, 0)
        for k := matSize; k > 1; k = k / 2 {
                for j := 0; j < k/2; j++ {
                        for i := 0; i < matSize/4; i++ {
@@ -89,12 +115,13 @@ func hashMatrix(result []uint8) *bc.Hash {
                }
        }
 
+       ui32data := make([]uint32, 0)
        for i := 0; i < matSize/4; i++ {
-               data = append(data, mat32[0][i])
+               ui32data = append(ui32data, mat32[0][i])
        }
 
        // Convert our destination slice to a byte buffer
-       header := *(*reflect.SliceHeader)(unsafe.Pointer(&data))
+       header := *(*reflect.SliceHeader)(unsafe.Pointer(&ui32data))
        header.Len *= 4
        header.Cap *= 4
        dataBytes := *(*[]byte)(unsafe.Pointer(&header))
index 99ab6dc..850d9ed 100644 (file)
@@ -1,26 +1,32 @@
 package tensority
 
 import (
-       "github.com/bytom/crypto"
+       "encoding/binary"
+       "unsafe"
+
        "github.com/bytom/crypto/scrypt"
+       "github.com/bytom/crypto/sha3pool"
        "github.com/bytom/protocol/bc"
 )
 
 func calcSeed(blockHashs []*bc.Hash) []byte {
-       date := []byte{}
+       data := []byte{}
        for _, blockHash := range blockHashs {
-               date = append(date, blockHash.Bytes()...)
+               data = append(data, blockHash.Bytes()...)
        }
-       return crypto.Sha256(date)
+       var s [32]byte
+       sha3pool.Sum256(s[:], data)
+
+       return s[:]
 }
 
-// TODO: clean the code, now it's hard to read
 func extendBytes(seed []byte, round int) []byte {
        extSeed := make([]byte, len(seed)*(round+1))
        copy(extSeed, seed)
 
        for i := 0; i < round; i++ {
-               h := crypto.Sha256(extSeed[i*32 : (i+1)*32])
+               var h [32]byte
+               sha3pool.Sum256(h[:], extSeed[i*32:(i+1)*32])
                copy(extSeed[(i+1)*32:(i+2)*32], h[:])
        }
 
@@ -29,11 +35,31 @@ func extendBytes(seed []byte, round int) []byte {
 
 func calcSeedCache(seed []byte) (cache []uint32) {
        extSeed := extendBytes(seed, 3)
-       xy := make([]uint32, 64)
        v := make([]uint32, 32*1024)
+
+       // Swap the byte order on big endian systems
+       if !isLittleEndian() {
+               swap(extSeed)
+       }
+
        for i := 0; i < 128; i++ {
-               scrypt.Smix(extSeed, 1, 1024, v, xy)
+               scrypt.Smix(extSeed, v)
                cache = append(cache, v...)
        }
+
        return cache
 }
+
+// isLittleEndian returns whether the local system is running in little or big
+// endian byte order.
+func isLittleEndian() bool {
+       n := uint32(0x01020304)
+       return *(*byte)(unsafe.Pointer(&n)) == 0x04
+}
+
+// swap changes the byte order of the buffer assuming a uint32 representation.
+func swap(buffer []byte) {
+       for i := 0; i < len(buffer); i += 4 {
+               binary.BigEndian.PutUint32(buffer[i:], binary.LittleEndian.Uint32(buffer[i:]))
+       }
+}
index 0f7f399..3a37066 100644 (file)
@@ -8,5 +8,6 @@ type AIHash struct{}
 func Hash(hash, seed *bc.Hash) *bc.Hash {
        cache := calcSeedCache(seed.Bytes())
        data := mulMatrix(hash.Bytes(), cache)
+
        return hashMatrix(data)
 }
diff --git a/mining/tensority/tensority_test.go b/mining/tensority/tensority_test.go
new file mode 100644 (file)
index 0000000..02f9f45
--- /dev/null
@@ -0,0 +1,131 @@
+package tensority
+
+import (
+       "reflect"
+       "testing"
+
+       "github.com/bytom/protocol/bc"
+)
+
+// Tests that tensority hash result is correct.
+func TestHash(t *testing.T) {
+       tests := []struct {
+               blockHeader [32]byte
+               seed        [32]byte
+               hash        [32]byte
+       }{
+               {
+                       blockHeader:    [32]byte{
+                               0xd0, 0xda, 0xd7, 0x3f, 0xb2, 0xda, 0xbf, 0x33,
+                               0x53, 0xfd, 0xa1, 0x55, 0x71, 0xb4, 0xe5, 0xf6,
+                               0xac, 0x62, 0xff, 0x18, 0x7b, 0x35, 0x4f, 0xad,
+                               0xd4, 0x84, 0x0d, 0x9f, 0xf2, 0xf1, 0xaf, 0xdf,
+                       },
+                       seed:                   [32]byte{
+                               0x07, 0x37, 0x52, 0x07, 0x81, 0x34, 0x5b, 0x11,
+                               0xb7, 0xbd, 0x0f, 0x84, 0x3c, 0x1b, 0xdd, 0x9a,
+                               0xea, 0x81, 0xb6, 0xda, 0x94, 0xfd, 0x14, 0x1c,
+                               0xc9, 0xf2, 0xdf, 0x53, 0xac, 0x67, 0x44, 0xd2,
+                       },
+                       hash:                   [32]byte{
+                               0xe3, 0x5d, 0xa5, 0x47, 0x95, 0xd8, 0x2f, 0x85,
+                               0x49, 0xc0, 0xe5, 0x80, 0xcb, 0xf2, 0xe3, 0x75,
+                               0x7a, 0xb5, 0xef, 0x8f, 0xed, 0x1b, 0xdb, 0xe4,
+                               0x39, 0x41, 0x6c, 0x7e, 0x6f, 0x8d, 0xf2, 0x27,
+                       },
+               },
+               {
+                       blockHeader:    [32]byte{
+                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       },
+                       seed:                   [32]byte{
+                               0x48, 0xdd, 0xa5, 0xbb, 0xe9, 0x17, 0x1a, 0x66,
+                               0x56, 0x20, 0x6e, 0xc5, 0x6c, 0x59, 0x5c, 0x58,
+                               0x34, 0xb6, 0xcf, 0x38, 0xc5, 0xfe, 0x71, 0xbc,
+                               0xb4, 0x4f, 0xe4, 0x38, 0x33, 0xae, 0xe9, 0xdf,
+                       },
+                       hash:                   [32]byte{
+                               0x26, 0xdb, 0x94, 0xef, 0xa4, 0x22, 0xd7, 0x6c,
+                               0x40, 0x2a, 0x54, 0xee, 0xb6, 0x1d, 0xd5, 0xf5,
+                               0x32, 0x82, 0xcd, 0x3c, 0xe1, 0xa0, 0xac, 0x67,
+                               0x7e, 0x17, 0x70, 0x51, 0xed, 0xaa, 0x98, 0xc1,
+                       },
+               },
+               {
+                       blockHeader:    [32]byte{
+                               0x8d, 0x96, 0x9e, 0xef, 0x6e, 0xca, 0xd3, 0xc2,
+                               0x9a, 0x3a, 0x62, 0x92, 0x80, 0xe6, 0x86, 0xcf,
+                               0x0c, 0x3f, 0x5d, 0x5a, 0x86, 0xaf, 0xf3, 0xca,
+                               0x12, 0x02, 0x0c, 0x92, 0x3a, 0xdc, 0x6c, 0x92,
+                       },
+                       seed:                   [32]byte{
+                               0x0e, 0x3b, 0x78, 0xd8, 0x38, 0x08, 0x44, 0xb0,
+                               0xf6, 0x97, 0xbb, 0x91, 0x2d, 0xa7, 0xf4, 0xd2,
+                               0x10, 0x38, 0x2c, 0x67, 0x14, 0x19, 0x4f, 0xd1,
+                               0x60, 0x39, 0xef, 0x2a, 0xcd, 0x92, 0x4d, 0xcf,
+                       },
+                       hash:                   [32]byte{
+                               0xfe, 0xce, 0xc3, 0x36, 0x69, 0x73, 0x75, 0x92,
+                               0xf7, 0x75, 0x4b, 0x21, 0x5b, 0x20, 0xba, 0xce,
+                               0xfb, 0xa6, 0x4d, 0x2e, 0x4c, 0xa1, 0x65, 0x6f,
+                               0x85, 0xea, 0x1d, 0x3d, 0xbe, 0x16, 0x28, 0x39,
+                       },
+               },
+               {
+                       blockHeader:    [32]byte{
+                               0x2f, 0x01, 0x43, 0x11, 0xe0, 0x92, 0x6f, 0xa8,
+                               0xb3, 0xd6, 0xe6, 0xde, 0x20, 0x51, 0xbf, 0x69,
+                               0x33, 0x21, 0x23, 0xba, 0xad, 0xfe, 0x52, 0x2b,
+                               0x62, 0xf4, 0x64, 0x56, 0x55, 0x85, 0x9e, 0x7a,
+                       },
+                       seed:                   [32]byte{
+                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                               0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                       },
+                       hash:                   [32]byte{
+                               0xc1, 0xc3, 0xcf, 0x4c, 0x76, 0x96, 0x8e, 0x29,
+                               0x67, 0xf0, 0x05, 0x3c, 0x76, 0xf2, 0x08, 0x4c,
+                               0xc0, 0x1e, 0xd0, 0xfe, 0x97, 0x66, 0x42, 0x8d,
+                               0xb9, 0x9c, 0x45, 0xbe, 0xdf, 0x0c, 0xdb, 0xe2,
+                       },
+               },
+               {
+                       blockHeader:    [32]byte{
+                               0xe0, 0xe3, 0xc4, 0x31, 0x78, 0xa1, 0x26, 0xd0,
+                               0x48, 0x71, 0xb9, 0xc5, 0xd0, 0xc6, 0x42, 0xe5,
+                               0xe0, 0x8b, 0x96, 0x79, 0xa5, 0xf6, 0x6b, 0x82,
+                               0x1b, 0xd9, 0xa0, 0x30, 0xef, 0xf0, 0x2c, 0xe7,
+                       },
+                       seed:                   [32]byte{
+                               0x6a, 0xb2, 0x1e, 0x13, 0x01, 0xf5, 0x75, 0x2c,
+                               0x2f, 0xca, 0x1b, 0x55, 0x98, 0xf4, 0x9d, 0x37,
+                               0x69, 0x48, 0x2e, 0x07, 0x3c, 0x1f, 0x26, 0xe3,
+                               0xb8, 0x36, 0x5f, 0x40, 0x55, 0x53, 0xea, 0x31,
+                       },
+                       hash:                   [32]byte{
+                               0xab, 0xbc, 0x2c, 0xb3, 0x96, 0x38, 0xf6, 0x84,
+                               0x23, 0x5f, 0xbc, 0x1b, 0x3f, 0xf1, 0x07, 0x94,
+                               0x59, 0x48, 0xc5, 0x81, 0xb6, 0x92, 0x9b, 0xae,
+                               0x2c, 0xd6, 0x81, 0x88, 0x9f, 0xf2, 0xd8, 0x24,
+                       },
+               },
+       }
+       for i, tt := range tests {
+               bhhash := bc.NewHash(tt.blockHeader)
+               bhseed := bc.NewHash(tt.seed)
+
+               result := Hash(&bhhash, &bhseed).Bytes()
+
+               var resArr [32]byte
+               copy(resArr[:], result)
+
+               if !reflect.DeepEqual(resArr, tt.hash) {
+                       t.Errorf("hash %d: content mismatch: have %x, correct %x", i, resArr, tt.hash)
+               }
+       }
+}