Skip to content

Commit 2a4ce2d

Browse files
committed
confidential: enable use_self lint
This greatly reduces the diff between the different confidential commitments.
1 parent 9a4c8b8 commit 2a4ce2d

6 files changed

Lines changed: 127 additions & 126 deletions

File tree

src/confidential/asset.rs

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -32,53 +32,53 @@ impl Asset {
3232
asset: AssetId,
3333
bf: AssetBlindingFactor,
3434
) -> Self {
35-
Asset::Confidential(Generator::new_blinded(secp, asset.into_tag(), bf.into_inner()))
35+
Self::Confidential(Generator::new_blinded(secp, asset.into_tag(), bf.into_inner()))
3636
}
3737

3838
/// Serialized length, in bytes
3939
pub fn encoded_length(&self) -> usize {
4040
match *self {
41-
Asset::Null => 1,
42-
Asset::Explicit(..) => 33,
43-
Asset::Confidential(..) => 33,
41+
Self::Null => 1,
42+
Self::Explicit(..) => 33,
43+
Self::Confidential(..) => 33,
4444
}
4545
}
4646

4747
/// Create from commitment.
4848
pub fn from_commitment(bytes: &[u8]) -> Result<Self, encode::Error> {
49-
Ok(Asset::Confidential(Generator::from_slice(bytes)?))
49+
Ok(Self::Confidential(Generator::from_slice(bytes)?))
5050
}
5151

5252
/// Check if the object is null.
53-
pub fn is_null(&self) -> bool { matches!(*self, Asset::Null) }
53+
pub fn is_null(&self) -> bool { matches!(*self, Self::Null) }
5454

5555
/// Check if the object is explicit.
56-
pub fn is_explicit(&self) -> bool { matches!(*self, Asset::Explicit(_)) }
56+
pub fn is_explicit(&self) -> bool { matches!(*self, Self::Explicit(_)) }
5757

5858
/// Check if the object is confidential.
59-
pub fn is_confidential(&self) -> bool { matches!(*self, Asset::Confidential(_)) }
59+
pub fn is_confidential(&self) -> bool { matches!(*self, Self::Confidential(_)) }
6060

6161
/// Returns the explicit inner value.
62-
/// Returns [None] if [`Asset::is_explicit`] returns false.
62+
/// Returns [None] if [`Self::is_explicit`] returns false.
6363
pub fn explicit(&self) -> Option<AssetId> {
6464
match *self {
65-
Asset::Explicit(i) => Some(i),
65+
Self::Explicit(i) => Some(i),
6666
_ => None,
6767
}
6868
}
6969

7070
/// Returns the confidential commitment in case of a confidential value.
71-
/// Returns [None] if [`Asset::is_confidential`] returns false.
71+
/// Returns [None] if [`Self::is_confidential`] returns false.
7272
pub fn commitment(&self) -> Option<Generator> {
7373
match *self {
74-
Asset::Confidential(i) => Some(i),
74+
Self::Confidential(i) => Some(i),
7575
_ => None,
7676
}
7777
}
7878

7979
/// Internally used function for getting the generator from asset
8080
/// Used in the amount verification check
81-
/// Returns [`None`] is the asset is [`Asset::Null`]
81+
/// Returns [`None`] is the asset is [`Self::Null`]
8282
/// Converts a explicit asset into a generator and returns the confidential
8383
/// generator as is.
8484
pub fn into_asset_gen<C: secp256k1_zkp::Signing>(
@@ -88,36 +88,36 @@ impl Asset {
8888
match self {
8989
// Only error is Null error which is dealt with later
9090
// when we have more context information about it.
91-
Asset::Null => None,
92-
Asset::Explicit(x) => Some(Generator::new_unblinded(secp, x.into_tag())),
93-
Asset::Confidential(gen) => Some(gen),
91+
Self::Null => None,
92+
Self::Explicit(x) => Some(Generator::new_unblinded(secp, x.into_tag())),
93+
Self::Confidential(gen) => Some(gen),
9494
}
9595
}
9696
}
9797

9898
impl From<Generator> for Asset {
99-
fn from(from: Generator) -> Self { Asset::Confidential(from) }
99+
fn from(from: Generator) -> Self { Self::Confidential(from) }
100100
}
101101

102102
impl fmt::Display for Asset {
103103
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104104
match *self {
105-
Asset::Null => f.write_str("null"),
106-
Asset::Explicit(n) => write!(f, "{}", n),
107-
Asset::Confidential(generator) => write!(f, "{:02x}", generator),
105+
Self::Null => f.write_str("null"),
106+
Self::Explicit(n) => write!(f, "{}", n),
107+
Self::Confidential(generator) => write!(f, "{:02x}", generator),
108108
}
109109
}
110110
}
111111

112112
impl Encodable for Asset {
113113
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
114114
match *self {
115-
Asset::Null => 0u8.consensus_encode(s),
116-
Asset::Explicit(n) => {
115+
Self::Null => 0u8.consensus_encode(s),
116+
Self::Explicit(n) => {
117117
1u8.consensus_encode(&mut s)?;
118118
Ok(1 + n.consensus_encode(&mut s)?)
119119
}
120-
Asset::Confidential(generator) => {
120+
Self::Confidential(generator) => {
121121
s.write_all(&generator.serialize())?;
122122
Ok(33)
123123
}
@@ -130,16 +130,16 @@ impl Decodable for Asset {
130130
let prefix = u8::consensus_decode(&mut d)?;
131131

132132
match prefix {
133-
0 => Ok(Asset::Null),
133+
0 => Ok(Self::Null),
134134
1 => {
135135
let explicit = Decodable::consensus_decode(&mut d)?;
136-
Ok(Asset::Explicit(explicit))
136+
Ok(Self::Explicit(explicit))
137137
}
138138
p if p == 0x0a || p == 0x0b => {
139139
let mut comm = [0u8; 33];
140140
comm[0] = p;
141141
d.read_exact(&mut comm[1..])?;
142-
Ok(Asset::Confidential(Generator::from_slice(&comm[..])?))
142+
Ok(Self::Confidential(Generator::from_slice(&comm[..])?))
143143
}
144144
p => Err(encode::Error::InvalidConfidentialPrefix(p)),
145145
}
@@ -152,18 +152,18 @@ impl Serialize for Asset {
152152
use serde::ser::SerializeSeq;
153153

154154
let seq_len = match *self {
155-
Asset::Null => 1,
156-
Asset::Explicit(_) | Asset::Confidential(_) => 2,
155+
Self::Null => 1,
156+
Self::Explicit(_) | Self::Confidential(_) => 2,
157157
};
158158
let mut seq = s.serialize_seq(Some(seq_len))?;
159159

160160
match *self {
161-
Asset::Null => seq.serialize_element(&0u8)?,
162-
Asset::Explicit(n) => {
161+
Self::Null => seq.serialize_element(&0u8)?,
162+
Self::Explicit(n) => {
163163
seq.serialize_element(&1u8)?;
164164
seq.serialize_element(&n)?;
165165
}
166-
Asset::Confidential(commitment) => {
166+
Self::Confidential(commitment) => {
167167
seq.serialize_element(&2u8)?;
168168
seq.serialize_element(&commitment)?;
169169
}
@@ -185,16 +185,16 @@ impl<'de> Deserialize<'de> for Asset {
185185
f.write_str("a committed value")
186186
}
187187

188-
fn visit_seq<A: SeqAccess<'de>>(self, mut access: A) -> Result<Asset, A::Error> {
188+
fn visit_seq<A: SeqAccess<'de>>(self, mut access: A) -> Result<Self::Value, A::Error> {
189189
let prefix = access.next_element::<u8>()?;
190190
match prefix {
191-
Some(0) => Ok(Asset::Null),
191+
Some(0) => Ok(Self::Value::Null),
192192
Some(1) => match access.next_element()? {
193-
Some(x) => Ok(Asset::Explicit(x)),
193+
Some(x) => Ok(Self::Value::Explicit(x)),
194194
None => Err(A::Error::custom("missing explicit asset")),
195195
},
196196
Some(2) => match access.next_element()? {
197-
Some(x) => Ok(Asset::Confidential(x)),
197+
Some(x) => Ok(Self::Value::Confidential(x)),
198198
None => Err(A::Error::custom("missing generator")),
199199
},
200200
_ => Err(A::Error::custom("wrong or missing prefix")),
@@ -212,27 +212,27 @@ pub struct AssetBlindingFactor(pub(crate) Tweak);
212212

213213
impl AssetBlindingFactor {
214214
/// Generate random asset blinding factor.
215-
pub fn new<R: Rng>(rng: &mut R) -> Self { AssetBlindingFactor(Tweak::new(rng)) }
215+
pub fn new<R: Rng>(rng: &mut R) -> Self { Self(Tweak::new(rng)) }
216216

217217
/// Parse a blinding factor from a 64-character hex string.
218218
#[deprecated(since = "0.27.0", note = "use s.parse() instead")]
219219
pub fn from_hex(s: &str) -> Result<Self, encode::Error> { s.parse() }
220220

221221
/// Create from bytes.
222222
pub fn from_byte_array(bytes: [u8; 32]) -> Result<Self, secp256k1_zkp::Error> {
223-
Ok(AssetBlindingFactor(Tweak::from_inner(bytes)?))
223+
Ok(Self(Tweak::from_inner(bytes)?))
224224
}
225225

226226
/// Create from bytes.
227227
pub fn from_slice(bytes: &[u8]) -> Result<Self, secp256k1_zkp::Error> {
228-
Ok(AssetBlindingFactor(Tweak::from_slice(bytes)?))
228+
Ok(Self(Tweak::from_slice(bytes)?))
229229
}
230230

231231
/// Returns the inner value.
232232
pub fn into_inner(self) -> Tweak { self.0 }
233233

234234
/// Get a unblinded/zero `AssetBlinding` factor
235-
pub fn zero() -> Self { AssetBlindingFactor(ZERO_TWEAK) }
235+
pub fn zero() -> Self { Self(ZERO_TWEAK) }
236236
}
237237

238238
impl core::borrow::Borrow<[u8]> for AssetBlindingFactor {
@@ -254,7 +254,7 @@ impl str::FromStr for AssetBlindingFactor {
254254
slice.reverse();
255255

256256
let inner = Tweak::from_inner(slice)?;
257-
Ok(AssetBlindingFactor(inner))
257+
Ok(Self(inner))
258258
}
259259
}
260260

@@ -271,7 +271,7 @@ impl Serialize for AssetBlindingFactor {
271271

272272
#[cfg(feature = "serde")]
273273
impl<'de> Deserialize<'de> for AssetBlindingFactor {
274-
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<AssetBlindingFactor, D::Error> {
274+
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
275275
if d.is_human_readable() {
276276
struct HexVisitor;
277277

src/confidential/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
//! Structures representing Pedersen commitments of various types
1818
//!
1919
20+
#![warn(clippy::use_self)]
21+
2022
mod asset;
2123
mod nonce;
2224
mod range_proof;
@@ -47,10 +49,10 @@ pub enum TweakHexDecodeError {
4749
impl fmt::Display for TweakHexDecodeError {
4850
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4951
match self {
50-
TweakHexDecodeError::InvalidHex(err) => {
52+
Self::InvalidHex(err) => {
5153
write!(f, "Invalid hex: {}", err)
5254
}
53-
TweakHexDecodeError::InvalidTweak(err) => {
55+
Self::InvalidTweak(err) => {
5456
write!(f, "Invalid tweak: {}", err)
5557
}
5658
}
@@ -59,28 +61,28 @@ impl fmt::Display for TweakHexDecodeError {
5961

6062
#[doc(hidden)]
6163
impl From<hex::DecodeFixedLengthBytesError> for TweakHexDecodeError {
62-
fn from(err: hex::DecodeFixedLengthBytesError) -> Self { TweakHexDecodeError::InvalidHex(err) }
64+
fn from(err: hex::DecodeFixedLengthBytesError) -> Self { Self::InvalidHex(err) }
6365
}
6466

6567
#[doc(hidden)]
6668
impl From<secp256k1_zkp::Error> for TweakHexDecodeError {
67-
fn from(err: secp256k1_zkp::Error) -> Self { TweakHexDecodeError::InvalidTweak(err) }
69+
fn from(err: secp256k1_zkp::Error) -> Self { Self::InvalidTweak(err) }
6870
}
6971

7072
impl From<TweakHexDecodeError> for encode::Error {
7173
fn from(value: TweakHexDecodeError) -> Self {
7274
match value {
73-
TweakHexDecodeError::InvalidHex(err) => encode::Error::HexFixedError(err),
74-
TweakHexDecodeError::InvalidTweak(err) => encode::Error::Secp256k1zkp(err),
75+
TweakHexDecodeError::InvalidHex(err) => Self::HexFixedError(err),
76+
TweakHexDecodeError::InvalidTweak(err) => Self::Secp256k1zkp(err),
7577
}
7678
}
7779
}
7880

7981
impl std::error::Error for TweakHexDecodeError {
8082
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
8183
match self {
82-
TweakHexDecodeError::InvalidHex(err) => Some(err),
83-
TweakHexDecodeError::InvalidTweak(err) => Some(err),
84+
Self::InvalidHex(err) => Some(err),
85+
Self::InvalidTweak(err) => Some(err),
8486
}
8587
}
8688
}

0 commit comments

Comments
 (0)