Skip to content

Commit

Permalink
math.big: fix `assert big.integer_from_int(1) == big.integer_from_byt…
Browse files Browse the repository at this point in the history
…es([u8(0), 0, 0, 0, 1])` (fix vlang#23115) (vlang#23124)
  • Loading branch information
spytheman authored Dec 10, 2024
1 parent 6db0a8b commit ef7fdd0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
20 changes: 20 additions & 0 deletions vlib/math/big/big_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -849,3 +849,23 @@ fn test_set_bit() {
a.set_bit(100, false)
assert a == b
}

fn test_integer_from_bytes_ignores_potential_leading_zero_bytes() {
bint0 := big.integer_from_int(0)
for j in 0 .. 10 {
assert bint0 == big.integer_from_bytes([]u8{len: j}, signum: 1)
assert bint0 == big.integer_from_bytes([]u8{len: j}, signum: 0)
assert bint0 == big.integer_from_bytes([]u8{len: j}, signum: -1)
}
for i in 0 .. 10 {
bint := big.integer_from_int(i)
nbint := big.integer_from_int(-i)
for j in 0 .. 15 {
mut input := []u8{len: j}
input << i
assert bint == big.integer_from_bytes(input)
assert bint == big.integer_from_bytes(input, signum: 1)
assert nbint == big.integer_from_bytes(input, signum: -1)
}
}
}
16 changes: 14 additions & 2 deletions vlib/math/big/integer.v
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,23 @@ pub:
// If you want a negative integer, use in the following manner:
// `value := big.integer_from_bytes(bytes, signum: -1)`
@[direct_array_access]
pub fn integer_from_bytes(input []u8, config IntegerConfig) Integer {
pub fn integer_from_bytes(oinput []u8, config IntegerConfig) Integer {
// Thank you to Miccah (@mcastorina) for this implementation and relevant unit tests.
if input.len == 0 {
if oinput.len == 0 {
return integer_from_int(0)
}
// Ignore leading 0 bytes:
mut first_non_zero_index := -1
for i in 0 .. oinput.len {
if oinput[i] != 0 {
first_non_zero_index = i
break
}
}
if first_non_zero_index == -1 {
return integer_from_int(0)
}
input := oinput[first_non_zero_index..]
// pad input
mut padded_input := []u8{len: ((input.len + 3) & ~0x3) - input.len, cap: (input.len + 3) & ~0x3}
padded_input << input
Expand Down

0 comments on commit ef7fdd0

Please sign in to comment.