-
Notifications
You must be signed in to change notification settings - Fork 5
/
opts.go
78 lines (66 loc) · 1.83 KB
/
opts.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
package wotsp
import (
"crypto"
"fmt"
"runtime"
)
var (
canPrecompute = map[crypto.Hash]bool{
crypto.SHA256: true,
crypto.SHA512_256: true,
crypto.BLAKE2b_256: true,
crypto.BLAKE2s_256: true,
}
)
// Opts groups the parameters required for W-OTS+ operations. It implements
// crypto.SignerOpts.
type Opts struct {
Mode Mode
Address [32]byte
// Concurrency specifies the amount of goroutines to use for WOTS
// operations. Concurrency follows the following logic for n:
// n > 0: divide chains over n goroutines.
// n == 0: default, use a single goroutine
// n < 0: automatically determine the number of goroutines based on
// runtime.NumCPU or runtime.GOMAXPROX(-1), whichever is lower.
Concurrency int
// Hash specifies the specific hash function to use. For a hash function to
// be accepted by the implementation, it needs to have a digest of 256 bits.
//
// Currently, the following values are supported:
// crypto.SHA256
// crypto.SHA512_256
// crypto.BLAKE2b_256
// crypto.BLAKE2s_256
//
// The default (for crypto.Hash(0)) is SHA256, as per the RFC.
crypto.Hash
// NOTE by embedding Hash we automatically implement crypto.SignerOpts, if
// this were ever to become relevant.
}
// hash returns the hash function to use for the run of W-OTS+.
func (o Opts) hash() crypto.Hash {
if o.Hash == crypto.Hash(0) {
return crypto.SHA256
}
if canPrecompute[o.Hash] {
return o.Hash
}
panic(fmt.Sprintf("unsupported value for Opts.Hash [%d]", o.Hash))
}
// routines returns the amount of simultaneous goroutines to use for W-OTS+
// operations, based on Opts.Concurrency.
func (o Opts) routines() int {
if o.Concurrency == 0 {
return 1
}
if o.Concurrency > 0 {
return o.Concurrency
}
procs := runtime.GOMAXPROCS(-1)
cpus := runtime.NumCPU()
if procs > cpus {
return cpus
}
return procs
}