Skip to content

Commit 2c6bc26

Browse files
committed
more changes
1 parent a0a2fae commit 2c6bc26

4 files changed

Lines changed: 55 additions & 31 deletions

File tree

cryptography/src/bls12381/primitives/group.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use blst::{
2222
};
2323
use bytes::{Buf, BufMut};
2424
use commonware_codec::{
25+
varint::UInt,
26+
EncodeSize,
2527
Error::{self, Invalid},
2628
FixedSize, Read, ReadExt, Write,
2729
};
@@ -347,21 +349,23 @@ impl Share {
347349

348350
impl Write for Share {
349351
fn write(&self, buf: &mut impl BufMut) {
350-
self.index.write(buf);
352+
UInt(self.index).write(buf);
351353
self.private.write(buf);
352354
}
353355
}
354356

355357
impl Read for Share {
356358
fn read_cfg(buf: &mut impl Buf, _: &()) -> Result<Self, Error> {
357-
let index = u32::read(buf)?;
359+
let index = UInt::read(buf)?.into();
358360
let private = Private::read(buf)?;
359361
Ok(Self { index, private })
360362
}
361363
}
362364

363-
impl FixedSize for Share {
364-
const SIZE: usize = u32::SIZE + Private::SIZE;
365+
impl EncodeSize for Share {
366+
fn encode_size(&self) -> usize {
367+
UInt(self.index).encode_size() + self.private.encode_size()
368+
}
365369
}
366370

367371
impl Display for Share {

examples/vrf/src/handlers/wire.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bytes::{Buf, BufMut};
2-
use commonware_codec::{EncodeSize, Error, FixedSize, Read, ReadExt, ReadRangeExt, Write};
2+
use commonware_codec::{varint::UInt, EncodeSize, Error, Read, ReadExt, ReadRangeExt, Write};
33
use commonware_cryptography::bls12381::primitives::{
44
group,
55
poly::{self, Eval},
@@ -20,22 +20,22 @@ pub struct Dkg<Sig: Array> {
2020

2121
impl<Sig: Array> Write for Dkg<Sig> {
2222
fn write(&self, buf: &mut impl BufMut) {
23-
self.round.write(buf);
23+
UInt(self.round).write(buf);
2424
self.payload.write(buf);
2525
}
2626
}
2727

2828
impl<Sig: Array> Read<usize> for Dkg<Sig> {
2929
fn read_cfg(buf: &mut impl Buf, num_players: &usize) -> Result<Self, Error> {
30-
let round = u64::read(buf)?;
30+
let round = UInt::read(buf)?.into();
3131
let payload = Payload::<Sig>::read_cfg(buf, num_players)?;
3232
Ok(Self { round, payload })
3333
}
3434
}
3535

3636
impl<Sig: Array> EncodeSize for Dkg<Sig> {
3737
fn encode_size(&self) -> usize {
38-
self.round.encode_size() + self.payload.encode_size()
38+
UInt(self.round).encode_size() + self.payload.encode_size()
3939
}
4040
}
4141

@@ -122,7 +122,7 @@ impl<Sig: Array> Write for Payload<Sig> {
122122
signature,
123123
} => {
124124
buf.put_u8(2);
125-
public_key.write(buf);
125+
UInt(*public_key).write(buf);
126126
signature.write(buf);
127127
}
128128
Payload::Commitment {
@@ -163,7 +163,7 @@ impl<Sig: Array> Read<usize> for Payload<Sig> {
163163
share: group::Share::read(buf)?,
164164
},
165165
2 => Payload::Ack {
166-
public_key: u32::read(buf)?,
166+
public_key: UInt::read(buf)?.into(),
167167
signature: Sig::read(buf)?,
168168
},
169169
3 => {
@@ -195,8 +195,11 @@ impl<Sig: Array> EncodeSize for Payload<Sig> {
195195
fn encode_size(&self) -> usize {
196196
1 + match self {
197197
Payload::Start { group } => group.encode_size(),
198-
Payload::Share { commitment, .. } => commitment.encode_size() + group::Share::SIZE,
199-
Payload::Ack { .. } => u32::SIZE + Sig::SIZE,
198+
Payload::Share { commitment, share } => commitment.encode_size() + share.encode_size(),
199+
Payload::Ack {
200+
public_key,
201+
signature,
202+
} => UInt(*public_key).encode_size() + signature.encode_size(),
200203
Payload::Commitment {
201204
commitment,
202205
acks,
@@ -226,27 +229,29 @@ pub struct Vrf {
226229

227230
impl Write for Vrf {
228231
fn write(&self, buf: &mut impl BufMut) {
229-
self.round.write(buf);
232+
UInt(self.round).write(buf);
230233
self.signature.write(buf);
231234
}
232235
}
233236

234237
impl Read for Vrf {
235238
fn read_cfg(buf: &mut impl Buf, _: &()) -> Result<Self, Error> {
236-
let round = u64::read(buf)?;
239+
let round = UInt::read(buf)?.into();
237240
let signature = Eval::<group::Signature>::read(buf)?;
238241
Ok(Self { round, signature })
239242
}
240243
}
241244

242-
impl FixedSize for Vrf {
243-
const SIZE: usize = u64::SIZE + Eval::<group::Signature>::SIZE;
245+
impl EncodeSize for Vrf {
246+
fn encode_size(&self) -> usize {
247+
UInt(self.round).encode_size() + self.signature.encode_size()
248+
}
244249
}
245250

246251
#[cfg(test)]
247252
mod tests {
248253
use super::*;
249-
use commonware_codec::{Decode, DecodeExt, Encode};
254+
use commonware_codec::{Decode, DecodeExt, Encode, FixedSize};
250255
use commonware_cryptography::{
251256
bls12381::primitives::{
252257
group::{self, Element},

p2p/src/authenticated/types.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,20 @@ pub struct BitVec {
110110

111111
impl EncodeSize for BitVec {
112112
fn encode_size(&self) -> usize {
113-
self.index.encode_size() + self.bits.encode_size()
113+
UInt(self.index).encode_size() + self.bits.encode_size()
114114
}
115115
}
116116

117117
impl Write for BitVec {
118118
fn write(&self, buf: &mut impl BufMut) {
119-
self.index.write(buf);
119+
UInt(self.index).write(buf);
120120
self.bits.write(buf);
121121
}
122122
}
123123

124124
impl Read<usize> for BitVec {
125125
fn read_cfg(buf: &mut impl Buf, max_bits: &usize) -> Result<Self, Error> {
126-
let index = u64::read(buf)?;
126+
let index = UInt::read(buf)?.into();
127127
let bits = UtilsBitVec::read_cfg(buf, &..=*max_bits)?;
128128
Ok(Self { index, bits })
129129
}
@@ -138,7 +138,7 @@ pub struct PeerInfo<C: Verifier> {
138138
/// The socket address of the peer.
139139
pub socket: SocketAddr,
140140

141-
/// The timestamp at which the socket was signed over.
141+
/// The timestamp (epoch milliseconds) at which the socket was signed over.
142142
pub timestamp: u64,
143143

144144
/// The public key of the peer.
@@ -163,7 +163,7 @@ impl<C: Verifier> PeerInfo<C> {
163163
impl<C: Verifier> EncodeSize for PeerInfo<C> {
164164
fn encode_size(&self) -> usize {
165165
self.socket.encode_size()
166-
+ self.timestamp.encode_size()
166+
+ UInt(self.timestamp).encode_size()
167167
+ self.public_key.encode_size()
168168
+ self.signature.encode_size()
169169
}
@@ -172,7 +172,7 @@ impl<C: Verifier> EncodeSize for PeerInfo<C> {
172172
impl<C: Verifier> Write for PeerInfo<C> {
173173
fn write(&self, buf: &mut impl BufMut) {
174174
self.socket.write(buf);
175-
self.timestamp.write(buf);
175+
UInt(self.timestamp).write(buf);
176176
self.public_key.write(buf);
177177
self.signature.write(buf);
178178
}
@@ -181,7 +181,7 @@ impl<C: Verifier> Write for PeerInfo<C> {
181181
impl<C: Verifier> Read for PeerInfo<C> {
182182
fn read_cfg(buf: &mut impl Buf, _: &()) -> Result<Self, Error> {
183183
let socket = SocketAddr::read(buf)?;
184-
let timestamp = u64::read(buf)?;
184+
let timestamp = UInt::read(buf)?.into();
185185
let public_key = C::PublicKey::read(buf)?;
186186
let signature = C::Signature::read(buf)?;
187187
Ok(PeerInfo {

stream/src/public_key/handshake.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
use super::x25519;
22
use crate::Error;
33
use bytes::{Buf, BufMut};
4-
use commonware_codec::{Encode, Error as CodecError, FixedSize, Read, ReadExt, Write};
4+
use commonware_codec::{
5+
varint::UInt, Encode, EncodeSize, Error as CodecError, Read, ReadExt, Write,
6+
};
57
use commonware_cryptography::Scheme;
68
use commonware_runtime::Clock;
79
use commonware_utils::SystemTimeExt;
810
use std::time::Duration;
911

1012
/// Handshake information that is signed over by the sender.
1113
pub struct Info<C: Scheme> {
14+
/// The public key of the recipient.
1215
recipient: C::PublicKey,
16+
17+
/// The ephemeral public key of the sender.
18+
///
19+
/// This is used to derive the shared secret for the encrypted connection.
1320
ephemeral_public_key: x25519::PublicKey,
21+
22+
/// Timestamp of the handshake (in epoch milliseconds).
1423
timestamp: u64,
1524
}
1625

@@ -32,15 +41,15 @@ impl<C: Scheme> Write for Info<C> {
3241
fn write(&self, buf: &mut impl BufMut) {
3342
self.recipient.write(buf);
3443
self.ephemeral_public_key.write(buf);
35-
self.timestamp.write(buf);
44+
UInt(self.timestamp).write(buf);
3645
}
3746
}
3847

3948
impl<C: Scheme> Read for Info<C> {
4049
fn read_cfg(buf: &mut impl Buf, _: &()) -> Result<Self, CodecError> {
4150
let recipient = C::PublicKey::read(buf)?;
4251
let ephemeral_public_key = x25519::PublicKey::read(buf)?;
43-
let timestamp = u64::read(buf)?;
52+
let timestamp = UInt::read(buf)?.into();
4453
Ok(Info {
4554
recipient,
4655
ephemeral_public_key,
@@ -49,8 +58,12 @@ impl<C: Scheme> Read for Info<C> {
4958
}
5059
}
5160

52-
impl<C: Scheme> FixedSize for Info<C> {
53-
const SIZE: usize = C::PublicKey::SIZE + x25519::PublicKey::SIZE + u64::SIZE;
61+
impl<C: Scheme> EncodeSize for Info<C> {
62+
fn encode_size(&self) -> usize {
63+
self.recipient.encode_size()
64+
+ self.ephemeral_public_key.encode_size()
65+
+ UInt(self.timestamp).encode_size()
66+
}
5467
}
5568

5669
// Allows recipient to verify that the sender has the private key
@@ -159,8 +172,10 @@ impl<C: Scheme> Read for Signed<C> {
159172
}
160173
}
161174

162-
impl<C: Scheme> FixedSize for Signed<C> {
163-
const SIZE: usize = Info::<C>::SIZE + C::PublicKey::SIZE + C::Signature::SIZE;
175+
impl<C: Scheme> EncodeSize for Signed<C> {
176+
fn encode_size(&self) -> usize {
177+
self.info.encode_size() + self.signer.encode_size() + self.signature.encode_size()
178+
}
164179
}
165180

166181
#[cfg(test)]

0 commit comments

Comments
 (0)