Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace rust_secp256k1 with k256 #3407

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Replace rust_secp256k1 with k256 #3407

wants to merge 5 commits into from

Conversation

deuszx
Copy link
Contributor

@deuszx deuszx commented Feb 25, 2025

Motivation

rust_secp256k1 did not work well with Wasm.

Proposal

Replace with k256.

Test Plan

CI should catch any regressions and @Twey will check if this indeed works with web client.

Release Plan

  • Nothing to do / These changes follow the usual release cycle.

Links

@deuszx deuszx force-pushed the secp256k1-use-k256 branch 4 times, most recently from f868144 to 4cf8f77 Compare February 25, 2025 11:58
@deuszx deuszx marked this pull request as ready for review February 25, 2025 12:22
@deuszx deuszx requested review from afck and Twey February 25, 2025 12:22
let message = Message::from_digest(CryptoHash::new(value).as_bytes().0);
let signature = secp.sign_ecdsa(&message, &secret.0);
let prehash = CryptoHash::new(value).as_bytes().0;
let (signature, _rid) = secret.0.sign_prehash_recoverable(&prehash).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.

TODO: @deuszx : fix 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.

Working on this right now. Simply changing this to return Result<Self, Error> affects a lot of the code - LiveVote/Vote creation etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After talking to @afck we decided that it's "ok" to panic here. From reading the code it looks like the sources of the errors here are not controllable by us (like a wrong choice of k for the secp256k1 signing algorithm, which is an internal details of k256 anyway) and would most likely either lead to all signature creation fail or would be non-deterministically unsafe.

@deuszx deuszx force-pushed the secp256k1-use-k256 branch from 26fcca2 to 72bc4a9 Compare February 25, 2025 13:45
@deuszx deuszx force-pushed the secp256k1-use-k256 branch from 72bc4a9 to 393f808 Compare February 25, 2025 13:53
Comment on lines 63 to 64
#[error("Could not parse public key: {0}. Public key point at inifinity.")]
Secp256k1PointAtIfinity(String),
Copy link
Contributor

@Twey Twey Feb 25, 2025

Choose a reason for hiding this comment

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

Suggested change
#[error("Could not parse public key: {0}. Public key point at inifinity.")]
Secp256k1PointAtIfinity(String),
#[error("could not parse public key `{0}`: point at infinity")]
Secp256k1PointAtInfinity(String),

Fixing some typos, and error message following the documentation of the Error trait:

Error messages are typically concise lowercase sentences without trailing punctuation:

let err = "NaN".parse::<u32>().unwrap_err();
assert_eq!(err.to_string(), "invalid digit found in string");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will this work in non-std?

Copy link
Contributor

Choose a reason for hiding this comment

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

Additionally, as a nit, I'd suggest that the context of ‘what we were trying to do when the error happened’ (parsing public key {0}) should be handled by thiserror-context rather than encoding every path to the error into the error enum.

Copy link
Contributor

@Twey Twey Feb 25, 2025

Choose a reason for hiding this comment

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

Will this work in non-std?

(assuming this is with regards to thiserror-context — sorry, I rearranged my comments for clarity)
thiserror already doesn't work in nostd. We don't use nostd anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

.context("") works only on anyhow errors though, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

thiserror-context is a crate you can use to wrap your errors into a type that supports collecting error context in the same way as anyhow::Context.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That requires adding new dependency to linera-base. What is this improving?

@Twey
Copy link
Contributor

Twey commented Feb 25, 2025

Modulo one non-nit comment I'm happy with this, but I'd appreciate a review from someone more familiar with elliptic curve cryptography.

@Twey Twey requested a review from ma2bd February 25, 2025 17:23
Comment on lines +62 to +64
Secp256k1Error(k256::ecdsa::Error),
#[error("could not parse public key: {0}: key point at inifinity.")]
Secp256k1PointAtIfinity(String),
Copy link
Contributor

@Twey Twey Feb 25, 2025

Choose a reason for hiding this comment

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

Suggested change
Secp256k1Error(k256::ecdsa::Error),
#[error("could not parse public key: {0}: key point at inifinity.")]
Secp256k1PointAtIfinity(String),
#[error("could not parse public key `{0}`: point at infinity")]
Secp256k1PointAtInfinity(String),

(re-commenting suggestion since it was partially applied, which breaks the accept feature)

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.

2 participants