-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ByteData.getUint64() can't able to generate integers greater than (2^63)-1 #35225
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
Comments
Dart 2 integers are 64-bit signed values (in the VM, they are doubles as ususal in the browser). So, when you read a To represent a positive number larger than 2^63-1, you need to use a var uint64Mask = ((BigInt.one << 64) - BigInt.one);
var big = BigInt.from(uint64List[index]) & uint64Mask; |
@lrhn Or BigInt.from(uint64List[index]).toUnsigned(64); |
If that's the case then what about the misleading information present in the API documentation https://api.dartlang.org/stable/2.1.0/dart-typed_data/ByteData/getUint64.html |
The misleading information should be fixed. The returned value is the listed value converted to Web compiled programs can't even use |
I am writing a flutter application which internally calculates the uint64 value from bytes as given in my first comment.. If the code couldn't generate the correct value then my entire application will be of no use because it entirely depends on the correct parsing of those bytes.. there should be some kind of workaround or a way native to dart should be available since most of other languages I've worked with has that support.. |
Even BigInt isn't able to generate uint64 numbers IIRC.. |
BigInt can represent unsigned numbers of any size. The Dart integer type (on the VM) can only represent signed 64-bit numbers, and Dart only has the one integer type, so you need to use |
Apologies for the remark on BigInt.. I have tested the example @rakudrama gave and it indeed worked.. But my main question is still - shouldn't the getUint64() method on a ByteData return a uint64 value? Instead of changing the documentation you guys can change its implementation to either return a num or BigInt object. If this change is done then Uint64List also needs to be updated as it should be able to store the generated uint64 numbers |
@rashedmyt What kind of math are you doing with the |
I am implementing keccak crypto in which from a given array of bytes a uint64 number is generated which is xor'ed with the state of hash.. you can see its implementation here which I am trying to port to dart https://github.com/turtlecoin/cs-turtlecoin/blob/master/CantiLib/Blockchain/Crypto/Keccak/Keccak.cs the exact line I'm concerned with is this https://github.com/turtlecoin/cs-turtlecoin/blob/master/CantiLib/Blockchain/Crypto/Keccak/Keccak.cs#L132 |
From a brief look I suspect that you don't actually need If you are only doing The only thing you need to be careful about is shift to the right But you can easily work-around those differences by masking upper bits away (or by using |
This is the input to my library I put a print before the loop to see the temp list
which is exactly the same as produced in any other language.. inside the loop it computes uint64 value from 8 bytes starting from The code in my first comment is failing to generate the required uint64 value.. it should generate |
@rashedmyt yes, the code in your first comment does not print Imagine you take uint64 value
If you post the whole Dart source you have I can take a look and tell you why you are not getting the result you want. (From what I see in the C# source there is only a single shift to the right in rotation helper - you need to write it using |
|
you are right.. the logicalShiftRight worked.. but looks like my implementation has gone somewhere wrong.. for 1 out of 4 inputs I tried its giving different results |
so at the end.. its just documentation misleading then.. |
@mraleph any work is being done on getting more crypto libraries added here https://github.com/dart-lang/crypto by the Dart Team or will it be just that |
@rashedmyt I am not aware of any effort to expand the crypto library. I think people are encouraged to publish their own packages if they would like to implement new algorithms. |
ok.. 👍 |
@mraleph since dart 2.2 is out.. is the |
I am writing code which takes bytes from a buffer and convert them to Uint64... but to my surprise I found out that the method isn't working as expected..
Here is a sample code to reproduce the error..
Code
Output
Expected Output:
Dart Version: Dart VM version: 2.1.0-dev.9.3.flutter-9c07fb64c4
OS: Windows 10 1803 Build 17134.407
The text was updated successfully, but these errors were encountered: