From aa8684e440c619cbff36bba1a9a96c9a503941ab Mon Sep 17 00:00:00 2001 From: Clawd Date: Thu, 19 Feb 2026 20:53:27 -0800 Subject: Add field_test.go, simplify main.go --- field_test.go | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 29 +----------- 2 files changed, 141 insertions(+), 27 deletions(-) create mode 100644 field_test.go 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 @@ +package main + +import ( + "math/big" + "testing" +) + +func TestFieldAddition(t *testing.T) { + a := NewFieldElementFromInt64(7) + b := NewFieldElementFromInt64(5) + result := a.Add(b) + + expected := NewFieldElementFromInt64(12) + if !result.Equal(expected) { + t.Errorf("7 + 5 = %s, want 12", result.String()) + } +} + +func TestFieldSubtraction(t *testing.T) { + a := NewFieldElementFromInt64(7) + b := NewFieldElementFromInt64(5) + result := a.Sub(b) + + expected := NewFieldElementFromInt64(2) + if !result.Equal(expected) { + t.Errorf("7 - 5 = %s, want 2", result.String()) + } +} + +func TestFieldSubtractionWraps(t *testing.T) { + // 5 - 7 should wrap around (mod P) + a := NewFieldElementFromInt64(5) + b := NewFieldElementFromInt64(7) + result := a.Sub(b) + + // 5 - 7 mod P = P - 2 + expected := new(big.Int).Sub(P, big.NewInt(2)) + if result.value.Cmp(expected) != 0 { + t.Errorf("5 - 7 mod P should be P-2, got %s", result.String()) + } +} + +func TestFieldMultiplication(t *testing.T) { + a := NewFieldElementFromInt64(7) + b := NewFieldElementFromInt64(5) + result := a.Mul(b) + + expected := NewFieldElementFromInt64(35) + if !result.Equal(expected) { + t.Errorf("7 * 5 = %s, want 35", result.String()) + } +} + +func TestFieldDivision(t *testing.T) { + a := NewFieldElementFromInt64(10) + b := NewFieldElementFromInt64(5) + result := a.Div(b) + + expected := NewFieldElementFromInt64(2) + if !result.Equal(expected) { + t.Errorf("10 / 5 = %s, want 2", result.String()) + } +} + +func TestFieldInverse(t *testing.T) { + a := NewFieldElementFromInt64(5) + inv := a.Inverse() + product := a.Mul(inv) + + one := NewFieldElementFromInt64(1) + if !product.Equal(one) { + t.Errorf("5 * inverse(5) = %s, want 1", product.String()) + } +} + +func TestFieldInverseLargeNumber(t *testing.T) { + // Test with a larger number + a := NewFieldElementFromInt64(123456789) + inv := a.Inverse() + product := a.Mul(inv) + + one := NewFieldElementFromInt64(1) + if !product.Equal(one) { + t.Errorf("a * inverse(a) = %s, want 1", product.String()) + } +} + +func TestFieldSquare(t *testing.T) { + a := NewFieldElementFromInt64(7) + result := a.Square() + + expected := NewFieldElementFromInt64(49) + if !result.Equal(expected) { + t.Errorf("7² = %s, want 49", result.String()) + } +} + +func TestFieldIsZero(t *testing.T) { + zero := NewFieldElementFromInt64(0) + if !zero.IsZero() { + t.Error("0 should be zero") + } + + one := NewFieldElementFromInt64(1) + if one.IsZero() { + t.Error("1 should not be zero") + } +} + +func TestFieldModReduction(t *testing.T) { + // Creating element with P should reduce to 0 + result := NewFieldElement(P) + if !result.IsZero() { + t.Errorf("P mod P = %s, want 0", result.String()) + } + + // P + 1 should reduce to 1 + pPlusOne := new(big.Int).Add(P, big.NewInt(1)) + result = NewFieldElement(pPlusOne) + expected := NewFieldElementFromInt64(1) + if !result.Equal(expected) { + t.Errorf("(P+1) mod P = %s, want 1", result.String()) + } +} + +func TestFieldClone(t *testing.T) { + a := NewFieldElementFromInt64(42) + b := a.Clone() + + if !a.Equal(b) { + t.Error("clone should equal original") + } + + // Modify original, clone should be unchanged + a.value.SetInt64(100) + if a.Equal(b) { + t.Error("clone should be independent of original") + } +} diff --git a/main.go b/main.go index 9e19d35..2f37773 100644 --- a/main.go +++ b/main.go @@ -3,31 +3,6 @@ package main import "fmt" func main() { - fmt.Println("=== secp256k1 from scratch ===") - fmt.Println() - - // Test field arithmetic with small numbers first - fmt.Println("Testing field arithmetic:") - - a := NewFieldElementFromInt64(7) - b := NewFieldElementFromInt64(5) - - fmt.Printf("a = %d\n", 7) - fmt.Printf("b = %d\n", 5) - fmt.Printf("a + b = %s (should end in ...c)\n", a.Add(b).String()) - fmt.Printf("a - b = %s (should end in ...2)\n", a.Sub(b).String()) - fmt.Printf("a * b = %s (should end in ...23, which is 35)\n", a.Mul(b).String()) - - // Test division: 10 / 5 = 2 - ten := NewFieldElementFromInt64(10) - five := NewFieldElementFromInt64(5) - fmt.Printf("10 / 5 = %s (should end in ...2)\n", ten.Div(five).String()) - - // Test inverse: 5 * inverse(5) should = 1 - inv := five.Inverse() - product := five.Mul(inv) - fmt.Printf("5 * inverse(5) = %s (should end in ...1)\n", product.String()) - - fmt.Println() - fmt.Println("Field arithmetic works!") + fmt.Println("secp256k1 from scratch") + fmt.Println("Run: go test -v") } -- cgit v1.2.3