-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmontgomery_test.go
86 lines (73 loc) · 2.2 KB
/
montgomery_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
package montgomery
import (
"fmt"
"math"
"math/big"
"testing"
"github.com/stretchr/testify/require"
)
const (
P string = "fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"
bx string = "b0007aa30c1c50f8"
by string = "30ffffffefffffbb"
)
func TestMontMul(t *testing.T) {
mod, ok := new(big.Int).SetString(P, 16)
require.True(t, ok)
bigx, ok := new(big.Int).SetString(bx, 16)
require.True(t, ok)
bigy, ok := new(big.Int).SetString(by, 16)
require.True(t, ok)
montx, np0 := bn2mont(bigx, mod)
require.NotEqual(t, np0, 0)
fmt.Println("np0", np0)
monty, _ := bn2mont(bigy, mod)
require.Equal(t, NP0(mod), np0)
fmt.Println("mont_x", montx.Text(16))
fmt.Println("mont_y", monty.Text(16))
montz, err := mont_mul(montx, monty, mod, np0)
require.Nil(t, err)
fmt.Println("mont_z", montz.Text(16))
bigz, err := mont2bn(montz, mod, np0)
require.Nil(t, err)
fmt.Println("z", bigz.Text(16))
// verify result
correct := new(big.Int).Mul(bigx, bigy)
correct.Mod(correct, mod)
require.Equal(t, correct.Cmp(bigz), 0)
}
func TestPowMod(t *testing.T) {
mod, ok := new(big.Int).SetString(P, 16)
require.True(t, ok)
base := big.NewInt(math.MaxInt64)
exp := big.NewInt(10000)
result, err := powm_odd(base, exp, mod)
require.Nil(t, err)
// verify result
correct := new(big.Int).Set(base)
correct.Exp(correct, exp, mod)
require.Equal(t, result.Cmp(correct), 0)
// print result
fmt.Println("powm", result.Text(16))
}
func BenchmarkMontMul(b *testing.B) {
mod, ok := new(big.Int).SetString(P, 16)
require.True(b, ok)
bigx, ok := new(big.Int).SetString(bx, 16)
require.True(b, ok)
bigy, ok := new(big.Int).SetString(by, 16)
require.True(b, ok)
montx, np0 := bn2mont(bigx, mod)
require.NotEqual(b, np0, 0)
fmt.Println("np0", np0)
monty, _ := bn2mont(bigy, mod)
require.Equal(b, NP0(mod), np0)
fmt.Println("mont_x", montx.Text(16))
fmt.Println("mont_y", monty.Text(16))
// b.StartTimer()
// for i := 0; i < b.N; i++ {
// _, err := mont_mul(montx, monty, mod, np0)
// require.Nil(b, err)
// }
// b.StopTimer()
}