Skip to content

Commit e9ffc69

Browse files
committed
confidential: directly use Read/Write in Encodable/Decodable
We eventually want to remove Encodable and Decodable. To help with that, start by cleaning up a few things: * don't use the traits on u8 or u64 * especially don't use the trait on u64 then call `swap_bytes` to deal with the fact that we want a BE encoding
1 parent c92694b commit e9ffc69

3 files changed

Lines changed: 45 additions & 33 deletions

File tree

src/confidential/asset.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,14 @@ impl fmt::Display for Asset {
120120
impl Encodable for Asset {
121121
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
122122
match *self {
123-
Self::Null => 0u8.consensus_encode(s),
123+
Self::Null => {
124+
s.write_all(&[0u8])?;
125+
Ok(1)
126+
}
124127
Self::Explicit(n) => {
125-
1u8.consensus_encode(&mut s)?;
126-
Ok(1 + n.consensus_encode(&mut s)?)
128+
s.write_all(&[1u8])?;
129+
s.write_all(n.as_byte_array())?;
130+
Ok(1 + EXPLICIT_LEN)
127131
}
128132
Self::Confidential(generator) => {
129133
s.write_all(&generator.serialize())?;
@@ -135,19 +139,19 @@ impl Encodable for Asset {
135139

136140
impl Decodable for Asset {
137141
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
138-
let prefix = u8::consensus_decode(&mut d)?;
142+
let mut buf = [0u8; CONFIDENTIAL_LEN];
143+
d.read_exact(&mut buf[0..1])?;
139144

140-
match prefix {
145+
match buf[0] {
141146
0 => Ok(Self::Null),
142147
1 => {
143-
let explicit = Decodable::consensus_decode(&mut d)?;
144-
Ok(Self::Explicit(explicit))
148+
let mut buf = [0; EXPLICIT_LEN];
149+
d.read_exact(&mut buf)?;
150+
Ok(Self::Explicit(AssetId::from_byte_array(buf)))
145151
}
146152
p if p == CONF_PREFIX_1 || p == CONF_PREFIX_2 => {
147-
let mut comm = [0u8; CONFIDENTIAL_LEN];
148-
comm[0] = p;
149-
d.read_exact(&mut comm[1..])?;
150-
Ok(Self::Confidential(ConfInner::from_slice(&comm[..])?))
153+
d.read_exact(&mut buf[1..])?;
154+
Ok(Self::Confidential(ConfInner::from_slice(&buf[..])?))
151155
}
152156
p => Err(encode::Error::InvalidConfidentialPrefix(p)),
153157
}

src/confidential/nonce.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,14 @@ impl fmt::Display for Nonce {
150150
impl Encodable for Nonce {
151151
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
152152
match *self {
153-
Self::Null => 0u8.consensus_encode(s),
153+
Self::Null => {
154+
s.write_all(&[0u8])?;
155+
Ok(1)
156+
}
154157
Self::Explicit(n) => {
155-
1u8.consensus_encode(&mut s)?;
156-
Ok(1 + n.consensus_encode(&mut s)?)
158+
s.write_all(&[1u8])?;
159+
s.write_all(&n)?;
160+
Ok(1 + EXPLICIT_LEN)
157161
}
158162
Self::Confidential(commitment) => {
159163
s.write_all(&commitment.serialize())?;
@@ -165,19 +169,19 @@ impl Encodable for Nonce {
165169

166170
impl Decodable for Nonce {
167171
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
168-
let prefix = u8::consensus_decode(&mut d)?;
172+
let mut buf = [0u8; CONFIDENTIAL_LEN];
173+
d.read_exact(&mut buf[0..1])?;
169174

170-
match prefix {
175+
match buf[0] {
171176
0 => Ok(Self::Null),
172177
1 => {
173-
let explicit = Decodable::consensus_decode(&mut d)?;
174-
Ok(Self::Explicit(explicit))
178+
let mut buf = [0; EXPLICIT_LEN];
179+
d.read_exact(&mut buf)?;
180+
Ok(Self::Explicit(buf))
175181
}
176182
p if p == CONF_PREFIX_1 || p == CONF_PREFIX_2 => {
177-
let mut comm = [0u8; CONFIDENTIAL_LEN];
178-
comm[0] = p;
179-
d.read_exact(&mut comm[1..])?;
180-
Ok(Self::Confidential(ConfInner::from_slice(&comm)?))
183+
d.read_exact(&mut buf[1..])?;
184+
Ok(Self::Confidential(ConfInner::from_slice(&buf)?))
181185
}
182186
p => Err(encode::Error::InvalidConfidentialPrefix(p)),
183187
}

src/confidential/value.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,14 @@ impl fmt::Display for Value {
123123
impl Encodable for Value {
124124
fn consensus_encode<S: io::Write>(&self, mut s: S) -> Result<usize, encode::Error> {
125125
match *self {
126-
Self::Null => 0u8.consensus_encode(s),
126+
Self::Null => {
127+
s.write_all(&[0u8])?;
128+
Ok(1)
129+
}
127130
Self::Explicit(n) => {
128-
1u8.consensus_encode(&mut s)?;
129-
Ok(1 + u64::swap_bytes(n).consensus_encode(&mut s)?)
131+
s.write_all(&[1u8])?;
132+
s.write_all(&n.to_be_bytes())?;
133+
Ok(1 + EXPLICIT_LEN)
130134
}
131135
Self::Confidential(commitment) => {
132136
s.write_all(&commitment.serialize())?;
@@ -138,19 +142,19 @@ impl Encodable for Value {
138142

139143
impl Decodable for Value {
140144
fn consensus_decode<D: io::Read>(mut d: D) -> Result<Self, encode::Error> {
141-
let prefix = u8::consensus_decode(&mut d)?;
145+
let mut buf = [0u8; CONFIDENTIAL_LEN];
146+
d.read_exact(&mut buf[0..1])?;
142147

143-
match prefix {
148+
match buf[0] {
144149
0 => Ok(Self::Null),
145150
1 => {
146-
let explicit = u64::swap_bytes(Decodable::consensus_decode(&mut d)?);
147-
Ok(Self::Explicit(explicit))
151+
let mut buf = [0; EXPLICIT_LEN];
152+
d.read_exact(&mut buf)?;
153+
Ok(Self::Explicit(u64::from_be_bytes(buf)))
148154
}
149155
p if p == CONF_PREFIX_1 || p == CONF_PREFIX_2 => {
150-
let mut comm = [0u8; CONFIDENTIAL_LEN];
151-
comm[0] = p;
152-
d.read_exact(&mut comm[1..])?;
153-
Ok(Self::Confidential(ConfInner::from_slice(&comm)?))
156+
d.read_exact(&mut buf[1..])?;
157+
Ok(Self::Confidential(ConfInner::from_slice(&buf)?))
154158
}
155159
p => Err(encode::Error::InvalidConfidentialPrefix(p)),
156160
}

0 commit comments

Comments
 (0)