Repository navigation
Expand file tree
/
Copy pathsearch_test.go
More file actions
126 lines (98 loc) · 3.28 KB
/
Copy pathsearch_test.go
File metadata and controls
126 lines (98 loc) · 3.28 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/sha512"
"fmt"
"math/big"
"strings"
"testing"
"github.com/offset/onion-vanity-address/internal/edwards25519"
"github.com/offset/onion-vanity-address/internal/assert"
"github.com/offset/onion-vanity-address/internal/require"
)
func TestSearch(t *testing.T) {
t.Run("onionjifniegtjbbifet65goa2siqubne6n2qfhiksryfvsbdhda", func(t *testing.T) {
const sk = "7bd6z6w72afftbr7aybfbgstm7exdnndgm74cocbrnfkjegnifca"
const pk = "onionjifniegtjbbifet65goa2siqubne6n2qfhiksryfvsbdhda"
skb, err := onionBase32Encoding.DecodeString(sk)
require.NoError(t, err)
pkb, err := onionBase32Encoding.DecodeString(pk)
require.NoError(t, err)
p0, err := new(edwards25519.Point).SetBytes(pkb)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
accept, err := hasPrefix("ayay", onionBase32Encoding)
require.NoError(t, err)
var found *big.Int
yield := func(publicKey []byte, offset *big.Int) {
found = offset
cancel()
}
search(ctx, p0.Bytes(), big.NewInt(0), 2, accept, yield)
po := new(edwards25519.Point).ScalarMult(scalarFromBigInt(found), _B8)
p := new(edwards25519.Point).Add(p0, po)
vpk := onionBase32Encoding.EncodeToString(p.Bytes())
t.Logf("vanity public key: %s at offest: %d", vpk, found)
assert.True(t, strings.HasPrefix(vpk, "ayay"))
vsk, err := add(skb, found)
assert.NoError(t, err)
vpk2, err := publicKeyFor(vsk)
require.NoError(t, err)
assert.Equal(t, vpk, onionBase32Encoding.EncodeToString(vpk2))
})
acceptAll := func(_ []byte) bool { return true }
t.Run("random", func(t *testing.T) {
for range 100 {
t.Run("", func(t *testing.T) {
pk, sk, err := ed25519.GenerateKey(rand.Reader)
require.NoError(t, err)
hs := sha512.New()
hs.Write(sk.Seed())
skb := hs.Sum(nil)[:32]
startOffset, _ := rand.Int(rand.Reader, new(big.Int).SetUint64(1<<64-1))
t.Logf("Start offset: %16x", startOffset)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
i := 0
search(ctx, pk, startOffset, 8, acceptAll, func(xb []byte, offset *big.Int) {
kb, err := add(skb, offset)
require.NoError(t, err)
pkb, err := publicKeyFor(kb)
assert.NoError(t, err)
// Check ignoring sign bit
pkb[31] &= 0x7f
assert.Equal(t, pkb, xb)
if i++; i == 100 {
cancel()
}
})
})
}
})
}
func BenchmarkSearchParallel(b *testing.B) {
startPublicKey, err := onionBase32Encoding.DecodeString("onionjifniegtjbbifet65goa2siqubne6n2qfhiksryfvsbdhda")
require.NoError(b, err)
testPrefix, err := hasPrefix("goodluckwiththisprefix", onionBase32Encoding)
require.NoError(b, err)
for _, batchSize := range []int{1024, 2048, 4096, 8192} {
b.Run(fmt.Sprintf("%d", batchSize), func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
startOffset, _ := rand.Int(rand.Reader, new(big.Int).SetUint64(1<<64-1))
search(ctx, startPublicKey, startOffset, batchSize, func(candidatePublicKey []byte) bool {
_ = testPrefix(candidatePublicKey)
if !pb.Next() {
cancel()
}
return false
}, nil)
})
b.ReportMetric(float64(b.N)/b.Elapsed().Seconds(), "keys/s")
})
}
}