Skip to content

Commit ea25ce9

Browse files
authored
Merge pull request #3290 from o1-labs/dw/misc-mina-hasher
mina-hasher: wrap 80 chars
2 parents dd03cdf + 79d8cc3 commit ea25ce9

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
lines changed

hasher/src/lib.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use ark_ff::PrimeField;
1515
use o1_utils::FieldHelpers;
1616

1717
/// The domain parameter trait is used during hashing to convey extra
18-
/// arguments to domain string generation. It is also used by generic signing code.
18+
/// arguments to domain string generation. It is also used by generic signing
19+
/// code.
1920
pub trait DomainParameter: Clone {
2021
/// Conversion into vector of bytes
2122
fn into_bytes(self) -> Vec<u8>;
@@ -41,13 +42,16 @@ impl DomainParameter for u64 {
4142

4243
/// Interface for hashable objects
4344
///
44-
/// Mina uses fixed-length hashing with domain separation for each type of object hashed.
45-
/// The prior means that `Hashable` only supports types whose size is not variable.
45+
/// Mina uses fixed-length hashing with domain separation for each type of
46+
/// object hashed. The prior means that `Hashable` only supports types whose
47+
/// size is not variable.
4648
///
47-
/// **Important:** The developer MUST assure that all domain strings used throughout the
48-
/// system are unique and that all structures hashed are of fixed size.
49+
/// **Important:** The developer MUST assure that all domain strings used
50+
/// throughout the system are unique and that all structures hashed are of fixed
51+
/// size.
4952
///
50-
/// Here is an example of how to implement the `Hashable` trait for am `Example` type.
53+
/// Here is an example of how to implement the `Hashable` trait for am `Example`
54+
/// type.
5155
///
5256
/// ```rust
5357
/// use mina_hasher::{Hashable, ROInput};
@@ -81,8 +85,8 @@ pub trait Hashable: Clone {
8185

8286
/// Generate unique domain string of length `<= 20`.
8387
///
84-
/// The length bound is guarded by an assertion, but uniqueness must
85-
/// be enforced by the developer implementing the traits (see [`Hashable`] for
88+
/// The length bound is guarded by an assertion, but uniqueness must be
89+
/// enforced by the developer implementing the traits (see [`Hashable`] for
8690
/// more details). The domain string may be parameterized by the contents of
8791
/// the generic `domain_param` argument.
8892
///
@@ -93,9 +97,11 @@ pub trait Hashable: Clone {
9397

9498
/// Interface for hashing [`Hashable`] inputs
9599
///
96-
/// Mina uses a unique hasher configured with domain separation for each type of object hashed.
97-
/// The underlying hash parameters are large and costly to initialize, so the [`Hasher`] interface
98-
/// provides a reusable context for efficient hashing with domain separation.
100+
/// Mina uses a unique hasher configured with domain separation for each type of
101+
/// object hashed.
102+
/// The underlying hash parameters are large and costly to initialize, so the
103+
/// [`Hasher`] interface provides a reusable context for efficient hashing with
104+
/// domain separation.
99105
///
100106
/// Example usage
101107
///
@@ -124,8 +130,8 @@ pub trait Hashable: Clone {
124130
/// ```
125131
///
126132
pub trait Hasher<H: Hashable> {
127-
/// Set the initial state based on domain separation string
128-
/// generated from `H::domain_string(domain_param)`
133+
/// Set the initial state based on domain separation string generated from
134+
/// `H::domain_string(domain_param)`
129135
fn init(&mut self, domain_param: H::D) -> &mut dyn Hasher<H>;
130136

131137
/// Restore the initial state that was set most recently

hasher/src/poseidon.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ where
7373
}
7474

7575
fn init(&mut self, domain_param: H::D) -> &mut dyn Hasher<H> {
76-
// Set sponge initial state and save it so the hasher context can be reused efficiently
77-
// N.B. Mina sets the sponge's initial state by hashing the input type's domain bytes
76+
// Set sponge initial state and save it so the hasher context can be
77+
// reused efficiently
78+
// N.B. Mina sets the sponge's initial state by hashing the input type's
79+
// domain bytes
7880
self.sponge.reset();
7981

8082
if let Some(domain_string) = H::domain_string(domain_param) {

hasher/src/roinput.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ const SINGLE_HEADER_SIZE: usize = 4; // number of bytes for each part of the hea
1717

1818
/// Random oracle input structure
1919
///
20-
/// The random oracle input encapsulates the serialization format and methods using during hashing.
20+
/// The random oracle input encapsulates the serialization format and methods
21+
/// using during hashing.
2122
///
22-
/// When implementing the [`Hashable`] trait to enable hashing for a type, you must implement
23-
/// its `to_roinput()` serialization method using the [`ROInput`] functions below.
23+
/// When implementing the [`Hashable`] trait to enable hashing for a type, you
24+
/// must implement its `to_roinput()` serialization method using the [`ROInput`]
25+
/// functions below.
2426
///
25-
/// The random oracle input structure is used (by generic code) to serialize the object into
26-
/// both a vector of `pasta::Fp` field elements and into a vector of bytes, depending on the situation.
27+
/// The random oracle input structure is used (by generic code) to serialize the
28+
/// object into both a vector of `pasta::Fp` field elements and into a vector of
29+
/// bytes, depending on the situation.
2730
///
28-
/// Here is an example of how `ROInput` is used during the definition of the `Hashable` trait.
31+
/// Here is an example of how `ROInput` is used during the definition of the
32+
/// `Hashable` trait.
2933
///
3034
/// ```rust
3135
/// use mina_hasher::{Hashable, ROInput};
@@ -53,11 +57,12 @@ const SINGLE_HEADER_SIZE: usize = 4; // number of bytes for each part of the hea
5357
/// }
5458
/// }
5559
/// ```
56-
/// **Details:** For technical reasons related to our proof system and performance,
57-
/// non-field-element members are serialized for signing differently than other types.
58-
/// Additionally, during signing all members of the random oracle input get serialized
59-
/// together in two different ways: both as *bytes* and as a vector of *field elements*.
60-
/// The random oracle input automates and encapsulates this complexity.
60+
/// **Details:** For technical reasons related to our proof system and
61+
/// performance, non-field-element members are serialized for signing
62+
/// differently than other types. Additionally, during signing all members of
63+
/// the random oracle input get serialized together in two different ways: both
64+
/// as *bytes* and as a vector of *field elements*. The random oracle input
65+
/// automates and encapsulates this complexity.
6166
#[derive(Default, Debug, Clone, PartialEq, Eq)]
6267
pub struct ROInput {
6368
fields: Vec<Fp>,

hasher/tests/hasher.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,30 @@ fn interfaces() {
9696
// Usage 1: incremental interface
9797
let mut hasher = create_legacy::<Foo>(0);
9898
hasher.update(&Foo { x: 3, y: 1 });
99-
let x1 = hasher.digest(); // Resets to previous init state (0)
99+
// Resets to previous init state (0)
100+
let x1 = hasher.digest();
100101
hasher.update(&Foo { x: 82, y: 834 });
101102
hasher.update(&Foo { x: 1235, y: 93 });
102-
hasher.digest(); // Resets to previous init state (0)
103+
// Resets to previous init state (0)
104+
hasher.digest();
103105
hasher.init(1);
104106
hasher.update(&Foo { x: 82, y: 834 });
105-
let x2 = hasher.digest(); // Resets to previous init state (1)
107+
// Resets to previous init state (1)
108+
let x2 = hasher.digest();
106109

107110
// Usage 2: builder interface with one-shot pattern
108111
let mut hasher = create_legacy::<Foo>(0);
109-
let y1 = hasher.update(&Foo { x: 3, y: 1 }).digest(); // Resets to previous init state (0)
112+
// Resets to previous init state (0)
113+
let y1 = hasher.update(&Foo { x: 3, y: 1 }).digest();
114+
110115
hasher.update(&Foo { x: 31, y: 21 }).digest();
111116

112117
// Usage 3: builder interface with one-shot pattern also setting init state
113118
let mut hasher = create_legacy::<Foo>(0);
114-
let y2 = hasher.init(0).update(&Foo { x: 3, y: 1 }).digest(); // Resets to previous init state (1)
115-
let y3 = hasher.init(1).update(&Foo { x: 82, y: 834 }).digest(); // Resets to previous init state (2)
119+
// Resets to previous init state (1)
120+
let y2 = hasher.init(0).update(&Foo { x: 3, y: 1 }).digest();
121+
// Resets to previous init state (2)
122+
let y3 = hasher.init(1).update(&Foo { x: 82, y: 834 }).digest();
116123

117124
// Usage 4: one-shot interfaces
118125
let mut hasher = create_legacy::<Foo>(0);

0 commit comments

Comments
 (0)