Sdk: Dart 2's BigInt.modPow is bugged

Created on 21 Mar 2018  路  5Comments  路  Source: dart-lang/sdk

Minimal test case:

void main() {
  const input = "14222378113547797484180443703718287710954963648021535057076143638672814000321219503871352719175100865184619168128345594681547640115731246638";
  const dp = "68805717049526308324575273108573116001662526577152473869179706227957595091947965115631041308417430436199124027318143092441125820376694663934988040410650411495368889020042904305193636218299757516719158446153874604179501966374024692112438317379995729651506791282977824993147390378095874103264534184571632120755";
  const p = "144173682842817587002196172066264549138375068078359231382946906898412792452632726597279520229873489736777248181678202636100459215718497240474064366927544074501134727745837254834206456400508719134610847814227274992298238973375146473350157304285346424982280927848339601514720098577525635486320547905945936448443";

  final dart2 = (<T>() => T == int)<int>();
  if (dart2) {
    print(BigInt.parse(input).modPow(BigInt.parse(dp), BigInt.parse(p)));
  } else {
    print(int.parse(input).modPow(int.parse(dp), int.parse(p)));
  }
}

Dart 1.24.2 prints:


Dart 2.0.0-dev.40.0 prints:


With smaller numbers, modPow seems to work OK.

area-vm

Most helpful comment

I've been looking at code using modPow for over a week! This is such good news! This might solve https://github.com/PointyCastle/pointycastle/pull/122. When will it land in the dev channel??

All 5 comments

For now, as a workaround, this implementation of modPow from the googleapis_auth package seems to work fine.

https://github.com/dart-lang/googleapis_auth/blob/02c35bb8c559f04e3ff449ee0071e711e73de393/lib/src/crypto/rsa.dart#L72

Sorry @ds84182 - this issue seems to have completely fallen through the sieve. I have confirmed that old and new bigints produce different results, which should not happen.

$ out/ReleaseX64/dart --preview-dart-2 /tmp/bi.dart                                                                                                   
107195413767532249511858342477196263115037563715887024727527619427156370691743018647443671518445164651770783796458102512150439532505589800981751463014511276958633276079197156133641055629548339601513111412358616661314784403746179252284131012505017667908497957864265639477986489348115439873392893965780954625183
$ out/ReleaseX64/dart --no-limit-ints-to-64-bits /tmp/bi.dart                                                                                         
41228476947144730491819644448449646627743926889389391986712371102685149844677539601093216100085332586762793443185970606905210276466134532567499467782091302786983591600568927680640814869848681411989432528418918299321385420296108046942018595594076729397423805685944237555128652606412065971965116137839721723231

Thanks for the report. How embarrassing! This was a typo introduced while migrating Bigints from Dart1 to Dart2. CL on its way: https://dart-review.googlesource.com/c/sdk/+/54070

Thanks!

I've been looking at code using modPow for over a week! This is such good news! This might solve https://github.com/PointyCastle/pointycastle/pull/122. When will it land in the dev channel??

Was this page helpful?
0 / 5 - 0 ratings