-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof.go
121 lines (97 loc) · 2.97 KB
/
proof.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package poc
import (
"math"
"math/big"
"github.com/fatzero/mass-core/consensus"
"github.com/fatzero/mass-core/poc/chiapos"
"github.com/fatzero/mass-core/poc/pocutil"
)
const (
// PoCSlot represents the unit Slot of PoC
PoCSlot = 3
// KiB represents KiByte
KiB = 1024
// MiB represents MiByte
MiB = 1024 * KiB
// MinValidDefaultBitLength represents smallest default proof BitLength
MinValidDefaultBitLength = 24
// MaxValidDefaultBitLength represents biggest default BitLength
MaxValidDefaultBitLength = 40
// MinValidDefaultBitLength represents smallest default proof BitLength
MinValidChiaBitLength = chiapos.MinPlotSize
// MaxValidDefaultBitLength represents biggest default BitLength
MaxValidChiaBitLength = chiapos.MaxPlotSize
// QualityConstantMASSIP0002 multiplies the original quality to fix proof filter
QualityConstantMASSIP0002 = 512
// QualityConstantMASSValidity represents the fix between mass and chia proof
QualityConstantMASSValidity = 0.329
)
type ProofType uint8
const (
ProofTypeDefault ProofType = 0
ProofTypeChia ProofType = 1
ProofTypeEmpty ProofType = math.MaxUint8
)
// EnsureBitLength returns whether it is a valid bitLength.
func (pt ProofType) EnsureBitLength(bl int) bool {
switch pt {
case ProofTypeDefault:
return bl >= MinValidDefaultBitLength && bl <= MaxValidDefaultBitLength && bl%2 == 0
case ProofTypeChia:
return MinValidChiaBitLength <= bl && bl <= MaxValidChiaBitLength
default:
return false
}
}
func (pt ProofType) PlotSize(bl int) uint64 {
if !pt.EnsureBitLength(bl) {
return 0
}
switch pt {
case ProofTypeDefault:
return DefaultPlotSize(bl)
case ProofTypeChia:
return ChiaPlotSize(bl)
default:
return 0
}
}
func PlotSize(proofType ProofType, bl int) uint64 {
return proofType.PlotSize(bl)
}
type Proof interface {
Type() ProofType
BitLength() int
Encode() []byte
Decode([]byte) error
Quality(slot, height uint64) *big.Int
Verify(seed pocutil.Hash, challenge pocutil.Hash, filter bool) error
VerifiedQuality(seed pocutil.Hash, challenge pocutil.Hash, filter bool, slot, height uint64) (*big.Int, error)
}
func EnforceMASSIP0002(height uint64) bool {
return height >= consensus.MASSIP0002Height
}
// VerifyProof verifies proof.
func VerifyProof(proof Proof, pubKeyHash pocutil.Hash, challenge pocutil.Hash, filter bool) error {
return proof.Verify(pubKeyHash, challenge, filter)
}
// GetQuality produces the relative quality with factor Q1
func GetQuality(Q1 *big.Float, hashVal pocutil.Hash) *big.Int {
// Note: FH = H in big.Float
FH := new(big.Float).SetInt(new(big.Int).SetBytes(hashVal[:]))
F64H, _ := FH.Float64()
// Note: log2FH = log2(H)
log2FH := big.NewFloat(math.Log2(F64H))
// Note: Q2 = 256 - log2(H)
Q2 := big.NewFloat(256)
Q2.Sub(Q2, log2FH)
if Q2.Cmp(big.NewFloat(0)) <= 0 {
return big.NewInt(0)
}
quality := big.NewInt(0)
big.NewFloat(0).Quo(Q1, Q2).Int(quality)
return quality
}
func IsValidProofType(pt ProofType) bool {
return pt == ProofTypeDefault || pt == ProofTypeChia
}