|
| 1 | +// Copyright 2026 International Digital Economy Academy |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +///| |
| 16 | +fn[T] @splitmix.RandomState::pick( |
| 17 | + self : @splitmix.RandomState, |
| 18 | + values : ReadOnlyArray[T], |
| 19 | +) -> T { |
| 20 | + let index = self.next_uint(limit=values.length().reinterpret_as_uint()) |
| 21 | + values[index.reinterpret_as_int()] |
| 22 | +} |
| 23 | + |
| 24 | +///| |
| 25 | +fn with_random_sign( |
| 26 | + magnitude : @bigint.BigInt, |
| 27 | + rs : @splitmix.RandomState, |
| 28 | +) -> @bigint.BigInt { |
| 29 | + if magnitude.is_zero() || rs.next_uint(limit=2U) == 0U { |
| 30 | + magnitude |
| 31 | + } else { |
| 32 | + -magnitude |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +///| |
| 37 | +fn around(base : @bigint.BigInt, rs : @splitmix.RandomState) -> @bigint.BigInt { |
| 38 | + with_random_sign(base + rs.pick([-1N, 0N, 1N]), rs) |
| 39 | +} |
| 40 | + |
| 41 | +///| |
| 42 | +fn random_bigint(size : Int, rs : @splitmix.RandomState) -> @bigint.BigInt { |
| 43 | + let chunk_limit = size.clamp(min=0, max=31) + 1 |
| 44 | + let chunk_count = for count in 1..<chunk_limit { |
| 45 | + guard rs.next_uint(limit=2U) == 0U else { break count } |
| 46 | + } nobreak { |
| 47 | + chunk_limit |
| 48 | + } |
| 49 | + let bytes = Bytes::makei(chunk_count * 8, _ => { |
| 50 | + rs.next_uint(limit=256U).to_byte() |
| 51 | + }) |
| 52 | + with_random_sign(@bigint.BigInt::from_octets(bytes), rs) |
| 53 | +} |
| 54 | + |
| 55 | +///| |
| 56 | +/// 35% arbitrary multi-word bit patterns, 25% canonical small values, |
| 57 | +/// 25% values next to common powers of two, and 15% values next to common |
| 58 | +/// powers of ten. `size` caps the random branch at one to 32 64-bit words; |
| 59 | +/// boundary branches deliberately exercise important widths independently. |
| 60 | +pub impl Arbitrary for @bigint.BigInt with fn arbitrary(size, rs) { |
| 61 | + match rs.next_uint(limit=100U) { |
| 62 | + 0..<35 => random_bigint(size, rs) |
| 63 | + 35..<60 => rs.pick([0N, 1N, -1N, 2N, -2N, 10N, -10N]) |
| 64 | + 60..<85 => |
| 65 | + around( |
| 66 | + 1N << |
| 67 | + rs.pick([0, 1, 7, 8, 15, 16, 31, 32, 52, 53, 63, 64, 127, 128, 255, 256]), |
| 68 | + rs, |
| 69 | + ) |
| 70 | + _ => { |
| 71 | + let exponent = rs.pick([1, 2, 3, 6, 9, 18, 19, 38, 76, 100]) |
| 72 | + around( |
| 73 | + (0 : Int).until(exponent).fold(init=1N, (power, _) => power * 10N), |
| 74 | + rs, |
| 75 | + ) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments