bcgit/pc-dart

Fails to build for web

Closed this issue · 14 comments

Attempting to build for web throws the following errors:

../../flutter/.pub-cache/hosted/pub.dartlang.org/pointycastle-3.1.3/lib/key_derivators/argon2.dart:42:27: Error: The integer literal 0xFFFFFFFFFFFFFFFF can't be represented exactly in JavaScript.
Try changing the literal to something that can be represented in Javascript. In Javascript 0x10000000000000000 is the nearest value that can be represented exactly.
  static const int M32L = 0xFFFFFFFFFFFFFFFF;
                          ^^^^^^^^^^^^^^^^^^
../../flutter/.pub-cache/hosted/pub.dartlang.org/pointycastle-3.1.3/lib/src/utils.dart:313:9: Error: The integer literal 0xFFFFFFFFFFFFFFFF can't be represented exactly in JavaScript.
Try changing the literal to something that can be represented in Javascript. In Javascript 0x10000000000000000 is the nearest value that can be represented exactly.
      ((0xFFFFFFFFFFFFFFFF) ^ ((1 << (64 - count)) - 1));
        ^^^^^^^^^^^^^^^^^^
../../flutter/.pub-cache/hosted/pub.dartlang.org/pointycastle-3.1.3/lib/src/platform_check/platform_check.dart:6:35: Error: Method not found: 'getPlatform'.
  static Platform get instance => getPlatform();
                                  ^^^^^^^^^^^
Failed to compile application.

Here's another issue with same base problem: Naddiseo/dart-sprintf#13

mwcw commented

Ok, this is a problem.

Integers are limited to include all integers between -2^53 and 2^53 when compiled to JS.

Given the argon2 implementation relies on Uint64List it will be impossible for it to build a correct result on JS.

Thoughts?

MW

A few options:

  • https://pub.dev/packages/fixnum the Dart team has this library, which normalizes 64-bit integers across platforms using two, 32-bit castrated integers. Replacing Uint64List with List<Int64> should work (theoretically).
  • We can use conditional imports to build a library that returns JS interop (or target-adjusted Dart) code when compiled for web and normal Dart code when compiled for native.
  • We can internally hack on some bitwise adjustments to allow building for JavaScript (as I recall, we did this with AES-GCM).

I have the same problem!

mwcw commented

Update:

I have dealt with the compilation issue but blindly releasing that would hide a bigger issue of integer mal-representation on platforms that only support IEEE 754 double precision numbers that cannot safely represent for example, positive integers > 9007199254740992.

I am in the process of applying a platform specific assertion to the transformations that fail their tests when running on "chrome" pub run test -p chrome after examining why they fail and if the failure is related integer representation.

I'll keep you posted.

MW

mwcw commented

Ok,

I have pushed to master code that will compile on web, Argon2 and Poly1305 will now assert that they have access to a full 64bit integer.

I would appreciate it if people wouldn't mind building the master branch and running the tests.

VM:
pub run test -r expanded
JS:
pub run test -p chrome -r expanded

Let me know how you go, I would be interested to know how much of this library is being used in the JS environment.

It will go out as 3.2.0 as there is a breaking change in function over the next 24 hours.

MW

I would be interested to know how much of this library is being used in the JS environment

FYI, Personally I use it in a node environment (firebase cloud function, alibaba function compute)! I know that is not a standard usage...I encountered the same issue. I also use it for testing in flutter projects, and it fails for flutter web too.

I tried on the latest master branch:

pub run test -p node
pub run test -p chrome

Both failed. Some new implementation could be removed from js (if too complex to implement) using conditional import

I would be interested to know how much of this library is being used in the JS environment.

I'm using web3dart. it would be ideal to work in browsers

mwcw commented

Ok I have pushed another update to master.

The issue on node js was dart not being able to find a source of entropy, I have added logic to call node's crypro library.

On my machine (osx) and on our CI (Ubuntu linux) using, I can get all the tests to pass. (Node v12.6.0, Dart 2.13.1)

pub run test
pub run test -p chrome
pub run test -p node

Let me know how you go.

Let me know how you go.

It works for me too, great!

joo commented

Please release.

Please release,I have the same problem.

Hi, when can we expect the new release v3.2.0. I am facing the same issue from yesterday.

@ArpanaSwarnkar If you need to get going now and you don't need to use Argon2, you can list version 3.1.1 directly in your pubspec.yaml and it will build to JS and run just fine.

mwcw commented

Released: #115