Advanced _Point*scalar multiplication - Chewhern/ASodium GitHub Wiki

For detailed documentation, kindly refer to official libsodium.

Scalarmult in libsodium allows the developer to do 2 of the following functions..

  1. Allow the developer to compute an elliptic curve diffie hellman public key given an elliptic curve diffie hellman private key.
  2. Allow the developer to compute a shared secret by using different pair of public and private keys.

Generating elliptic curve diffie hellman public key given a private key

Initial Function

public static Byte[] Base(Byte[] CurrentUserSecretKey,Boolean ClearKey=false)

Example Code

RevampedKeyPair MyKeyPair = SodiumPublicKeyBox.GenerateRevampedKeyPair();
Byte[] PublicKey = SodiumScalarMult.Base(MyKeyPair.PrivateKey);
MessageBox.Show(PublicKey.SequenceEqual(MyKeyPair.PublicKey).ToString());

Calculating shared secret by using public key which originates from other private key

Initial Function

public static Byte[] Mult(Byte[] CurrentUserSecretKey,Byte[] OtherUserPublicKey,Boolean ClearKey=false)
public static IntPtr MultIntPtr(Byte[] CurrentUserSecretKey, Byte[] OtherUserPublicKey, Boolean ClearKey = false)

Example Code

RevampedKeyPair AliceKeyPair = SodiumPublicKeyBox.GenerateRevampedKeyPair();
RevampedKeyPair BobKeyPair = SodiumPublicKeyBox.GenerateRevampedKeyPair();
Byte[] SharedSecret1 = SodiumScalarMult.Mult(AliceKeyPair.PrivateKey, BobKeyPair.PublicKey);
IntPtr SharedSecret2IntPtr = SodiumScalarMult.MultIntPtr(BobKeyPair.PrivateKey, AliceKeyPair.PublicKey);
SodiumGuardedHeapAllocation.Sodium_MProtect_ReadWrite(SharedSecret2IntPtr);
Byte[] SharedSecret2 = new Byte[SharedSecret1.Length];
Marshal.Copy(SharedSecret2IntPtr, SharedSecret2, 0, SharedSecret1.Length);
SodiumGuardedHeapAllocation.Sodium_MProtect_NoAccess(SharedSecret2IntPtr);
MessageBox.Show(SharedSecret1.SequenceEqual(SharedSecret2).ToString());