-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlp_test.go
88 lines (77 loc) · 2.72 KB
/
dlp_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
86
87
88
package ecc
import (
"math/big"
"testing"
)
func TestECDLP(t *testing.T) {
if !testing.Short() {
return
}
t.Run("ecdlp", func(t *testing.T) {
t.Parallel()
curve := &Curve{
P: big.NewInt(7919),
A: big.NewInt(1001),
B: big.NewInt(75),
Gx: big.NewInt(4023),
Gy: big.NewInt(6036),
N: big.NewInt(7889),
}
curve.BitSize = curve.N.BitLen()
for m := big.NewInt(1); m.Cmp(curve.N) < 0; m.Add(m, big.NewInt(1)) {
px, py := curve.Gx, curve.Gy
hx, hy := curve.ScalarBaseMult(m)
k := curve.Shank(px, py, hx, hy)
if k == nil || k.Cmp(m) != 0 {
t.Errorf("[Shank] (%d,%d) want: %d, got: %d", hx, hy, m, k)
}
k = curve.PollardRho(px, py, hx, hy)
if k == nil || k.Cmp(m) != 0 {
t.Errorf("[PollardRho] (%d,%d) want: %d, got: %d", hx, hy, m, k)
}
k = curve.PohligHellman(px, py, hx, hy)
if k == nil || k.Cmp(m) != 0 {
t.Errorf("[PohligHellman] (%d,%d) want: %d, got: %d", hx, hy, m, k)
}
}
})
t.Run("PohligHellman-1", func(t *testing.T) {
t.Parallel()
// https://gist.github.com/jproney/7e6cb7a40a8bf342e978a900a32e4dfc
curve := &Curve{
H: big.NewInt(1),
P: BigFromDecimal("93556643250795678718734474880013829509320385402690660619699653921022012489089"),
A: BigFromDecimal("66001598144012865876674115570268990806314506711104521036747533612798434904785"),
B: BigFromDecimal("25255205054024371783896605039267101837972419055969636393425590261926131199030"),
N: BigFromDecimal("93556643250795678718734474880013829509196181230338248789325711173791286325820"),
}
curve.BitSize = curve.N.BitLen()
want := BigFromDecimal("124194987912445918487544544020")
px := BigFromDecimal("56027910981442853390816693056740903416379421186644480759538594137486160388926")
py := BigFromDecimal("65533262933617146434438829354623658858649726233622196512439589744498050226926")
hx := BigFromDecimal("79745356646949069441279781387743208137742538544495675881933883371885177103895")
hy := BigFromDecimal("34529309219406689418881493671300037164559702076524725195399995669560101677178")
k := curve.PohligHellman(px, py, hx, hy)
if k == nil || k.Cmp(want) != 0 {
t.Errorf("[PohligHellman-1] (%d,%d) want: %d, got: %d", hx, hy, want, k)
}
})
t.Run("PohligHellman-2", func(t *testing.T) {
t.Parallel()
curve := &Curve{
P: BigFromDecimal("4516284508517"),
A: big.NewInt(7),
B: big.NewInt(1),
N: BigFromDecimal("4516285972627"),
}
curve.BitSize = curve.N.BitLen()
want := big.NewInt(21345332)
px := BigFromDecimal("816487529800")
py := BigFromDecimal("1845320358420")
hx, hy := curve.ScalarMult(px, py, want)
k := curve.PohligHellman(px, py, hx, hy)
if k == nil || k.Cmp(want) != 0 {
t.Errorf("[PohligHellman-2] (%d,%d) want: %d, got: %d", hx, hy, want, k)
}
})
}