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

crypto: use k256 to implement adaptor sig #43

Merged
merged 7 commits into from
Aug 27, 2024
Merged

Conversation

SebastianElvis
Copy link
Member

@SebastianElvis SebastianElvis commented Aug 23, 2024

Resolves #13

This PR replaces the usage of schnorr_fun and secp256kfun with k256 (which is native in CosmWasm) for implementing the adaptor signature verification. This gets rid of schnorr_fun and secp256kfun which can bloat the contract size.

This PR also runs cargo fix-lint to lint the entire project.

@SebastianElvis SebastianElvis marked this pull request as ready for review August 23, 2024 06:38
schnorr_fun = "0.10"
secp256kfun = { version = "0.10", default-features = false, features = ["libsecp_compat"] }
k256 = { version = "0.13.1", default-features = false, features = ["schnorr"]}
anybuf = { version = "0.5.0" }
Copy link
Contributor

Choose a reason for hiding this comment

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

:-( It's better with the other indendation IMO.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm it seems that Cursor enforces to align = in that way. Any ideas how to fix it?

Comment on lines +80 to +88
codegen-units = 1
debug = false
debug-assertions = false
lto = true
panic = 'abort'
rpath = false
opt-level = 3
overflow-checks = true
incremental = false
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏼

packages/btcstaking/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 46 to 47
fn bytes_to_point(bytes: &[u8]) -> Result<ProjectivePoint, String> {
let is_y_odd = bytes[0] == 0x03;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is considering only compressed points. It would be better to support both, compressed and uncompressed formats.

packages/btcstaking/src/asig.rs Outdated Show resolved Hide resolved
packages/btcstaking/src/asig.rs Outdated Show resolved Hide resolved
packages/btcstaking/src/asig.rs Outdated Show resolved Hide resolved
Comment on lines 35 to 43
// Adapted from https://github.com/RustCrypto/elliptic-curves/blob/520f67d26be1773bd600d05796cc26d797dd7182/k256/src/schnorr.rs#L181-L187
fn tagged_hash(tag: &[u8]) -> Sha256 {
let tag_hash = Sha256::digest(tag);
let mut digest = Sha256::new();
// The hash is in sha256d, so we need to hash twice
digest.update(tag_hash);
digest.update(tag_hash);
digest
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this function already there in another file / module?

Comment on lines 45 to 47
#[allow(non_snake_case)]
fn bytes_to_point(bytes: &[u8]) -> Result<ProjectivePoint, String> {
let is_y_odd = bytes[0] == 0x03;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, isn't this duplicated somewhere? it would be nice to have a sig_utils module or similar to put these there and avoid duplication.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. Exported tagged_hash in eots and let btcstaking module use that. Re bytes_to_point this function makes a lot of assumptions (e.g., bytes are from a compressed point) and is only used for serialising pubkey in staking tx scripts. I would suggest keeping it private here. Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK with me. At some point it would be nice to remove those assumptions, that is, have a conversion that works for all kinds of point formats. Tried to do that at some point, to no avail.

Copy link
Contributor

@maurolacy maurolacy left a comment

Choose a reason for hiding this comment

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

LGTM.

@SebastianElvis SebastianElvis merged commit a470519 into main Aug 27, 2024
1 check passed
@SebastianElvis SebastianElvis deleted the fix-adaptor-sig branch August 27, 2024 06:57
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.

crypto: implement adaptor signature using k256
2 participants