summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-09-04 15:24:59 +0200
committerDimitri Sokolyuk <demon@dim13.org>2015-09-04 15:24:59 +0200
commit5d6d018b87f84e6493b6c605cdcb11d4ec89664b (patch)
tree7d4238cfd45c47e9b5e48ed448ad658642fcc6ea
parent43ac68eb42be0ae60808a3778129c977d7807e71 (diff)
Refactor
-rw-r--r--main.go41
1 files changed, 21 insertions, 20 deletions
diff --git a/main.go b/main.go
index 1837e00..11a7356 100644
--- a/main.go
+++ b/main.go
@@ -15,8 +15,14 @@ import (
"github.com/cznic/mathutil"
)
-func next(N *big.Int) <-chan *big.Int {
- ch := make(chan *big.Int)
+func seed(N *big.Int) (<-chan *big.Int, <-chan *big.Int) {
+ p := mathutil.SqrtBig(N)
+ p.SetBit(p, 0, 1) // ensure it's odd
+
+ q := new(big.Int).Div(N, p)
+ q.SetBit(p, 0, 1) // ensure it's odd
+
+ nxt := make(chan *big.Int)
go func(c chan *big.Int, n *big.Int) {
two := big.NewInt(2)
for {
@@ -25,12 +31,9 @@ func next(N *big.Int) <-chan *big.Int {
c <- new(big.Int).Set(n)
}
}
- }(ch, N)
- return ch
-}
+ }(nxt, p)
-func prev(N *big.Int) <-chan *big.Int {
- ch := make(chan *big.Int)
+ prv := make(chan *big.Int)
go func(c chan *big.Int, n *big.Int) {
two := big.NewInt(2)
for {
@@ -39,15 +42,17 @@ func prev(N *big.Int) <-chan *big.Int {
c <- new(big.Int).Set(n)
}
}
- }(ch, N)
- return ch
+ }(prv, q)
+
+ return nxt, prv
}
func factor(N *big.Int) (*big.Int, *big.Int) {
- p, q := seed(N)
- nxt := next(p)
- prv := prev(q)
+ nxt, prv := seed(N)
m := new(big.Int)
+ p := <-nxt
+ q := <-prv
+
for {
switch m.Mul(p, q).Cmp(N) {
case -1:
@@ -58,15 +63,8 @@ func factor(N *big.Int) (*big.Int, *big.Int) {
q = <-prv
}
}
- return nil, nil
-}
-func seed(N *big.Int) (*big.Int, *big.Int) {
- p := mathutil.SqrtBig(N)
- p.SetBit(p, 0, 1) // ensure it's odd
- q := new(big.Int).Div(N, p)
- q.SetBit(p, 0, 1) // ensure it's odd
- return p, q
+ return nil, nil
}
func pubKey(f string) rsa.PublicKey {
@@ -74,11 +72,14 @@ func pubKey(f string) rsa.PublicKey {
if err != nil {
log.Fatal(err)
}
+
block, _ := pem.Decode(file)
+
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
log.Fatal(err)
}
+
return key.PublicKey
}