Skip to content

perf(handshake): reuse the parsed X25519 private key - #40

Open
JkLondon wants to merge 1 commit into
XTLS:mainfrom
JkLondon:jklondon/reality-x25519
Open

JkLondon wants to merge 1 commit into
XTLS:mainfrom
JkLondon:jklondon/reality-x25519

Conversation

@JkLondon

@JkLondon JkLondon commented Sep 21, 2026

Copy link
Copy Markdown

Config.PrivateKey is now *ecdh.PrivateKey, parsed once by the caller. This avoids deriving the same static public key on every authentication attempt.

Auth DH: 58.08 → 29.13 µs, 7 → 3 allocs/op (Go 1.27.0, darwin/arm64, Apple M3 Max; 10 runs). Microbenchmark only.

Checked locally: race detector, amd64 correctness and FIPS enforcement.

@RPRX

RPRX commented Sep 21, 2026

Copy link
Copy Markdown
Member

Rebase,@yuhan6665 你看看

@JkLondon
JkLondon force-pushed the jklondon/reality-x25519 branch from 344f62e to 315178e Compare September 21, 2026 00:03
@JkLondon
JkLondon marked this pull request as ready for review September 21, 2026 00:05
@RPRX

RPRX commented Sep 21, 2026

Copy link
Copy Markdown
Member

需要再次 rebase,且我不确定这么搞是不是并发安全的,如果是的话就直接改接口吧别加锁了,test 文件删掉

顺便试试能不能 linkname #37 (comment) ,都放这个 PR 里吧

@yuhan6665

Copy link
Copy Markdown
Member

需要再次 rebase,且我不确定这么搞是不是并发安全的,如果是的话就直接改接口吧别加锁了,test 文件删掉

顺便试试能不能 linkname #37 (comment) ,都放这个 PR 里吧

跟原版比似乎少了一个copy 不知道会不会有问题?
https://cs.opensource.google/go/x/crypto/+/master:curve25519/curve25519.go;l=91

@RPRX

RPRX commented Sep 21, 2026

Copy link
Copy Markdown
Member

依稀记得不太并发安全,不然一开始就会这样写了,查一下吧,再让 AI 看看深层代码确认下,AI 最适合干这种事

@RPRX RPRX mentioned this pull request Sep 21, 2026
@Fangliding

Copy link
Copy Markdown
Member

个人感觉无所谓 在非瓶颈的路径上加缓存快点是快点但是终归是反模式 也就让握手快了 0.029 ms

@JkLondon
JkLondon force-pushed the jklondon/reality-x25519 branch 2 times, most recently from 9bcc460 to 160630b Compare September 21, 2026 07:43
`Config.PrivateKey` is now `*ecdh.PrivateKey`, parsed once by the caller. This avoids deriving the same static public key on every authentication attempt.

Auth DH: 58.08 → 29.13 µs, 7 → 3 allocs/op (Go 1.27.0, darwin/arm64, Apple M3 Max; 10 runs). Microbenchmark only.

Checked locally: race detector, amd64 correctness and FIPS enforcement.
@JkLondon
JkLondon force-pushed the jklondon/reality-x25519 branch from 160630b to 920c977 Compare September 21, 2026 08:00
@JkLondon JkLondon changed the title perf(handshake): cache the REALITY X25519 private key perf(handshake): reuse the parsed X25519 private key Sep 21, 2026
@JkLondon

JkLondon commented Sep 21, 2026

Copy link
Copy Markdown
Author

Thanks for the discussion) Sadly, I don't know Chinese but as I understand from the translation :

  1. According to the Go 1.27 X25519 implementation ECDH only reads and allocates a separate result for each call, so it's concurrently safe, I've done several tests, including race ones, all was ok.
  2. talking about perf point @Fangliding mentioned: I'm maintaining pretty large xray server when I pprof it I saw that X25519 accounted for 8.00% of process CPU, the repeated derivation alone accounted for 1.99%. So, from my side it would not be THAT significant increase of CPU perf but anyway worth trying.
  3. As @RPRX asked, I have deleted tests, and changed Config.PrivateKey to *ecdh.PrivateKey.

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.

4 participants