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

feat: poseidon hash #388

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

feat: poseidon hash #388

wants to merge 131 commits into from

Conversation

qalisander
Copy link
Member

@qalisander qalisander commented Oct 31, 2024

Resolves #264

PR Checklist

  • Tests
  • Documentation

Copy link

codecov bot commented Nov 5, 2024

Codecov Report

Attention: Patch coverage is 94.91525% with 24 lines in your changes missing coverage. Please review.

Project coverage is 68.3%. Comparing base (91af9e7) to head (088b5c1).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/crypto/src/poseidon2/mod.rs 94.5% 9 Missing ⚠️
lib/crypto/src/poseidon2/params.rs 0.0% 9 Missing ⚠️
lib/crypto/src/bigint.rs 96.5% 1 Missing ⚠️
lib/crypto/src/poseidon2/instance/babybear.rs 97.9% 1 Missing ⚠️
lib/crypto/src/poseidon2/instance/bn256.rs 97.6% 1 Missing ⚠️
lib/crypto/src/poseidon2/instance/goldilocks.rs 97.2% 1 Missing ⚠️
lib/crypto/src/poseidon2/instance/pallas.rs 97.6% 1 Missing ⚠️
lib/crypto/src/poseidon2/instance/vesta.rs 96.2% 1 Missing ⚠️
Additional details and impacted files
Files with missing lines Coverage Δ
lib/crypto/src/bits.rs 100.0% <ø> (ø)
lib/crypto/src/field/fp.rs 47.1% <ø> (+2.6%) ⬆️
lib/crypto/src/poseidon2/instance/bls12.rs 100.0% <100.0%> (ø)
lib/crypto/src/bigint.rs 86.0% <96.5%> (+3.0%) ⬆️
lib/crypto/src/poseidon2/instance/babybear.rs 97.9% <97.9%> (ø)
lib/crypto/src/poseidon2/instance/bn256.rs 97.6% <97.6%> (ø)
lib/crypto/src/poseidon2/instance/goldilocks.rs 97.2% <97.2%> (ø)
lib/crypto/src/poseidon2/instance/pallas.rs 97.6% <97.6%> (ø)
lib/crypto/src/poseidon2/instance/vesta.rs 96.2% <96.2%> (ø)
lib/crypto/src/poseidon2/mod.rs 94.5% <94.5%> (ø)
... and 1 more
---- 🚨 Try these New Features:

@qalisander qalisander marked this pull request as ready for review November 27, 2024 10:42
Copy link
Collaborator

@0xNeshi 0xNeshi left a comment

Choose a reason for hiding this comment

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

Skimmed the implementation, looks wizard-y :smile

Cargo.toml Show resolved Hide resolved
};
}

#[cfg(test)]
mod test {
use alloc::vec::Vec;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
use alloc::vec::Vec;
use alloc::{vec, vec::Vec};

This should completely remove those vec-related errors in tests

};
}

#[cfg(test)]
mod test {
use alloc::vec::Vec;

use super::*;

Copy link
Collaborator

Choose a reason for hiding this comment

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

At least a couple of tests for from_str_hex would be very useful to document how it works, we could then later add fuzz tests when we start working on them

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! Agree. I'll add a few fuzz tests

@@ -32,6 +32,7 @@ impl_bit_iter_be!(usize);

#[cfg(test)]
mod tests {
use alloc::vec::Vec;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
use alloc::vec::Vec;
use alloc::{vec, vec::Vec};

$crate::field::fp::Fp::new(
$crate::bigint::crypto_bigint::Uint::from_be_hex($num),
)
$crate::field::fp::Fp::new($crate::bigint::from_str_hex($num))
}};
}

Copy link
Collaborator

@0xNeshi 0xNeshi Nov 28, 2024

Choose a reason for hiding this comment

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

I see errors in this file's tests like cannot find macro `format` in this scope, and it seems these go away after importing:

use alloc::format;

These do not fail the build, or stop the test execution, they show up in my IDE (maybe it's the IDE 🤔 ).

Anyway, seems to be some phantom issue due to proptest using std::format internally.

$crate::field::fp::Fp::new(
$crate::bigint::crypto_bigint::Uint::from_be_hex($num),
)
$crate::field::fp::Fp::new($crate::bigint::from_str_hex($num))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curios, why was Uint::from_be_hex not appropriate to use here?

Copy link
Member Author

@qalisander qalisander Nov 28, 2024

Choose a reason for hiding this comment

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

Uint::from_be_hex requires specific length of hex string. Like 2 byte "1234" number wont be converted to U64 with leading zeros. Only 8 byte hex number does so. Of course we could format all our const values to proper length, but I've just decided to add our const hex conversion and copy constants from referenced implementation here in whitepaper

@@ -32,6 +32,7 @@ impl_bit_iter_be!(usize);

#[cfg(test)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again curios, why are these not marked as other tests with:

#[cfg(all(test, feature = "std"))]

though merkle and hash have it?

const ROUNDS_F: usize = 8;
const ROUNDS_P: usize = 21;
const MAT_INTERNAL_DIAG_M_1: &'static [Scalar] = &[
fp_from_hex!("409133f0"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there some resource we can use to to verify the correctness of the hex values used in all these instances?

Copy link
Member Author

@qalisander qalisander Nov 28, 2024

Choose a reason for hiding this comment

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

There is an implementation referenced in the whitepaper. This implementation is built by one of the authors of the hash

@qalisander
Copy link
Member Author

qalisander commented Nov 28, 2024

looks wizard-y

I see I should add a bit more clarifying comments

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.

[Feature]: Poseidon Hash
3 participants