-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add and test initRandomBigInt for issue #101 #112
Draft
dlesnoff
wants to merge
11
commits into
nim-lang:master
Choose a base branch
from
dlesnoff:random
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
49a7817
Add and test initRandomBigInt
dlesnoff b4a1beb
Add a new type and optional parameter to generate random bigints with…
dlesnoff 87b1af3
Adapt tests with new random bigints function, add a memory limit, and…
dlesnoff 682af3d
Update tests/trandom.nim
dlesnoff bbaa414
Update tests/trandom.nim
dlesnoff c23a6c4
Update src/bigints.nim
dlesnoff 5063ca3
Replace limbs.len-1 by limbs.high
dlesnoff a4ae17f
Replace limbs.len-1 by limbs.high
dlesnoff f54aba1
initRandomBigint first review after rebasing
dlesnoff 8d152ba
Move randomBigInt into another utility file, random.nim
dlesnoff 50ba335
Remove random from the list of imports
dlesnoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import std/random | ||
import ../bigints | ||
|
||
const zero = initBigInt(0) | ||
type | ||
RandomMode* = enum | ||
Limbs, Bits | ||
|
||
proc randomizeBigInt(container: var seq[uint32], number: Natural, mode: RandomMode = Limbs) = | ||
case mode | ||
of Limbs: | ||
if number == 0: | ||
raise newException(ValueError, "A Bigint must have at least one limb !") | ||
# result.limbs.setLen(number) | ||
for i in 0 ..< number-1: | ||
container[i] = rand(uint32) | ||
var word = rand(uint32) | ||
# Bigint's last limb can be zero, iff there is only one limb | ||
# We can't normalize instead, since we need no less than number limbs | ||
if number != 1: | ||
while word == 0: # Very low probability | ||
word = rand(uint32) | ||
container[number-1] = word | ||
|
||
of Bits: # unit == Bits | ||
if number == 0: | ||
container = @[] | ||
let | ||
remainder = number mod 32 | ||
n_limbs = (if remainder == 0: number shr 5 else: number shr 5 + 1) | ||
remainingBits = (if remainder == 0: 32 else: remainder) | ||
# result.limbs.setLen(n_limbs) | ||
# mask ensures only remainingBits bits can be set to 1 | ||
# mask2 ensures the first bit is set to 1 | ||
var | ||
mask: uint32 = 0xFFFF_FFFF'u32 | ||
mask2: uint32 = 0x8000_0000'u32 | ||
if remainingBits != 32: | ||
mask = 1'u32 shl remainingBits - 1 | ||
mask2 = 1'u32 shl (remainingBits-1) | ||
for i in 0 ..< container.high: | ||
container[i] = rand(uint32) | ||
let word = rand(uint32) | ||
container[container.high] = word and mask or mask2 | ||
|
||
proc initRandomBigInt*(number: Natural, mode: RandomMode = Limbs): BigInt = | ||
## Initializes a `BigInt` whose value is chosen randomly with exactly | ||
## `number` bits or limbs, depending on the value of `unit`. By default, the | ||
## `BigInt` is chosen with `number` limbs chosen randomly. | ||
## Generates only positive bigints. | ||
var limbs: seq[uint32] | ||
let | ||
remainder = number mod 32 | ||
n_limbs = (if remainder == 0: number shr 5 else: number shr 5 + 1) | ||
case mode | ||
of Limbs: | ||
limbs.setLen(number) | ||
of Bits: | ||
if number == 0: | ||
return zero | ||
let | ||
remainder = number mod 32 | ||
len_limbs = (if remainder == 0: number shr 5 else: number shr 5 + 1) | ||
limbs.setLen(len_limbs) | ||
randomizeBigInt(limbs, number, mode) | ||
result = initBigInt(limbs, false) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import bigints | ||
import ../src/bigints/utilities | ||
import std/random | ||
|
||
type | ||
MemSizeUnit = enum | ||
o, Kio, Mio, Gio | ||
|
||
const | ||
zero = initBigInt(0) | ||
one = initBigInt(1) | ||
dlesnoff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
memSize = 2 # Max number of allocated memory for the tests | ||
memSizeUnit = Mio # Unit in which memSize is expressed | ||
|
||
proc computeLimit(memSize: Natural, memSizeUnit: MemSizeUnit): Natural = | ||
result = memSize | ||
for _ in 1..ord(memSizeUnit): | ||
result *= 1024 | ||
|
||
const | ||
memLimit = computeLimit(memSize, memSizeUnit) # Number of bytes | ||
maxLimbs = memLimit div 8 | ||
maxBits = 4*memLimit | ||
|
||
proc main() = | ||
randomize() | ||
|
||
block: | ||
let a: BigInt = initRandomBigInt(0, Bits) | ||
doAssert a == zero | ||
let b: BigInt = initRandomBigInt(1, Bits) | ||
doAssert b == one | ||
|
||
block: | ||
for nBits in [29, 32, 1037]: | ||
dlesnoff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _ in 1 .. 5: # Repeat probabilistic tests | ||
let a: BigInt = initRandomBigInt(nBits, Bits) | ||
doAssert fastLog2(a) == (nBits - 1) | ||
doAssert (toString(a, 2)).len == nBits | ||
# For bigger bigints, remove the test with slow conversion to string | ||
for nBits in [rand(1..maxBits), 32*rand(1..maxLimbs)]: | ||
for _ in 1 .. 5: | ||
let a: BigInt = initRandomBigInt(nBits, Bits) | ||
doAssert fastLog2(a) == (nBits - 1) | ||
|
||
block: | ||
for nLimbs in [1, 2, 3, 5, 10, 25, 100]: | ||
for _ in 1 .. 5: | ||
let a: BigInt = initRandomBigInt(nLimbs) | ||
let n_bitsA = fastLog2(a) + 1 | ||
doAssert n_bitsA <= 32*nlimbs | ||
doAssert n_bitsA > 32*(nlimbs-1) | ||
|
||
block: # GCD properties but tested on random Bigints | ||
let limitGCD = 100_000 # Special limit for the GCD, otherwise the tests run for hours | ||
let (nBitsA, nBitsB, nBitsC) = (rand(1..limitGCD), rand(1..limitGCD), rand(1..limitGCD)) | ||
let a = initRandomBigInt(nBitsA, Bits) | ||
let b = initRandomBigInt(nBitsB, Bits) | ||
let c = initRandomBigInt(nBitsC, Bits) | ||
doAssert gcd(a, b) == gcd(b, a) | ||
doAssert gcd(a, zero) == a | ||
doAssert gcd(a, a) == a | ||
doAssert gcd(c * a, c * b) == c * gcd(a,b) | ||
doAssert gcd(a, gcd(b, c)) == gcd(gcd(a, b), c) | ||
doAssert gcd(a, b) == gcd(b, a mod b) | ||
|
||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There already is a trailing newline, isn't there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No there is not!
I added one, It makes the code fits better in the buffer.