forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LDPC_utils.go
202 lines (168 loc) · 4.62 KB
/
LDPC_utils.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package eccpow
import (
crand "crypto/rand"
"math"
"math/big"
"math/rand"
"github.com/cryptoecc/WorldLand/core/types"
)
//Parameters for matrix and seed
const (
BigInfinity = 1000000.0
Inf = 64.0
MaxNonce = 1<<32 - 1
// These parameters are only used for the decoding function.
maxIter = 20 // The maximum number of iteration in the decoder
crossErr = 0.01 // A transisient error probability. This is also fixed as a small value
)
type Parameters struct {
n int
m int
wc int
wr int
seed int
}
// setParameters sets n, wc, wr, m, seed return parameters and difficulty level
func setParameters(header *types.Header) (Parameters, int) {
//level := SearchLevel(header.Difficulty)
level := SearchLevel(header.Difficulty)
parameters := Parameters{
n: Table[level].n,
wc: Table[level].wc,
wr: Table[level].wr,
}
parameters.m = int(parameters.n * parameters.wc / parameters.wr)
parameters.seed = generateSeed(header.ParentHash)
return parameters, level
}
// setParameters sets n, wc, wr, m, seed return parameters and difficulty level
func setParameters_Seoul(header *types.Header) (Parameters, int) {
//level := SearchLevel(header.Difficulty)
level := SearchLevel_Seoul(header.Difficulty)
table := getTable(level)
parameters := Parameters{
n: table.n,
wc: table.wc,
wr: table.wr,
}
parameters.m = int(parameters.n * parameters.wc / parameters.wr)
parameters.seed = generateSeed(header.ParentHash)
return parameters, level
}
//generateRandomNonce generate 64bit random nonce with similar way of ethereum block nonce
func generateRandomNonce() uint64 {
seed, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
source := rand.New(rand.NewSource(seed.Int64()))
return uint64(source.Int63())
}
func funcF(x float64) float64 {
if x >= BigInfinity {
return 1.0 / BigInfinity
} else if x <= (1.0 / BigInfinity) {
return BigInfinity
} else {
return math.Log((math.Exp(x) + 1) / (math.Exp(x) - 1))
}
}
func infinityTest(x float64) float64 {
if x >= Inf {
return Inf
} else if x <= -Inf {
return -Inf
} else {
return x
}
}
//generateSeed generate seed using previous hash vector
func generateSeed(phv [32]byte) int {
sum := 0
for i := 0; i < len(phv); i++ {
sum += int(phv[i])
}
return sum
}
//generateH generate H matrix using parameters
//generateH Cannot be sure rand is same with original implementation of C++
func generateH(parameters Parameters) [][]int {
var H [][]int
var hSeed int64
var colOrder []int
hSeed = int64(parameters.seed)
k := parameters.m / parameters.wc
H = make([][]int, parameters.m)
for i := range H {
H[i] = make([]int, parameters.n)
}
for i := 0; i < k; i++ {
for j := i * parameters.wr; j < (i+1)*parameters.wr; j++ {
H[i][j] = 1
}
}
for i := 1; i < parameters.wc; i++ {
colOrder = nil
for j := 0; j < parameters.n; j++ {
colOrder = append(colOrder, j)
}
src := rand.NewSource(hSeed)
rnd := rand.New(src)
rnd.Seed(hSeed)
rnd.Shuffle(len(colOrder), func(i, j int) {
colOrder[i],colOrder[j] = colOrder[j], colOrder[i]
})
hSeed--
for j := 0; j < parameters.n; j++ {
index := (colOrder[j]/parameters.wr + k*i)
H[index][j] = 1
}
}
return H
}
//generateQ generate colInRow and rowInCol matrix using H matrix
func generateQ(parameters Parameters, H [][]int) ([][]int, [][]int) {
colInRow := make([][]int, parameters.wr)
for i := 0; i < parameters.wr; i++ {
colInRow[i] = make([]int, parameters.m)
}
rowInCol := make([][]int, parameters.wc)
for i := 0; i < parameters.wc; i++ {
rowInCol[i] = make([]int, parameters.n)
}
rowIndex := 0
colIndex := 0
for i := 0; i < parameters.m; i++ {
for j := 0; j < parameters.n; j++ {
if H[i][j] == 1 {
colInRow[colIndex%parameters.wr][i] = j
colIndex++
rowInCol[rowIndex/parameters.n][j] = i
rowIndex++
}
}
}
return colInRow, rowInCol
}
//generateHv generate hashvector
//It needs to compare with origin C++ implementation Especially when sha256 function is used
func generateHv(parameters Parameters, encryptedHeaderWithNonce []byte) []int {
hashVector := make([]int, parameters.n)
/*
if parameters.n <= 256 {
tmpHashVector = sha256.Sum256(headerWithNonce)
} else {
/*
This section is for a case in which the size of a hash vector is larger than 256.
This section will be implemented soon.
}
transform the constructed hexadecimal array into an binary array
ex) FE01 => 11111110000 0001
*/
for i := 0; i < parameters.n/8; i++ {
decimal := int(encryptedHeaderWithNonce[i])
for j := 7; j >= 0; j-- {
hashVector[j+8*(i)] = decimal % 2
decimal /= 2
}
}
//outputWord := hashVector[:parameters.n]
return hashVector
}