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	"github.com/stretchr/testify/assert"
 9)
10
11const (
12	bits = 2048
13)
14
15func TestDiffieHellman(t *testing.T) {
16	p, err := rand.Prime(rand.Reader, bits/2)
17	assert.Nil(t, err)
18
19	g, err := rand.Prime(rand.Reader, bits/2)
20	assert.Nil(t, err)
21
22	max, err := rand.Prime(rand.Reader, bits/2)
23	assert.Nil(t, err)
24
25	a, err := rand.Int(rand.Reader, max)
26	assert.Nil(t, err)
27
28	b, err := rand.Int(rand.Reader, max)
29	assert.Nil(t, err)
30
31	A := new(big.Int).Exp(g, a, p)
32	B := new(big.Int).Exp(g, b, p)
33
34	sA := new(big.Int).Exp(B, a, p)
35	sB := new(big.Int).Exp(A, b, p)
36
37	if sA.Int64() != sB.Int64() {
38		t.Errorf("secret not equal")
39	}
40}