have lib function take map instead of list
This commit is contained in:
parent
0fdb60275f
commit
8333be5d2d
|
|
@ -28,21 +28,37 @@ import (
|
||||||
var modulus string
|
var modulus string
|
||||||
var tpf []string
|
var tpf []string
|
||||||
|
|
||||||
|
func count(tpf []string) map[string]*big.Int {
|
||||||
|
counter := make(map[string]*big.Int)
|
||||||
|
for _, s := range tpf {
|
||||||
|
count, ok := counter[s]
|
||||||
|
if ok {
|
||||||
|
count.Add(count, big.NewInt(1))
|
||||||
|
} else {
|
||||||
|
counter[s] = big.NewInt(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return counter
|
||||||
|
}
|
||||||
|
|
||||||
func primitiveRoot(cmd *cobra.Command, args []string) {
|
func primitiveRoot(cmd *cobra.Command, args []string) {
|
||||||
m, ok := new(big.Int).SetString(modulus, 10)
|
m, ok := new(big.Int).SetString(modulus, 10)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Fatal("invalid input " + modulus)
|
log.Fatal("invalid input " + modulus)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
factors := count(tpf)
|
||||||
|
|
||||||
root := new(big.Int)
|
root := new(big.Int)
|
||||||
var err error
|
var err error
|
||||||
if len(tpf) == 0 {
|
if len(factors) == 0 {
|
||||||
root, err = lib.PrimitiveRoot(m)
|
root, err = lib.PrimitiveRoot(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
root, err = lib.PrimitiveRootFast(m, tpf)
|
root, err = lib.PrimitiveRootFast(m, factors)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -141,17 +141,16 @@ func PrimitiveRoot(modulus *big.Int) (*big.Int, error) {
|
||||||
return nil, errors.New("no primitive root")
|
return nil, errors.New("no primitive root")
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrimitiveRootFast(modulus *big.Int, tpf []string) (*big.Int, error) {
|
func PrimitiveRootFast(modulus *big.Int, tpf map[string]*big.Int) (*big.Int, error) {
|
||||||
phi := big.NewInt(1)
|
phi := big.NewInt(1)
|
||||||
factors := make(map[string]bool)
|
for p, exp := range tpf {
|
||||||
for _, s := range tpf {
|
pow, ok := new(big.Int).SetString(p, 10)
|
||||||
p, ok := new(big.Int).SetString(s, 10)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("invalid input " + s)
|
return nil, errors.New("invalid factor " + p)
|
||||||
}
|
}
|
||||||
|
|
||||||
phi.Mul(phi, p)
|
pow.Exp(pow, exp, nil)
|
||||||
factors[p.Text(10)] = true
|
phi.Mul(phi, pow)
|
||||||
}
|
}
|
||||||
|
|
||||||
for g := big.NewInt(1); g.Cmp(modulus) == -1; g.Add(g, big.NewInt(1)) {
|
for g := big.NewInt(1); g.Cmp(modulus) == -1; g.Add(g, big.NewInt(1)) {
|
||||||
|
|
@ -161,7 +160,7 @@ func PrimitiveRootFast(modulus *big.Int, tpf []string) (*big.Int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
isPrimitive := true
|
isPrimitive := true
|
||||||
for p := range factors {
|
for p := range tpf {
|
||||||
e := new(big.Int)
|
e := new(big.Int)
|
||||||
f, _ := new(big.Int).SetString(p, 10)
|
f, _ := new(big.Int).SetString(p, 10)
|
||||||
k := new(big.Int).Div(phi, f)
|
k := new(big.Int).Div(phi, f)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue