-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
110 lines (98 loc) · 2.13 KB
/
main_test.go
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
// Package math
// Copyright 2023 Oleg Fomenko. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package math
import (
"crypto/rand"
"fmt"
"math/big"
"testing"
)
func TestLCM(t *testing.T) {
fmt.Println(LCM(big.NewInt(7), big.NewInt(4), big.NewInt(2), big.NewInt(14))) // 28
}
func TestMu(t *testing.T) {
fmt.Println(Mu(big.NewInt(31))) // -1
fmt.Println(Mu(big.NewInt(10))) // 1
fmt.Println(Mu(big.NewInt(7))) // -1
fmt.Println(Mu(big.NewInt(98))) // -0
}
func TestPhi(t *testing.T) {
fmt.Println(Phi(big.NewInt(31))) // 30
fmt.Println(Phi(big.NewInt(10))) // 4
fmt.Println(Phi(big.NewInt(7))) // 7
}
func TestFindSquareRoot(t *testing.T) {
fmt.Println(FindSquareRoot(big.NewInt(10), big.NewInt(13))) // 6 or 7
fmt.Println(FindSquareRoot(big.NewInt(362), big.NewInt(7919))) // 7828 or 91
}
func TestJacobi(t *testing.T) {
tests := []struct {
a *big.Int
p *big.Int
res *big.Int
}{
{
a: big.NewInt(7),
p: big.NewInt(35),
res: big.NewInt(0),
},
{
a: big.NewInt(2),
p: big.NewInt(41),
res: big.NewInt(1),
},
{
a: big.NewInt(21),
p: big.NewInt(9),
res: big.NewInt(0),
},
{
a: big.NewInt(8),
p: big.NewInt(13),
res: big.NewInt(-1),
},
{
a: big.NewInt(4),
p: big.NewInt(55),
res: big.NewInt(1),
},
{
a: big.NewInt(9),
p: big.NewInt(37),
res: big.NewInt(1),
},
}
for i, t := range tests {
fmt.Printf("Running %d\n", i)
res, err := Jacobi(t.a, t.p)
if err != nil {
panic(err)
}
if res.Cmp(t.res) != 0 {
panic(fmt.Sprintf("test cast %d failed", i))
}
fmt.Printf("Finished %d\n", i)
}
}
func TestTestPrimes(t *testing.T) {
for i := 0; i < 10; i++ {
fmt.Printf("Running %d prime check\n", i)
prime, err := rand.Prime(rand.Reader, 256)
if err != nil {
panic(err)
}
ok, err := TestPrime(prime)
if err != nil {
panic(err)
}
if !ok {
fmt.Println(prime.String())
panic("verification failed on prime number")
}
}
}
func TestLegendre(t *testing.T) {
fmt.Println(Legendre(big.NewInt(219), big.NewInt(383))) // Should be 1
}