Commit 2c177f2a authored by Viktor Trón's avatar Viktor Trón Committed by GitHub

fix(pss): improve trojan mining (#2287)

parent b0e9ca5b
...@@ -30,11 +30,11 @@ func BenchmarkWrap(b *testing.B) { ...@@ -30,11 +30,11 @@ func BenchmarkWrap(b *testing.B) {
depth int depth int
}{ }{
{1, 1}, {1, 1},
{4, 1}, {256, 2},
{16, 1}, {8, 1},
{256, 1},
{16, 2}, {16, 2},
{64, 2}, {64, 2},
{256, 2},
{256, 3}, {256, 3},
{4096, 3}, {4096, 3},
{16384, 3}, {16384, 3},
......
...@@ -201,8 +201,7 @@ func contains(col Targets, elem []byte) bool { ...@@ -201,8 +201,7 @@ func contains(col Targets, elem []byte) bool {
} }
// mine iteratively enumerates different nonces until the address (BMT hash) of the chunkhas one of the targets as its prefix // mine iteratively enumerates different nonces until the address (BMT hash) of the chunkhas one of the targets as its prefix
func mine(ctx context.Context, odd bool, f func(nonce []byte) (swarm.Chunk, error)) (r swarm.Chunk, e error) { func mine(ctx context.Context, odd bool, f func(nonce []byte) (swarm.Chunk, error)) (swarm.Chunk, error) {
seeds := make([]uint32, 8)
initnonce := make([]byte, 32) initnonce := make([]byte, 32)
if _, err := io.ReadFull(random.Reader, initnonce); err != nil { if _, err := io.ReadFull(random.Reader, initnonce); err != nil {
return nil, err return nil, err
...@@ -212,26 +211,23 @@ func mine(ctx context.Context, odd bool, f func(nonce []byte) (swarm.Chunk, erro ...@@ -212,26 +211,23 @@ func mine(ctx context.Context, odd bool, f func(nonce []byte) (swarm.Chunk, erro
} else { } else {
initnonce[28] &= 0xfe initnonce[28] &= 0xfe
} }
for i := range seeds {
seeds[i] = binary.BigEndian.Uint32(initnonce[i*4 : i*4+4])
}
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()
eg, ctx := errgroup.WithContext(ctx) eg, ctx := errgroup.WithContext(ctx)
result := make(chan swarm.Chunk, 8) result := make(chan swarm.Chunk, 8)
for i := 0; i < 8; i++ { for i := 0; i < 8; i++ {
j := i
eg.Go(func() error { eg.Go(func() error {
nonce := make([]byte, 32) nonce := make([]byte, 32)
copy(nonce, initnonce) copy(nonce, initnonce)
for seed := seeds[j]; ; seed += 2 { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return ctx.Err()
default: default:
} }
binary.BigEndian.PutUint32(nonce[j*4:j*4+4], seed) if _, err := io.ReadFull(random.Reader, nonce[:4]); err != nil {
return err
}
res, err := f(nonce) res, err := f(nonce)
if err != nil { if err != nil {
return err return err
...@@ -243,15 +239,16 @@ func mine(ctx context.Context, odd bool, f func(nonce []byte) (swarm.Chunk, erro ...@@ -243,15 +239,16 @@ func mine(ctx context.Context, odd bool, f func(nonce []byte) (swarm.Chunk, erro
} }
}) })
} }
errc := make(chan error) var err error
go func() { go func() {
errc <- eg.Wait() err = eg.Wait()
result <- nil
}() }()
select { r := <-result
case r = <-result: if r == nil {
case e = <-errc: return nil, err
} }
return r, e return r, nil
} }
// extracts ephemeral public key from the chunk data to use with el-Gamal // extracts ephemeral public key from the chunk data to use with el-Gamal
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment