Просмотр исходного кода

Switch from bincode to postcard

The bincode crate is no longer maintained.
Ian Goldberg 1 месяц назад
Родитель
Сommit
47a617c90b
5 измененных файлов с 23 добавлено и 23 удалено
  1. 1 1
      Cargo.toml
  2. 12 12
      cmz-core/src/lib.rs
  3. 3 3
      src/lib.rs
  4. 5 5
      tests/basic.rs
  5. 2 2
      tests/priv_serialize.rs

+ 1 - 1
Cargo.toml

@@ -7,13 +7,13 @@ repository = "https://git-crysp.uwaterloo.ca/SigmaProtocol/cmz"
 description = "A crate to automatically create protocols that use CMZ14 or µCMZ credentials, by specifying an extremely compact description of the protocol."
 
 [dependencies]
-bincode = "1"
 cmz-derive = "=0.2.1"
 ff = "0.13"
 generic_static = "0.2"
 group = "0.13"
 hex = { version = "0.4", features = [ "serde" ] }
 lazy_static = "1"
+postcard = { version = "1", features = [ "alloc" ] }
 rand = "0.8.5"
 serde = { version = "1", features = [ "derive" ] }
 serde_bytes = "0.11"

+ 12 - 12
cmz-core/src/lib.rs

@@ -1378,16 +1378,16 @@ pub fn cmz_core(
             }
 
             impl TryFrom<&[u8]> for ClientState {
-                type Error = bincode::Error;
+                type Error = postcard::Error;
 
-                fn try_from(bytes: &[u8]) -> bincode::Result<ClientState> {
-                    bincode::deserialize::<ClientState>(bytes)
+                fn try_from(bytes: &[u8]) -> postcard::Result<ClientState> {
+                    postcard::from_bytes::<ClientState>(bytes)
                 }
             }
 
             impl From<&ClientState> for Vec<u8> {
                 fn from(req: &ClientState) -> Vec<u8> {
-                    bincode::serialize(req).unwrap()
+                    postcard::to_allocvec(req).unwrap()
                 }
             }
 
@@ -1411,16 +1411,16 @@ pub fn cmz_core(
             }
 
             impl TryFrom<&[u8]> for Request {
-                type Error = bincode::Error;
+                type Error = postcard::Error;
 
-                fn try_from(bytes: &[u8]) -> bincode::Result<Request> {
-                    bincode::deserialize::<Request>(bytes)
+                fn try_from(bytes: &[u8]) -> postcard::Result<Request> {
+                    postcard::from_bytes::<Request>(bytes)
                 }
             }
 
             impl From<&Request> for Vec<u8> {
                 fn from(req: &Request) -> Vec<u8> {
-                    bincode::serialize(req).unwrap()
+                    postcard::to_allocvec(req).unwrap()
                 }
             }
 
@@ -1437,16 +1437,16 @@ pub fn cmz_core(
             }
 
             impl TryFrom<&[u8]> for Reply {
-                type Error = bincode::Error;
+                type Error = postcard::Error;
 
-                fn try_from(bytes: &[u8]) -> bincode::Result<Reply> {
-                    bincode::deserialize::<Reply>(bytes)
+                fn try_from(bytes: &[u8]) -> postcard::Result<Reply> {
+                    postcard::from_bytes::<Reply>(bytes)
                 }
             }
 
             impl From<&Reply> for Vec<u8> {
                 fn from(rep: &Reply) -> Vec<u8> {
-                    bincode::serialize(rep).unwrap()
+                    postcard::to_allocvec(rep).unwrap()
                 }
             }
 

+ 3 - 3
src/lib.rs

@@ -425,13 +425,13 @@ where
     /// default serializer skips the private key for safety reasons.
     fn serialize_with_privkey(&self) -> Vec<u8> {
         // Accomplish this by serializing the pair (privkey, self)
-        bincode::serialize(&(self.get_privkey(), self)).unwrap()
+        postcard::to_allocvec(&(self.get_privkey(), self)).unwrap()
     }
 
     /// Deserialize this credential, _including_ the private key.  The
     /// default serializer skips the private key for safety reasons.
-    fn deserialize_with_privkey(bytes: &[u8]) -> bincode::Result<Self> {
-        let (privkey, mut cred) = bincode::deserialize::<(CMZPrivkey<Self::Point>, Self)>(bytes)?;
+    fn deserialize_with_privkey(bytes: &[u8]) -> postcard::Result<Self> {
+        let (privkey, mut cred) = postcard::from_bytes::<(CMZPrivkey<Self::Point>, Self)>(bytes)?;
         cred.set_privkey(&privkey);
         Ok(cred)
     }

+ 5 - 5
tests/basic.rs

@@ -26,18 +26,18 @@ fn test_basic() {
     let (privkey, pubkey) = Basic::cmz14_gen_keys(&mut rng);
 
     // Serialize and deserialize
-    let privkey_bytes = bincode::serialize(&privkey).unwrap();
-    let pubkey_bytes = bincode::serialize(&pubkey).unwrap();
+    let privkey_bytes = postcard::to_allocvec(&privkey).unwrap();
+    let pubkey_bytes = postcard::to_allocvec(&pubkey).unwrap();
 
-    let privkey_serde = bincode::deserialize::<CMZPrivkey<RistrettoPoint>>(&privkey_bytes).unwrap();
-    let pubkey_serde = bincode::deserialize::<CMZPubkey<RistrettoPoint>>(&pubkey_bytes).unwrap();
+    let privkey_serde = postcard::from_bytes::<CMZPrivkey<RistrettoPoint>>(&privkey_bytes).unwrap();
+    let pubkey_serde = postcard::from_bytes::<CMZPubkey<RistrettoPoint>>(&pubkey_bytes).unwrap();
 
     assert!(privkey == privkey_serde);
     assert!(pubkey == pubkey_serde);
 
     let mut basic_cred = Basic::using_privkey(&privkey_serde);
 
-    let basic_cred_bytes = bincode::serialize(&basic_cred).unwrap();
+    let basic_cred_bytes = postcard::to_allocvec(&basic_cred).unwrap();
 
     println!("{:#?}", basic_cred);
 

+ 2 - 2
tests/priv_serialize.rs

@@ -33,9 +33,9 @@ fn test_default_serialize() {
     basic_cred.attr2 = Some(Scalar::ONE);
     basic_cred.create_MAC(&mut rng, &privkey).unwrap();
 
-    let basic_cred_bytes = bincode::serialize(&basic_cred).unwrap();
+    let basic_cred_bytes = postcard::to_allocvec(&basic_cred).unwrap();
 
-    let basic_cred_deser = bincode::deserialize::<Basic>(&basic_cred_bytes).unwrap();
+    let basic_cred_deser = postcard::from_bytes::<Basic>(&basic_cred_bytes).unwrap();
 
     assert_eq!(
         *basic_cred_deser.get_privkey(),