aboutsummaryrefslogtreecommitdiffstats
path: root/field_test.go
blob: 9905eecdfe0a13b9ef06ef05d73a26080aaf2080 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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")
	}
}