@oazmi/kitchensink - v0.10.1
    Preparing search index...

    Function curve25519ScalarMult

    • curve25519 scalar multiplication (via montgomery-ladder), implementing RFC7748.

      the implementation follows the pseudo code presented in wikipedia.

      Parameters

      • scalar: bigint

        your private key (must be clamped beforehand when being used for x25519 key generation).

      • basepoint: bigint = 9n

        the basepoint "common secret". this is typically 9n for x25519 key generation, and hence it is also the default value.

      Returns bigint

      the "public key" as a bigint.

      an "alice and bob" example but with bob replaced with "bobby", so that both names are 5 characters long.

      import { assertEquals } from "jsr:@std/assert"

      // the private keys of both parties
      const
      alice_private_key = 123n,
      bobby_private_key = 789n

      // both compute a public key, based on a globally agreed upon basekey (global common secret).
      // this "globally common secret" is `9n` for the `x25519` key-generation.
      const
      alice_public_key = curve25519ScalarMult(alice_private_key, 9n),
      bobby_public_key = curve25519ScalarMult(bobby_private_key, 9n)

      assertEquals(alice_public_key, 36775675751433867979441935675960806006270981998619265997805716105314274742222n)
      assertEquals(bobby_public_key, 4776225643423455257904064614728753759233983901518081694545578805283024213294n)

      // now, they both exchange their public keys, and then use it as the "basepoint" (a secret between the two),
      // to _derive_ a _common shared_ secret key, that is the same for both of them.
      const
      alice_shared_secret_key = curve25519ScalarMult(alice_private_key, bobby_public_key),
      bobby_shared_secret_key = curve25519ScalarMult(bobby_private_key, alice_public_key)

      assertEquals(alice_shared_secret_key, bobby_shared_secret_key)
      assertEquals(alice_shared_secret_key, 26692159771081237073471702917999531026498379047472371972701945697203840355541n)

      // notice that neither alice nor bobby had to exchange this secret key with one another;
      // they both just computed the same value due to the symmetric nature of `curve25519`.
      // if a third person, say cluky, were to intercept both public keys,
      // they won't be able to derive the same secret value that's between alice and bobby, no matter what they try,
      // unless they manage to get their hands on either one's private key.
      //
      // and so now, both alice and bobby can safely use this secret key to encrypt and decrypt each other's messages.
      // there are many encryption protocols, but a few common ones are: AES256, ChaCha20, HMAC, and the list goes.