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

Use Keccak256 when hashing for signing #3138

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

Use Keccak256 when hashing for signing #3138

wants to merge 12 commits into from

Conversation

deuszx
Copy link
Contributor

@deuszx deuszx commented Jan 15, 2025

Motivation

We want to verify Linera blocks on Ethereum (and other EVMs) but they don't have opcodes/precompiles for Sha256 that we're using .

Proposal

Replace Sha256 with Keccak256.

Notice that I'm only changing hashing scheme used in CryptoHash struct - i.e. there are still places in the code where we use sha266 (like for example linera views and cache).

Test Plan

Release Plan

This is backwards incompatible change.

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

Links

Closes #3125

@deuszx deuszx marked this pull request as ready for review January 17, 2025 17:43
@slava1310
Copy link

Testnet

Copy link
Contributor

@MathieuDutSik MathieuDutSik left a comment

Choose a reason for hiding this comment

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

I have only one objection to the PR.
We agree that SHA-3 is almost the same as Keccak256.
However, the implementations are distinct.

Could you put a runtime comparison of the relative speed of SHA-3 vs Keccak256?

@@ -167,7 +166,7 @@ impl Serialize for CryptoHash {
if serializer.is_human_readable() {
serializer.serialize_str(&self.to_string())
} else {
serializer.serialize_newtype_struct("CryptoHash", &self.0)
serializer.serialize_newtype_struct("CryptoHash", &self.0 .0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe CryptoHash deserves an as_bytes method to avoid the .0 .0.

@@ -396,6 +393,20 @@ where
}
}

// Temporary struct to extend `Keccak256` with `io::Write`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Temporary struct to extend `Keccak256` with `io::Write`.
/// Temporary struct to extend `Keccak256` with `io::Write`.

@@ -396,6 +393,20 @@ where
}
}

// Temporary struct to extend `Keccak256` with `io::Write`.
struct Keccak256Ext(Keccak256);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this used in places other than CryptoHash::new? Otherwise we might as well just call update directly there.


integers
}

/// Returns the bytes that represent the `integers` in little-endian.
fn u64_array_to_le_bytes(integers: [u64; 4]) -> [u8; 32] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we still using this? (And should we be using this at all?)

@@ -1026,15 +1026,15 @@ mod tests {
fn chain_ids() {
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be helpful to add root chains 1 and 2 here as well: They are probably used in a few READMEs, so this serves as documentation where one can look up the hash. It might also simplify any future changes to how chain IDs are computed.

@@ -1333,22 +1333,23 @@ async fn test_multiple_messages_from_different_applications() -> anyhow::Result<

// The registration message for the first destination chain
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// The registration message for the first destination chain
// The registration message for the second destination chain

Copy link
Contributor

Choose a reason for hiding this comment

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

Or we could rename first_message and second_message to message0 and message1 and the destination chains similarly. (But keep the *_registration_messages.)

},
};

// The registration message for the second destination chain
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// The registration message for the second destination chain
// The registration message for the first destination chain

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.

Switch hashing to Keccak
4 participants