arnaucube
arnaucube/cryptofunPublic Self-hosted repository
cryptofun/dh/dh_test.go
dh/dh_test.goRaw
 1package dh
 2
 3import (
 4	"crypto/rand"
 5	"math/big"
 6	"testing"
 7)
 8
 9const (
10	bits = 2048
11)
12
13func TestDiffieHellman(t *testing.T) {
14	p, err := rand.Prime(rand.Reader, bits/2)
15	if err != nil {
16		t.Errorf(err.Error())
17	}
18	g, err := rand.Prime(rand.Reader, bits/2)
19	if err != nil {
20		t.Errorf(err.Error())
21	}
22
23	max, err := rand.Prime(rand.Reader, bits/2)
24	if err != nil {
25		t.Errorf(err.Error())
26	}
27	a, err := rand.Int(rand.Reader, max)
28	if err != nil {
29		t.Errorf(err.Error())
30	}
31	b, err := rand.Int(rand.Reader, max)
32	if err != nil {
33		t.Errorf(err.Error())
34	}
35
36	A := new(big.Int).Exp(g, a, p)
37	B := new(big.Int).Exp(g, b, p)
38
39	sA := new(big.Int).Exp(B, a, p)
40	sB := new(big.Int).Exp(A, b, p)
41
42	if sA.Int64() != sB.Int64() {
43		t.Errorf("secret not equal")
44	}
45}