Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_bigint_bytes: strip leading 0s (and also sometimes leading 255s) #69

Merged
merged 1 commit into from Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/writer/mod.rs
Expand Up @@ -470,6 +470,21 @@ impl<'a> DERWriter<'a> {
/// # }
/// ```
pub fn write_bigint_bytes(mut self, bytes: &[u8], positive: bool) {
let mut bytes = bytes;

// Remove leading zero bytes
while bytes.get(0) == Some(&0) {
bytes = &bytes[1..];
}

if !positive {
// Remove leading 255 bytes
// (the order is important here, [255, 0] should be a fixpoint input)
while bytes.len() > 1 && bytes[0] == 255 && bytes.get(1).unwrap_or(&0) & 128 != 0 {
bytes = &bytes[1..];
}
}

self.write_identifier(TAG_INTEGER, PCBit::Primitive);
if bytes.len() == 0 || bytes[0] == 0 {
self.write_length(1);
Expand Down
38 changes: 38 additions & 0 deletions src/writer/tests.rs
Expand Up @@ -205,6 +205,44 @@ fn test_der_write_u8() {
}
}


#[test]
fn test_der_write_bigint_bytes() {
let tests: &[(_, &[_], &[_])] = &[
// First group: identical to test_der_write_bigint
(false, &(-9223372036854775808_i64).to_be_bytes(), &[2, 8, 128, 0, 0, 0, 0, 0, 0, 0]),
(false, &(-65537_i32).to_be_bytes(), &[2, 3, 254, 255, 255]),
(false, &(-65536_i32).to_be_bytes(), &[2, 3, 255, 0, 0]),
(false, &(-32769_i32).to_be_bytes(), &[2, 3, 255, 127, 255]),
(false, &(-32768_i32).to_be_bytes(), &[2, 2, 128, 0]),
(false, &(-129_i16).to_be_bytes(), &[2, 2, 255, 127]),
(false, &(-128_i16).to_be_bytes(), &[2, 1, 128]),
(false, &(-1_i16).to_be_bytes(), &[2, 1, 255]),
(false, &[0], &[2, 1, 0]),
(true, &[1], &[2, 1, 1]),
(true, &127_i16.to_be_bytes(), &[2, 1, 127]),
(true, &128_i16.to_be_bytes(), &[2, 2, 0, 128]),
(true, &32767_u16.to_be_bytes(), &[2, 2, 127, 255]),
(true, &32768_u16.to_be_bytes(), &[2, 3, 0, 128, 0]),
(true, &65535_u16.to_be_bytes(), &[2, 3, 0, 255, 255]),
(true, &65536_u32.to_be_bytes(), &[2, 3, 1, 0, 0]),
(true, &9223372036854775807_u64.to_be_bytes(), &[2, 8, 127, 255, 255, 255, 255, 255, 255, 255]),
// Second group: more special tests
// Leading zeros are stripped:
(true, &[0, 1, 2, 3], &[2, 3, 1, 2, 3]),
(true, &[0, 0, 1, 2, 3], &[2, 3, 1, 2, 3]),
(true, &[0, 0, 0, 1, 2, 3], &[2, 3, 1, 2, 3]),
// [255, 0] is a fixpoint of normalization (it encodes -256):
(false, &[255, 0], &[2, 2, 255, 0]),
];
for &(is_nonnegative, ref value, edata) in tests {
let data = construct_der(|writer| {
writer.write_bigint_bytes(*value, is_nonnegative)
});
assert_eq!(data, edata);
}
}

#[cfg(feature = "num-bigint")]
#[test]
fn test_der_write_bigint() {
Expand Down