Skip to content

Commit 27ee338

Browse files
committed
Match public key with prefix with only bytes that matter
1 parent 147774c commit 27ee338

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

main.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ const b32Lower = "abcdefghijklmnopqrstuvwxyz234567"
2020

2121
var b32Enc = base32.NewEncoding(b32Lower).WithPadding(base32.NoPadding)
2222

23-
func generate(wg *sync.WaitGroup, prefix string) {
23+
func generate(wg *sync.WaitGroup, prefix string, prefixDecodedLen int) {
2424
for {
2525
publicKey, secretKey, err := ed25519.GenerateKey(nil)
2626
checkErr(err)
2727

28-
publicKeyB32 := b32Enc.EncodeToString(publicKey)
28+
// Match the public key with prefix.
29+
// No need to encode all PK to B32 if prefix shorter
30+
publicKeyB32 := b32Enc.EncodeToString(publicKey[0:prefixDecodedLen])
2931
// If a matching address is found, save key and notify wait group
3032
if strings.HasPrefix(publicKeyB32, prefix) {
3133
onionAddress := encodePublicKey(publicKey)
@@ -92,6 +94,19 @@ func main() {
9294
os.Exit(1)
9395
}
9496

97+
prefixLen := len(prefix)
98+
prefixDecodedLen := b32Enc.DecodedLen(prefixLen)
99+
// Same DecodedLen(prefix) but without rounding to floor
100+
prefixDecodedLenRatio := float64(prefixLen) * 5.0 / 8.0
101+
// if there is some division remainder
102+
if prefixDecodedLenRatio > float64(prefixDecodedLen) {
103+
// 8 bytes encoded into 5 chars. We must add them all because don't know which byte changes a char
104+
prefixDecodedLen += 8
105+
}
106+
if prefixDecodedLen > 32 {
107+
prefixDecodedLen = 32
108+
}
109+
95110
// Get the number of desired addresses from second argument.
96111
numAddresses, _ := strconv.Atoi(os.Args[2])
97112

@@ -101,7 +116,7 @@ func main() {
101116

102117
// For each CPU, run a generate goroutine
103118
for i := 0; i < runtime.NumCPU(); i++ {
104-
go generate(&wg, prefix)
119+
go generate(&wg, prefix, prefixDecodedLen)
105120
}
106121

107122
// Exit after the desired number of addresses have been found.

0 commit comments

Comments
 (0)