sonr/internal/crypto/core/primes.go
Prad Nukala 47c3a53080
refactor/internal (#1216)
* refactor: update import paths in gateway handlers

* refactor: remove obsolete devtools Makefile and README

* build: optimize build process for improved efficiency

* refactor: remove obsolete pkl files related to Matrix and Sonr network configurations

* refactor: move embed code to x/dwn/types
2024-12-24 16:10:20 +00:00

43 lines
799 B
Go
Executable File

//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package core
import (
"crypto/rand"
"fmt"
"math"
"math/big"
)
// GenerateSafePrime creates a prime number `p`
// where (`p`-1)/2 is also prime with at least `bits`
func GenerateSafePrime(bits uint) (*big.Int, error) {
if bits < 3 {
return nil, fmt.Errorf("safe prime size must be at least 3-bits")
}
var p *big.Int
var err error
checks := int(math.Max(float64(bits)/16, 8))
for {
// rand.Prime throws an error if bits < 2
// -1 so the Sophie-Germain prime is 1023 bits
// and the Safe prime is 1024
p, err = rand.Prime(rand.Reader, int(bits)-1)
if err != nil {
return nil, err
}
p.Add(p.Lsh(p, 1), One)
if p.ProbablyPrime(checks) {
break
}
}
return p, nil
}