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: Add r1 ecrecover and verify to Move #7773

Merged
merged 2 commits into from
Feb 21, 2023
Merged

crypto: Add r1 ecrecover and verify to Move #7773

merged 2 commits into from
Feb 21, 2023

Conversation

joyqvq
Copy link
Contributor

@joyqvq joyqvq commented Jan 27, 2023

What changed

ecdsa_k1::ecrecover(sig, hashed_msg, hash_function) -> ecdsa_k1::secp256k1_ecrecover(sig, msg, hash_function)
ecdsa_k1::secp256k1_verify(sig, pk, hashed_msg) -> ecdsa_k1::secp256k1_verify(sig, pk, msg, hash_function)
new: ecdsa_r1::secp256k1_ecrecover(sig, msg, hash_function)
new: ecdsa_r1::secp256k1_verify(sig, pk, msg)

See md file for detailed descriptiosn.

What to do

Caller of these APIs required to provide the raw message instead of the hashed message, and provide the hash_function name (represented by u8, move does not have a concept of enum)

@vercel
Copy link

vercel bot commented Jan 27, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
explorer ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 21, 2023 at 4:50PM (UTC)
explorer-storybook ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 21, 2023 at 4:50PM (UTC)
frenemies ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 21, 2023 at 4:50PM (UTC)
wallet-adapter ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 21, 2023 at 4:50PM (UTC)

@joyqvq joyqvq mentioned this pull request Jan 27, 2023
3 tasks
@joyqvq joyqvq force-pushed the updatefc branch 4 times, most recently from e558a0f to 9b0956a Compare January 30, 2023 16:31
Base automatically changed from updatefc to main January 30, 2023 17:13
joyqvq added a commit that referenced this pull request Jan 30, 2023
)

## What Changed

- For CLI and SDK, a ECDSA k1 and r1 signature is produced using the
nonrecoverable form. This means the signature is 64 bytes instead of 65.
- The signature verification in sui also uses the nonrecoverable option.
A valid signature should have 64 bytes.
- For wallet, since only Ed25519 is supported, the secp256k1 change
should not affect.
- Also exposes secp256k1_verify and secp256k1_verify_recoverable API in
move.
- Ser/de of public keys and signatures now uses the most compact
serialization with ToFromBytes.

## What Do You Need To Do
- If you are using SDK to produce a Secp256k1 signature, no change is
needed as long as you are using the latest version.
- If you are using something else to produce a signature, your old
signature will not be considered valid. You should just need to remove
the last byte (65->64 bytes) to make it a valid signature again.

Next:
- r1 verify and verify_recoverable added in
#7773
@joyqvq joyqvq force-pushed the r1-move branch 2 times, most recently from bb9bcbb to b10895f Compare January 30, 2023 18:50
@joyqvq joyqvq marked this pull request as ready for review January 30, 2023 18:54
@joyqvq joyqvq requested review from jonas-lj and kchalkias January 30, 2023 18:55
Ok(NativeResult::ok(cost, smallvec![Value::bool(result)]))
}

pub fn secp256r1_verify_recoverable(
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's the difference between this and the above function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the above verifies a nonrecoverable sig (64 bytes) vs this one verifies a recoverable one (65 bytes). they are documented at the move caller function signature instead of here since that's where the doc is generated.

Copy link
Contributor

Choose a reason for hiding this comment

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

The verify function will recover the public key and compare it with the given public key, so if we want it this function, it could also be done using Move.

williampsmith pushed a commit that referenced this pull request Feb 3, 2023
)

## What Changed

- For CLI and SDK, a ECDSA k1 and r1 signature is produced using the
nonrecoverable form. This means the signature is 64 bytes instead of 65.
- The signature verification in sui also uses the nonrecoverable option.
A valid signature should have 64 bytes.
- For wallet, since only Ed25519 is supported, the secp256k1 change
should not affect.
- Also exposes secp256k1_verify and secp256k1_verify_recoverable API in
move.
- Ser/de of public keys and signatures now uses the most compact
serialization with ToFromBytes.

## What Do You Need To Do
- If you are using SDK to produce a Secp256k1 signature, no change is
needed as long as you are using the latest version.
- If you are using something else to produce a signature, your old
signature will not be considered valid. You should just need to remove
the last byte (65->64 bytes) to make it a valid signature again.

