Skip to content

feat: add Pedersen hash #627

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

Closed
wants to merge 12 commits into from
Closed

feat: add Pedersen hash #627

wants to merge 12 commits into from

Conversation

bidzyyys
Copy link
Collaborator

@bidzyyys bidzyyys commented Apr 10, 2025

Resolves #269

PR Checklist

  • Tests
  • Documentation
  • Changelog

@bidzyyys bidzyyys linked an issue Apr 10, 2025 that may be closed by this pull request
1 task
Copy link

netlify bot commented Apr 10, 2025

Deploy Preview for contracts-stylus canceled.

Name Link
🔨 Latest commit de31feb
🔍 Latest deploy log https://app.netlify.com/sites/contracts-stylus/deploys/67f802e63cb4050008284342

Copy link

netlify bot commented Apr 10, 2025

Deploy Preview for contracts-stylus canceled.

Name Link
🔨 Latest commit a34d610
🔍 Latest deploy log https://app.netlify.com/sites/contracts-stylus/deploys/6808e660d124f60008ecb383

Copy link

codecov bot commented Apr 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.3%. Comparing base (16cc1ff) to head (2492e86).

✅ All tests successful. No failed tests found.

Additional details and impacted files
Files with missing lines Coverage Δ
lib/crypto/src/pedersen/instance/starknet.rs 99.9% <ø> (ø)
lib/crypto/src/pedersen/mod.rs 100.0% <100.0%> (ø)

... and 2 files with indirect coverage changes

@bidzyyys bidzyyys force-pushed the feat/pedersen-hash branch from 8b3af08 to 53e3f6f Compare April 22, 2025 18:14
@bidzyyys bidzyyys marked this pull request as ready for review April 22, 2025 18:15
@bidzyyys bidzyyys force-pushed the feat/pedersen-hash branch from 53e3f6f to eadb81f Compare April 22, 2025 18:21
Copy link
Member

@qalisander qalisander left a comment

Choose a reason for hiding this comment

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

Good job!
First pass

type Output = Affine<P>;

/// Update the hash state with a new element.
fn update(&mut self, input: impl AsRef<[u8]>) {
Copy link
Member

@qalisander qalisander Apr 23, 2025

Choose a reason for hiding this comment

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

I don't think it is a good API for this kind of hash. Since after update is called you recreate U256 from variable size of bytes.
And e.g. calling update() two times with inputs [42, 42] and [43,43]. Or calling it with inputs [42, 42, 0, 0] and [43, 43] should produce the same hash, since bytes are not padded. And [42, 42] and [42, 42, 0, 0] will be parsed to the same integer. So we can easily peek different set of bytes, that will produce the same hash. It is an actual vulnerability to preimage attack. I don't think we should implement Hasher in this case (more simple) or redesign the trait and make it more generic (able to accept U256 as an argument)

I see that starknet-rs has only api where you can pass 256-bit integer.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fair point, I removed Hasher trait implementation.

@bidzyyys bidzyyys changed the base branch from main to v0.2 April 23, 2025 13:25
@bidzyyys bidzyyys requested a review from qalisander April 23, 2025 16:01
Comment on lines +13 to +16
};
use stylus_sdk::prelude::*;
#[entrypoint]
#[storage]
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 stylus_sdk::prelude::*;
#[entrypoint]
#[storage]
};
use stylus_sdk::prelude::*;
#[entrypoint]
#[storage]

#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 9f552914238459525586fa596ededdce984599cffe1af7515181e7f8890fabf6 # shrinks to input = [3618502788666131219974424518481750869458896638539263116075447500599906533376]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this proptest regression important enough to have in repo? If this was the result on some mishap in the impl, we can remove it (this is the PR that first introduces the Pedersen hash after all); if this is a specific edge case, we can leave it in

Comment on lines +1 to +2
//! This module contains pedersen hash instances for some popular curve
//! instances.
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
//! This module contains pedersen hash instances for some popular curve
//! instances.
//! This module contains pedersen hash instances for some popular curves.

Comment on lines +16 to +19
};
#[derive(Clone, Default, PartialEq, Eq)]
/// Starknet's Curve Details.
pub struct StarknetCurveConfig;
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
};
#[derive(Clone, Default, PartialEq, Eq)]
/// Starknet's Curve Details.
pub struct StarknetCurveConfig;
};
/// Starknet's Curve Details.
#[derive(Clone, Default, PartialEq, Eq)]
pub struct StarknetCurveConfig;

Comment on lines +50 to +51
// TODO: confirm this
const COFACTOR: &'static [u64] = &[0x1];
Copy link
Collaborator

Choose a reason for hiding this comment

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

TODO

Comment on lines +19 to +20
params: core::marker::PhantomData<F>,
curve: core::marker::PhantomData<P>,
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
params: core::marker::PhantomData<F>,
curve: core::marker::PhantomData<P>,
params: PhantomData<F>,
curve: PhantomData<P>,

nit: import PhantomData directly instead of using absolute path repeatedly

Comment on lines +3 to +5
pub mod instance;
pub mod params;
use alloc::{vec, 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.

nit: place pub mods below imports for clarity, I almost didn't realize these modules were declared here (confused them with additional imports)

Comment on lines +83 to +84
/// Constant points for Starknet Curve.
static CONSTANT_POINTS: Lazy<Vec<Affine<StarknetCurveConfig>>> = Lazy::new(
Copy link
Collaborator

Choose a reason for hiding this comment

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

A link to where these constant points were taken from would be useful, helps maintainability and review

Comment on lines +29 to +30
// Starknet's base field modulus.
const MODULUS: U256 = from_num!("3618502788666131213697322783095070105623107215331596699973092056135872020481");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Link to source of this const, if not here then at the module-level docs //!

assert!(point_list.len() == F::N_ELEMENT_BITS_HASH);

for pt in point_list {
assert!(pt.x != point.x, "Unhashable input.");
Copy link
Collaborator

@0xNeshi 0xNeshi Apr 23, 2025

Choose a reason for hiding this comment

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

We should assert! things that must always hold for our code to be valid, the "invariants" of our code.
Otherwise returning a Result::Err might be clearer and more in line with Rust's way of doing things.

Not saying this or any other assert should be converted to an error as I lack enough in-depth knowledge to determine whether it should be done, I'm just pointing this out so that you can determine the right approach and that we're on the same page.

@0xNeshi 0xNeshi self-requested a review April 23, 2025 16:12
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.

Approved by mistake

@0xNeshi 0xNeshi self-requested a review April 24, 2025 11:31
@bidzyyys
Copy link
Collaborator Author

bidzyyys commented May 6, 2025

Solved in #644.

@bidzyyys bidzyyys closed this May 6, 2025
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]: Pedersen Hash
3 participants