FireStack-Lab / LaksaDart

Laska, Dart version: https://pub.dartlang.org/documentation/laksadart/latest/laksadart/laksadart-library.html
MIT License
17 stars 2 forks source link

Tests failing while migrating to null safety #5

Closed sjosegarcia closed 5 months ago

sjosegarcia commented 3 years ago

I forked the project to try and migrate to null safety, but there seems to be three tests that fail. I was wondering what might be a potential fix for some of the tests.

The culprit:

  @override
  BigInt nextBigInteger(int bitLength) {
    int fullBytes = bitLength ~/ 8;

    /// var remainingBits = bitLength % 8;

    /// Generate a number from the full bytes. Then, prepend a smaller number
    /// covering the remaining bits.
    BigInt main = numbers.bytesToInt(nextBytes(fullBytes));

    /// forcing remainingBits to be calculate with bitLength
    int remainingBits = (bitLength - main.bitLength);
    int additional = remainingBits < 4
        ? dartRandom.nextInt(pow(2, remainingBits))
        : remainingBits;
    BigInt additionalBit = (new BigInt.from(additional) << (fullBytes * 8));
    BigInt result = main + additionalBit;
    print(result.toString());
    return result;
  }

^ Returns back a negative (signed) on the result. I have tried using result.toUnsigned(result.bitLength) to no avail. Now I still have not researched why the other two tests are failing.

00:00 +1: /Users/steven/Projects/LaksaDart/test/keyGen.dart: test with 1000 keypairs                                                                                                                     
147587440077215482179262220947048256480457171726512571813821872888079478892669
-33447245391790795383774714820149278745318359833994383661338911822243363241714
00:00 +1 -1: /Users/steven/Projects/LaksaDart/test/keyGen.dart: test with 1000 keypairs [E]                                                                                                              
  Invalid argument(s): The multiplicator cannot be negative
  package:pointycastle/ecc/ecc_base.dart 142:7                     ECPointBase.*
  package:pointycastle/key_generators/ec_key_generator.dart 46:23  ECKeyGenerator.generateKeyPair
  package:laksadart/src/crypto/schnorr.dart 36:37                  generateNewPrivateKey
  package:laksadart/src/crypto/schnorr.dart 61:25                  generatePrivateKey
  test/keyGen.dart 9:23                                            main.<fn>

00:00 +1 -1: loading /Users/steven/Projects/LaksaDart/test/keyGen.dart                                                                                                                                   
Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
For example, 'pub run test --chain-stack-traces'.
00:01 +1 -2: /Users/steven/Projects/LaksaDart/test/schnorr.dart: Test Schnorr Signature with preset json [E]                                                                                             
  Expected: BigInt:<32488314169504238758641785015761338749482435938106244664205285362352711712684>
    Actual: BigInt:<52192130779442106267695258957333864339507133843386781845113627445046336399929>

  package:test_api        expect
  test/schnorr.dart 44:9  main.<fn>.<fn>
  ===== asynchronous gap ===========================
  test/schnorr.dart 12:5  main.<fn>

00:01 +1 -2: loading /Users/steven/Projects/LaksaDart/test/schnorr.dart                                                                                                                                  
Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
For example, 'pub run test --chain-stack-traces'.
00:02 +2 -3: /Users/steven/Projects/LaksaDart/test/contract.dart: Test call to scilla-runner [E]                                                                                                         
  Expected: <true>
    Actual: <false>

  package:test_api         expect
  test/contract.dart 59:7  main.<fn>.<fn>
  ===== asynchronous gap ===========================
  test/contract.dart 41:5  main.<fn>

00:02 +2 -3: loading /Users/steven/Projects/LaksaDart/test/contract.dart                                                                                                                                 
Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
For example, 'pub run test --chain-stack-traces'.
00:32 +4 -3: Some tests failed.  
neeboo commented 3 years ago

I forked the project to try and migrate to null safety, but there seems to be three tests that fail. I was wondering what might be a potential fix for some of the tests.

The culprit:

  @override
  BigInt nextBigInteger(int bitLength) {
    int fullBytes = bitLength ~/ 8;

    /// var remainingBits = bitLength % 8;

    /// Generate a number from the full bytes. Then, prepend a smaller number
    /// covering the remaining bits.
    BigInt main = numbers.bytesToInt(nextBytes(fullBytes));

    /// forcing remainingBits to be calculate with bitLength
    int remainingBits = (bitLength - main.bitLength);
    int additional = remainingBits < 4
        ? dartRandom.nextInt(pow(2, remainingBits))
        : remainingBits;
    BigInt additionalBit = (new BigInt.from(additional) << (fullBytes * 8));
    BigInt result = main + additionalBit;
    print(result.toString());
    return result;
  }

^ Returns back a negative (signed) on the result. I have tried using result.toUnsigned(result.bitLength) to no avail. Now I still have not researched why the other two tests are failing.

00:00 +1: /Users/steven/Projects/LaksaDart/test/keyGen.dart: test with 1000 keypairs                                                                                                                     
147587440077215482179262220947048256480457171726512571813821872888079478892669
-33447245391790795383774714820149278745318359833994383661338911822243363241714
00:00 +1 -1: /Users/steven/Projects/LaksaDart/test/keyGen.dart: test with 1000 keypairs [E]                                                                                                              
  Invalid argument(s): The multiplicator cannot be negative
  package:pointycastle/ecc/ecc_base.dart 142:7                     ECPointBase.*
  package:pointycastle/key_generators/ec_key_generator.dart 46:23  ECKeyGenerator.generateKeyPair
  package:laksadart/src/crypto/schnorr.dart 36:37                  generateNewPrivateKey
  package:laksadart/src/crypto/schnorr.dart 61:25                  generatePrivateKey
  test/keyGen.dart 9:23                                            main.<fn>

00:00 +1 -1: loading /Users/steven/Projects/LaksaDart/test/keyGen.dart                                                                                                                                   
Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
For example, 'pub run test --chain-stack-traces'.
00:01 +1 -2: /Users/steven/Projects/LaksaDart/test/schnorr.dart: Test Schnorr Signature with preset json [E]                                                                                             
  Expected: BigInt:<32488314169504238758641785015761338749482435938106244664205285362352711712684>
    Actual: BigInt:<52192130779442106267695258957333864339507133843386781845113627445046336399929>

  package:test_api        expect
  test/schnorr.dart 44:9  main.<fn>.<fn>
  ===== asynchronous gap ===========================
  test/schnorr.dart 12:5  main.<fn>

00:01 +1 -2: loading /Users/steven/Projects/LaksaDart/test/schnorr.dart                                                                                                                                  
Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
For example, 'pub run test --chain-stack-traces'.
00:02 +2 -3: /Users/steven/Projects/LaksaDart/test/contract.dart: Test call to scilla-runner [E]                                                                                                         
  Expected: <true>
    Actual: <false>

  package:test_api         expect
  test/contract.dart 59:7  main.<fn>.<fn>
  ===== asynchronous gap ===========================
  test/contract.dart 41:5  main.<fn>

00:02 +2 -3: loading /Users/steven/Projects/LaksaDart/test/contract.dart                                                                                                                                 
Consider enabling the flag chain-stack-traces to receive more detailed exceptions.
For example, 'pub run test --chain-stack-traces'.
00:32 +4 -3: Some tests failed.  

It may be some problem with the crypto lib, let me find out what happen

sjosegarcia commented 3 years ago

Sounds good! Let me know about your finding.

sjosegarcia commented 3 years ago

So it seems like the pointycastle library changed how they handled the decodeBigInt. Because of the changes BigInt main = numbers.bytesToInt(nextBytes(fullBytes)); would overflow and the rest is history. I tried playing around with the function but I would always go over 256 bytes.