Next:
- r1 verify and verify_recoverable added in
#7773
@joyqvq joyqvq force-pushed the r1-move branch 2 times, most recently from 586bd00 to cb815fd Compare February 9, 2023 00:49
@joyqvq joyqvq requested a review from kchalkias February 9, 2023 00:49
@joyqvq joyqvq force-pushed the r1-move branch 2 times, most recently from 79afa4c to d1d5c4a Compare February 9, 2023 01:33
Err(_) => return Ok(NativeResult::ok(cost, smallvec![Value::bool(false)])),
};

let result = public_key.verify(&hashed_msg_ref, &signature).is_ok();
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that when fastcrypto is updated to most recent version, this should be verify_hashed instead of verify.

@joyqvq joyqvq requested a review from jonas-lj February 17, 2023 16:32
@joyqvq joyqvq changed the title cryto: Add r1 verify and verify_recoverable to Move crypto: Add r1 verify and verify_recoverable to Move Feb 17, 2023
@joyqvq joyqvq changed the title crypto: Add r1 verify and verify_recoverable to Move crypto: Add r1 ecrecover and verify to Move Feb 17, 2023
@joyqvq joyqvq requested a review from benr-ml February 17, 2023 16:51

pub const FAIL_TO_RECOVER_PUBKEY: u64 = 0;
pub const INVALID_SIGNATURE: u64 = 1;
pub const INVALID_PUBKEY: u64 = 2;

pub const KECCAK256: u8 = 3;
Copy link
Contributor

Choose a reason for hiding this comment

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

why does it start at 3?

Copy link
Collaborator

@kchalkias kchalkias left a comment

Choose a reason for hiding this comment

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

Overall LGTM, I'd prefer we have a common place where we access the hash flags from different functions (so this gets reusable).

const EInvalidSignature: u64 = 1;

/// Hash function name that are valid for ecrecover and secp256k1_verify.
const KECCAK256: u8 = 3;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should have a common shared list somewhere and document why KECCAK256: u8 = 3; sha256 = 4 etc (btw it doesn't matter if they collide with error codes (ie starting from zero), these flags serve a different role). Can we share these flags between k1 and r1 (instead of rewriting them?)

Copy link
Contributor Author

@joyqvq joyqvq Feb 21, 2023

Choose a reason for hiding this comment

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

reason why I had them here is bc they are accessible for ppl using these contract to easily find the definition. i can put this in hash.move, and link them in ecdsa_k1 and ecdsa_r1 module?

edit: Constants are internal to their module, and cannot can be accessed outside of their module <- came across this move error. revised to 0/1 instead of 3/4, but i will leave them within the k1 vs r1 modules.

@joyqvq joyqvq enabled auto-merge (squash) February 21, 2023 15:57
@vercel vercel bot temporarily deployed to Preview – wallet-adapter February 21, 2023 15:59 Inactive
@vercel vercel bot temporarily deployed to Preview – frenemies February 21, 2023 15:59 Inactive
@vercel vercel bot temporarily deployed to Preview – explorer-storybook February 21, 2023 16:01 Inactive
@vercel vercel bot temporarily deployed to Preview – explorer-storybook February 21, 2023 16:49 Inactive
@vercel vercel bot temporarily deployed to Preview – frenemies February 21, 2023 16:50 Inactive
@vercel vercel bot temporarily deployed to Preview – wallet-adapter February 21, 2023 16:50 Inactive
@joyqvq joyqvq merged commit 3919544 into main Feb 21, 2023
@joyqvq joyqvq deleted the r1-move branch February 21, 2023 17:05
alexsporn pushed a commit to iotaledger/iota that referenced this pull request Sep 6, 2024
…423)

## What Changed

- For CLI and SDK, a ECDSA k1 and r1 signature is produced using the
nonrecoverable form. This means the signature is 64 bytes instead of 65.
- The signature verification in sui also uses the nonrecoverable option.
A valid signature should have 64 bytes.
- For wallet, since only Ed25519 is supported, the secp256k1 change
should not affect.
- Also exposes secp256k1_verify and secp256k1_verify_recoverable API in
move.
- Ser/de of public keys and signatures now uses the most compact
serialization with ToFromBytes.

## What Do You Need To Do
- If you are using SDK to produce a Secp256k1 signature, no change is
needed as long as you are using the latest version.
- If you are using something else to produce a signature, your old
signature will not be considered valid. You should just need to remove
the last byte (65->64 bytes) to make it a valid signature again.

Next:
- r1 verify and verify_recoverable added in
MystenLabs/sui#7773
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants