-
Notifications
You must be signed in to change notification settings - Fork 95
/
pow.go
64 lines (52 loc) · 1.73 KB
/
pow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Package pow implements the Curl-based proof of work for arbitrary binary data.
package pow
import (
"crypto"
"encoding/binary"
"math"
_ "golang.org/x/crypto/blake2b" // BLAKE2b_256 is the default hash function for the PoW digest
legacy "github.com/iotaledger/iota.go/consts"
"github.com/iotaledger/iota.go/curl"
"github.com/iotaledger/iota.go/encoding/b1t6"
"github.com/iotaledger/iota.go/trinary"
)
// Hash defines the hash function that is used to compute the PoW digest.
var Hash = crypto.BLAKE2b_256
const (
nonceBytes = 8 // len(uint64)
)
// Score returns the PoW score of msg.
func Score(msg []byte) float64 {
if len(msg) < nonceBytes {
panic("pow: invalid message length")
}
h := Hash.New()
dataLen := len(msg) - nonceBytes
// the PoW digest is the hash of msg without the nonce
h.Write(msg[:dataLen])
powDigest := h.Sum(nil)
// extract the nonce from msg and compute the number of trailing zeros
nonce := binary.LittleEndian.Uint64(msg[dataLen:])
zeros := trailingZeros(powDigest, nonce)
// compute the score
return math.Pow(legacy.TrinaryRadix, float64(zeros)) / float64(len(msg))
}
func trailingZeros(powDigest []byte, nonce uint64) int {
// allocate exactly one Curl block
buf := make(trinary.Trits, legacy.HashTrinarySize)
n := b1t6.Encode(buf, powDigest)
// add the nonce to the trit buffer
encodeNonce(buf[n:], nonce)
c := curl.NewCurlP81()
if err := c.Absorb(buf); err != nil {
panic(err)
}
digest, _ := c.Squeeze(legacy.HashTrinarySize)
return trinary.TrailingZeros(digest)
}
// encodeNonce encodes nonce as 48 trits using the b1t6 encoding.
func encodeNonce(dst trinary.Trits, nonce uint64) {
var nonceBuf [nonceBytes]byte
binary.LittleEndian.PutUint64(nonceBuf[:], nonce)
b1t6.Encode(dst, nonceBuf[:])
}