Skip to content

Commit

Permalink
Add verushash support
Browse files Browse the repository at this point in the history
  • Loading branch information
Asherda committed Sep 28, 2020
1 parent 8abe75a commit ef23963
Show file tree
Hide file tree
Showing 53 changed files with 12,830 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/asherda/lightwalletd
go 1.12

require (
github.com/asherda/go-verushash v0.0.0-20200826043445-bbbaa481fb00
github.com/btcsuite/btcd v0.20.1-beta
github.com/golang/protobuf v1.4.1
github.com/gopherjs/gopherjs v0.0.0-20191106031601-ce3c9ade29de // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/asherda/go-verushash v0.0.0-20200826043445-bbbaa481fb00 h1:Xb01DuneZ5o/FGcgd0fsV8geANOnldVKk4xofGr5EMc=
github.com/asherda/go-verushash v0.0.0-20200826043445-bbbaa481fb00/go.mod h1:yEd2IXjQPJEufs9rjOgQCacBsAA7mAvJezdtHpyocVY=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down
46 changes: 38 additions & 8 deletions parser/block_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ package parser

import (
"bytes"
"crypto/sha256"
"encoding/binary"
"math/big"
"unsafe"

"github.com/asherda/go-verushash"
"github.com/asherda/lightwalletd/parser/internal/bytestring"
"github.com/pkg/errors"
)
Expand All @@ -20,6 +21,9 @@ const (
equihashSizeMainnet = 1344 // size of a mainnet / testnet Equihash solution in bytes
)

// Initialize verushash object once.
var verusHash = verushash.NewVerushash()

// RawBlockHeader implements the block header as defined in version
// 2018.0-beta-29 of the Zcash Protocol Spec.
type RawBlockHeader struct {
Expand Down Expand Up @@ -206,11 +210,16 @@ func (hdr *BlockHeader) GetDisplayHash() []byte {
}

// SHA256d
digest := sha256.Sum256(serializedHeader)
digest = sha256.Sum256(digest[:])
//digest := sha256.Sum256(serializedHeader)
//digest = sha256.Sum256(digest[:])

// VerusHash

hash := make([]byte, 32)
ptrHash := uintptr(unsafe.Pointer(&hash[0]))
hashHeader(serializedHeader, ptrHash)
// Convert to big-endian
hdr.cachedHash = Reverse(digest[:])
hdr.cachedHash = Reverse(hash)
return hdr.cachedHash
}

Expand All @@ -223,13 +232,34 @@ func (hdr *BlockHeader) GetEncodableHash() []byte {
}

// SHA256d
digest := sha256.Sum256(serializedHeader)
digest = sha256.Sum256(digest[:])

return digest[:]
//digest := sha256.Sum256(serializedHeader)
//digest = sha256.Sum256(digest[:])

// Verushash
hash := make([]byte, 32)
ptrHash := uintptr(unsafe.Pointer(&hash[0]))
hashHeader(serializedHeader, ptrHash)
return hash
}

// GetDisplayPrevHash returns the block hash in big-endian order.
func (hdr *BlockHeader) GetDisplayPrevHash() []byte {
return Reverse(hdr.HashPrevBlock)
}

func hashHeader(serializedHeader []byte, ptrHash uintptr) {
length := len(serializedHeader)
if serializedHeader[0] == 4 && serializedHeader[2] >= 1 {
if length < 144 || serializedHeader[143] < 3 {
verusHash.Verushash_v2b(string(serializedHeader), length, ptrHash)
} else {
if serializedHeader[143] < 4 {
verusHash.Verushash_v2b1(string(serializedHeader), length, ptrHash)
} else {
verusHash.Verushash_v2b2(string(serializedHeader), ptrHash)
}
}
} else {
verusHash.Verushash(string(serializedHeader), length, ptrHash)
}
}
100 changes: 100 additions & 0 deletions vendor/github.com/asherda/go-verushash/CMakeLists.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions vendor/github.com/asherda/go-verushash/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions vendor/github.com/asherda/go-verushash/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions vendor/github.com/asherda/go-verushash/compat/byteswap.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ef23963

Please sign in to comment.