package diffiehellman import ( "crypto/rand" "math/big" ) // PrivateKey greater then 1 and less then p func PrivateKey(p *big.Int) *big.Int { two := big.NewInt(2) priv, _ := rand.Int(rand.Reader, new(big.Int).Sub(p, two)) return priv.Add(priv, two) } func PublicKey(a, p *big.Int, g int64) *big.Int { return new(big.Int).Exp(big.NewInt(g), a, p) } func SecretKey(a, B, p *big.Int) *big.Int { return new(big.Int).Exp(B, a, p) } func NewPair(p *big.Int, g int64) (*big.Int, *big.Int) { priv := PrivateKey(p) pub := PublicKey(priv, p, g) return priv, pub }