Skip to content

Instantly share code, notes, and snippets.

@dgrijalva
Created August 25, 2010 22:55
Show Gist options
  • Save dgrijalva/550458 to your computer and use it in GitHub Desktop.
Save dgrijalva/550458 to your computer and use it in GitHub Desktop.
commit c9a6811cc39b6fe9a3a8cf460b89eccecc9b5135
Author: Dave Grijalva <[email protected]>
Date: Wed Aug 25 15:52:17 2010 -0700
fixed encoding of negative bignums
diff --git a/lib/bert/encode.rb b/lib/bert/encode.rb
index 98ef5b2..73606f5 100644
--- a/lib/bert/encode.rb
+++ b/lib/bert/encode.rb
@@ -78,7 +78,7 @@ module BERT
end
def write_bignum(num)
- n = (num.to_s(2).size / 8.0).ceil
+ n = (num.abs.to_s(2).size / 8.0).ceil
if n < 256
write_1 SMALL_BIGNUM
write_1 n
diff --git a/test/encoder_test.rb b/test/encoder_test.rb
index b304c3e..0241990 100644
--- a/test/encoder_test.rb
+++ b/test/encoder_test.rb
@@ -82,6 +82,9 @@ class EncoderTest < Test::Unit::TestCase
should "handle bignums" do
bert = [131,110,8,0,0,0,232,137,4,35,199,138].pack('c*')
assert_equal bert, BERT::Encoder.encode(10_000_000_000_000_000_000)
+
+ bert = [131,110,8,1,0,0,232,137,4,35,199,138].pack('c*')
+ assert_equal bert, BERT::Encoder.encode(-10_000_000_000_000_000_000)
end
should "leave other stuff alone" do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment