Skip to content

Serde/bincode serialization of SecretKey and PublicKey are wasteful, compared with newtypes wrapping [u8; 32] or [u8; 33]. #295

@fadedbee

Description

@fadedbee

Using serde/bincode to serialize SecretKey or PublicKey results in an unneeded eight-byte length.

This length is unneeded, as we already know that the SecretKey is 32 bytes and the PublicKey is 33 bytes.

        #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
        struct Hash([u8; 32]);

        let hash = Hash([1u8; 32]);
        let hash_bin = bincode::serialize(&hash).unwrap();
		assert_eq!(hash_bin, [1u8; 32]);

        let skey = SecretKey::from_slice(&[2u8; 32]).unwrap();
        let skey_bin = bincode::serialize(&skey).unwrap();
		assert_eq!(skey_bin, [2u8; 32]);

fails with:

thread 'tests::it_works' panicked at 'assertion failed: `(left == right)`
  left: `[32, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]`,
 right: `[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]`'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions