Skip to content

Commit df7520e

Browse files
committed
Merge #340: Improve documentation
c73eb2f Use 'extra' instead of 'cheap' (Tobin Harding) c79eb97 Remove unnecessary explanation (Tobin Harding) f95e91a Use isn't instead of shouldn't (Tobin Harding) c9e6ca1 Use rust-bitcoin module doc style (Tobin Harding) 3fa6762 Add link to referenced commit (Tobin Harding) f5e68f3 Add ticks around code snippet (Tobin Harding) d25431c Use 3rd person tense for function docs (Tobin Harding) c3be285 Fix size constant docs (Tobin Harding) 5e07e75 Add period to sentences (Tobin Harding) 269bde0 Remove unnecessary capitalisation (Tobin Harding) Pull request description: In a continued effort to find my feet around here, and inspired by issue #128 I've done a codebase wide audit of the docs (primarily just rustdocs but I glanced at `//` docs as well). Each change is in a separate commit so can be removed if resistance is met. (_"resistance is futile"_). I've based the stylistic decisions on [work done](rust-bitcoin/rust-bitcoin#704) in rust-bitcoin. I believe the only controversial change is the last (commit: da161c9 Use rust-bitcoin module doc style), please review that one carefully. ACKs for top commit: apoelstra: ACK c73eb2f Tree-SHA512: 5ea215de3fd23ca2a4f25d8f8d59a85a299044fe495269c43b621291ea50c58856fa8544e36cc109b7bdb1a7a59bcab8711f30113572ddce4509d3b06ff0d3b6
2 parents 3e815b7 + c73eb2f commit df7520e

File tree

6 files changed

+67
-72
lines changed

6 files changed

+67
-72
lines changed

src/constants.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,34 @@
1313
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1414
//
1515

16-
//! # Constants
17-
//! Constants related to the API and the underlying curve
16+
//! Constants related to the API and the underlying curve.
17+
//!
1818
19-
/// The size (in bytes) of a message
19+
/// The size (in bytes) of a message.
2020
pub const MESSAGE_SIZE: usize = 32;
2121

22-
/// The size (in bytes) of a secret key
22+
/// The size (in bytes) of a secret key.
2323
pub const SECRET_KEY_SIZE: usize = 32;
2424

2525
/// The size (in bytes) of a serialized public key.
2626
pub const PUBLIC_KEY_SIZE: usize = 33;
2727

28-
/// The size (in bytes) of an serialized uncompressed public key
28+
/// The size (in bytes) of an serialized uncompressed public key.
2929
pub const UNCOMPRESSED_PUBLIC_KEY_SIZE: usize = 65;
3030

31-
/// The maximum size of a signature
31+
/// The maximum size of a signature.
3232
pub const MAX_SIGNATURE_SIZE: usize = 72;
3333

34-
/// The maximum size of a compact signature
34+
/// The maximum size of a compact signature.
3535
pub const COMPACT_SIGNATURE_SIZE: usize = 64;
3636

37-
/// Size of a Schnorr signature
37+
/// The size of a Schnorr signature.
3838
pub const SCHNORRSIG_SIGNATURE_SIZE: usize = 64;
3939

40-
/// Size of a Schnorr public key
40+
/// The size of a Schnorr public key.
4141
pub const SCHNORRSIG_PUBLIC_KEY_SIZE: usize = 32;
4242

43-
/// Size of a key pair
43+
/// The size of a key pair.
4444
pub const KEY_PAIR_SIZE: usize = 96;
4545

4646
/// The Prime for the secp256k1 field element.
@@ -51,23 +51,23 @@ pub const FIELD_SIZE: [u8; 32] = [
5151
0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xfc, 0x2f
5252
];
5353

54-
/// The order of the secp256k1 curve
54+
/// The order of the secp256k1 curve.
5555
pub const CURVE_ORDER: [u8; 32] = [
5656
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5757
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
5858
0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b,
5959
0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41
6060
];
6161

62-
/// The X coordinate of the generator
62+
/// The X coordinate of the generator.
6363
pub const GENERATOR_X: [u8; 32] = [
6464
0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac,
6565
0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07,
6666
0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, 0xd9,
6767
0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, 0x98
6868
];
6969

