Skip to content
This repository has been archived by the owner on Oct 17, 2022. It is now read-only.

crypto: replace ed25519-dalek with ed25519-consensus #624

Merged
merged 3 commits into from
Aug 4, 2022

Conversation

joyqvq
Copy link
Contributor

@joyqvq joyqvq commented Jul 29, 2022

@joyqvq joyqvq requested a review from punwai July 29, 2022 18:41
crypto/src/ed25519.rs Outdated Show resolved Hide resolved
@@ -4,7 +4,7 @@ expression: committee
---
{
"authorities": {
"IP26ybELdYe7p7W8FjvOaeeW1x5O1EwQ/LRIhon3oUQ=": {
"ildi4hrBzbOHBELHe0w69Yx87bh3nQJw5tTx4vc2fXQ=": {
Copy link
Contributor Author

@joyqvq joyqvq Jul 30, 2022

Choose a reason for hiding this comment

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

this is expected as rand 0.8.5 uses ChaChaRng for 12 rounds whereas rand 0.7.3 uses 20 rounds with reference to rust-random/rand#932

from 0.7.3:

/// The current algorithm used is the ChaCha block cipher with 20 rounds.
/// This may change as new evidence of cipher security and performance
/// becomes available.

from 0.8.5:

/// The current algorithm used is the ChaCha block cipher with 12 rounds. Please
/// see this relevant [rand issue] for the discussion. This may change as new 
/// evidence of cipher security and performance becomes available.

/// [rand issue]: https://github.com/rust-random/rand/issues/932

i can't think of a way to preserve the key files as the upgrade would affect all rand lib usage in narwhal

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the context! I expect this would change procedurally generated keys / formats only, correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, only for the generate function in KeyPair trait that takes in the Rng

}
}

impl Serialize for Ed25519Signature {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

as discussed serde_as would produce {"sig": base64_encoded_string} instead of a flattened base64_encoded_string, removed related functions in serder_helper

crypto/src/ed25519.rs Show resolved Hide resolved
@@ -41,7 +41,7 @@ fn serialize_deserialize() {

let private_key = kpref.private();
let bytes = bincode::serialize(&private_key).unwrap();
let privkey = bincode::deserialize::<Ed25519PublicKey>(&bytes).unwrap();
let privkey = bincode::deserialize::<Ed25519PrivateKey>(&bytes).unwrap();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i dont know why this test passed on main branch.

Copy link
Contributor

Choose a reason for hiding this comment

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

It is indeed worrrisome, I suspect an abuse of ToFromBytes since ed25519-dalek and ed25519-consensus share the same underlying EdwardsPoint representation for the decompressed form of the key. Could we add a test for deserializing Ed25519PublicKey with a point known to not be on curve?

@@ -43,8 +43,6 @@ Committee:
- epoch: U64
Ed25519PublicKey:
NEWTYPESTRUCT: STR
Ed25519Signature:
NEWTYPESTRUCT: BYTES
Copy link
Contributor Author

Choose a reason for hiding this comment

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

is there any downstream impact on this file? it seems like just a documentation

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it lets us set up the binary representation manifest for our project. In fact, this makes me think we should open an issue to inspect the key types and use serde annotations to make sure the OnceCell is elided when serializing, and reset when deserializing. We should open an issue.

@joyqvq joyqvq requested review from mwtian and removed request for punwai July 30, 2022 17:27
@joyqvq joyqvq requested a review from punwai July 31, 2022 20:40
@joyqvq joyqvq force-pushed the use-25519-consensus branch 3 times, most recently from 9f77c69 to 96cd44a Compare August 1, 2022 21:19
Copy link
Contributor

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

Thanks @joyqvq ! This looks good, I just have 3 questions:

  1. Is this change sufficient to fix Use input Rng across all KeyPair impl #544 as well?
  2. Can we write one negative test for invalid EdDSA serialization? The full issue is #500, no need to solve it, I just want to see a non-regression on the unit test you fix: you fix the code, I would like to see the non-fixed code fail.
  3. We should open an issue to inspect our now pervasive use of OnceCell in key/signature material and make sure it complies with serde best practices.

@@ -4,7 +4,7 @@ expression: committee
---
{
"authorities": {
"IP26ybELdYe7p7W8FjvOaeeW1x5O1EwQ/LRIhon3oUQ=": {
"ildi4hrBzbOHBELHe0w69Yx87bh3nQJw5tTx4vc2fXQ=": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the context! I expect this would change procedurally generated keys / formats only, correct?

@@ -41,7 +41,7 @@ fn serialize_deserialize() {

let private_key = kpref.private();
let bytes = bincode::serialize(&private_key).unwrap();
let privkey = bincode::deserialize::<Ed25519PublicKey>(&bytes).unwrap();
let privkey = bincode::deserialize::<Ed25519PrivateKey>(&bytes).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

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

It is indeed worrrisome, I suspect an abuse of ToFromBytes since ed25519-dalek and ed25519-consensus share the same underlying EdwardsPoint representation for the decompressed form of the key. Could we add a test for deserializing Ed25519PublicKey with a point known to not be on curve?

@@ -43,8 +43,6 @@ Committee:
- epoch: U64
Ed25519PublicKey:
NEWTYPESTRUCT: STR
Ed25519Signature:
NEWTYPESTRUCT: BYTES
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it lets us set up the binary representation manifest for our project. In fact, this makes me think we should open an issue to inspect the key types and use serde annotations to make sure the OnceCell is elided when serializing, and reset when deserializing. We should open an issue.

@joyqvq
Copy link
Contributor Author

joyqvq commented Aug 4, 2022

Thanks @joyqvq ! This looks good, I just have 3 questions:

  1. Is this change sufficient to fix Use input Rng across all KeyPair impl #544 as well?
  2. Can we write one negative test for invalid EdDSA serialization? The full issue is [cryptography] Integrate Wycheproof tests in unit testing fastcrypto#15, no need to solve it, I just want to see a non-regression on the unit test you fix: you fix the code, I would like to see the non-fixed code fail.
  3. We should open an issue to inspect our now pervasive use of OnceCell in key/signature material and make sure it complies with serde best practices.
  1. yes - although i only touched ed25519 files to keep the scope for review, will follow up a diff to upgrade rand across the board and eliminate the workaround
  2. 👍 good point, test added, will pick up full issue next
  3. opened

@joyqvq joyqvq requested a review from huitseeker August 4, 2022 02:50
Copy link
Contributor

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

Tip top! thanks a bunch, @joyqvq !

@joyqvq joyqvq merged commit f067539 into main Aug 4, 2022
@joyqvq joyqvq deleted the use-25519-consensus branch August 4, 2022 13:46
huitseeker pushed a commit that referenced this pull request Aug 5, 2022
* crypto: replace ed25519-dalek with ed25519-consensus

* cargo xclippy and hakari generate

* serialize test negative case
mwtian pushed a commit to mwtian/sui that referenced this pull request Sep 30, 2022
…hal#624)

* crypto: replace ed25519-dalek with ed25519-consensus

* cargo xclippy and hakari generate

* serialize test negative case
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants