-
-
Notifications
You must be signed in to change notification settings - Fork 661
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 BigInteger type #10750
Open
flashultra
wants to merge
147
commits into
HaxeFoundation:development
Choose a base branch
from
flashultra:development
base: development
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.
Open
Add BigInteger type #10750
Changes from 68 commits
Commits
Show all changes
147 commits
Select commit
Hold shift + click to select a range
ac9648b
Add BigInteger type
flashultra d502605
Add BigInt test and allow unit package
flashultra b6ebec0
Merge branch 'HaxeFoundation:development' into development
flashultra 6aa9204
Add abs,pow,modPow,isProbablePrime,getLowestSetBit,bitLength methods
flashultra baa9010
Merge branch 'development' of https://github.com/flashultra/haxe into…
flashultra 17a09e6
Add missing tests and counting of trailing zeros
flashultra 60de60d
Run tests for BigInt
flashultra e2fdb58
Add missing const
flashultra 65bd87c
Fiexes for Int32 ( javascript)
flashultra 2979ee9
More fixes for Int32
flashultra e0b0218
Add trace for macro crash
flashultra b63329f
Fix missing ;
flashultra 91688ac
Fix macro test
flashultra 4da8487
Remove macrotest crash
flashultra 99adeca
Fix Int32 (python)
flashultra f7c405f
More fixes
flashultra c5dfc60
More fixes for python
flashultra 2dca9df
more fixes
flashultra 4cf361f
Merge branch 'HaxeFoundation:development' into development
flashultra 984a696
Fix for Python target
flashultra 2f45acc
Correct test for mutable bigint
flashultra 39e31fa
Add method to create BigInt from bytes
flashultra 7b5b70b
Add auto convert from String
flashultra 4648790
Add greater common divisor (gcd) of two BigInt values
flashultra 45980ef
Add lcm,min,max,nextprobableprime methods
flashultra 1e8051d
Merge branch 'HaxeFoundation:development' into development
flashultra 4f99754
Add method to create random BigInt
flashultra d2916d7
Add test for random and small fixes
flashultra e6fe7c5
More random methods and some fixes
flashultra ce5378d
Fix pow bug
flashultra 76a883f
Add test for random prime and random in range + prime fix
flashultra 57927b8
Remove trace(..)
flashultra 39252d4
Fix test
flashultra 70fbb33
Fix unsigned compare for randomInRange
flashultra f2de369
Fix for randomInRange
flashultra 8bf946a
Hope dies last
flashultra 2866088
Fix for JS
flashultra c26f036
Better check for sign
flashultra 6e8f2d8
Thanks to cbatson
flashultra bec8abe
Add modInverse method
flashultra f4d5e6d
Add bitcount(),isPositive,isEven and isOdd methods
flashultra a0e1598
Add testBit() method
flashultra 3c5c0a2
Add tests for modInverse,isPositive,isEven,isOdd, testBit, bitCount
flashultra 157c0e2
Fix bitcount
flashultra 8bd592c
Try to fix bitCount()
flashultra 3eb46e4
Debug cpp (couldn't repeat)
flashultra 880f710
Fix debug
flashultra b15c9f0
Debug
flashultra edbb6d4
[cpp] Fix bitCount()
flashultra 257e751
Add square() and Karatsuba multiplication
flashultra 326d026
Add test for Karatsuba square
flashultra 5273e4c
Merge branch 'HaxeFoundation:development' into development
flashultra 2aebe42
Merge branch 'HaxeFoundation:development' into development
flashultra 197ab84
Merge branch 'HaxeFoundation:development' into development
flashultra 8b65d53
Add divMod and hashCode functions.Auto convert to string
flashultra b030dec
Fix last commit.
flashultra 21900e6
Fix overflow behaviour
flashultra 39038bf
Add negative base for modInverse
flashultra de89828
Add OR and AND (bigint) operations
flashultra 786cbec
Fix for negative numbers
flashultra d1a15f8
Correct OR for bigint
flashultra 3733409
For OR small optimization
flashultra aec1211
Add XOR and NOT bitwise operators
flashultra e76288c
Add bit operations and powerOfTwo method
flashultra c4fe08c
Test bit for negative numbers
flashultra b877aa9
Use enum abstract for exception. Reformat the code
flashultra 5fa6c53
Add missing tests
flashultra b7626bc
Temp fix for BigIntException
flashultra f9417ea
Fix for BigIntExceptions
flashultra 3ab6538
Fix BigIntExceptions test
flashultra 3b87a7e
Fix tests for String conversion
flashultra 1245ce8
Temp fix for flipBit (python)
flashultra 704100c
Add @:noDoc and @:noCompletion .Fix for BigIntException and tests
flashultra b4faf0a
Change var NEGATIVE_ONE to MINUS_ONE
flashultra f1f1e11
New implementation for BigIntException class
flashultra e4c22e6
Remove memory limit for PHP test
flashultra ec9646c
Fix for php test
flashultra 5177449
Add from/to different base
flashultra 0a7500c
Remove trace()
flashultra b664dcc
Fix for toString()
flashultra 611d36f
Merge branch 'development' of https://github.com/HaxeFoundation/haxe …
flashultra fab6b83
Merge branch 'development' of https://github.com/HaxeFoundation/haxe …
flashultra 5da8f75
Add Montgomery and Barrett reduction for modPow
flashultra 0d6f584
Add bitlen()
flashultra 4cb131d
Fix typo
flashultra 2173c8a
Add compareMonty() and subtractMonty()
flashultra 9293500
Change millerRabin().Fix compareMonty()
flashultra 0011ee5
Merge branch 'HaxeFoundation:development' into development
flashultra 4a1b149
Prime check for small numbers
flashultra d8d1b3d
Merge branch 'development' of https://github.com/flashultra/haxe into…
flashultra bffa60e
Debug Lua
flashultra e4faadc
Always init m_data
flashultra 79e5779
Merge branch 'HaxeFoundation:development' into development
flashultra 7bc65dc
Trigger GitHub actions
flashultra 502f950
Debug BigIntRandomPrime [php,python]
flashultra 8849e8b
Fix syntax err
flashultra 422ec2d
Lua test - modInverse
flashultra 5051966
Is toString() the problem ?
flashultra 7452b85
Debug Lua
flashultra 4448668
Debug compare()
flashultra 274a2fd
Is the unsigned shift the problem ?
flashultra bddad70
Test sign() Lua
flashultra 7c59327
Lua clampInt32 for compareInt()
flashultra 71a8da6
Check compare() - Lua
flashultra 9b376fc
Clamp left shift - Lua
flashultra 8dcf4e5
[lua] Shift left doesn't work correctly
flashultra f851be7
Test left shift for Lua
flashultra ffec547
Sign test - Lua
flashultra 4bbf648
Clamp lshift too, because -1 << 1 is 4294967294, but should be -2
flashultra 6b634a2
Check for nil
flashultra 0589e3d
What is nil ?
flashultra ad29bc8
Check testPrimeNumber
flashultra dee19e9
Lua nil debug
flashultra d2bbbcd
Debug
flashultra 1c418e5
Debug Lua
flashultra 3c19c45
Dbg Lua
flashultra eb9bd4b
Debug
flashultra fd76eb9
Remove debug ( failed)
flashultra 975ab17
Last chance Lua
flashultra e91d2d2
Init m_count
flashultra 8fbfcdb
Set m_data to null
flashultra 83a3ae9
Debug
flashultra 4a785f3
Debug
flashultra 965a274
Debug
flashultra 380e066
How num is null?
flashultra 27e52a9
Trigger GitHub actions
flashultra 6596762
Spooky compare call
flashultra 008f6b0
wheel of fortune
flashultra abf4385
Check if this it the init poblem for Lua
flashultra 5ee77da
Remove trace() and workaround
flashultra 194cd6f
Remove Lua fix
flashultra df9906f
Merge branch 'HaxeFoundation:development' into development
flashultra bdf1e32
Add test for Python
flashultra ebfc736
Check why Python failed
flashultra 903db1e
Check Python
flashultra 428232e
Fix Python target
flashultra 7778f19
Fix for Neko
flashultra 1fd473b
Fix Neko II
flashultra 3696602
Remove prime test for Lua and Cppia
flashultra 3ec3f61
Fix for Cppia (see issue #11897)
flashultra 4bf7251
Reorder const
flashultra 542d1bd
Fix typo
flashultra 51eaff1
Reorder static var
flashultra 65f0dd7
Workaround Cppia
flashultra ab17d83
Fix Cppia II
flashultra 432a3d1
Give up
flashultra c188704
I won't never give up, no, never give up, no
flashultra 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,34 @@ | ||
/* | ||
* Copyright (C)2005-2022 Haxe Foundation | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package haxe.math.bigint; | ||
|
||
/* Original code courtesy Chuck Batson (github.com/cbatson) */ | ||
enum abstract BigIntExceptions(String) to String { | ||
var INVALID_ARGUMENT = "Invalid argument"; | ||
var BUFFER_TOO_SMALL = "Buffer too small"; | ||
var DIVISION_BY_ZERO = "Division by zero"; | ||
var NEGATIVE_EXPONENT = "Negative exponent"; | ||
var INVALID_OPERATION = "Invalid operation"; | ||
var NEGATIVE_MODULUS = "Modulus should be positive"; | ||
var EVEN_VALUES = "Both values are even"; | ||
} |
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,151 @@ | ||
/* | ||
* Copyright (C)2005-2022 Haxe Foundation | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package haxe.math.bigint; | ||
|
||
/* Original code courtesy Chuck Batson (github.com/cbatson) */ | ||
class BigIntHelper { | ||
/** | ||
"Numbler of leading zeros" - return the number of leading | ||
0-value bits in the binary representation of `x`. | ||
**/ | ||
public static function nlz(x:Int):Int { | ||
// From "Hacker's Delight", Second Edition; Henry S. Warren, Jr.; 2013. Figure 5-15, p. 102. | ||
var y:Int, m:Int, n:Int; | ||
|
||
y = -(x >>> 16); | ||
m = (y >> 16) & 16; | ||
n = 16 - m; | ||
x = x >>> m; | ||
|
||
y = x - 0x100; | ||
m = (y >> 16) & 8; | ||
n = n + m; | ||
x = x << m; | ||
|
||
y = x - 0x1000; | ||
m = (y >> 16) & 4; | ||
n = n + m; | ||
x = x << m; | ||
|
||
y = x - 0x4000; | ||
m = (y >> 16) & 2; | ||
n = n + m; | ||
x = x << m; | ||
|
||
y = x >> 14; | ||
m = y & (~y >> 1); | ||
return n + 2 - m; | ||
} | ||
|
||
/** | ||
"Ceiling power of two" -- round up to the least power of two | ||
greater than or equal to input `x`, which is interpreted as | ||
unsigned. | ||
**/ | ||
public static function clp2(x:Int32):Int { | ||
// From "Hacker's Delight", Second Edition; Henry S. Warren, Jr.; 2013. Figure 3-3, p. 62. | ||
x = x - 1; | ||
x = x | (x >> 1); | ||
x = x | (x >> 2); | ||
x = x | (x >> 4); | ||
x = x | (x >> 8); | ||
x = x | (x >> 16); | ||
return x + 1; | ||
} | ||
|
||
/** | ||
Unsigned greater than comparison. | ||
|
||
Returns `true` if `a > b` when both `a` and `b` are | ||
interpreted as unsigned integers; `false` otherwise. | ||
**/ | ||
public static inline function u32gtu32(a:Int, b:Int):Bool { | ||
return (a ^ -2147483648) > (b ^ -2147483648); // unsigned comparison, see "Hacker's Delight" p. 25. | ||
} | ||
|
||
/** | ||
Integer division of unsigned 32-bit integer by unsigned 16-bit integer. | ||
|
||
Result is undefined when `divisor` <= 0 or `divisor` >= 2^16. | ||
**/ | ||
public static function u32divu16(dividend:Int32, divisor:Int32):Int { | ||
/* | ||
Complicated because Haxe's division is always performed as | ||
floating-point. Here we rely on the ability to exactly represent | ||
a 31-bit integer as a Float. In other words, 64-bit floating | ||
point is required. | ||
|
||
TODO: Implement a method without this restriction. | ||
TODO: Consider C++-specific optimization here. | ||
*/ | ||
|
||
// From "Hacker's Delight", Second Edition; Henry S. Warren, Jr.; 2013. Section 9-3, p. 192. | ||
var t:Int = divisor >> 31; | ||
var nprime:Int = dividend & ~t; | ||
var q:Int32 = Std.int((nprime >>> 1) / divisor) << 1; | ||
var r:Int = dividend - q * divisor; | ||
var c:Int = u32geu32(r, divisor) ? 1 : 0; | ||
return q + c; | ||
} | ||
|
||
/** | ||
Unsigned greater than or equal comparison. | ||
Returns `true` if `a >= b` when both `a` and `b` are | ||
interpreted as unsigned integers; `false` otherwise. | ||
**/ | ||
public static inline function u32geu32(a:Int, b:Int):Bool { | ||
return (a ^ -2147483648) >= (b ^ -2147483648); // unsigned comparison, see "Hacker's Delight" p. 25. | ||
} | ||
|
||
/** | ||
Number of trailing zeros - return the number of trailing | ||
0-value bits | ||
**/ | ||
public static function ntz(x:Int32):Int { | ||
if (x == 0) | ||
return 32; | ||
var y:Int; | ||
var n:Int = 31; | ||
y = x << 16; | ||
if (y != 0) { | ||
n -= 16; | ||
x = y; | ||
} | ||
y = x << 8; | ||
if (y != 0) { | ||
n -= 8; | ||
x = y; | ||
} | ||
y = x << 4; | ||
if (y != 0) { | ||
n -= 4; | ||
x = y; | ||
} | ||
y = x << 2; | ||
if (y != 0) { | ||
n -= 2; | ||
x = y; | ||
} | ||
return (n - ((x << 1) >>> 31)); | ||
} | ||
} |
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,68 @@ | ||
/* | ||
* Copyright (C)2005-2022 Haxe Foundation | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a | ||
* copy of this software and associated documentation files (the "Software"), | ||
* to deal in the Software without restriction, including without limitation | ||
* the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
* and/or sell copies of the Software, and to permit persons to whom the | ||
* Software is furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
* DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package haxe.math.bigint; | ||
|
||
import haxe.math.bigint.BigIntExceptions; | ||
|
||
/* Original code courtesy Chuck Batson (github.com/cbatson) */ | ||
class BigIntTools { | ||
public static inline function isNull(value:BigInt):Bool { | ||
var a:BigInt_ = value; | ||
return a == null; | ||
} | ||
|
||
public static inline function isBigInt(value:Dynamic):Bool { | ||
return Std.isOfType(value, BigInt_); | ||
} | ||
|
||
public static inline function castFrom(value:Dynamic):BigInt { | ||
return new BigInt(Std.downcast(value, BigInt_)); | ||
} | ||
|
||
public static function parseValueUnsigned(value:Dynamic):BigInt { | ||
var bi:BigInt; | ||
if (Std.isOfType(value, String)) { | ||
bi = parseStringUnsigned(cast(value, String)); | ||
} else if (isBigInt(value)) { | ||
var t = new MutableBigInt_(); | ||
t.copyFrom(castFrom(value)); | ||
return new BigInt(t); | ||
} else if (Std.isOfType(value, Int)) { | ||
bi = BigInt.fromInt(cast(value, Int)); | ||
} else { | ||
throw BigIntExceptions.INVALID_ARGUMENT; | ||
} | ||
return bi; | ||
} | ||
|
||
private static function parseStringUnsigned(value:String):BigInt { | ||
var result = new MutableBigInt_(); | ||
if (StringTools.startsWith(value, "0x")) { | ||
result.setFromHexUnsigned(value.substr(2)); | ||
} else { | ||
result.setFromString(value); | ||
} | ||
var result2:MutableBigInt = result; | ||
return result2; | ||
} | ||
} |
Oops, something went wrong.
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.
I just looked at the failing test. I think it is actually the other way around.
enum abstract BigIntExceptions(String) from String
if you want to use it likeMaybe even better you do a catch-all:
Then I believe you should don't need the from/to declarations at all.
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.
I don't think adding
from String
there would be nice.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.
So there are at least three options here:
haxe.Exception
The question here is how does the end user want to proceed with the error ?
Does it want to match it with if/else/switch case or trace the stack or something else ?
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.
If the exceptions are strings and some other code throws a string exception with exactly the same value e.g.
Invalid argument
then the end user is not be able to distinguish that properly. So maybe typed exceptions might be better. When you search through the haxe std library however, there is no consistency and no clear direction visible what is the preferred approach (strings vs exception objects). No one really seems to care.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.
Yes, the other solution that I think was to create a BigInteException class (extends haxe.Exception) and create а new BigIntError.hx class ( which will use the current enum abstract implementation)
Example:
and call it with
throw new BigIntException(BigIntError.INVALID_ARGUMENT);
If this is a better solution, I could implement it.
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.
I think this is a good idea. Exceptions even if thrown as strings anyway result in the instantiation of an exception object, in this case
haxe.ValueException
. So there is also no performance drawback if you use a dedicated exception class.