MixinNetwork / libsignal_protocol_dart

Signal Protocol library for Dart/Flutter
https://pub.dev/packages/libsignal_protocol_dart
GNU General Public License v3.0
159 stars 42 forks source link

Expected a value of type 'int', but got one of type 'double' flutter web #42

Closed RahulAlchemy closed 2 years ago

RahulAlchemy commented 3 years ago

I'm trying integrate Signal protocol in flutter web .

I get this error Expected a value of type 'int', but got one of type 'double'

When i call this method //3.0.0 nullsafety version KeyHelper.generateSignedPreKey(identityKeyPair, signedPreKeyId);

//0.5.3 version generateSignedPreKey(identityKeyPair, signedPreKeyId);

on both version I get same issue.

I was able to trace the issue. Issue is in edwards25519.dart.

Tougee commented 3 years ago

Some libraries we use in this code base are incompatible with dart2js, so we made some modifications to this workaround, but we can't merge them into the main branch because the changes lack performance.

You can use this library for the Flutter web app as shown below for now, until we find a better way to do it.

libsignal_protocol_dart:
    git:
      url: git@github.com:MixinNetwork/libsignal_protocol_dart.git
      ref: web
RahulAlchemy commented 3 years ago

@Tougee it worked but it is kinda slow.

Tougee commented 3 years ago

I think that's all for now :(

lemoony commented 3 years ago

Hi @Tougee ,

have you had a look at https://pub.dev/packages/cryptography by any chance? It did a quick test with it in the browser and to me it seems like that curve25519 is working with better performance. However, the interface is quite different and thus, would require some refactoring i guess...

Tougee commented 3 years ago

We tried using that lib but lacked some APIs to implement this part.

lemoony commented 3 years ago

Hi @Tougee & @RahulAlchemy

i was able to boost performance on the web significantly. I have created a simple example app to demonstrate how it works:

https://github.com/lemoony/signal_protocol_example_app

In total, i made the following changes:

The example app then uses the overwrites to call a Javascript library which is able to do the processing much faster than the DartJs version of x25519.

I'm aware that this is more a workaround than an actual solution, but still a pragmatic way to get it running with decent web performance. The nice thing is that performance on mobile platforms does not suffer and the the changes to the libsignal_protocol_dart package are minimal. Maybe it would be even faster if I also add overwrites for other methods as well, but as of now, it is fast enough for me.

@Tougee If this is something you would be interested in merging into your repository (or maybe just parts of it, like the forks of the crypto modules) let me know. I'm happy to create corresponding pull requests.

Tougee commented 3 years ago

I'm just glancing at it now, and it seems to be a delightful solution to the problem. I'll take a deeper look soon and give you a response.

crossle commented 3 years ago

Hi @Tougee & @RahulAlchemy

i was able to boost performance on the web significantly. I have created a simple example app to demonstrate how it works:

https://github.com/lemoony/signal_protocol_example_app

In total, i made the following changes:

  • Forks of ed25519_edwards and x25519 which only use a kind-of BigInt data type on the web, but the normal Dart int type on all other platforms. By making this change, performance on all non-web platforms isn't slowed down. However, some more object allocation is happening, so i'm not yet sure if it is quite as fast as the current main branch implementation. It would be possible to get rid of the additinal object allocation too, however, i don't think it is worth the effort since it is still very fast (i think).
  • Fork of libsignal_protocol_dart which provides the possibility to pass a custom key pair generator + aggreement calculator. These are the two most time consuming methods provided by the x25519 package.

The example app then uses the overwrites to call a Javascript library which is able to do the processing much faster than the DartJs version of x25519.

I'm aware that this is more a workaround than an actual solution, but still a pragmatic way to get it running with decent web performance. The nice thing is that performance on mobile platforms does not suffer and the the changes to the libsignal_protocol_dart package are minimal. Maybe it would be even faster if I also add overwrites for other methods as well, but as of now, it is fast enough for me.

@Tougee If this is something you would be interested in merging into your repository (or maybe just parts of it, like the forks of the crypto modules) let me know. I'm happy to create corresponding pull requests.

Good idea for web. Welcome contribute the PR

Tougee commented 3 years ago

Hi @lemoony

I have run tests and benchmarks of the libraries you fork which work perfectly, please PR for this, so we can check if some details need polishing. Thanks for your deep investigation into this!

lemoony commented 3 years ago

Thanks for the quick feedback! I've started to create a Draft PR : #43 It's still a draft since I suggest to wait and see if the other PRs mentioned in it are merged into the main branch. If you want me to mark it as ready for review anyway, just say so.

crossle commented 2 years ago

DONE on https://github.com/MixinNetwork/libsignal_protocol_dart/pull/43