Skip to content

fix(crypto): add explicit key copy method - #3600

Merged
vladopajic merged 5 commits into
masterfrom
keys-fix-copy
Dec 4, 2026
Merged

vladopajic merged 5 commits into
masterfrom
keys-fix-copy

Conversation

@vladopajic

@vladopajic vladopajic commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

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

Ed25519 and Secp256k1 keys are copied by value.

@vladopajic

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

OpenCodeReview · Complete review: 2 of 2 selected files covered

0 findings.

Review info

Reviewed commits: 8220e58816c6e065362546ecb25a2d1a30f2b9f3 → 47c357275d6c8b8b850dd23b0749b0a72237c6a6

File Result
libp2p/crypto/crypto.nim Reviewed
tests/libp2p/crypto/test_crypto.nim Reviewed

Model: qwen4-max
Review generated in: 6m 5s

@vladopajic
vladopajic marked this pull request as ready for review December 3, 2026 11:33
@vladopajic
vladopajic requested review from a team, gmelodie and richard-ramos December 3, 2026 11:33
@vladopajic

Copy link
Copy Markdown
Member Author

@greptileai review this pr

@vladopajic
vladopajic enabled auto-merge December 3, 2026 11:34
@greptile-apps

greptile-apps Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

[Medium impact] The PR appears safe to merge; no new actionable issue was found.

RetriggerView in Greptile

Reviews (10) · Last reviewed commit: "Merge branch 'master' into keys-fix-copy" · Reviewed by Greptile

Comment thread tests/libp2p/crypto/test_crypto.nim Resolved
@vladopajic

Copy link
Copy Markdown
Member Author

@greptileai review this pr

@vladopajic

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

OpenCodeReview · Complete review: 2 of 2 selected files covered

5 findings.

  • Successfully posted inline: 5 comment(s)
Review info

Reviewed commits: 8220e58816c6e065362546ecb25a2d1a30f2b9f3 → d2c2b70d67aaae5efcb359e271134dfa4b39fd10

File Result
libp2p/crypto/crypto.nim Reviewed
tests/libp2p/crypto/test_crypto.nim Reviewed

Model: qwen4-max
Review generated in: 8m 23s

Comment thread libp2p/crypto/crypto.nim Resolved
Comment thread libp2p/crypto/crypto.nim Resolved
Comment thread tests/libp2p/crypto/test_crypto.nim Resolved
Comment thread tests/libp2p/crypto/test_crypto.nim Resolved
Comment thread tests/libp2p/crypto/test_crypto.nim Resolved
@vladopajic

Copy link
Copy Markdown
Member Author

/review

@vladopajic

Copy link
Copy Markdown
Member Author

@greptileai review this pr

Comment thread tests/libp2p/crypto/test_crypto.nim Resolved
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

OpenCodeReview · Complete review: 2 of 2 selected files covered

4 findings.

  • Successfully posted inline: 4 comment(s)
Review info

Reviewed commits: 8220e58816c6e065362546ecb25a2d1a30f2b9f3 → 283414910521e2cac22985e819ab44d5bbda6fc2

File Result
libp2p/crypto/crypto.nim Reviewed
tests/libp2p/crypto/test_crypto.nim Reviewed

Model: qwen4-max
Review generated in: 7m 44s

Comment thread libp2p/crypto/crypto.nim Resolved
Comment thread libp2p/crypto/crypto.nim Resolved
Comment thread libp2p/crypto/crypto.nim Resolved
Comment thread libp2p/crypto/crypto.nim Resolved
@vladopajic

Copy link
Copy Markdown
Member Author

/review

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

OpenCodeReview · Complete review: 2 of 2 selected files covered

3 findings.

  • Successfully posted inline: 3 comment(s)
Review info

Reviewed commits: 8220e58816c6e065362546ecb25a2d1a30f2b9f3 → a3869f91b12c5f37b8ba80b54772c4a3830680d8

File Result
libp2p/crypto/crypto.nim Reviewed
tests/libp2p/crypto/test_crypto.nim Reviewed

Model: qwen4-max
Review generated in: 9m 48s

Comment thread libp2p/crypto/crypto.nim Resolved
Comment thread libp2p/crypto/crypto.nim Resolved
Comment thread tests/libp2p/crypto/test_crypto.nim Resolved
Comment thread libp2p/crypto/crypto.nim Resolved
@github-project-automation github-project-automation Bot moved this from new to In Progress in nim-libp2p Dec 3, 2026
@richard-ramos
richard-ramos requested a balanced review from Copilot December 3, 2026 12:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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 richard-ramos left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread libp2p/crypto/crypto.nim Resolved
@vladopajic

Copy link
Copy Markdown
Member Author

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

https://github.com/iftech/nim-libp2p/pull/3599/changes#diff-7df9e19b777d6d0bcd42fcc398e8edad45336a20aa9be5ee131075d216d28f71R31-R38
copy is needed ^there now.

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.

@vladopajic vladopajic changed the title fix(crypto): give RSA and ECDSA key wrappers independent copy semantics fix(crypto): add explicit key copy method Dec 3, 2026
@vladopajic

Copy link
Copy Markdown
Member Author

/review

@vladopajic
vladopajic requested a balanced review from Copilot December 3, 2026 15:40
@vladopajic

Copy link
Copy Markdown
Member Author

@greptileai review this pr

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

OpenCodeReview · Complete review: 2 of 2 selected files covered

0 findings.

Review info

Reviewed commits: 8220e58816c6e065362546ecb25a2d1a30f2b9f3 → 2c25849573639e615880347329dd39c28e714cc7

File Result
libp2p/crypto/crypto.nim Reviewed
tests/libp2p/crypto/test_crypto.nim Reviewed

Model: qwen4-max
Review generated in: 3m 8s

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread libp2p/crypto/crypto.nim Resolved
Comment thread tests/libp2p/crypto/test_crypto.nim Resolved
@vladopajic

Copy link
Copy Markdown
Member Author

/review

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No actionable issues were found in the reviewed crypto key-copying changes and associated tests.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

OpenCodeReview · Complete review: 2 of 2 selected files covered

0 findings.

Review info

Reviewed commits: 8220e58816c6e065362546ecb25a2d1a30f2b9f3 → 3ba3e5f317308a8874843f707b26d57599e7968c

File Result
libp2p/crypto/crypto.nim Reviewed
tests/libp2p/crypto/test_crypto.nim Reviewed

Model: qwen4-max
Review generated in: 5m 43s

@vladopajic
vladopajic added this pull request to the merge queue Dec 4, 2026
Merged via the queue into master with commit 1b9ed45 Dec 4, 2026
47 checks passed
@vladopajic
vladopajic deleted the keys-fix-copy branch December 4, 2026 15:08
@github-project-automation github-project-automation Bot moved this from In Progress to done in nim-libp2p Dec 4, 2026
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