Skip to content
This repository was archived by the owner on Feb 16, 2024. It is now read-only.

Commit 3e3fd33

Browse files
committed
added octopus (conflux) algorithm
1 parent d9442ee commit 3e3fd33

File tree

14 files changed

+1202
-17
lines changed

14 files changed

+1202
-17
lines changed

cmd/gen-lookup/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ func genCacheLookupTable(initBytes, growthBytes uint64, epochs int) []uint64 {
2929
return lookupTable
3030
}
3131

32-
func genDatasetLookupTable(initBytes, growthBytes uint64, epochs int) []uint64 {
32+
func genDatasetLookupTable(mixBytes, initBytes, growthBytes uint64, epochs int) []uint64 {
3333
d := &dag.DAG{
3434
Config: dag.Config{
35-
CacheInitBytes: initBytes,
36-
CacheGrowthBytes: growthBytes,
35+
MixBytes: mixBytes,
36+
DatasetInitBytes: initBytes,
37+
DatasetGrowthBytes: growthBytes,
3738
},
3839
}
3940

@@ -96,10 +97,11 @@ var datasetSizes = {{.DatasetLookup}}
9697
func main() {
9798
var packageName string
9899
var epochs int
99-
var cacheInitBytes, cacheGrowthBytes, datasetInitBytes, datasetGrowthBytes uint64
100+
var mixBytes, cacheInitBytes, cacheGrowthBytes, datasetInitBytes, datasetGrowthBytes uint64
100101

101102
flag.StringVar(&packageName, "package", "", "The package name where the lookup table will be generated")
102103
flag.IntVar(&epochs, "epochs", 2048, "The number of epochs to generate for")
104+
flag.Uint64Var(&mixBytes, "mixBytes", 128, "The number of bytes in a mix hash")
103105
flag.Uint64Var(&cacheInitBytes, "cacheInit", 0, "The cache initialization size in bytes")
104106
flag.Uint64Var(&cacheGrowthBytes, "cacheGrowth", 0, "The cache growth factor in bytes")
105107
flag.Uint64Var(&datasetInitBytes, "datasetInit", 0, "The dataset initialization size in bytes")
@@ -111,7 +113,7 @@ func main() {
111113
}
112114

113115
cacheLookupTable := genCacheLookupTable(cacheInitBytes, cacheGrowthBytes, epochs)
114-
datasetLookupTable := genDatasetLookupTable(datasetInitBytes, datasetGrowthBytes, epochs)
116+
datasetLookupTable := genDatasetLookupTable(mixBytes, datasetInitBytes, datasetGrowthBytes, epochs)
115117

116118
data := map[string]interface{}{
117119
"Package": packageName,

ethash/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
//go:generate ../.bin/gen-lookup -package ethash -cacheInit 16777216 -cacheGrowth 131072 -datasetInit 1073741824 -datasetGrowth 8388608
17+
//go:generate ../.bin/gen-lookup -package ethash -mixBytes 128 -cacheInit 16777216 -cacheGrowth 131072 -datasetInit 1073741824 -datasetGrowth 8388608
1818

1919
package ethash
2020

@@ -51,6 +51,7 @@ func NewEthereum() *Client {
5151
CacheSizes: dag.NewLookupTable(cacheSizes, 2048),
5252
DatasetSizes: dag.NewLookupTable(datasetSizes, 2048),
5353

54+
MixBytes: 128,
5455
DatasetParents: 256,
5556
EpochLength: 30000,
5657
SeedEpochLength: 30000,

firopow/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:generate ../.bin/gen-lookup -package firopow -cacheInit 16777216 -cacheGrowth 131072 -datasetInit 1610612736 -datasetGrowth 8388608
1+
//go:generate ../.bin/gen-lookup -package firopow -mixBytes 128 -cacheInit 16777216 -cacheGrowth 131072 -datasetInit 1610612736 -datasetGrowth 8388608
22

33
package firopow
44

@@ -35,6 +35,7 @@ func NewFiro() *Client {
3535
CacheSizes: dag.NewLookupTable(cacheSizes, 2048),
3636
DatasetSizes: dag.NewLookupTable(datasetSizes, 2048),
3737

38+
MixBytes: 128,
3839
DatasetParents: 512,
3940
EpochLength: 1300,
4041
SeedEpochLength: 1300,

internal/crypto/fnv.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
package crypto
77

88
const (
9-
fnvPrime uint32 = 0x01000193
9+
FnvPrime = 0x01000193
1010
)
1111

1212
// See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1_hash.
1313
func Fnv1(u, v uint32) uint32 {
14-
return (u * fnvPrime) ^ v
14+
return (u * FnvPrime) ^ v
15+
}
16+
17+
func Fnv1Uint64(u, v uint64) uint64 {
18+
return (u * FnvPrime) ^ v
1519
}
1620

1721
// See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash.
1822
func Fnv1a(u, v uint32) uint32 {
19-
return (u ^ v) * fnvPrime
23+
return (u ^ v) * FnvPrime
2024
}
2125

2226
// fnvHash mixes in data into mix using the ethash fnv method.

internal/dag/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
package dag
1818

1919
const (
20-
mixBytes = 128 // Width of mix
21-
hashBytes = 64 // Hash length in bytes
22-
hashWords = 16 // Number of 32 bit ints in a hash
20+
hashBytes = 64 // Hash length in bytes
21+
hashWords = 16 // Number of 32 bit ints in a hash
2322
)
2423

2524
type LookupTable struct {
@@ -52,6 +51,7 @@ type Config struct {
5251
CacheSizes *LookupTable
5352

5453
// algorithm variables
54+
MixBytes uint64
5555
DatasetParents uint32
5656
EpochLength uint64
5757
SeedEpochLength uint64 // ETC uses a different seed epoch length

internal/dag/dag.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ func (d *DAG) calcCacheSize(epoch uint64) uint64 {
108108
}
109109

110110
func (d *DAG) calcDatasetSize(epoch uint64) uint64 {
111-
size := d.DatasetInitBytes + d.DatasetGrowthBytes*epoch - mixBytes
111+
size := d.DatasetInitBytes + d.DatasetGrowthBytes*epoch - d.MixBytes
112112

113113
// Always accurate for n < 2^64
114-
for !new(big.Int).SetUint64(size / mixBytes).ProbablyPrime(1) {
115-
size -= 2 * mixBytes
114+
for !new(big.Int).SetUint64(size / d.MixBytes).ProbablyPrime(1) {
115+
size -= 2 * d.MixBytes
116116
}
117117

118118
return size

internal/dag/dag_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ func TestCalcDatasetSize(t *testing.T) {
305305
DatasetSizes: nil,
306306
CacheSizes: nil,
307307

308+
MixBytes: 128,
308309
DatasetParents: 256,
309310
EpochLength: 30000,
310311
SeedEpochLength: 30000,

internal/progpow/progpow094_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func TestProgpow094(t *testing.T) {
9393
DatasetSizes: nil,
9494
CacheSizes: nil,
9595

96+
MixBytes: 128,
9697
DatasetParents: 512,
9798
EpochLength: 30000,
9899
SeedEpochLength: 30000,

kawpow/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
//go:generate ../.bin/gen-lookup -package kawpow -cacheInit 16777216 -cacheGrowth 131072 -datasetInit 1073741824 -datasetGrowth 8388608
17+
//go:generate ../.bin/gen-lookup -package kawpow -mixBytes 128 -cacheInit 16777216 -cacheGrowth 131072 -datasetInit 1073741824 -datasetGrowth 8388608
1818

1919
package kawpow
2020

@@ -51,6 +51,7 @@ func NewRavencoin() *Client {
5151
CacheSizes: dag.NewLookupTable(cacheSizes, 2048),
5252
DatasetSizes: dag.NewLookupTable(datasetSizes, 2048),
5353

54+
MixBytes: 128,
5455
DatasetParents: 512,
5556
EpochLength: 7500,
5657
SeedEpochLength: 7500,

octopus/client.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//go:generate ../.bin/gen-lookup -package octopus -mixBytes 256 -cacheInit 16777216 -cacheGrowth 65536 -datasetInit 4294967296 -datasetGrowth 16777216
2+
3+
package octopus
4+
5+
import (
6+
"runtime"
7+
8+
"github.com/sencha-dev/powkit/internal/common"
9+
"github.com/sencha-dev/powkit/internal/dag"
10+
)
11+
12+
type Client struct {
13+
*dag.DAG
14+
}
15+
16+
func New(cfg dag.Config) *Client {
17+
client := &Client{
18+
DAG: dag.New(cfg),
19+
}
20+
21+
return client
22+
}
23+
24+
func NewConflux() *Client {
25+
var cfg = dag.Config{
26+
Name: "CFX",
27+
Revision: 1,
28+
StorageDir: common.DefaultDir(".powcache"),
29+
30+
DatasetInitBytes: 2 * (1 << 31),
31+
DatasetGrowthBytes: 1 << 24,
32+
CacheInitBytes: 2 * (1 << 23),
33+
CacheGrowthBytes: 1 << 16,
34+
35+
CacheSizes: dag.NewLookupTable(cacheSizes, 2048),
36+
DatasetSizes: dag.NewLookupTable(datasetSizes, 2048),
37+
38+
MixBytes: 256,
39+
DatasetParents: 256,
40+
EpochLength: 1 << 19,
41+
SeedEpochLength: 1 << 19,
42+
43+
CacheRounds: 3,
44+
CachesCount: 3,
45+
CachesLockMmap: false,
46+
47+
L1Enabled: false,
48+
}
49+
50+
return New(cfg)
51+
}
52+
53+
func (c *Client) Compute(height, nonce uint64, hash []byte) []byte {
54+
epoch := c.CalcEpoch(height)
55+
size := c.DatasetSize(epoch)
56+
cache := c.GetCache(epoch)
57+
lookup := c.NewLookupFunc512(cache, epoch)
58+
59+
digest := octopus(hash, nonce, size, lookup)
60+
runtime.KeepAlive(cache)
61+
62+
return digest
63+
}

0 commit comments

Comments
 (0)