27 lines
573 B
Rust
27 lines
573 B
Rust
use num_bigint::BigUint;
|
|
use num_bigint::RandBigInt;
|
|
|
|
#[derive(Clone)]
|
|
pub struct PublicKey(pub BigUint);
|
|
|
|
#[derive(Clone)]
|
|
pub struct PrivateKey(pub BigUint);
|
|
|
|
#[derive(Clone)]
|
|
pub struct Keypair {
|
|
pub private: PrivateKey,
|
|
pub public: PublicKey,
|
|
}
|
|
|
|
impl Keypair {
|
|
pub fn make(p: &BigUint, g: &BigUint) -> Self {
|
|
let mut rng = rand::thread_rng();
|
|
let private = rng.gen_biguint_below(p);
|
|
let public = g.modpow(&private, p);
|
|
Self {
|
|
private: PrivateKey(private),
|
|
public: PublicKey(public),
|
|
}
|
|
}
|
|
}
|