From a294f067a96bdc0fb696af785940ac4f8bb18f22 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Mon, 15 Apr 2024 05:28:22 -0400 Subject: [PATCH] Fix `big.internal_random_prime` with `Second_MSB_On` --- core/math/big/prime.odin | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/math/big/prime.odin b/core/math/big/prime.odin index b02b7cb4e..5e7c02f37 100644 --- a/core/math/big/prime.odin +++ b/core/math/big/prime.odin @@ -1247,6 +1247,20 @@ internal_random_prime :: proc(a: ^Int, size_in_bits: int, trials: int, flags := a.digit[0] |= 3 } if .Second_MSB_On in flags { + /* + Ensure there's enough space for the bit to be set. + */ + if a.used * _DIGIT_BITS < size_in_bits - 1 { + new_size := (size_in_bits - 1) / _DIGIT_BITS + + if new_size % _DIGIT_BITS > 0 { + new_size += 1 + } + + internal_grow(a, new_size) or_return + a.used = new_size + } + internal_int_bitfield_set_single(a, size_in_bits - 2) or_return }