Remove the need for rand in core:math/big

This commit is contained in:
gingerBill
2024-06-15 15:43:57 +01:00
parent 7ec17ecf98
commit 149ecafdef
4 changed files with 13 additions and 16 deletions
+5 -5
View File
@@ -362,11 +362,11 @@ platform_count_lsb :: #force_inline proc(a: $T) -> (count: int)
count_lsb :: proc { int_count_lsb, platform_count_lsb, } count_lsb :: proc { int_count_lsb, platform_count_lsb, }
int_random_digit :: proc(r: ^rnd.Rand = nil) -> (res: DIGIT) { int_random_digit :: proc() -> (res: DIGIT) {
when _DIGIT_BITS == 60 { // DIGIT = u64 when _DIGIT_BITS == 60 { // DIGIT = u64
return DIGIT(rnd.uint64(r)) & _MASK return DIGIT(rnd.uint64()) & _MASK
} else when _DIGIT_BITS == 28 { // DIGIT = u32 } else when _DIGIT_BITS == 28 { // DIGIT = u32
return DIGIT(rnd.uint32(r)) & _MASK return DIGIT(rnd.uint32()) & _MASK
} else { } else {
panic("Unsupported DIGIT size.") panic("Unsupported DIGIT size.")
} }
@@ -374,12 +374,12 @@ int_random_digit :: proc(r: ^rnd.Rand = nil) -> (res: DIGIT) {
return 0 // We shouldn't get here. return 0 // We shouldn't get here.
} }
int_random :: proc(dest: ^Int, bits: int, r: ^rnd.Rand = nil, allocator := context.allocator) -> (err: Error) { int_random :: proc(dest: ^Int, bits: int, allocator := context.allocator) -> (err: Error) {
/* /*
Check that `a` is usable. Check that `a` is usable.
*/ */
assert_if_nil(dest) assert_if_nil(dest)
return #force_inline internal_int_random(dest, bits, r, allocator) return #force_inline internal_int_random(dest, bits, allocator)
} }
random :: proc { int_random, } random :: proc { int_random, }
+5 -5
View File
@@ -2817,11 +2817,11 @@ internal_platform_count_lsb :: #force_inline proc(a: $T) -> (count: int)
internal_count_lsb :: proc { internal_int_count_lsb, internal_platform_count_lsb, } internal_count_lsb :: proc { internal_int_count_lsb, internal_platform_count_lsb, }
internal_int_random_digit :: proc(r: ^rnd.Rand = nil) -> (res: DIGIT) { internal_int_random_digit :: proc() -> (res: DIGIT) {
when _DIGIT_BITS == 60 { // DIGIT = u64 when _DIGIT_BITS == 60 { // DIGIT = u64
return DIGIT(rnd.uint64(r)) & _MASK return DIGIT(rnd.uint64()) & _MASK
} else when _DIGIT_BITS == 28 { // DIGIT = u32 } else when _DIGIT_BITS == 28 { // DIGIT = u32
return DIGIT(rnd.uint32(r)) & _MASK return DIGIT(rnd.uint32()) & _MASK
} else { } else {
panic("Unsupported DIGIT size.") panic("Unsupported DIGIT size.")
} }
@@ -2829,7 +2829,7 @@ internal_int_random_digit :: proc(r: ^rnd.Rand = nil) -> (res: DIGIT) {
return 0 // We shouldn't get here. return 0 // We shouldn't get here.
} }
internal_int_random :: proc(dest: ^Int, bits: int, r: ^rnd.Rand = nil, allocator := context.allocator) -> (err: Error) { internal_int_random :: proc(dest: ^Int, bits: int, allocator := context.allocator) -> (err: Error) {
context.allocator = allocator context.allocator = allocator
bits := bits bits := bits
@@ -2846,7 +2846,7 @@ internal_int_random :: proc(dest: ^Int, bits: int, r: ^rnd.Rand = nil, allocator
#force_inline internal_grow(dest, digits) or_return #force_inline internal_grow(dest, digits) or_return
for i := 0; i < digits; i += 1 { for i := 0; i < digits; i += 1 {
dest.digit[i] = int_random_digit(r) & _MASK dest.digit[i] = int_random_digit() & _MASK
} }
if bits > 0 { if bits > 0 {
dest.digit[digits - 1] &= ((1 << uint(bits)) - 1) dest.digit[digits - 1] &= ((1 << uint(bits)) - 1)
+3 -5
View File
@@ -12,8 +12,6 @@
package math_big package math_big
import rnd "core:math/rand"
/* /*
Determines if an Integer is divisible by one of the _PRIME_TABLE primes. Determines if an Integer is divisible by one of the _PRIME_TABLE primes.
Returns true if it is, false if not. Returns true if it is, false if not.
@@ -315,7 +313,7 @@ internal_int_prime_miller_rabin :: proc(a, b: ^Int, allocator := context.allocat
Assumes `a` not to be `nil` and to have been initialized. Assumes `a` not to be `nil` and to have been initialized.
*/ */
internal_int_is_prime :: proc(a: ^Int, miller_rabin_trials := int(-1), miller_rabin_only := USE_MILLER_RABIN_ONLY, r: ^rnd.Rand = nil, allocator := context.allocator) -> (is_prime: bool, err: Error) { internal_int_is_prime :: proc(a: ^Int, miller_rabin_trials := int(-1), miller_rabin_only := USE_MILLER_RABIN_ONLY, allocator := context.allocator) -> (is_prime: bool, err: Error) {
context.allocator = allocator context.allocator = allocator
miller_rabin_trials := miller_rabin_trials miller_rabin_trials := miller_rabin_trials
@@ -461,7 +459,7 @@ internal_int_is_prime :: proc(a: ^Int, miller_rabin_trials := int(-1), miller_ra
for ix := 0; ix < miller_rabin_trials; ix += 1 { for ix := 0; ix < miller_rabin_trials; ix += 1 {
// rand() guarantees the first digit to be non-zero // rand() guarantees the first digit to be non-zero
internal_random(b, _DIGIT_TYPE_BITS, r) or_return internal_random(b, _DIGIT_TYPE_BITS) or_return
// Reduce digit before casting because DIGIT might be bigger than // Reduce digit before casting because DIGIT might be bigger than
// an unsigned int and "mask" on the other side is most probably not. // an unsigned int and "mask" on the other side is most probably not.
@@ -1183,7 +1181,7 @@ internal_int_prime_next_prime :: proc(a: ^Int, trials: int, bbs_style: bool, all
This is possibly the mother of all prime generation functions, muahahahahaha! This is possibly the mother of all prime generation functions, muahahahahaha!
*/ */
internal_random_prime :: proc(a: ^Int, size_in_bits: int, trials: int, flags := Primality_Flags{}, r: ^rnd.Rand = nil, allocator := context.allocator) -> (err: Error) { internal_random_prime :: proc(a: ^Int, size_in_bits: int, trials: int, flags := Primality_Flags{}, allocator := context.allocator) -> (err: Error) {
context.allocator = allocator context.allocator = allocator
flags := flags flags := flags
trials := trials trials := trials
-1
View File
@@ -6,7 +6,6 @@ package rand
import "base:intrinsics" import "base:intrinsics"
import "base:runtime" import "base:runtime"
import "core:crypto"
import "core:math" import "core:math"
import "core:mem" import "core:mem"