pow

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2021 License: ISC Imports: 10 Imported by: 3

Documentation

Overview

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Copyright (c) 2020-2021 The bitcoinpay developers license that can be found in the LICENSE file. Reference resources of rust bitVector

Index

Constants

View Source
const (
	PROOF_DATA_EDGE_BITS_START  = 0
	PROOF_DATA_EDGE_BITS_END    = 1
	PROOF_DATA_CIRCLE_NONCE_END = 169
)
View Source
const MAX_CUCKAROOEDGEBITS = 32
View Source
const MAX_CUCKAROOMMEDGEBITS = 29
View Source
const MAX_CUCKATOOEDGEBITS = 32
View Source
const MIN_CUCKAROOEDGEBITS = 24
View Source
const MIN_CUCKAROOMMEDGEBITS = 29
View Source
const MIN_CUCKATOOEDGEBITS = 29
View Source
const POW_LENGTH = 174

proof data length 188

View Source
const PROOFDATA_LENGTH = 169

except pow type 4bytes and nonce 8 bytes 176 bytes

Variables

View Source
var (

	// oneLsh256 is 1 shifted left 256 bits.  It is defined here to avoid
	// the overhead of creating it multiple times.
	OneLsh256 = new(big.Int).Lsh(bigOne, 256)
)
View Source
var PowMapString = map[PowType]interface{}{
	BLAKE2BD:            "blake2bd",
	CUCKAROO:            "cuckaroo",
	CUCKATOO:            "cuckatoo",
	CUCKAROOM:           "cuckaroom",
	X16RV3:              "x16rv3",
	X8R16:               "x8r16",
	BITCOINPAYKECCAK256: "bitcoinpay_keccak256",
}

Functions

func BigToCompact

func BigToCompact(n *big.Int) uint32

BigToCompact converts a whole number N to a compact representation using an unsigned 32-bit number. The compact representation only provides 23 bits of precision, so values larger than (2^23 - 1) only encode the most significant digits of the number. See CompactToBig for details.

func CalcCuckooDiff

func CalcCuckooDiff(scale uint64, blockHash hash.Hash) *big.Int

calc cuckoo diff

func CalcWork

func CalcWork(bits uint32, powType PowType) *big.Int

CalcWork calculates a work value from difficulty bits. it increases the difficulty for generating a block by decreasing the value which the generated hash must be less than.

This difficulty target is stored in each block header using a compact representation as described in the documentation for CompactToBig.

The main chain is selected by choosing the chain that has the most proof of work (highest difficulty).

Since a lower target difficulty value equates to higher actual difficulty, the work value which will be accumulated must be the inverse of the difficulty. Also, in order to avoid potential division by zero and really small floating point numbers, the result adds 1 to the denominator and multiplies the numerator by 2^256.

func CompactToBig

func CompactToBig(compact uint32) *big.Int

CompactToBig converts a compact representation of a whole number N to an unsigned 32-bit number. The representation is similar to IEEE754 floating point numbers.

Like IEEE754 floating point, there are three basic components: the sign, the exponent, and the mantissa. They are broken out as follows:

  • the most significant 8 bits represent the unsigned base 256 exponent

  • bit 23 (the 24th bit) represents the sign bit

  • the least significant 23 bits represent the mantissa

    ------------------------------------------------- | Exponent | Sign | Mantissa | ------------------------------------------------- | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] | -------------------------------------------------

The formula to calculate N is:

N = (-1^sign) * mantissa * 256^(exponent-3)

This compact form is only used to encode unsigned 256-bit numbers which represent difficulty targets, thus there really is not a need for a sign bit, but it is implemented here to stay consistent with bitcoind. TODO, revisit the compact difficulty form design

func ConvertBytesToUint32Array

func ConvertBytesToUint32Array(data []byte) []uint32

func CuckooDiffToTarget

func CuckooDiffToTarget(scale uint64, diff *big.Int) string

calc cuckoo diff convert to target hash like 7fff000000000000000000000000000000000000000000000000000000000000

func HashToBig

func HashToBig(hash *hash.Hash) *big.Int

HashToBig converts a hash.Hash into a big.Int that can be used to perform math comparisons.

Types

type BitcoinpayKeccak256

type BitcoinpayKeccak256 struct {
	Pow
}

func (*BitcoinpayKeccak256) BlockData

