|
|
@ -1,51 +1,46 @@ |
|
|
|
package ecc |
|
|
|
|
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
"math/big" |
|
|
|
"testing" |
|
|
|
) |
|
|
|
|
|
|
|
func TestECC(t *testing.T) { |
|
|
|
ec := NewEC(0, 7, 11) |
|
|
|
p1, p1_, err := ec.At(big.NewInt(int64(7))) |
|
|
|
p1, p1i, err := ec.At(big.NewInt(int64(7))) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
if !p1.Equal(Point{big.NewInt(int64(7)), big.NewInt(int64(3))}) { |
|
|
|
t.Errorf("p1!=(7, 11)") |
|
|
|
} |
|
|
|
if !p1_.Equal(Point{big.NewInt(int64(7)), big.NewInt(int64(8))}) { |
|
|
|
t.Errorf("p1_!=(7, 8)") |
|
|
|
if !p1i.Equal(Point{big.NewInt(int64(7)), big.NewInt(int64(8))}) { |
|
|
|
t.Errorf("p1i!=(7, 8)") |
|
|
|
} |
|
|
|
} |
|
|
|
func TestNeg(t *testing.T) { |
|
|
|
ec := NewEC(0, 7, 11) |
|
|
|
p1, p1_, err := ec.At(big.NewInt(int64(7))) |
|
|
|
p1, p1i, err := ec.At(big.NewInt(int64(7))) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
p1Neg := ec.Neg(p1) |
|
|
|
if !p1Neg.Equal(p1_) { |
|
|
|
t.Errorf("p1Neg!=p1_") |
|
|
|
if !p1Neg.Equal(p1i) { |
|
|
|
t.Errorf("p1Neg!=p1i") |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
func TestAdd(t *testing.T) { |
|
|
|
ec := NewEC(0, 7, 11) |
|
|
|
p1, _, err := ec.At(big.NewInt(int64(7))) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
p2, _, err := ec.At(big.NewInt(int64(6))) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(7))} |
|
|
|
p2 := Point{big.NewInt(int64(2)), big.NewInt(int64(2))} |
|
|
|
q, err := ec.Add(p1, p2) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
if !q.Equal(Point{big.NewInt(int64(2)), big.NewInt(int64(9))}) { |
|
|
|
t.Errorf("q!=(2, 9)") |
|
|
|
if !q.Equal(Point{big.NewInt(int64(3)), big.NewInt(int64(1))}) { |
|
|
|
t.Errorf("q!=(3, 1)") |
|
|
|
} |
|
|
|
|
|
|
|
// check that q exists on the elliptic curve
|
|
|
@ -61,47 +56,50 @@ func TestAdd(t *testing.T) { |
|
|
|
|
|
|
|
func TestAddSamePoint(t *testing.T) { |
|
|
|
ec := NewEC(0, 7, 11) |
|
|
|
p1, p1_, err := ec.At(big.NewInt(int64(4))) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(7))} |
|
|
|
p1i := Point{big.NewInt(int64(4)), big.NewInt(int64(4))} |
|
|
|
|
|
|
|
q, err := ec.Add(p1, p1) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
if !q.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(6))}) { |
|
|
|
t.Errorf("q!=(6, 6)") |
|
|
|
if !q.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(5))}) { |
|
|
|
t.Errorf("q!=(6, 5)") |
|
|
|
} |
|
|
|
|
|
|
|
q_, err := ec.Add(p1_, p1_) |
|
|
|
q_, err := ec.Add(p1i, p1i) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
if !q_.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(5))}) { |
|
|
|
t.Errorf("q_!=(6, 5)") |
|
|
|
if !q_.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(6))}) { |
|
|
|
t.Errorf("q_!=(6, 6)") |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestMulEqualSelfAdd(t *testing.T) { |
|
|
|
ec := NewEC(0, 7, 11) |
|
|
|
p1, _, err := ec.At(big.NewInt(int64(4))) |
|
|
|
ec := NewEC(0, 7, 29) |
|
|
|
p1 := Point{big.NewInt(int64(11)), big.NewInt(int64(27))} |
|
|
|
|
|
|
|
p1p1, err := ec.Add(p1, p1) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
p1p1, err := ec.Add(p1, p1) |
|
|
|
p1p1, err = ec.Add(p1p1, p1) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
q, err := ec.Mul(p1, 1) |
|
|
|
q, err := ec.Mul(p1, 3) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
if !q.Equal(p1p1) { |
|
|
|
fmt.Println(q) |
|
|
|
fmt.Println(p1p1) |
|
|
|
t.Errorf("q!=p1*p1") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestMul(t *testing.T) { |
|
|
|
ec := NewEC(0, 7, 29) |
|
|
|
p1 := Point{big.NewInt(int64(4)), big.NewInt(int64(19))} |
|
|
@ -109,22 +107,22 @@ func TestMul(t *testing.T) { |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
if !q3.Equal(Point{big.NewInt(int64(19)), big.NewInt(int64(15))}) { |
|
|
|
t.Errorf("q3!=(19, 15)") |
|
|
|
if !q3.Equal(Point{big.NewInt(int64(6)), big.NewInt(int64(7))}) { |
|
|
|
t.Errorf("q3!=(6, 7)") |
|
|
|
} |
|
|
|
q7, err := ec.Mul(p1, 7) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
if !q7.Equal(Point{big.NewInt(int64(19)), big.NewInt(int64(15))}) { |
|
|
|
t.Errorf("q7!=(19, 15)") |
|
|
|
if !q7.Equal(Point{big.NewInt(int64(19)), big.NewInt(int64(14))}) { |
|
|
|
t.Errorf("q7!=(19, 14)") |
|
|
|
} |
|
|
|
|
|
|
|
q8, err := ec.Mul(p1, 8) |
|
|
|
if err != nil { |
|
|
|
t.Errorf(err.Error()) |
|
|
|
} |
|
|
|
if !q8.Equal(Point{big.NewInt(int64(4)), big.NewInt(int64(19))}) { |
|
|
|
t.Errorf("q8!=(4, 19)") |
|
|
|
if !q8.Equal(Point{big.NewInt(int64(19)), big.NewInt(int64(15))}) { |
|
|
|
t.Errorf("q8!=(19, 15)") |
|
|
|
} |
|
|
|
} |