70-
/// The Y coordinate of the generator
70+
/// The Y coordinate of the generator.
7171
pub const GENERATOR_Y: [u8; 32] = [
7272
0x48, 0x3a, 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65,
7373
0x5d, 0xa4, 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8,

src/context.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use self::alloc_only::*;
1111

1212
#[cfg(all(feature = "global-context", feature = "std"))]
1313
#[cfg_attr(docsrs, doc(cfg(all(feature = "global-context", feature = "std"))))]
14-
/// Module implementing a singleton pattern for a global `Secp256k1` context
14+
/// Module implementing a singleton pattern for a global `Secp256k1` context.
1515
pub mod global {
1616
#[cfg(feature = "rand-std")]
1717
use rand;
@@ -20,7 +20,7 @@ pub mod global {
2020
use std::sync::Once;
2121
use {Secp256k1, All};
2222

23-
/// Proxy struct for global `SECP256K1` context
23+
/// Proxy struct for global `SECP256K1` context.
2424
#[derive(Debug, Copy, Clone)]
2525
pub struct GlobalContext {
2626
__private: (),
@@ -60,8 +60,8 @@ pub mod global {
6060
}
6161

6262

63-
/// A trait for all kinds of Context's that lets you define the exact flags and a function to deallocate memory.
64-
/// It shouldn't be possible to implement this for types outside this crate.
63+
/// A trait for all kinds of contexts that lets you define the exact flags and a function to
64+
/// deallocate memory. It isn't possible to implement this for types outside this crate.
6565
pub unsafe trait Context : private::Sealed {
6666
/// Flags for the ffi.
6767
const FLAGS: c_uint;
@@ -97,9 +97,6 @@ pub struct AllPreallocated<'buf> {
9797

9898
mod private {
9999
use super::*;
100-
// A trick to prevent users from implementing a trait.
101-
// on one hand this trait is public, on the other it's in a private module
102-
// so it's not visible to anyone besides it's parent (the context module)
103100
pub trait Sealed {}
104101

105102
impl<'buf> Sealed for AllPreallocated<'buf> {}
@@ -289,7 +286,7 @@ unsafe impl<'buf> Context for VerifyOnlyPreallocated<'buf> {
289286
const DESCRIPTION: &'static str = "verification only";
290287

291288
unsafe fn deallocate(_ptr: *mut u8, _size: usize) {
292-
// Allocated by the user
289+
// Allocated by the user.
293290
}
294291
}
295292

@@ -298,7 +295,7 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
298295
const DESCRIPTION: &'static str = "all capabilities";
299296

300297
unsafe fn deallocate(_ptr: *mut u8, _size: usize) {
301-
// Allocated by the user
298+
// Allocated by the user.
302299
}
303300
}
304301

@@ -328,7 +325,7 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
328325
pub fn preallocated_new(buf: &'buf mut [AlignedType]) -> Result<Secp256k1<AllPreallocated<'buf>>, Error> {
329326
Secp256k1::preallocated_gen_new(buf)
330327
}
331-
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context
328+
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context.
332329
pub fn preallocate_size() -> usize {
333330
Self::preallocate_size_gen()
334331
}
@@ -354,12 +351,12 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
354351
}
355352

356353
impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
357-
/// Creates a new Secp256k1 context that can only be used for signing
354+
/// Creates a new Secp256k1 context that can only be used for signing.
358355
pub fn preallocated_signing_only(buf: &'buf mut [AlignedType]) -> Result<Secp256k1<SignOnlyPreallocated<'buf>>, Error> {
359356
Secp256k1::preallocated_gen_new(buf)
360357
}
361358

362-
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context
359+
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context.
363360
#[inline]
364361
pub fn preallocate_signing_size() -> usize {
365362
Self::preallocate_size_gen()
@@ -374,7 +371,7 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
374371
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
375372
/// when generating the context.
376373
/// * The user must handle the freeing of the context(using the correct functions) by himself.
377-
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.,
374+
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
378375
///
379376
pub unsafe fn from_raw_signining_only(raw_ctx: *mut ffi::Context) -> ManuallyDrop<Secp256k1<SignOnlyPreallocated<'buf>>> {
380377
ManuallyDrop::new(Secp256k1 {
@@ -391,7 +388,7 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
391388
Secp256k1::preallocated_gen_new(buf)
392389
}
393390

394-
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context
391+
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for the context.
395392
#[inline]
396393
pub fn preallocate_verification_size() -> usize {
397394
Self::preallocate_size_gen()
@@ -406,7 +403,7 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
406403
/// * The capabilities (All/SignOnly/VerifyOnly) of the context *must* match the flags passed to libsecp256k1
407404
/// when generating the context.
408405
/// * The user must handle the freeing of the context(using the correct functions) by himself.
409-
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.,
406+
/// * This list *is not* exhaustive, and any violation may lead to Undefined Behavior.
410407
///
411408
pub unsafe fn from_raw_verification_only(raw_ctx: *mut ffi::Context) -> ManuallyDrop<Secp256k1<VerifyOnlyPreallocated<'buf>>> {
412409
ManuallyDrop::new(Secp256k1 {

src/ecdh.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1313
//
1414

15-
//! # ECDH
16-
//! Support for shared secret computations
15+
//! Support for shared secret computations.
1716
//!
1817
1918
use core::ptr;
@@ -54,35 +53,35 @@ impl_from_array_len!(SharedSecret, 256, (16 20 28 32 48 64 96 128 256));
5453

5554
impl SharedSecret {
5655

57-
/// Create an empty SharedSecret
56+
/// Creates an empty `SharedSecret`.
5857
pub(crate) fn empty() -> SharedSecret {
5958
SharedSecret {
6059
data: [0u8; 256],
6160
len: 0,
6261
}
6362
}
6463

65-
/// Get a pointer to the underlying data with the specified capacity.
64+
/// Gets a pointer to the underlying data with the specified capacity.
6665
pub(crate) fn get_data_mut_ptr(&mut self) -> *mut u8 {
6766
self.data.as_mut_ptr()
6867
}
6968

70-
/// Get the capacity of the underlying data buffer.
69+
/// Gets the capacity of the underlying data buffer.
7170
pub fn capacity(&self) -> usize {
7271
self.data.len()
7372
}
7473

75-
/// Get the len of the used data.
74+
/// Gets the len of the used data.
7675
pub fn len(&self) -> usize {
7776
self.len
7877
}
7978

80-
/// True if the underlying data buffer is empty.
79+
/// Returns true if the underlying data buffer is empty.
8180
pub fn is_empty(&self) -> bool {
8281
self.data.is_empty()
8382
}
8483

85-
/// Set the length of the object.
84+
/// Sets the length of the object.
8685
pub(crate) fn set_len(&mut self, len: usize) {
8786
debug_assert!(len <= self.data.len());
8887
self.len = len;
@@ -116,7 +115,7 @@ unsafe extern "C" fn c_callback(output: *mut c_uchar, x: *const c_uchar, y: *con
116115
}
117116

118117
impl SharedSecret {
119-
/// Creates a new shared secret from a pubkey and secret key
118+
/// Creates a new shared secret from a pubkey and secret key.
120119
#[inline]
121120
pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret {
122121
let mut ss = SharedSecret::empty();
@@ -138,7 +137,7 @@ impl SharedSecret {
138137
}
139138

140139

141-
/// Creates a new shared secret from a pubkey and secret key with applied custom hash function
140+
/// Creates a new shared secret from a pubkey and secret key with applied custom hash function.
142141
/// The custom hash function must be in the form of `fn(x: [u8;32], y: [u8;32]) -> SharedSecret`
143142
/// `SharedSecret` can be easily created via the `From` impl from arrays.
144143
/// # Examples

src/ecdsa/recovery.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1414
//
1515

16-
//! # Recovery module
1716
//! Provides a signing function that allows recovering the public key from the
1817
//! signature.
18+
//!
1919
2020
use core::ptr;
2121
use key;
@@ -25,11 +25,11 @@ use ffi::recovery as ffi;
2525
use super::*;
2626
use {Verification, Secp256k1, Signing, Message};
2727

28-
/// A tag used for recovering the public key from a compact signature
28+
/// A tag used for recovering the public key from a compact signature.
2929
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
3030
pub struct RecoveryId(i32);
3131

32-
/// An ECDSA signature with a recovery ID for pubkey recovery
32+
/// An ECDSA signature with a recovery ID for pubkey recovery.
3333
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
3434
pub struct RecoverableSignature(ffi::RecoverableSignature);
3535

@@ -53,8 +53,7 @@ pub fn to_i32(self) -> i32 {
5353
impl RecoverableSignature {
5454
#[inline]
5555
/// Converts a compact-encoded byte slice to a signature. This
56-
/// representation is nonstandard and defined by the libsecp256k1
57-
/// library.
56+
/// representation is nonstandard and defined by the libsecp256k1 library.
5857
pub fn from_compact(data: &[u8], recid: RecoveryId) -> Result<RecoverableSignature, Error> {
5958
if data.is_empty() {return Err(Error::InvalidSignature);}
6059

@@ -77,20 +76,20 @@ impl RecoverableSignature {
7776
}
7877
}
7978

80-
/// Obtains a raw pointer suitable for use with FFI functions
79+
/// Obtains a raw pointer suitable for use with FFI functions.
8180
#[inline]
8281
pub fn as_ptr(&self) -> *const ffi::RecoverableSignature {
8382
&self.0
8483
}
8584

86-
/// Obtains a raw mutable pointer suitable for use with FFI functions
85+
/// Obtains a raw mutable pointer suitable for use with FFI functions.
8786
#[inline]
8887
pub fn as_mut_ptr(&mut self) -> *mut ffi::RecoverableSignature {
8988
&mut self.0
9089
}
9190

9291
#[inline]
93-
/// Serializes the recoverable signature in compact format
92+
/// Serializes the recoverable signature in compact format.
9493
pub fn serialize_compact(&self) -> (RecoveryId, [u8; 64]) {
9594
let mut ret = [0u8; 64];
9695
let mut recid = 0i32;
@@ -107,7 +106,7 @@ impl RecoverableSignature {
107106
}
108107

109108
/// Converts a recoverable signature to a non-recoverable one (this is needed
110-
/// for verification
109+
/// for verification).
111110
#[inline]
112111
pub fn to_standard(&self) -> Signature {
113112
unsafe {
@@ -144,7 +143,7 @@ impl CPtr for RecoverableSignature {
144143
}
145144
}
146145

147-
/// Creates a new recoverable signature from a FFI one
146+
/// Creates a new recoverable signature from a FFI one.
148147
impl From<ffi::RecoverableSignature> for RecoverableSignature {
149148
#[inline]
150149
fn from(sig: ffi::RecoverableSignature) -> RecoverableSignature {
@@ -153,7 +152,7 @@ impl From<ffi::RecoverableSignature> for RecoverableSignature {
153152
}
154153

155154
impl<C: Signing> Secp256k1<C> {
156-
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce
155+
/// Constructs a signature for `msg` using the secret key `sk` and RFC6979 nonce.
157156
/// Requires a signing-capable context.
158157
#[deprecated(since = "0.21.0", note = "Use sign_ecdsa_recoverable instead.")]
159158
pub fn sign_recoverable(&self, msg: &Message, sk: &key::SecretKey) -> RecoverableSignature {

src/key.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl str::FromStr for SecretKey {
6262
}
6363
}
6464

65-
/// The number 1 encoded as a secret key
65+
/// The number 1 encoded as a secret key.
6666
pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0,
6767
0, 0, 0, 0, 0, 0, 0, 0,
6868
0, 0, 0, 0, 0, 0, 0, 0,
@@ -355,7 +355,7 @@ impl PublicKey {
355355
unsafe {
356356
let mut pk = ffi::PublicKey::new();
357357
// We can assume the return value because it's not possible to construct
358-
// an invalid `SecretKey` without transmute trickery or something
358+
// an invalid `SecretKey` without transmute trickery or something.
359359
let res = ffi::secp256k1_ec_pubkey_create(secp.ctx, &mut pk, sk.as_c_ptr());
360360
debug_assert_eq!(res, 1);
361361
PublicKey(pk)

0 commit comments

Comments
 (0)