Skip to main content

MimcHasher

Struct MimcHasher 

Source
pub struct MimcHasher { /* private fields */ }
Expand description

MiMC-Feistel sponge hasher.

This struct provides the MiMC hash function with configurable parameters. For most use cases, use MimcHasher::default() which provides parameters compatible with Tornado Cash / circomlib.

§Example

use stealth_lib::hash::MimcHasher;

// Use default parameters (compatible with Tornado Cash)
let hasher = MimcHasher::default();
let hash = hasher.hash(123, 456);

// Hash is deterministic
assert_eq!(hasher.hash(123, 456), hasher.hash(123, 456));

// Different inputs produce different outputs
assert_ne!(hasher.hash(123, 456), hasher.hash(123, 789));

Implementations§

Source§

impl MimcHasher

Source

pub fn new(field_prime: u128, num_rounds: usize, constants: Vec<u128>) -> Self

Creates a new MimcHasher with custom parameters.

§Arguments
  • field_prime - The field modulus for arithmetic operations
  • num_rounds - Number of Feistel rounds
  • constants - Round constants (must have at least num_rounds * 2 elements)
§Example
use stealth_lib::hash::MimcHasher;

let constants = vec![0u128; 20];
let hasher = MimcHasher::new(
    340282366920938463463374607431768211455, // 2^128 - 1
    10,
    constants,
);
Source

pub fn field_prime(&self) -> u128

Returns the field prime used by this hasher.

Source

pub fn num_rounds(&self) -> usize

Returns the number of rounds used by this hasher.

Source

pub fn hash(&self, left: u128, right: u128) -> u128

MiMC sponge hash function.

Computes the MiMC-Feistel-Sponge hash of two input values. This is the primary hash function used for Merkle tree construction.

§Arguments
  • left - First input value
  • right - Second input value
§Returns

The hash output as a u128.

§Example
use stealth_lib::hash::MimcHasher;

let hasher = MimcHasher::default();

// Hash two values
let hash = hasher.hash(123, 456);

// Hash is deterministic
assert_eq!(hash, hasher.hash(123, 456));
Source

pub fn mimc_sponge(&self, left: u128, right: u128, key: u128) -> u128

MiMC sponge with explicit key parameter.

Lower-level function that allows specifying a custom key. Most users should use hash instead.

§Arguments
  • left - First input value
  • right - Second input value
  • key - Sponge key
§Returns

The hash output as a u128.

Source

pub fn hash_single(&self, input: u128) -> u128

Hash a single value.

Convenience method to hash a single input by pairing it with zero.

§Arguments
  • input - The value to hash
§Returns

The hash output as a u128.

§Example
use stealth_lib::hash::MimcHasher;

let hasher = MimcHasher::default();
let hash = hasher.hash_single(12345);

Trait Implementations§

Source§

impl Clone for MimcHasher

Source§

fn clone(&self) -> MimcHasher

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MimcHasher

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MimcHasher

Source§

fn default() -> Self

Creates a MimcHasher with default parameters compatible with Tornado Cash.

  • Field prime: 2^128 - 1
  • Rounds: 10
  • Constants: circomlib-compatible

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.