Content · Programming · PHP

PHP sodium_crypto_scalarmult_base() Function: Usage and Examples

sodium_crypto_scalarmult_base calculates the Curve25519 public point that corresponds to a raw private.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

sodium_crypto_scalarmult_base calculates the Curve25519 public point that corresponds to a raw private scalar.

The examples explain the data format, expected results and the difference between this API and nearby cryptographic operations.

What It Does


The operation is intended for: sodium_crypto_scalarmult_base calculates the Curve25519 public point that corresponds to a raw private scalar.

The sodium extension must be enabled.

Syntax and Return Value


sodium_crypto_scalarmult_base(string $secret_key): string
ParameterPurpose
$secret_key (string)A private secret key or cryptographic keypair, as required by the function.

The declared return type is shown above. Errors can include false results or exceptions depending on this API: Use valid key material, validate peers, and pass a raw shared secret through an appropriate KDF before using it as an encryption key.

Example 1: Basic Operation


The result is raw group data. Do not use it as a session key without a suitable key-derivation protocol.

<?php
$scalar = random_bytes(SODIUM_CRYPTO_SCALARMULT_SCALARBYTES);
$peer = sodium_crypto_scalarmult_base(random_bytes(SODIUM_CRYPTO_SCALARMULT_SCALARBYTES));
$result = sodium_crypto_scalarmult_base($scalar);
echo strlen($result);

Example 2: A Different Use Case


Check the curve-point output size or its non-secret test fingerprint.

<?php
// Check the alternative case.
$scalar = random_bytes(SODIUM_CRYPTO_SCALARMULT_SCALARBYTES);
$peer = sodium_crypto_scalarmult_base(random_bytes(SODIUM_CRYPTO_SCALARMULT_SCALARBYTES));
$result = sodium_crypto_scalarmult_base($scalar);
var_dump(strlen($result) === 32);

Practical Example: Checking the Result


Check the curve-point output size or its non-secret test fingerprint.

<?php
// Verify the expected behavior before using results.
$scalar = random_bytes(SODIUM_CRYPTO_SCALARMULT_SCALARBYTES);
$peer = sodium_crypto_scalarmult_base(random_bytes(SODIUM_CRYPTO_SCALARMULT_SCALARBYTES));
$result = sodium_crypto_scalarmult_base($scalar);
echo bin2hex(hash("sha256", $result, true));

Important Behavior and Edge Cases


Use valid key material, validate peers, and pass a raw shared secret through an appropriate KDF before using it as an encryption key.

Use valid key material, validate peers, and pass a raw shared secret through an appropriate KDF before using it as an encryption key.

Comparison with Related Functions


ApproachKey distinction
sodium_crypto_scalarmult_basesodium_crypto_scalarmult_base calculates the Curve25519 public point that corresponds to a raw private scalar.
Related techniquesodium_crypto_kx provides directional session keys with a clearer application-level protocol.

Common Beginner Mistakes


MistakeCorrect approach
Confusing this operation with a neighboring cryptographic or security mechanismsodium_crypto_kx provides directional session keys with a clearer application-level protocol.
Ignoring a security prerequisite or failure caseUse valid key material, validate peers, and pass a raw shared secret through an appropriate KDF before using it as an encryption key.

Key Takeaways


  • sodium_crypto_scalarmult_base calculates the Curve25519 public point that corresponds to a raw private scalar.
  • Use valid key material, validate peers, and pass a raw shared secret through an appropriate KDF before using it as an encryption key.
  • sodium_crypto_kx provides directional session keys with a clearer application-level protocol.

Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.

Share this resourceSend this page to someone who may find it useful.