-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswu_test.go
85 lines (66 loc) · 1.43 KB
/
swu_test.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
79
80
81
82
83
84
85
package swu
import (
"crypto/elliptic"
"crypto/rand"
"math/big"
"testing"
"github.com/minio/sha256-simd"
"github.com/stretchr/testify/assert"
)
var (
c = elliptic.P256()
buf = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
t = []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
)
func init() {
}
func TestSWU(t *testing.T) {
for i := 0; i < 10000; i++ {
b := make([]byte, 32)
rand.Read(b)
x, y := HashToPoint(b)
assert.True(t, elliptic.P256().IsOnCurve(x, y))
}
}
func BenchmarkSWU(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
HashToPoint(buf)
}
}
func BenchmarkTryInc(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
HashIntoCurvePoint(buf)
}
}
func HashIntoCurvePoint(r []byte) (x, y *big.Int) {
copy(t, r)
x, y = tryPoint(t)
for y == nil || !c.IsOnCurve(x, y) {
increment(t)
x, y = tryPoint(t)
}
return
}
func tryPoint(r []byte) (x, y *big.Int) {
hash := sha256.Sum256(r)
x = new(big.Int).SetBytes(hash[:])
// y² = x³ - 3x + b
x3 := new(big.Int).Mul(x, x)
x3.Mul(x3, x)
threeX := new(big.Int).Lsh(x, 1)
threeX.Add(threeX, x)
x3.Sub(x3, threeX)
x3.Add(x3, c.Params().B)
y = x3.ModSqrt(x3, c.Params().P)
return
}
func increment(counter []byte) {
for i := len(counter) - 1; i >= 0; i-- {
counter[i]++
if counter[i] != 0 {
break
}
}
}