Skip to content

Commit

Permalink
Fix incorrect number serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Nov 15, 2023
1 parent 0f70262 commit 8690886
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
1 change: 1 addition & 0 deletions clvm-traits/src/from_clvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ mod tests {
assert_eq!(decode(a, "81e5"), Ok(-27i32));
assert_eq!(decode(a, "80"), Ok(-0));
assert_eq!(decode(a, "8180"), Ok(-128i8));
assert_eq!(decode(a, "8600e8d4a51000"), Ok(1000000000000u64));
}

#[test]
Expand Down
30 changes: 11 additions & 19 deletions clvm-traits/src/to_clvm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clvmr::allocator::NodePtr;
use num_bigint::BigInt;

use crate::{ClvmValue, ToClvmError};

Expand Down Expand Up @@ -26,24 +27,13 @@ macro_rules! to_clvm {
};
}

pub fn simplify_int_bytes(bytes: &[u8]) -> &[u8] {
let mut slice = bytes;

// Check if the number is negative
let is_negative = !bytes.is_empty() && (bytes[0] & 0x80) != 0;

if is_negative {
// Remove leading 0xFF for negative numbers
while slice.len() > 1 && slice[0] == 0xFF && (slice[1] & 0x80) == 0x80 {
slice = &slice[1..];
}
} else {
// Remove leading zeros for positive numbers
while !slice.is_empty() && slice[0] == 0 {
slice = &slice[1..];
pub fn simplify_int_bytes(mut slice: &[u8]) -> &[u8] {
while (!slice.is_empty()) && (slice[0] == 0) {
if slice.len() > 1 && (slice[1] & 0x80 == 0x80) {
break;
}
slice = &slice[1..];
}

slice
}

Expand All @@ -54,9 +44,10 @@ macro_rules! clvm_ints {
Node: Clone,
{
to_clvm!(Node, self, f, {
let bytes = self.to_be_bytes();
let slice = simplify_int_bytes(&bytes);
f(ClvmValue::Atom(slice))
let bytes = BigInt::from(*self);
f(ClvmValue::Atom(simplify_int_bytes(
&bytes.to_signed_bytes_be(),
)))
});
}
};
Expand Down Expand Up @@ -201,6 +192,7 @@ mod tests {
assert_eq!(encode(a, -27i32), Ok("81e5".to_owned()));
assert_eq!(encode(a, -0), Ok("80".to_owned()));
assert_eq!(encode(a, -128i8), Ok("8180".to_owned()));
assert_eq!(encode(a, 1000000000000u64), Ok("8600e8d4a51000".to_owned()));
}

#[test]
Expand Down

0 comments on commit 8690886

Please sign in to comment.