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
| Parameter | Purpose |
|---|---|
| $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
| Approach | Key distinction |
|---|---|
| sodium_crypto_scalarmult_base | sodium_crypto_scalarmult_base calculates the Curve25519 public point that corresponds to a raw private scalar. |
| Related technique | sodium_crypto_kx provides directional session keys with a clearer application-level protocol. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Confusing this operation with a neighboring cryptographic or security mechanism | sodium_crypto_kx provides directional session keys with a clearer application-level protocol. |
| Ignoring a security prerequisite or failure case | Use 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.