-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
72 lines (59 loc) · 1.66 KB
/
field.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
// 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 "math/big"
// F2 represents field extension for F(sqrt(i)): {x + y*sqrt(i) | x, y from F_modulo}
type F2 struct {
x, y *big.Int
i *big.Int
modulo *big.Int
}
func addF2(val1, val2 *F2) *F2 {
return &F2{
add(val1.x, val2.x, val1.modulo),
add(val1.y, val2.y, val1.modulo),
val1.i,
val1.modulo,
}
}
func mulF2(val1, val2 *F2) *F2 {
return &F2{
x: add(mul(val1.x, val2.x, val1.modulo), mul(mul(val1.y, val2.y, val1.modulo), val1.i, val1.modulo), val1.modulo),
y: add(mul(val1.x, val2.y, val1.modulo), mul(val1.y, val2.x, val1.modulo), val1.modulo),
i: val1.i,
modulo: val1.modulo,
}
}
func powF2(val *F2, exp *big.Int) *F2 {
if exp.Cmp(big.NewInt(0)) == 0 {
return &F2{
big.NewInt(0),
big.NewInt(0),
val.i,
val.modulo,
}
}
if exp.Cmp(big.NewInt(1)) == 0 {
return val
}
f := powF2(val, new(big.Int).Div(exp, big.NewInt(2)))
f = mulF2(f, f)
if new(big.Int).Mod(exp, big.NewInt(2)).Cmp(big.NewInt(1)) == 0 {
f = mulF2(f, val)
}
return f
}
func add(x *big.Int, y *big.Int, mod *big.Int) *big.Int {
return new(big.Int).Mod(new(big.Int).Add(x, y), mod)
}
func sub(x *big.Int, y *big.Int, mod *big.Int) *big.Int {
return new(big.Int).Mod(new(big.Int).Sub(x, y), mod)
}
func mul(x *big.Int, y *big.Int, mod *big.Int) *big.Int {
return new(big.Int).Mod(new(big.Int).Mul(x, y), mod)
}
func div(x *big.Int, y *big.Int, mod *big.Int) *big.Int {
return new(big.Int).Mod(new(big.Int).Mul(x, new(big.Int).ModInverse(y, mod)), mod)
}