added Diffie-Hellman. Started impl ECC

This commit is contained in:
arnaucode
2018-07-28 11:43:34 +02:00
parent 9aa4a2d1a0
commit 57d3547cd8
7 changed files with 234 additions and 3 deletions

1
dh/dh.go Normal file
View File

@@ -0,0 +1 @@
package dh

45
dh/dh_test.go Normal file
View File

@@ -0,0 +1,45 @@
package dh
import (
"crypto/rand"
"math/big"
"testing"
)
const (
bits = 2048
)
func TestDiffieHellman(t *testing.T) {
p, err := rand.Prime(rand.Reader, bits/2)
if err != nil {
t.Errorf(err.Error())
}
g, err := rand.Prime(rand.Reader, bits/2)
if err != nil {
t.Errorf(err.Error())
}
max, err := rand.Prime(rand.Reader, bits/2)
if err != nil {
t.Errorf(err.Error())
}
a, err := rand.Int(rand.Reader, max)
if err != nil {
t.Errorf(err.Error())
}
b, err := rand.Int(rand.Reader, max)
if err != nil {
t.Errorf(err.Error())
}
A := new(big.Int).Exp(g, a, p)
B := new(big.Int).Exp(g, b, p)
sA := new(big.Int).Exp(B, a, p)
sB := new(big.Int).Exp(A, b, p)
if sA.Int64() != sB.Int64() {
t.Errorf("secret not equal")
}
}