Skip to content

Optimize curve25519 field element by removing array - #198

Merged
paragonie-security merged 1 commit into
masterfrom
optimizations
Oct 4, 2025
Merged

paragonie-security merged 1 commit into
masterfrom
optimizations

Conversation

@paragonie-security

Copy link
Copy Markdown
Contributor

By leveraging PHP's optimized object key access for what is effectively always a fixed-length array, we can remove some computational overhead and memory usage in by the PHP interpreter.

Since these memory access patterns are not secret (they were array indices from 0 to 9), the risk of side-channel leakage by this change is at least as low as what was previously implemented.

Unit Test Suite Runtime Ccomparison

CI Run PHP 8.1 PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
This Pull Request Time: 00:28.808, Memory: 22.00 MB Time: 00:28.185, Memory: 20.00 MB Time: 00:28.469, Memory: 20.00 MB Time: 00:29.143, Memory: 20.00 MB Time: 00:28.431, Memory: 18.00 MB
CI for v2.3.1 Time: 00:36.502, Memory: 22.00 MB Time: 00:33.545, Memory: 20.00 MB Time: 00:33.295, Memory: 20.00 MB Time: 00:32.855, Memory: 20.00 MB Time: 00:34.210, Memory: 18.00 MB
CI for v2.3.0 Time: 00:36.373, Memory: 22.00 MB Time: 00:32.035, Memory: 20.00 MB Time: 00:33.618, Memory: 20.00 MB Time: 00:32.713, Memory: 20.00 MB Time: 00:33.804, Memory: 18.00 MB

I need to backport this change to v1.x and see if we get similar performance wins from the same strategy there (without sacrificing PHP 5.2 support) before I merge this pull request.

By leveraging PHP's optimized object key access for what is effectively always a fixed-length array, we can remove some computational overhead and memory usage in by the PHP interpreter.

Since these memory access patterns are not secret (they were array indices from 0 to 9), the risk of side-channel leakage by this change is at least as low as what was previously implemented.
paragonie-security added a commit that referenced this pull request Oct 4, 2025
By leveraging PHP's optimized object key access for what is effectively always a fixed-length array, we can remove some computational overhead and memory usage in by the PHP interpreter.

Since these memory access patterns are not secret (they were array indices from 0 to 9), the risk of side-channel leakage by this change is at least as low as what was previously implemented.

This is a backport of #198 to the v1.x branch. It's not a 1:1 copy, as some code style changes occur in PHP 8.1+.
@paragonie-security

Copy link
Copy Markdown
Contributor Author

The performance results from #199 are also telling:

PHP Version #199 v1.22.0 v1.21.1
5.3 1.92 minutes 2.11 minutes 2.12 minutes
5.4 1.54 minutes 1.8 minutes 1.71 minutes
5.5 1.33 minutes 1.64 minutes 1.64 minutes
5.6 1.24 minutes 1.54 minutes 1.53 minutes
7.0 38.43 seconds 46.38 seconds 44.93 seconds
7.1 31.86 seconds 36.24 seconds 35.78 seconds
7.2 41.27 seconds 45.9 seconds 45.77 seconds
7.3 00:41.152 00:45.740 00:46.385
7.4 00:39.549 00:43.711 00:45.834
8.0 00:34.710 00:41.857 00:39.286
8.1 00:31.464 00:38.130 00:38.373
8.2 00:31.056 00:34.393 00:33.866
8.3 00:33.630 00:37.106 00:36.239
8.4 00:32.698 00:34.855 00:35.049
8.5 00:32.682 00:34.838 00:37.018

paragonie-security added a commit that referenced this pull request Oct 4, 2025
@paragonie-security
paragonie-security merged commit 27f0b05 into master Oct 4, 2025
7 of 8 checks passed
@paragonie-security
paragonie-security deleted the optimizations branch October 4, 2025 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant