Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions solutions/csharp/diffie-hellman/1/DiffieHellman.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Numerics;

public static class DiffieHellman
{
private static Random rng = new();
public static BigInteger PrivateKey(BigInteger primeP) => rng.Next(1, (int)primeP);

// use BigInteger.ModPow() instead of Math.Pow() since double has less digits and cuts off!
public static BigInteger PublicKey(BigInteger primeP, BigInteger primeG, BigInteger privateKey) => BigInteger.ModPow(primeG, privateKey, primeP);

public static BigInteger Secret(BigInteger primeP, BigInteger publicKey, BigInteger privateKey) => BigInteger.ModPow(publicKey, privateKey, primeP);
}