aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--field_test.go139
-rw-r--r--main.go29
2 files changed, 141 insertions, 27 deletions
diff --git a/field_test.go b/field_test.go
new file mode 100644
index 0000000..9905eec
--- /dev/null
+++ b/field_test.go
@@ -0,0 +1,139 @@
1package main
2
3import (
4 "math/big"
5 "testing"
6)
7
8func TestFieldAddition(t *testing.T) {
9 a := NewFieldElementFromInt64(7)
10 b := NewFieldElementFromInt64(5)
11 result := a.Add(b)
12
13 expected := NewFieldElementFromInt64(12)
14 if !result.Equal(expected) {
15 t.Errorf("7 + 5 = %s, want 12", result.String())
16 }
17}
18
19func TestFieldSubtraction(t *testing.T) {
20 a := NewFieldElementFromInt64(7)
21 b := NewFieldElementFromInt64(5)
22 result := a.Sub(b)
23
24 expected := NewFieldElementFromInt64(2)
25 if !result.Equal(expected) {
26 t.Errorf("7 - 5 = %s, want 2", result.String())
27 }
28}
29
30func TestFieldSubtractionWraps(t *testing.T) {
31 // 5 - 7 should wrap around (mod P)
32 a := NewFieldElementFromInt64(5)
33 b := NewFieldElementFromInt64(7)
34 result := a.Sub(b)
35
36 // 5 - 7 mod P = P - 2
37 expected := new(big.Int).Sub(P, big.NewInt(2))
38 if result.value.Cmp(expected) != 0 {
39 t.Errorf("5 - 7 mod P should be P-2, got %s", result.String())
40 }
41}
42
43func TestFieldMultiplication(t *testing.T) {
44 a := NewFieldElementFromInt64(7)
45 b := NewFieldElementFromInt64(5)
46 result := a.Mul(b)
47
48 expected := NewFieldElementFromInt64(35)
49 if !result.Equal(expected) {
50 t.Errorf("7 * 5 = %s, want 35", result.String())
51 }
52}
53
54func TestFieldDivision(t *testing.T) {
55 a := NewFieldElementFromInt64(10)
56 b := NewFieldElementFromInt64(5)
57 result := a.Div(b)
58
59 expected := NewFieldElementFromInt64(2)
60 if !result.Equal(expected) {
61 t.Errorf("10 / 5 = %s, want 2", result.String())
62 }
63}
64
65func TestFieldInverse(t *testing.T) {
66 a := NewFieldElementFromInt64(5)
67 inv := a.Inverse()
68 product := a.Mul(inv)
69
70 one := NewFieldElementFromInt64(1)
71 if !product.Equal(one) {
72 t.Errorf("5 * inverse(5) = %s, want 1", product.String())
73 }
74}
75
76func TestFieldInverseLargeNumber(t *testing.T) {
77 // Test with a larger number
78 a := NewFieldElementFromInt64(123456789)
79 inv := a.Inverse()
80 product := a.Mul(inv)
81
82 one := NewFieldElementFromInt64(1)
83 if !product.Equal(one) {
84 t.Errorf("a * inverse(a) = %s, want 1", product.String())
85 }
86}
87
88func TestFieldSquare(t *testing.T) {
89 a := NewFieldElementFromInt64(7)
90 result := a.Square()
91
92 expected := NewFieldElementFromInt64(49)
93 if !result.Equal(expected) {
94 t.Errorf("7² = %s, want 49", result.String())
95 }
96}
97
98func TestFieldIsZero(t *testing.T) {
99 zero := NewFieldElementFromInt64(0)
100 if !zero.IsZero() {
101 t.Error("0 should be zero")
102 }
103
104 one := NewFieldElementFromInt64(1)
105 if one.IsZero() {
106 t.Error("1 should not be zero")
107 }
108}
109
110func TestFieldModReduction(t *testing.T) {
111 // Creating element with P should reduce to 0
112 result := NewFieldElement(P)
113 if !result.IsZero() {
114 t.Errorf("P mod P = %s, want 0", result.String())
115 }
116
117 // P + 1 should reduce to 1
118 pPlusOne := new(big.Int).Add(P, big.NewInt(1))
119 result = NewFieldElement(pPlusOne)
120 expected := NewFieldElementFromInt64(1)
121 if !result.Equal(expected) {
122 t.Errorf("(P+1) mod P = %s, want 1", result.String())
123 }
124}
125
126func TestFieldClone(t *testing.T) {
127 a := NewFieldElementFromInt64(42)
128 b := a.Clone()
129
130 if !a.Equal(b) {
131 t.Error("clone should equal original")
132 }
133
134 // Modify original, clone should be unchanged
135 a.value.SetInt64(100)
136 if a.Equal(b) {
137 t.Error("clone should be independent of original")
138 }
139}
diff --git a/main.go b/main.go
index 9e19d35..2f37773 100644
--- a/main.go
+++ b/main.go
@@ -3,31 +3,6 @@ package main
3import "fmt" 3import "fmt"
4 4
5func main() { 5func main() {
6 fmt.Println("=== secp256k1 from scratch ===") 6 fmt.Println("secp256k1 from scratch")
7 fmt.Println() 7 fmt.Println("Run: go test -v")
8
9 // Test field arithmetic with small numbers first
10 fmt.Println("Testing field arithmetic:")
11
12 a := NewFieldElementFromInt64(7)
13 b := NewFieldElementFromInt64(5)
14
15 fmt.Printf("a = %d\n", 7)
16 fmt.Printf("b = %d\n", 5)
17 fmt.Printf("a + b = %s (should end in ...c)\n", a.Add(b).String())
18 fmt.Printf("a - b = %s (should end in ...2)\n", a.Sub(b).String())
19 fmt.Printf("a * b = %s (should end in ...23, which is 35)\n", a.Mul(b).String())
20
21 // Test division: 10 / 5 = 2
22 ten := NewFieldElementFromInt64(10)
23 five := NewFieldElementFromInt64(5)
24 fmt.Printf("10 / 5 = %s (should end in ...2)\n", ten.Div(five).String())
25
26 // Test inverse: 5 * inverse(5) should = 1
27 inv := five.Inverse()
28 product := five.Mul(inv)
29 fmt.Printf("5 * inverse(5) = %s (should end in ...1)\n", product.String())
30
31 fmt.Println()
32 fmt.Println("Field arithmetic works!")
33} 8}