-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
87 lines (66 loc) · 2.24 KB
/
Copy pathexample_test.go
File metadata and controls
87 lines (66 loc) · 2.24 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
package vanity25519_test
import (
"context"
"crypto/ecdh"
"crypto/rand"
"encoding/base64"
"fmt"
"math/big"
"os"
"runtime"
"sync"
"sync/atomic"
"github.com/AlexanderYastrebov/vanity25519"
)
func ExampleSearch() {
startKey, _ := ecdh.X25519().GenerateKey(rand.Reader)
startPublicKey := startKey.PublicKey().Bytes()
prefix, _ := base64.StdEncoding.DecodeString("AY/" + "x") // pad to 4 characters to decode properly
testPrefix := vanity25519.HasPrefixBits(prefix, 3*6) // search for 3-character prefix, i.e. 18 bits
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var found *big.Int
attempts := vanity25519.Search(ctx, startPublicKey, big.NewInt(0), 4096, testPrefix, func(_ []byte, offset *big.Int) {
found = offset
cancel()
})
vkb, _ := vanity25519.Add(startKey.Bytes(), found)
vk, _ := ecdh.X25519().NewPrivateKey(vkb)
vpk := base64.StdEncoding.EncodeToString(vk.PublicKey().Bytes())
fmt.Fprintf(os.Stderr, "Found %s after %d attempts\n", vpk, attempts)
fmt.Printf("Found key: %s...\n", vpk[:3])
// Output:
// Found key: AY/...
}
func ExampleSearch_parallel() {
startKey, _ := ecdh.X25519().GenerateKey(rand.Reader)
startPublicKey := startKey.PublicKey().Bytes()
prefix, _ := base64.StdEncoding.DecodeString("AY/" + "x") // pad to 4 characters to decode properly
testPrefix := vanity25519.HasPrefixBits(prefix, 3*6) // search for 3-character prefix, i.e. 18 bits
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var attempts atomic.Uint64
var found atomic.Pointer[big.Int]
var wg sync.WaitGroup
for range runtime.NumCPU() {
wg.Add(1)
go func() {
defer wg.Done()
skip, _ := rand.Int(rand.Reader, new(big.Int).SetUint64(1<<64-1))
n := vanity25519.Search(ctx, startPublicKey, skip, 4096, testPrefix, func(_ []byte, offset *big.Int) {
if found.CompareAndSwap(nil, offset) {
cancel()
}
})
attempts.Add(n)
}()
}
wg.Wait()
vkb, _ := vanity25519.Add(startKey.Bytes(), found.Load())
vk, _ := ecdh.X25519().NewPrivateKey(vkb)
vpk := base64.StdEncoding.EncodeToString(vk.PublicKey().Bytes())
fmt.Fprintf(os.Stderr, "Found %s after %d attempts\n", vpk, attempts.Load())
fmt.Printf("Found key: %s...\n", vpk[:3])
// Output:
// Found key: AY/...
}