diff --git a/.gitignore b/.gitignore index 34c385a..b399eda 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ fmt +schnorr.goBACKUP diff --git a/README.md b/README.md index 3c56947..1cfc130 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# cryptofun [![Go Report Card](https://goreportcard.com/badge/github.com/arnaucode/cryptofun)](https://goreportcard.com/report/github.com/arnaucode/cryptofun) +# cryptofun [![Go Report Card](https://goreportcard.com/badge/github.com/arnaucube/cryptofun)](https://goreportcard.com/report/github.com/arnaucube/cryptofun) Crypto algorithms from scratch. Academic purposes only. @@ -51,6 +51,12 @@ https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm - [x] ECDSA Verify signature +## Schnorr signature +https://en.wikipedia.org/wiki/Schnorr_signature +- [x] Hash[M || R] (where M is the msg bytes and R is a Point on the ECC, using sha256 hash function) +- [x] Generate Schnorr scheme +- [x] Sign +- [x] Verify signature --- diff --git a/dh/dh_test.go b/dh/dh_test.go index 7aa0178..ea6b07d 100644 --- a/dh/dh_test.go +++ b/dh/dh_test.go @@ -4,6 +4,8 @@ import ( "crypto/rand" "math/big" "testing" + + "github.com/stretchr/testify/assert" ) const ( @@ -12,26 +14,19 @@ const ( func TestDiffieHellman(t *testing.T) { p, err := rand.Prime(rand.Reader, bits/2) - if err != nil { - t.Errorf(err.Error()) - } + assert.Nil(t, err) + g, err := rand.Prime(rand.Reader, bits/2) - if err != nil { - t.Errorf(err.Error()) - } + assert.Nil(t, err) max, err := rand.Prime(rand.Reader, bits/2) - if err != nil { - t.Errorf(err.Error()) - } + assert.Nil(t, err) + a, err := rand.Int(rand.Reader, max) - if err != nil { - t.Errorf(err.Error()) - } + assert.Nil(t, err) + b, err := rand.Int(rand.Reader, max) - if err != nil { - t.Errorf(err.Error()) - } + assert.Nil(t, err) A := new(big.Int).Exp(g, a, p) B := new(big.Int).Exp(g, b, p) diff --git a/ecc/ecc.go b/ecc/ecc.go index 06dbfc4..4195326 100644 --- a/ecc/ecc.go +++ b/ecc/ecc.go @@ -49,20 +49,6 @@ func (ec *EC) Neg(p Point) Point { return Point{p.X, new(big.Int).Sub(ec.Q, p.Y)} } -// Order returns smallest n where nG = O (point at zero) -func (ec *EC) Order(g Point) (int, error) { - for i := 1; i < int(ec.Q.Int64())+1; i++ { - mPoint, err := ec.Mul(g, i) - if err != nil { - return i, err - } - if mPoint.Equal(zeroPoint) { - return i, nil - } - } - return -1, errors.New("invalid order") -} - // Add adds two points p1 and p2 and gets q, returns the negate of q func (ec *EC) Add(p1, p2 Point) (Point, error) { if p1.Equal(zeroPoint) { @@ -126,23 +112,41 @@ func (ec *EC) Add(p1, p2 Point) (Point, error) { } // Mul multiplies a point n times on the elliptic curve -func (ec *EC) Mul(p Point, n int) (Point, error) { +func (ec *EC) Mul(p Point, n *big.Int) (Point, error) { var err error p2 := p r := zeroPoint - for 0 < n { - if n&1 == 1 { + for bigZero.Cmp(n) == -1 { // 0> 1 + n = n.Rsh(n, 1) // n = n>>1 p2, err = ec.Add(p2, p2) if err != nil { return p, err } - } return r, nil } + +// Order returns smallest n where nG = O (point at zero) +func (ec *EC) Order(g Point) (*big.Int, error) { + // loop from i:=1 to i 0 { return nil, errors.New("Error: need k