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

Commit d9442ee

Browse files
committed
allow endianness choice in convutil
1 parent 77aba32 commit d9442ee

File tree

7 files changed

+21
-26
lines changed

7 files changed

+21
-26
lines changed

autolykos2/autolykos2.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ func (cfg *Config) genIndexes(seed []byte, n uint32) []uint32 {
8484

8585
func (cfg *Config) Compute(msg []byte, nonce, height uint64) []byte {
8686
m := generateM(1024)
87-
h := convutil.Uint32ToBytesBE(uint32(height))
88-
nonceBytes := convutil.Uint64ToBytesBE(nonce)
87+
h := convutil.Uint32ToBytes(uint32(height), binary.BigEndian)
88+
nonceBytes := convutil.Uint64ToBytes(nonce, binary.BigEndian)
8989

9090
n := cfg.calcN(height)
9191
bigN := new(big.Int).SetUint64(uint64(n))
@@ -94,15 +94,15 @@ func (cfg *Config) Compute(msg []byte, nonce, height uint64) []byte {
9494
msgHash := crypto.Blake2b256(fullMsg)
9595
prei8 := new(big.Int).SetBytes(msgHash[24:32])
9696

97-
i := convutil.Uint32ToBytesBE(uint32(prei8.Mod(prei8, bigN).Uint64()))
97+
i := convutil.Uint32ToBytes(uint32(prei8.Mod(prei8, bigN).Uint64()), binary.BigEndian)
9898
f := crypto.Blake2b256(concatBytes(i, concatBytes(h, m)))[1:32]
9999

100100
seed := concatBytes(f, concatBytes(msg, nonceBytes))
101101
indexes := cfg.genIndexes(seed, n)
102102

103103
f2 := new(big.Int)
104104
for _, index := range indexes {
105-
elem := concatBytes(convutil.Uint32ToBytesBE(index), concatBytes(h, m))
105+
elem := concatBytes(convutil.Uint32ToBytes(index, binary.BigEndian), concatBytes(h, m))
106106
elemHash := crypto.Blake2b256(elem)[1:]
107107
f2.Add(f2, new(big.Int).SetBytes(elemHash))
108108
}

firopow/firopow.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ func finalize(seed [25]uint32, mixHash []byte) []byte {
4242

4343
crypto.KeccakF800(&state)
4444

45-
digest := convutil.Uint32ArrayToBytesLE(state[:8])
46-
47-
return digest
45+
return convutil.Uint32ArrayToBytes(state[:8], binary.LittleEndian)
4846
}
4947

5048
func firopow(hash []byte, height, nonce, datasetSize uint64, lookup func(index uint32) []uint32, l1 []uint32) ([]byte, []byte) {

internal/common/convutil/convutil.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,35 @@ import (
44
"encoding/binary"
55
)
66

7-
func Uint32ArrayToBytesLE(arr []uint32) []byte {
7+
func Uint32ArrayToBytes(arr []uint32, byteOrder binary.ByteOrder) []byte {
88
buf := make([]byte, len(arr)*4)
9-
109
for i, v := range arr {
11-
binary.LittleEndian.PutUint32(buf[i*4:], v)
10+
byteOrder.PutUint32(buf[i*4:], v)
1211
}
1312

1413
return buf
1514
}
1615

17-
func BytesToUint32ArrayLE(buf []byte) []uint32 {
16+
func BytesToUint32Array(buf []byte, byteOrder binary.ByteOrder) []uint32 {
1817
length := len(buf) / 4
1918
arr := make([]uint32, length)
20-
2119
for i := 0; i < length; i++ {
22-
arr[i] = binary.LittleEndian.Uint32(buf[i*4 : (i+1)*4])
20+
arr[i] = byteOrder.Uint32(buf[i*4 : (i+1)*4])
2321
}
2422

2523
return arr
2624
}
2725

28-
func Uint32ToBytesBE(val uint32) []byte {
26+
func Uint32ToBytes(val uint32, byteOrder binary.ByteOrder) []byte {
2927
data := make([]byte, 4)
30-
binary.BigEndian.PutUint32(data, val)
28+
byteOrder.PutUint32(data, val)
3129

3230
return data
3331
}
3432

35-
func Uint64ToBytesBE(val uint64) []byte {
33+
func Uint64ToBytes(val uint64, byteOrder binary.ByteOrder) []byte {
3634
data := make([]byte, 8)
37-
binary.BigEndian.PutUint64(data, val)
35+
byteOrder.PutUint64(data, val)
3836

3937
return data
4038
}

internal/dag/generate_test.go

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

1919
import (
20+
"encoding/binary"
2021
"reflect"
2122
"testing"
2223

@@ -107,7 +108,7 @@ func TestCacheGeneration(t *testing.T) {
107108
seed := d.SeedHash(uint64(tt.epoch)*d.EpochLength + 1)
108109
d.generateCache(cache, tt.epoch, seed)
109110

110-
want := convutil.BytesToUint32ArrayLE(tt.cache)
111+
want := convutil.BytesToUint32Array(tt.cache, binary.LittleEndian)
111112
if !reflect.DeepEqual(cache, want) {
112113
t.Errorf("failed on %d: cache mismatch: have %x, want %x", i, cache, want)
113114
}
@@ -160,7 +161,7 @@ func TestCacheGenerationHashed(t *testing.T) {
160161
seed := d.SeedHash(uint64(tt.epoch)*d.EpochLength + 1)
161162
d.generateCache(cache, tt.epoch, seed)
162163

163-
raw := convutil.Uint32ArrayToBytesLE(cache)
164+
raw := convutil.Uint32ArrayToBytes(cache, binary.LittleEndian)
164165
hash := crypto.Keccak256(raw)
165166

166167
if !reflect.DeepEqual(hash, tt.hash) {

internal/progpow/algorithm.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package progpow
66

77
import (
8+
"encoding/binary"
9+
810
"github.com/sencha-dev/powkit/internal/common/convutil"
911
"github.com/sencha-dev/powkit/internal/crypto"
1012
"github.com/sencha-dev/powkit/internal/dag"
@@ -136,5 +138,5 @@ func Hash(cfg *Config, height, seed, datasetSize uint64, lookup dag.LookupFunc,
136138
mixHash[l%numWords] = crypto.Fnv1a(mixHash[l%numWords], laneHash[l])
137139
}
138140

139-
return convutil.Uint32ArrayToBytesLE(mixHash)
141+
return convutil.Uint32ArrayToBytes(mixHash, binary.LittleEndian)
140142
}

internal/progpow/progpow094.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ func finalize(seed [25]uint32, mixHash []byte) []byte {
4747

4848
crypto.KeccakF800(&state)
4949

50-
digest := convutil.Uint32ArrayToBytesLE(state[:8])
51-
52-
return digest
50+
return convutil.Uint32ArrayToBytes(state[:8], binary.LittleEndian)
5351
}
5452

5553
func compute(hash []byte, height, nonce, datasetSize uint64, lookup func(index uint32) []uint32, l1 []uint32) ([]byte, []byte) {

kawpow/kawpow.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ func finalize(seed [25]uint32, mixHash []byte) []byte {
6363

6464
crypto.KeccakF800(&state)
6565

66-
digest := convutil.Uint32ArrayToBytesLE(state[:8])
67-
68-
return digest
66+
return convutil.Uint32ArrayToBytes(state[:8], binary.LittleEndian)
6967
}
7068

7169
func kawpow(hash []byte, height, nonce, datasetSize uint64, lookup func(index uint32) []uint32, l1 []uint32) ([]byte, []byte) {

0 commit comments

Comments
 (0)