sodium_crypto_scalarmult calculates a Curve25519 scalar product using a private scalar and a validated peer public point.
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 calculates a Curve25519 scalar product using a private scalar and a validated peer public point.
The sodium extension must be enabled.
Syntax and Return Value
sodium_crypto_scalarmult(string $n, string $p): string
| Parameter | Purpose |
|---|---|
| $n (string) | Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling. |
| $p (string) | Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling. |
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($scalar, $peer);
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($scalar, $peer);
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($scalar, $peer);
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 | sodium_crypto_scalarmult calculates a Curve25519 scalar product using a private scalar and a validated peer public point. |
| 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 calculates a Curve25519 scalar product using a private scalar and a validated peer public point.
- 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.