Repository navigation
fix(crypto): add explicit key copy method - #3600
Conversation
|
/review |
|
OpenCodeReview · Complete review: 2 of 2 selected files covered 0 findings. Review infoReviewed commits:
Model: |
|
@greptileai review this pr |
|
[Medium impact] The PR appears safe to merge; no new actionable issue was found. Reviews (10) · Last reviewed commit: "Merge branch 'master' into keys-fix-copy" · Reviewed by Greptile |
|
@greptileai review this pr |
|
/review |
|
OpenCodeReview · Complete review: 2 of 2 selected files covered 5 findings.
Review infoReviewed commits:
Model: |
|
/review |
|
@greptileai review this pr |
|
OpenCodeReview · Complete review: 2 of 2 selected files covered 4 findings.
Review infoReviewed commits:
Model: |
|
/review |
|
OpenCodeReview · Complete review: 2 of 2 selected files covered 3 findings.
Review infoReviewed commits:
Model: |
There was a problem hiding this comment.
🔵 Needs a closer look
The custom sink hooks destroy and bitwise-transfer variant objects containing managed references. Their ownership accounting and replacement behavior still need runtime validation under both refc and ORC, which could not be performed in this review environment.
No open issues 🎉
What changed in this PR
Gives RSA and ECDSA key wrappers independent copy semantics, preventing mutations in one copy from affecting another and aligning them with Ed25519 and Secp256k1.
Changes:
- Adds deep-copy hooks for public and private keys.
- Adds move hooks that transfer ownership without cloning.
- Adds tests for copy independence, moves, empty keys, cross-scheme assignments, and invalid ECDSA copies.
| File | Description |
|---|---|
| tests/libp2p/crypto/test_crypto.nim | Adds regression coverage for key copy and move semantics. |
| libp2p/crypto/crypto.nim | Implements independent copies and ownership-preserving moves for key wrappers. |
Review effort: Balanced
richard-ramos
left a comment
There was a problem hiding this comment.
I'm not sure we need this. Nothing in the library changes or clears a PublicKey/PrivateKey after it is created, so in practice the keys are immutable. Sharing them only matters if someoen writes into rsakey.buffer or eckey.buffer or calls rsa.clear or ecnist.clear on the inner key, and that would mean using internals
also, in minor importance, there is also a privacy cost associated to it: for keys, sharing the buffer can actually be better, because with one shared buffer, clearing it wipes every copy. With this PR each copy holds its own secret data, and dropped copies are not wiped
https://github.com/iftech/nim-libp2p/pull/3599/changes#diff-7df9e19b777d6d0bcd42fcc398e8edad45336a20aa9be5ee131075d216d28f71R31-R38 but in general all downstream dep can use keys, they might need copying. and regardless of it is used or not, code should be better if it behaves the same way for types of keys. |
42588c4 to
5ed4038
Compare
|
/review |
|
@greptileai review this pr |
|
OpenCodeReview · Complete review: 2 of 2 selected files covered 0 findings. Review infoReviewed commits:
Model: |
There was a problem hiding this comment.
🟡 Changes recommended
In filtered builds, the value-copy fast path accepts disabled Ed25519/Secp256k1 schemes and returns successful but unusable keys instead of SchemeError. Separately, the RSA copy tests mutate a DER tag that serialization rebuilds rather than actual key material, so they do not demonstrate independence between copies and their sources.
2 open issues
Review effort: Balanced
|
/review |
|
OpenCodeReview · Complete review: 2 of 2 selected files covered 0 findings. Review infoReviewed commits:
Model: |

adds an explicit copy method for public and private keys.
RSAandECDSAkeys are copied through serialization so the result has independent key material. The method returns an error if copying fails.Ed25519andSecp256k1keys are copied by value.