From 57d3547cd8296ad881086ca47d3433a303536edf Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sat, 28 Jul 2018 11:43:34 +0200 Subject: [PATCH] added Diffie-Hellman. Started impl ECC --- README.md | 12 +++++-- dh/dh.go | 1 + dh/dh_test.go | 45 ++++++++++++++++++++++++ ecc/coord.go | 23 +++++++++++++ ecc/ecc.go | 83 +++++++++++++++++++++++++++++++++++++++++++++ ecc/ecc_test.go | 71 ++++++++++++++++++++++++++++++++++++++ secrets/sercrets.go | 2 +- 7 files changed, 234 insertions(+), 3 deletions(-) create mode 100644 dh/dh.go create mode 100644 dh/dh_test.go create mode 100644 ecc/coord.go create mode 100644 ecc/ecc.go create mode 100644 ecc/ecc_test.go diff --git a/README.md b/README.md index cba0cf2..fdbb26c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# cryptofun +# cryptofun [![Go Report Card](https://goreportcard.com/badge/github.com/arnaucode/cryptofun)](https://goreportcard.com/report/github.com/arnaucode/cryptofun) Crypto algorithms from scratch. Academic purposes only. @@ -23,7 +23,15 @@ https://en.wikipedia.org/wiki/Paillier_cryptosystem ## Shamir Secret Sharing https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing -- [x] create secret sharing from NumOfSecretsNeed, NumOfShares, RandPointP, SecretToShare +- [x] create secret sharing from number of secrets needed, number of shares, random point p, secret to share - [x] Lagrange Interpolation to restore the secret from the shares +## Diffie-Hellman +https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange +- [x] key exchange + ## ECC +https://en.wikipedia.org/wiki/Elliptic-curve_cryptography +- [x] define elliptic curve +- [x] get point at X +- [x] Add two points on the elliptic curve diff --git a/dh/dh.go b/dh/dh.go new file mode 100644 index 0000000..6867766 --- /dev/null +++ b/dh/dh.go @@ -0,0 +1 @@ +package dh diff --git a/dh/dh_test.go b/dh/dh_test.go new file mode 100644 index 0000000..7aa0178 --- /dev/null +++ b/dh/dh_test.go @@ -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") + } +} diff --git a/ecc/coord.go b/ecc/coord.go new file mode 100644 index 0000000..4ac06a0 --- /dev/null +++ b/ecc/coord.go @@ -0,0 +1,23 @@ +package ecc + +import "math/big" + +var ( + bigZero = big.NewInt(int64(0)) + zeroPoint = Point{bigZero, bigZero} +) + +type Point struct { + X *big.Int + Y *big.Int +} + +func (c1 *Point) Equal(c2 Point) bool { + if c1.X.Int64() != c2.X.Int64() { + return false + } + if c1.Y.Int64() != c2.Y.Int64() { + return false + } + return true +} diff --git a/ecc/ecc.go b/ecc/ecc.go new file mode 100644 index 0000000..e219b67 --- /dev/null +++ b/ecc/ecc.go @@ -0,0 +1,83 @@ +package ecc + +import ( + "errors" + "math/big" +) + +type EC struct { + A *big.Int + B *big.Int + Q *big.Int +} + +/* + (y^2 = x^3 + Ax + B ) mod Q + Q: prime number +*/ +func NewEC(a, b, q int) (ec EC) { + ec.A = big.NewInt(int64(a)) + ec.B = big.NewInt(int64(b)) + ec.Q = big.NewInt(int64(q)) + return ec +} + +// At gets a point x in the curve +func (ec *EC) At(x *big.Int) (Point, Point, error) { + if x.Cmp(ec.Q) > 0 { + return Point{}, Point{}, errors.New("x 0 { return nil, errors.New("Error: need k