Compare commits
No commits in common. "master" and "main" have entirely different histories.
10
LICENSE
Normal file
10
LICENSE
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
This is free and unencumbered software released into the public domain.
|
||||||
|
|
||||||
|
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
|
||||||
|
|
||||||
|
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and
|
||||||
|
successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
For more information, please refer to <http://unlicense.org/>
|
54
README.md
54
README.md
@ -1,55 +1,3 @@
|
|||||||
# golcg
|
# golcg
|
||||||
|
|
||||||
Chilling in `#dev` on `irc.supernets.org` one day, `acidvegas` hit me up like:
|
An example of how to write an LCG in golang.
|
||||||
|
|
||||||
```
|
|
||||||
<@acidvegas> darkmage: how easily do you think you could
|
|
||||||
<@acidvegas> isolate masscans
|
|
||||||
<@acidvegas> range function
|
|
||||||
<@acidvegas> into golang or python
|
|
||||||
<@acidvegas> aka
|
|
||||||
<@acidvegas> generating ip addresses
|
|
||||||
<@acidvegas> randomly
|
|
||||||
<@acidvegas> for massive ranges like 0.0.0.0/0
|
|
||||||
<@acidvegas> in a memory safe manner
|
|
||||||
```
|
|
||||||
|
|
||||||
Really easily, it turns out.
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
# usage
|
|
||||||
|
|
||||||
```
|
|
||||||
go run main.go <seed> <shard_count>
|
|
||||||
```
|
|
||||||
|
|
||||||
-----
|
|
||||||
|
|
||||||
# how it works
|
|
||||||
|
|
||||||
You can generate numbers in a predictable manner by feeding a custom seed to an RNG to find a random starting position, modifying it with an offset relative to which shard (assuming you're using multiple shards but not required) is generating addresses (so that no two shards generate the same address).
|
|
||||||
|
|
||||||
```
|
|
||||||
func run_sieve(max int64, start int64, move int64, shard_num int64, shard_count int64) {
|
|
||||||
var current int64 = start + shard_num
|
|
||||||
if current > max-1 {
|
|
||||||
current = current % max
|
|
||||||
}
|
|
||||||
for i := int64(0); i <= max/shard_count; i++ {
|
|
||||||
sieve[current] = true
|
|
||||||
current = current + move
|
|
||||||
if current > max-1 {
|
|
||||||
current = current % max
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defer wg.Done()
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The meat of this process is handled with this function, `run_sieve`.
|
|
||||||
|
|
||||||
We can generate random start indices or we can start at 0, and then we apply the shard offset.
|
|
||||||
If the resulting start value is beyond our range, we roll the number around to the beginning again with a modulus.
|
|
||||||
We also keep track of a global sieve that contains booleans to verify we hit every number.
|
|
||||||
|
|
83
main.go
83
main.go
@ -1,83 +0,0 @@
|
|||||||
// linear congruential generator written in go
|
|
||||||
// by darkmage
|
|
||||||
// https://www.evildojo.com
|
|
||||||
// https://www.twitter.com/evildojo666
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
"os"
|
|
||||||
"math"
|
|
||||||
"math/rand"
|
|
||||||
"strconv"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
// define a global sieve
|
|
||||||
var sieve []bool
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
|
|
||||||
func run_sieve(max int64, start int64, move int64, shard_num int64, shard_count int64) {
|
|
||||||
var current int64 = start + shard_num
|
|
||||||
if current > max-1 {
|
|
||||||
current = current % max
|
|
||||||
}
|
|
||||||
for i := int64(0); i <= max/shard_count; i++ {
|
|
||||||
sieve[current] = true
|
|
||||||
current = current + move
|
|
||||||
if current > max-1 {
|
|
||||||
current = current % max
|
|
||||||
}
|
|
||||||
}
|
|
||||||
defer wg.Done()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
if len(os.Args) != 3 {
|
|
||||||
fmt.Println("Usage: go run main.go <seed> <shard_count>")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
seed, err := strconv.ParseInt(os.Args[1], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error: ", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
rand := rand.New(rand.NewSource(seed))
|
|
||||||
var max int64 = 256*256*256*256
|
|
||||||
var start int64 = rand.Int63n(max)
|
|
||||||
fmt.Println("start:", start)
|
|
||||||
shard_count, err := strconv.ParseInt(os.Args[2], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error: ", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
fmt.Println("shard_count:", shard_count)
|
|
||||||
var move int64 = (int64(math.Sqrt(float64(max))) + 1) * shard_count
|
|
||||||
fmt.Println("move:", move)
|
|
||||||
sieve = make([]bool, max)
|
|
||||||
start_time := time.Now()
|
|
||||||
for i := int64(0); i < shard_count; i++ {
|
|
||||||
fmt.Println("shard:", i)
|
|
||||||
wg.Add(1)
|
|
||||||
go run_sieve(max, start, move, i, shard_count)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
end_time := time.Now()
|
|
||||||
fmt.Println("Completed iterating numbers")
|
|
||||||
fmt.Println("Time elapsed:", end_time.Sub(start_time))
|
|
||||||
fmt.Println("Checking sieve")
|
|
||||||
start_time = time.Now()
|
|
||||||
for i := int64(0); i < max; i++ {
|
|
||||||
if !sieve[i] {
|
|
||||||
fmt.Println(os.Stderr, "Error, not all numbers flipped")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end_time = time.Now()
|
|
||||||
fmt.Println("Time elapsed:", end_time.Sub(start_time))
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user