12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package curve
- import (
- "context"
- "errors"
- "math/big"
- "math/rand"
- "time"
- )
- var (
- rnd = rand.New(rand.NewSource(time.Now().UnixNano())) // Генератор случайных чисел
- intZero = big.NewInt(0)
- intMax, _ = new(big.Int).SetString("9223372036854775807", 10)
- )
- func intCopy(val *big.Int) (res *big.Int) {
- if val != nil {
- res = new(big.Int).Set(val)
- }
- return
- }
- // Возвращает число или 0 при пустом указателе
- func mustBigInt(val *big.Int) (res *big.Int) {
- res = val
- if res == nil {
- res = big.NewInt(0)
- }
- return res
- }
- func IsPrime(val *big.Int) bool {
- return val.ProbablyPrime(100)
- }
- func SearchP(min *big.Int, ctx context.Context) (p *big.Int, g *big.Int, err error) {
- ch := make(chan struct{})
- go func() {
- defer func() {
- close(ch)
- }()
- p = intCopy(min)
- var check bool
- for {
- select {
- case <-ctx.Done():
- err = errors.New("Поиск поля и первообразного корня - время вышло")
- return
- default:
- if g, check = CheckP(p); check {
- ch <- struct{}{}
- return
- }
- p = Add64(p, 1)
- }
- }
- }()
- <-ch
- return
- }
- func CheckP(val *big.Int) (res *big.Int, check bool) {
- if IsPrime(val) {
- //log.Println("PRIME", val, Div64(Sub64(val, 1), 2))
- res = Div64(Sub64(val, 1), 2)
- check = IsPrime(res)
- }
- return
- }
- func SearchPrime(val *big.Int) *big.Int {
- rVal := new(big.Int).Set(val)
- //log.Println(rVal)
- for {
- if rVal.ProbablyPrime(100) {
- break
- }
- rVal.Add(rVal, big.NewInt(1))
- //log.Println(rVal)
- }
- return rVal
- }
- func Random(min, max *big.Int) *big.Int {
- if min == nil {
- min = intZero
- }
- if max == nil {
- max = intMax
- }
- res := new(big.Int).Rand(rnd, max)
- for res.Cmp(min) == -1 {
- res = new(big.Int).Rand(rnd, max)
- }
- return res
- }
|