func (this *BitcoinpayKeccak256) BlockData() PowBytes

pow proof data

func (*BitcoinpayKeccak256) Bytes

func (this *BitcoinpayKeccak256) Bytes() PowBytes

pow proof data

func (*BitcoinpayKeccak256) CheckAvailable

func (this *BitcoinpayKeccak256) CheckAvailable() bool

check pow is available

func (*BitcoinpayKeccak256) CompareDiff

func (this *BitcoinpayKeccak256) CompareDiff(newTarget *big.Int, target *big.Int) bool

compare the target wether target match the target diff

func (*BitcoinpayKeccak256) GetNextDiffBig

func (this *BitcoinpayKeccak256) GetNextDiffBig(weightedSumDiv *big.Int, oldDiffBig *big.Int, currentPowPercent *big.Int) *big.Int

func (*BitcoinpayKeccak256) GetPowResult

func (this *BitcoinpayKeccak256) GetPowResult() json.PowResult

func (*BitcoinpayKeccak256) GetSafeDiff

func (this *BitcoinpayKeccak256) GetSafeDiff(cur_reduce_diff uint64) *big.Int

func (*BitcoinpayKeccak256) PowPercent

func (this *BitcoinpayKeccak256) PowPercent() *big.Int

func (*BitcoinpayKeccak256) Verify

func (this *BitcoinpayKeccak256) Verify(headerData []byte, blockHash hash.Hash, targetDiffBits uint32) error

type Blake2bd

type Blake2bd struct {
	Pow
}

func (*Blake2bd) BlockData

func (this *Blake2bd) BlockData() PowBytes

pow proof data

func (*Blake2bd) Bytes

func (this *Blake2bd) Bytes() PowBytes

pow proof data

func (*Blake2bd) CheckAvailable

func (this *Blake2bd) CheckAvailable() bool

check pow is available

func (*Blake2bd) CompareDiff

func (this *Blake2bd) CompareDiff(newTarget *big.Int, target *big.Int) bool

compare the target wether target match the target diff

func (*Blake2bd) GetNextDiffBig

func (this *Blake2bd) GetNextDiffBig(weightedSumDiv *big.Int, oldDiffBig *big.Int, currentPowPercent *big.Int) *big.Int

func (*Blake2bd) GetPowResult

func (this *Blake2bd) GetPowResult() json.PowResult

func (*Blake2bd) GetSafeDiff

func (this *Blake2bd) GetSafeDiff(cur_reduce_diff uint64) *big.Int

func (*Blake2bd) PowPercent

func (this *Blake2bd) PowPercent() *big.Int

func (*Blake2bd) Verify

func (this *Blake2bd) Verify(headerData []byte, blockHash hash.Hash, targetDiffBits uint32) error

type Cuckaroo

type Cuckaroo struct {
	Cuckoo
}

func (*Cuckaroo) CheckAvailable

func (this *Cuckaroo) CheckAvailable() bool

check pow is available

func (*Cuckaroo) GetNextDiffBig

func (this *Cuckaroo) GetNextDiffBig(weightedSumDiv *big.Int, oldDiffBig *big.Int, currentPowPercent *big.Int) *big.Int

func (*Cuckaroo) GetSafeDiff

func (this *Cuckaroo) GetSafeDiff(cur_reduce_diff uint64) *big.Int

func (*Cuckaroo) GraphWeight

func (this *Cuckaroo) GraphWeight() uint64

calc scale the edge_bits is bigger ,then scale is bigger Reference resources https://eprint.iacr.org/2014/059.pdf 9. Difficulty control page 6 while the average number of cycles found increases slowly with size; from 2 at 2^20 to 3 at 2^30 Less times of hash calculation with the same difficulty 24 => 48 25 => 100 26 => 208 27 => 432 28 => 896 29 => 1856 30 => 3840 31 => 7936 assume init difficulty is 1000 24 target is 0c49ba5e353f7ced000000000000000000000000000000000000000000000000 (The meaning of difficulty needs to be found 1000/48 * 50 ≈ 1000 times in edge_bits 24, and the answer may be obtained once.) why * 50 , because the when edge_count/nodes = 1/2,to find 42 cycles the probality is 2.2% 29 target is db22d0e560418937000000000000000000000000000000000000000000000000 (The difficulty needs to be found 1000/1856 * 50 ≈ 26 times in edge_bits 29, and the answer may be obtained once.) so In order to ensure the fairness of different edge indexes, the mining difficulty is different.

func (*Cuckaroo) PowPercent

func (this *Cuckaroo) PowPercent() *big.Int

func (*Cuckaroo) Verify

func (this *Cuckaroo) Verify(headerData []byte, blockHash hash.Hash, targetDiffBits uint32) error

type Cuckaroom

type Cuckaroom struct {
	Cuckoo
}

func (*Cuckaroom) CheckAvailable

func (this *Cuckaroom) CheckAvailable() bool

check pow is available

func (*Cuckaroom) GetNextDiffBig

func (this *Cuckaroom) GetNextDiffBig(weightedSumDiv *big.Int, oldDiffBig *big.Int, currentPowPercent *big.Int) *big.Int

func (*Cuckaroom) GetSafeDiff

func (this *Cuckaroom) GetSafeDiff(cur_reduce_diff uint64) *big.Int

func (*Cuckaroom) GraphWeight

func (this *Cuckaroom) GraphWeight() uint64

calc scale the edge_bits is bigger ,then scale is bigger Reference resources https://eprint.iacr.org/2014/059.pdf 9. Difficulty control page 6 while the average number of cycles found increases slowly with size; from 2 at 2^20 to 3 at 2^30 Less times of hash calculation with the same difficulty 24 => 48 25 => 100 26 => 208 27 => 432 28 => 896 29 => 1856 30 => 3840 31 => 7936 assume init difficulty is 1000 24 target is 0c49ba5e353f7ced000000000000000000000000000000000000000000000000 (The meaning of difficulty needs to be found 1000/48 * 50 ≈ 1000 times in edge_bits 24, and the answer may be obtained once.) why * 50 , because the when edge_count/nodes = 1/2,to find 42 cycles the probality is 2.2% 29 target is db22d0e560418937000000000000000000000000000000000000000000000000 (The difficulty needs to be found 1000/1856 * 50 ≈ 26 times in edge_bits 29, and the answer may be obtained once.) so In order to ensure the fairness of different edge indexes, the mining difficulty is different.

func (*Cuckaroom) PowPercent

func (this *Cuckaroom) PowPercent() *big.Int

func (*Cuckaroom) Verify

func (this *Cuckaroom) Verify(headerData []byte, blockHash hash.Hash, targetDiffBits uint32) error

type Cuckatoo

type Cuckatoo struct {
	Cuckoo
}

func (*Cuckatoo) CheckAvailable

func (this *Cuckatoo) CheckAvailable() bool

check pow is available

func (*Cuckatoo) GetNextDiffBig

func (this *Cuckatoo) GetNextDiffBig(weightedSumDiv *big.Int, oldDiffBig *big.Int, currentPowPercent *big.Int) *big.Int

func (*Cuckatoo) GetSafeDiff

func (this *Cuckatoo) GetSafeDiff(cur_reduce_diff uint64) *big.Int

func (*Cuckatoo) GraphWeight

func (this *Cuckatoo) GraphWeight() uint64

calc scale the edge_bits is bigger ,then scale is bigger Reference resources https://eprint.iacr.org/2014/059.pdf 9. Difficulty control page 6 while the average number of cycles found increases slowly with size; from 2 at 2^20 to 3 at 2^30 Less times of hash calculation with the same difficulty 24 => 48 25 => 100 26 => 208 27 => 432 28 => 896 29 => 1856 30 => 3840 31 => 7936 assume init difficulty is 1000 24 target is 0c49ba5e353f7ced000000000000000000000000000000000000000000000000 (The meaning of difficulty needs to be found 1000/48 * 50 ≈ 1000 times in edge_bits 24, and the answer may be obtained once.) why * 50 , because the when edge_count/nodes = 1/2,to find 42 cycles the probality is 2.2% 29 target is db22d0e560418937000000000000000000000000000000000000000000000000 (The difficulty needs to be found 1000/1856 * 50 ≈ 26 times in edge_bits 29, and the answer may be obtained once.) so In order to ensure the fairness of different edge indexes, the mining difficulty is different.

func (*Cuckatoo) PowPercent

func (this *Cuckatoo) PowPercent() *big.Int

func (*Cuckatoo) Verify

func (this *Cuckatoo) Verify(headerData []byte, blockHash hash.Hash, targetDiffBits uint32) error

type Cuckoo

type Cuckoo struct {
	Pow
}

func (*Cuckoo) BlockData

func (this *Cuckoo) BlockData() PowBytes

pow proof data

func (*Cuckoo) Bytes

func (this *Cuckoo) Bytes() PowBytes

cuckoo pow proof data

func (*Cuckoo) CompareDiff

func (this *Cuckoo) CompareDiff(newTarget *big.Int, target *big.Int) bool

compare the target wether target match the target diff

func (*Cuckoo) GetCircleNonces

func (this *Cuckoo) GetCircleNonces() (nonces [cuckoo.ProofSize]uint32)

func (*Cuckoo) GetEdgeBits

func (this *Cuckoo) GetEdgeBits() uint8

get edge bits

func (*Cuckoo) GetPowResult

func (this *Cuckoo) GetPowResult() json.PowResult

func (*Cuckoo) GetSipHash

func (this *Cuckoo) GetSipHash(headerData []byte) hash.Hash

get sip hash first header data 113 bytes hash

func (*Cuckoo) GraphWeight

func (this *Cuckoo) GraphWeight() uint64

func (*Cuckoo) SetCircleEdges

func (this *Cuckoo) SetCircleEdges(edges []uint32)

set edge circles

func (*Cuckoo) SetEdgeBits

func (this *Cuckoo) SetEdgeBits(edge_bits uint8)

set edge bits

type IPow

type IPow interface {
	// verify result difficulty
	Verify(headerData []byte, blockHash hash.Hash, targetDiff uint32) error
	//set header nonce
	SetNonce(nonce uint32)
	//calc next diff
	GetNextDiffBig(weightedSumDiv *big.Int, oldDiffBig *big.Int, currentPowPercent *big.Int) *big.Int
	GetNonce() uint32
	GetPowType() PowType
	//set pow type
	SetPowType(powType PowType)
	GetProofData() string
	//set proof data
	SetProofData([]byte)
	Bytes() PowBytes
	BlockData() PowBytes
	//if cur_reduce_diff > 0 compare cur_reduce_diff with powLimitBits or minDiff ,the cur_reduce_diff should less than powLimitBits , and should more than min diff
	//if cur_reduce_diff <=0 return powLimit or min diff
	GetSafeDiff(cur_reduce_diff uint64) *big.Int
	// pow percent
	PowPercent() *big.Int
	//pow result
	GetPowResult() json.PowResult
	//SetParams
	SetParams(params *PowConfig)
	//SetHeight
	SetMainHeight(height int64)
	CheckAvailable() bool
	CompareDiff(newtarget *big.Int, target *big.Int) bool
}

func GetInstance

func GetInstance(powType PowType, nonce uint32, proofData []byte) IPow

get pow instance

type Percent

type Percent struct {
	//percent of every pow sum of them must be 100
	CuckarooPercent            int
	CuckatooPercent            int
	Blake2bDPercent            int
	CuckaroomPercent           int
	BitcoinpayKeccak256Percent int
	X16rv3Percent              int
	X8r16Percent               int
	MainHeight                 int64
}

type Pow

type Pow struct {
	PowType   PowType       //header pow type 1 bytes
	Nonce     uint32        //header nonce 4 bytes
	ProofData ProofDataType // 1 edge_bits  168  bytes circle length total 169 bytes
	// contains filtered or unexported fields
}

func (*Pow) GetNonce

func (this *Pow) GetNonce() uint32

func (*Pow) GetPowType

func (this *Pow) GetPowType() PowType

func (*Pow) GetProofData

func (this *Pow) GetProofData() string

func (*Pow) SetMainHeight

func (this *Pow) SetMainHeight(mainHeight int64)

func (*Pow) SetNonce

func (this *Pow) SetNonce(nonce uint32)

func (*Pow) SetParams

func (this *Pow) SetParams(params *PowConfig)

func (*Pow) SetPowType

func (this *Pow) SetPowType(powType PowType)

func (*Pow) SetProofData

func (this *Pow) SetProofData(data []byte)

set proof data except pow type

type PowBytes

type PowBytes []byte

type PowConfig

type PowConfig struct {
	// PowLimit defines the highest allowed proof of work value for a block
	// as a uint256.
	Blake2bdPowLimit *big.Int
	// PowLimitBits defines the highest allowed proof of work value for a
	// block in compact form.
	// highest value is mean min difficulty
	Blake2bdPowLimitBits uint32

	X16rv3PowLimit     *big.Int
	X16rv3PowLimitBits uint32

	X8r16PowLimit     *big.Int
	X8r16PowLimitBits uint32

	BitcoinpayKeccak256PowLimit     *big.Int
	BitcoinpayKeccak256PowLimitBits uint32

	// cuckoo difficulty calc params  min difficulty
	CuckarooMinDifficulty  uint32
	CuckaroomMinDifficulty uint32
	CuckatooMinDifficulty  uint32

	Percent []Percent

	AdjustmentStartMainHeight int64
	// contains filtered or unexported fields
}
var PowConfigInstance *PowConfig

func GetPowConfig

func GetPowConfig() *PowConfig

global cache

func (*PowConfig) Check

func (this *PowConfig) Check() error

check percent

func (*PowConfig) GetPercentByHeight

func (this *PowConfig) GetPercentByHeight(h int64) (res Percent)

get Percent By height

func (*PowConfig) Set

func (this *PowConfig) Set(p *PowConfig) *PowConfig

set config GetPowConfig().Set(params.PowConfig)

type PowType

type PowType byte
const (
	//pow type enum
	BLAKE2BD            PowType = 0
	CUCKAROO            PowType = 1
	CUCKATOO            PowType = 2
	CUCKAROOM           PowType = 3
	X16RV3              PowType = 4
	X8R16               PowType = 5
	BITCOINPAYKECCAK256 PowType = 6
)

type ProofDataType

type ProofDataType [PROOFDATA_LENGTH]byte

func (*ProofDataType) Bytes

func (this *ProofDataType) Bytes() []byte

func (*ProofDataType) String

func (this *ProofDataType) String() string

type X16rv3

type X16rv3 struct {
	Pow
}

func (*X16rv3) BlockData

func (this *X16rv3) BlockData() PowBytes

pow proof data

func (*X16rv3) Bytes

func (this *X16rv3) Bytes() PowBytes

pow proof data

func (*X16rv3) CheckAvailable

func (this *X16rv3) CheckAvailable() bool

check pow is available

func (*X16rv3) CompareDiff

func (this *X16rv3) CompareDiff(newTarget *big.Int, target *big.Int) bool

compare the target wether target match the target diff

func (*X16rv3) GetNextDiffBig

func (this *X16rv3) GetNextDiffBig(weightedSumDiv *big.Int, oldDiffBig *big.Int, currentPowPercent *big.Int) *big.Int

func (*X16rv3) GetPowResult

func (this *X16rv3) GetPowResult() json.PowResult

func (*X16rv3) GetSafeDiff

func (this *X16rv3) GetSafeDiff(cur_reduce_diff uint64) *big.Int

func (*X16rv3) PowPercent

func (this *X16rv3) PowPercent() *big.Int

func (*X16rv3) Verify

func (this *X16rv3) Verify(headerData []byte, blockHash hash.Hash, targetDiffBits uint32) error

type X8r16

type X8r16 struct {
	Pow
}

func (*X8r16) BlockData

func (this *X8r16) BlockData() PowBytes

pow proof data

func (*X8r16) Bytes

func (this *X8r16) Bytes() PowBytes

pow proof data

func (*X8r16) CheckAvailable

func (this *X8r16) CheckAvailable() bool

check pow is available

func (*X8r16) CompareDiff

func (this *X8r16) CompareDiff(newTarget *big.Int, target *big.Int) bool

compare the target wether target match the target diff

func (*X8r16) GetNextDiffBig

func (this *X8r16) GetNextDiffBig(weightedSumDiv *big.Int, oldDiffBig *big.Int, currentPowPercent *big.Int) *big.Int

func (*X8r16) GetPowResult

func (this *X8r16) GetPowResult() json.PowResult

func (*X8r16) GetSafeDiff

func (this *X8r16) GetSafeDiff(cur_reduce_diff uint64) *big.Int

func (*X8r16) PowPercent

func (this *X8r16) PowPercent() *big.Int

func (*X8r16) Verify

func (this *X8r16) Verify(headerData []byte, blockHash hash.Hash, targetDiffBits uint32) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL