Skip to content

Commit a777942

Browse files
committed
Merge #499: Introduce rustfmt
e0e575d Run cargo fmt (Tobin C. Harding) 41449e4 Prepare codebase for formatting (Tobin C. Harding) 7e3c893 Introduce rustfmt config file (Tobin C. Harding) Pull request description: (Includes the patch from #504, I pulled it out of this to merge faster) Introduce `rustfmt` by doing: - Copy the `rustfmt` config file from `rust-bitcoin` - Prepare the codebase by adding `#[rustfmt::skip]` as needed and doing some manual format improvements. - Run the formatter: `cargo +nightly fmt` - Add formatting checks to CI and the pre-commit hook Thanks in advance for doing the painful review on patch 3. ACKs for top commit: apoelstra: ACK e0e575d Tree-SHA512: 1b6fdbaf81480c0446e660cc3f6ab7ac0697f272187f6fdfd6b95d894a418cde8cf1c423f1d18ebbe03ac5c43489630a35ad07912afaeb6107cfbe7338a9bed7
2 parents 7a00b83 + e0e575d commit a777942

17 files changed

+849
-759
lines changed

examples/sign_verify.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ extern crate bitcoin_hashes;
22
extern crate secp256k1;
33

44
use bitcoin_hashes::{sha256, Hash};
5-
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, ecdsa, Signing, Verification};
6-
7-
fn verify<C: Verification>(secp: &Secp256k1<C>, msg: &[u8], sig: [u8; 64], pubkey: [u8; 33]) -> Result<bool, Error> {
5+
use secp256k1::{ecdsa, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification};
6+
7+
fn verify<C: Verification>(
8+
secp: &Secp256k1<C>,
9+
msg: &[u8],
10+
sig: [u8; 64],
11+
pubkey: [u8; 33],
12+
) -> Result<bool, Error> {
813
let msg = sha256::Hash::hash(msg);
914
let msg = Message::from_slice(&msg)?;
1015
let sig = ecdsa::Signature::from_compact(&sig)?;
@@ -13,7 +18,11 @@ fn verify<C: Verification>(secp: &Secp256k1<C>, msg: &[u8], sig: [u8; 64], pubke
1318
Ok(secp.verify_ecdsa(&msg, &sig, &pubkey).is_ok())
1419
}
1520

16-
fn sign<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<ecdsa::Signature, Error> {
21+
fn sign<C: Signing>(
22+
secp: &Secp256k1<C>,
23+
msg: &[u8],
24+
seckey: [u8; 32],
25+
) -> Result<ecdsa::Signature, Error> {
1726
let msg = sha256::Hash::hash(msg);
1827
let msg = Message::from_slice(&msg)?;
1928
let seckey = SecretKey::from_slice(&seckey)?;
@@ -23,8 +32,14 @@ fn sign<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result
2332
fn main() {
2433
let secp = Secp256k1::new();
2534

26-
let seckey = [59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203, 174, 253, 102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28];
27-
let pubkey = [2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245, 41, 91, 141, 134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54];
35+
let seckey = [
36+
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203, 174, 253,
37+
102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
38+
];
39+
let pubkey = [
40+
2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245, 41, 91, 141,
41+
134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54,
42+
];
2843
let msg = b"This is some message";
2944

3045
let signature = sign(&secp, msg, seckey).unwrap();

examples/sign_verify_recovery.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
21
extern crate bitcoin_hashes;
32
extern crate secp256k1;
43

54
use bitcoin_hashes::{sha256, Hash};
6-
use secp256k1::{Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification, ecdsa};
7-
8-
fn recover<C: Verification>(secp: &Secp256k1<C>,msg: &[u8],sig: [u8; 64],recovery_id: u8) -> Result<PublicKey, Error> {
5+
use secp256k1::{ecdsa, Error, Message, PublicKey, Secp256k1, SecretKey, Signing, Verification};
6+
7+
fn recover<C: Verification>(
8+
secp: &Secp256k1<C>,
9+
msg: &[u8],
10+
sig: [u8; 64],
11+
recovery_id: u8,
12+
) -> Result<PublicKey, Error> {
913
let msg = sha256::Hash::hash(msg);
1014
let msg = Message::from_slice(&msg)?;
1115
let id = ecdsa::RecoveryId::from_i32(recovery_id as i32)?;
@@ -14,7 +18,11 @@ fn recover<C: Verification>(secp: &Secp256k1<C>,msg: &[u8],sig: [u8; 64],recover
1418
secp.recover_ecdsa(&msg, &sig)
1519
}
1620

17-
fn sign_recovery<C: Signing>(secp: &Secp256k1<C>, msg: &[u8], seckey: [u8; 32]) -> Result<ecdsa::RecoverableSignature, Error> {
21+
fn sign_recovery<C: Signing>(
22+
secp: &Secp256k1<C>,
23+
msg: &[u8],
24+
seckey: [u8; 32],
25+
) -> Result<ecdsa::RecoverableSignature, Error> {
1826
let msg = sha256::Hash::hash(msg);
1927
let msg = Message::from_slice(&msg)?;
2028
let seckey = SecretKey::from_slice(&seckey)?;
@@ -25,22 +33,19 @@ fn main() {
2533
let secp = Secp256k1::new();
2634

2735
let seckey = [
28-
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107,
29-
94, 203, 174, 253, 102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
36+
59, 148, 11, 85, 134, 130, 61, 253, 2, 174, 59, 70, 27, 180, 51, 107, 94, 203, 174, 253,
37+
102, 39, 170, 146, 46, 252, 4, 143, 236, 12, 136, 28,
3038
];
3139
let pubkey = PublicKey::from_slice(&[
32-
2,
33-
29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231,
34-
245, 41, 91, 141, 134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54,
35-
]).unwrap();
40+
2, 29, 21, 35, 7, 198, 183, 43, 14, 208, 65, 139, 14, 112, 205, 128, 231, 245, 41, 91, 141,
41+
134, 245, 114, 45, 63, 82, 19, 251, 210, 57, 79, 54,
42+
])
43+
.unwrap();
3644
let msg = b"This is some message";
3745

3846
let signature = sign_recovery(&secp, msg, seckey).unwrap();
3947

4048
let (recovery_id, serialize_sig) = signature.serialize_compact();
4149

42-
assert_eq!(
43-
recover(&secp, msg, serialize_sig, recovery_id.to_i32() as u8),
44-
Ok(pubkey)
45-
);
50+
assert_eq!(recover(&secp, msg, serialize_sig, recovery_id.to_i32() as u8), Ok(pubkey));
4651
}

rustfmt.toml

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,82 @@
1-
disable_all_formatting = true
1+
# Eventually this shoud be: ignore = []
2+
ignore = [
3+
"secp256k1-sys"
4+
]
5+
6+
hard_tabs = false
7+
tab_spaces = 4
8+
newline_style = "Auto"
9+
indent_style = "Block"
10+
11+
max_width = 100 # This is number of characters.
12+
# `use_small_heuristics` is ignored if the granular width config values are explicitly set.
13+
use_small_heuristics = "Max" # "Max" == All granular width settings same as `max_width`.
14+
# # Granular width configuration settings. These are percentages of `max_width`.
15+
# fn_call_width = 60
16+
# attr_fn_like_width = 70
17+
# struct_lit_width = 18
18+
# struct_variant_width = 35
19+
# array_width = 60
20+
# chain_width = 60
21+
# single_line_if_else_max_width = 50
22+
23+
wrap_comments = false
24+
format_code_in_doc_comments = false
25+
comment_width = 100 # Default 80
26+
normalize_comments = false
27+
normalize_doc_attributes = false
28+
format_strings = false
29+
format_macro_matchers = false
30+
format_macro_bodies = true
31+
hex_literal_case = "Preserve"
32+
empty_item_single_line = true
33+
struct_lit_single_line = true
34+
fn_single_line = true # Default false
35+
where_single_line = false
36+
imports_indent = "Block"
37+
imports_layout = "Mixed"
38+
imports_granularity = "Module" # Default "Preserve"
39+
group_imports = "StdExternalCrate" # Default "Preserve"
40+
reorder_imports = true
41+
reorder_modules = true
42+
reorder_impl_items = false
43+
type_punctuation_density = "Wide"
44+
space_before_colon = false
45+
space_after_colon = true
46+
spaces_around_ranges = false
47+
binop_separator = "Front"
48+
remove_nested_parens = true
49+
combine_control_expr = true
50+
overflow_delimited_expr = false
51+
struct_field_align_threshold = 0
52+
enum_discrim_align_threshold = 0
53+
match_arm_blocks = false # Default true
54+
match_arm_leading_pipes = "Never"
55+
force_multiline_blocks = false
56+
fn_args_layout = "Tall"
57+
brace_style = "SameLineWhere"
58+
control_brace_style = "AlwaysSameLine"
59+
trailing_semicolon = true
60+
trailing_comma = "Vertical"
61+
match_block_trailing_comma = false
62+
blank_lines_upper_bound = 1
63+
blank_lines_lower_bound = 0
64+
edition = "2018"
65+
version = "One"
66+
inline_attribute_width = 0
67+
format_generated_files = true
68+
merge_derives = true
69+
use_try_shorthand = false
70+
use_field_init_shorthand = false
71+
force_explicit_abi = true
72+
condense_wildcard_suffixes = false
73+
color = "Auto"
74+
required_version = "1.5.1"
75+
unstable_features = false
76+
disable_all_formatting = false
77+
skip_children = false
78+
hide_parse_errors = false
79+
error_on_line_overflow = false
80+
error_on_unformatted = false
81+
emit_mode = "Files"
82+
make_backup = false

src/constants.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub const SCHNORR_PUBLIC_KEY_SIZE: usize = 32;
4444
pub const KEY_PAIR_SIZE: usize = 96;
4545

4646
/// The Prime for the secp256k1 field element.
47+
#[rustfmt::skip]
4748
pub const FIELD_SIZE: [u8; 32] = [
4849
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
4950
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -52,6 +53,7 @@ pub const FIELD_SIZE: [u8; 32] = [
5253
];
5354

5455
/// The order of the secp256k1 curve.
56+
#[rustfmt::skip]
5557
pub const CURVE_ORDER: [u8; 32] = [
5658
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5759
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
@@ -60,6 +62,7 @@ pub const CURVE_ORDER: [u8; 32] = [
6062
];
6163

6264
/// The X coordinate of the generator.
65+
#[rustfmt::skip]
6366
pub const GENERATOR_X: [u8; 32] = [
6467
0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, 0xac,
6568
0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, 0x07,
@@ -68,6 +71,7 @@ pub const GENERATOR_X: [u8; 32] = [
6871
];
6972

7073
/// The Y coordinate of the generator.
74+
#[rustfmt::skip]
7175
pub const GENERATOR_Y: [u8; 32] = [
7276
0x48, 0x3a, 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65,
7377
0x5d, 0xa4, 0xfb, 0xfc, 0x0e, 0x11, 0x08, 0xa8,

0 commit comments

Comments
 (0)