Skip to content

Commit 02f2209

Browse files
authored
docs: add docs for starknet-crypto (#634)
1 parent a989555 commit 02f2209

File tree

8 files changed

+81
-37
lines changed

8 files changed

+81
-37
lines changed

starknet-accounts/src/single_owner.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ where
4747
{
4848
/// Create a new account controlled by a single signer.
4949
///
50-
/// ### Arguments
50+
/// ### Parameters
5151
///
52-
/// * `provider`: A `Provider` implementation that provides access to the Starknet network.
53-
/// * `signer`: A `Signer` implementation that can generate valid signatures for this account.
54-
/// * `address`: Account contract address.
55-
/// * `chain_id`: Network chain ID.
56-
/// * `encoding`: How `__execute__` calldata should be encoded.
52+
/// - `provider`: A `Provider` implementation that provides access to the Starknet network.
53+
/// - `signer`: A `Signer` implementation that can generate valid signatures for this account.
54+
/// - `address`: Account contract address.
55+
/// - `chain_id`: Network chain ID.
56+
/// - `encoding`: How `__execute__` calldata should be encoded.
5757
pub const fn new(
5858
provider: P,
5959
signer: S,

starknet-crypto/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Low-level cryptography utilities for Starknet
22

3-
`starknet-crypto` contains utilities for performing **low-level** cryptographic operations in Starknet.
3+
`starknet-crypto` contains utilities for performing **low-level** cryptographic operations in Starknet:
4+
5+
- ECDSA operations
6+
- Signing hashes
7+
- Verifying signatures
8+
- Recovering public keys from signatures
9+
- Pedersen hash
10+
- Poseidon hash
11+
- RFC-6979
412

513
> _You're advised to use high-level crypto utilities implemented by the `starknet-core` crate (or use it through the `starknet::core` re-export) if you're not familiar with cryptographic primitives. Using these low-level functions incorrectly could result in leaking your private key, for example._
614

starknet-crypto/src/ecdsa.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ use crate::{
77
use starknet_types_core::curve::{AffinePoint, ProjectivePoint};
88
use starknet_types_core::felt::Felt;
99

10+
/// The (exclusive) upper bound on many ECDSA-related elements based on the original C++
11+
/// implementation from [`crypto-cpp`](https://github.com/starkware-libs/crypto-cpp).
12+
///
13+
/// The C++ implementation [imposes](https://github.com/starkware-libs/crypto-cpp/blob/78e3ed8dc7a0901fe6d62f4e99becc6e7936adfd/src/starkware/crypto/ecdsa.cc#L23)
14+
/// an upper bound of `0x0800000000000000000000000000000000000000000000000000000000000000`.
15+
///
16+
/// When a compuated value is greater than or equal to this bound, the modulus is taken to ensure
17+
/// the resulting value falls under the bound.
1018
const ELEMENT_UPPER_BOUND: Felt = Felt::from_raw([
1119
576459263475450960,
1220
18446744073709255680,
1321
160989183,
1422
18446743986131435553,
1523
]);
1624

17-
/// Stark ECDSA signature
25+
/// Stark ECDSA signature.
1826
#[derive(Debug)]
1927
pub struct Signature {
2028
/// The `r` value of a signature
@@ -23,7 +31,7 @@ pub struct Signature {
2331
pub s: Felt,
2432
}
2533

26-
/// Stark ECDSA signature with `v`
34+
/// Stark ECDSA signature with `v`, useful for recovering the public key.
2735
#[derive(Debug)]
2836
pub struct ExtendedSignature {
2937
/// The `r` value of a signature
@@ -70,9 +78,9 @@ impl core::fmt::Display for ExtendedSignature {
7078

7179
/// Computes the public key given a Stark private key.
7280
///
73-
/// ### Arguments
81+
/// ### Parameters
7482
///
75-
/// * `private_key`: The private key
83+
/// - `private_key`: The private key.
7684
pub fn get_public_key(private_key: &Felt) -> Felt {
7785
mul_by_bits(&GENERATOR, private_key)
7886
.to_affine()
@@ -82,11 +90,11 @@ pub fn get_public_key(private_key: &Felt) -> Felt {
8290

8391
/// Computes ECDSA signature given a Stark private key and message hash.
8492
///
85-
/// ### Arguments
93+
/// ### Parameters
8694
///
87-
/// * `private_key`: The private key
88-
/// * `message`: The message hash
89-
/// * `k`: A random `k` value. You **MUST NOT** use the same `k` on different signatures
95+
/// - `private_key`: The private key.
96+
/// - `message`: The message hash.
97+
/// - `k`: A random `k` value. You **MUST NOT** use the same `k` on different signatures.
9098
pub fn sign(private_key: &Felt, message: &Felt, k: &Felt) -> Result<ExtendedSignature, SignError> {
9199
if message >= &ELEMENT_UPPER_BOUND {
92100
return Err(SignError::InvalidMessageHash);
@@ -120,12 +128,12 @@ pub fn sign(private_key: &Felt, message: &Felt, k: &Felt) -> Result<ExtendedSign
120128
/// Verifies if a signature is valid over a message hash given a public key. Returns an error
121129
/// instead of `false` if the public key is invalid.
122130
///
123-
/// ### Arguments
131+
/// ### Parameters
124132
///
125-
/// * `public_key`: The public key
126-
/// * `message`: The message hash
127-
/// * `r`: The `r` value of the signature
128-
/// * `s`: The `s` value of the signature
133+
/// - `public_key`: The public key.
134+
/// - `message`: The message hash.
135+
/// - `r`: The `r` value of the signature.
136+
/// - `s`: The `s` value of the signature.
129137
pub fn verify(public_key: &Felt, message: &Felt, r: &Felt, s: &Felt) -> Result<bool, VerifyError> {
130138
if message >= &ELEMENT_UPPER_BOUND {
131139
return Err(VerifyError::InvalidMessageHash);
@@ -162,12 +170,12 @@ pub fn verify(public_key: &Felt, message: &Felt, r: &Felt, s: &Felt) -> Result<b
162170

163171
/// Recovers the public key from a message and (r, s, v) signature parameters
164172
///
165-
/// ### Arguments
173+
/// ### Parameters
166174
///
167-
/// * `msg_hash`: The message hash
168-
/// * `r_bytes`: The `r` value of the signature
169-
/// * `s_bytes`: The `s` value of the signature
170-
/// * `v_bytes`: The `v` value of the signature
175+
/// - `msg_hash`: The message hash.
176+
/// - `r_bytes`: The `r` value of the signature.
177+
/// - `s_bytes`: The `s` value of the signature.
178+
/// - `v_bytes`: The `v` value of the signature.
171179
pub fn recover(message: &Felt, r: &Felt, s: &Felt, v: &Felt) -> Result<Felt, RecoverError> {
172180
if message >= &ELEMENT_UPPER_BOUND {
173181
return Err(RecoverError::InvalidMessageHash);

starknet-crypto/src/error.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
mod sign_error {
2-
/// Errors when performing ECDSA [`sign`](fn.sign) operations
2+
/// Errors when performing ECDSA [`sign`](fn.sign) operations.
33
#[derive(Debug)]
44
pub enum SignError {
5+
/// The message hash is not in the range of `[0, 2^251)`.
56
InvalidMessageHash,
7+
/// The random `k` value results in an invalid signature. A different `k` value should be
8+
/// used instead, typically by using a new seed per RFC-6979.
69
InvalidK,
710
}
811

@@ -21,12 +24,16 @@ mod sign_error {
2124
pub use sign_error::SignError;
2225

2326
mod verify_error {
24-
/// Errors when performing ECDSA [`verify`](fn.verify) operations
27+
/// Errors when performing ECDSA [`verify`](fn.verify) operations.
2528
#[derive(Debug)]
2629
pub enum VerifyError {
30+
/// The public key is not a valid point on the STARK curve.
2731
InvalidPublicKey,
32+
/// The message hash is not in the range of `[0, 2^251)`.
2833
InvalidMessageHash,
34+
/// The `r` value is not in the range of `[0, 2^251)`.
2935
InvalidR,
36+
/// The `s` value is not in the range of `[0, 2^251)`.
3037
InvalidS,
3138
}
3239

@@ -47,12 +54,17 @@ mod verify_error {
4754
pub use verify_error::VerifyError;
4855

4956
mod recover_error {
50-
/// Errors when performing ECDSA [`recover`](fn.recover) operations
57+
/// Errors when performing ECDSA [`recover`](fn.recover) operations.
5158
#[derive(Debug)]
5259
pub enum RecoverError {
60+
/// The message hash is not in the range of `[0, 2^251)`.
5361
InvalidMessageHash,
62+
/// The `r` value is not in the range of `[0, 2^251)`.
5463
InvalidR,
64+
/// The `s` value is not in the range of `[0,
65+
/// 0x0800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f)`.
5566
InvalidS,
67+
/// The `v` value is neither `0` nor `1`.
5668
InvalidV,
5769
}
5870

starknet-crypto/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1+
//! Low-level cryptography utilities for Starknet. Features include:
2+
//!
3+
//! - ECDSA operations
4+
//! - [Signing hashes](fn.sign)
5+
//! - [Verifying signatures](fn.verify)
6+
//! - [Recovering public keys from signatures](fn.recover)
7+
//! - [Pedersen hash](fn.pedersen_hash)
8+
//! - Poseidon hash
9+
//! - [RFC-6979](fn.rfc6979_generate_k)
10+
//!
11+
//! # Warning
12+
//!
13+
//! You're advised to use high-level crypto utilities implemented by the `starknet-core` crate if
14+
//! you're not familiar with cryptographic primitives. Using these low-level functions incorrectly
15+
//! could result in catastrophic consequences like leaking your private key.
16+
17+
#![deny(missing_docs)]
118
#![cfg_attr(not(feature = "std"), no_std)]
2-
#![doc = include_str!("../README.md")]
319

420
#[allow(unused_extern_crates)]
521
#[cfg(all(not(feature = "std"), any(test, feature = "alloc")))]

starknet-crypto/src/pedersen_hash.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use starknet_types_core::{
55

66
/// Computes the Starkware version of the Pedersen hash of x and y. All inputs are little-endian.
77
///
8-
/// ### Arguments
8+
/// ### Parameters
99
///
10-
/// * `x`: The x coordinate
11-
/// * `y`: The y coordinate
10+
/// - `x`: The x coordinate.
11+
/// - `y`: The y coordinate.
1212
pub fn pedersen_hash(x: &Felt, y: &Felt) -> Felt {
1313
Pedersen::hash(x, y)
1414
}

starknet-crypto/src/poseidon_hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use starknet_types_core::{felt::Felt, hash::Poseidon};
55

6-
/// A hasher for Starknet Poseidon hash.
6+
/// A stateful hasher for Starknet Poseidon hash.
77
///
88
/// Using this hasher is the same as calling [`poseidon_hash_many`].
99
#[derive(Debug, Default)]

starknet-crypto/src/rfc6979.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ const EC_ORDER: U256 =
99

1010
/// Deterministically generate ephemeral scalar `k` based on RFC 6979.
1111
///
12-
/// ### Arguments
12+
/// ### Parameters
1313
///
14-
/// * `message_hash`: message hash
15-
/// * `private_key`: private key
16-
/// * `seed`: extra seed for additional entropy
14+
/// - `message_hash`: Message hash.
15+
/// - `private_key`: Private key.
16+
/// - `seed`: Extra seed for additional entropy.
1717
pub fn generate_k(message_hash: &Felt, private_key: &Felt, seed: Option<&Felt>) -> Felt {
1818
// The message hash padding as implemented in `cairo-lang` is not needed here. The hash is
1919
// padded in `cairo-lang` only to make sure the lowest 4 bits won't get truncated, but here it's

0 commit comments

Comments
 (0)