LongArray.cs 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Text;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Math.EC
  6. {
  7. internal class LongArray
  8. {
  9. //private static long DEInterleave_MASK = 0x5555555555555555L;
  10. /*
  11. * This expands 8 bit indices into 16 bit contents (high bit 14), by inserting 0s between bits.
  12. * In a binary field, this operation is the same as squaring an 8 bit number.
  13. */
  14. private static readonly ushort[] INTERLEAVE2_TABLE = new ushort[]
  15. {
  16. 0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015,
  17. 0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055,
  18. 0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115,
  19. 0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155,
  20. 0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415,
  21. 0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455,
  22. 0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515,
  23. 0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555,
  24. 0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015,
  25. 0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055,
  26. 0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115,
  27. 0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155,
  28. 0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415,
  29. 0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455,
  30. 0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515,
  31. 0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555,
  32. 0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015,
  33. 0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055,
  34. 0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115,
  35. 0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155,
  36. 0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415,
  37. 0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455,
  38. 0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515,
  39. 0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555,
  40. 0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015,
  41. 0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055,
  42. 0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115,
  43. 0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155,
  44. 0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415,
  45. 0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455,
  46. 0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515,
  47. 0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555
  48. };
  49. /*
  50. * This expands 7 bit indices into 21 bit contents (high bit 18), by inserting 0s between bits.
  51. */
  52. private static readonly int[] INTERLEAVE3_TABLE = new int[]
  53. {
  54. 0x00000, 0x00001, 0x00008, 0x00009, 0x00040, 0x00041, 0x00048, 0x00049,
  55. 0x00200, 0x00201, 0x00208, 0x00209, 0x00240, 0x00241, 0x00248, 0x00249,
  56. 0x01000, 0x01001, 0x01008, 0x01009, 0x01040, 0x01041, 0x01048, 0x01049,
  57. 0x01200, 0x01201, 0x01208, 0x01209, 0x01240, 0x01241, 0x01248, 0x01249,
  58. 0x08000, 0x08001, 0x08008, 0x08009, 0x08040, 0x08041, 0x08048, 0x08049,
  59. 0x08200, 0x08201, 0x08208, 0x08209, 0x08240, 0x08241, 0x08248, 0x08249,
  60. 0x09000, 0x09001, 0x09008, 0x09009, 0x09040, 0x09041, 0x09048, 0x09049,
  61. 0x09200, 0x09201, 0x09208, 0x09209, 0x09240, 0x09241, 0x09248, 0x09249,
  62. 0x40000, 0x40001, 0x40008, 0x40009, 0x40040, 0x40041, 0x40048, 0x40049,
  63. 0x40200, 0x40201, 0x40208, 0x40209, 0x40240, 0x40241, 0x40248, 0x40249,
  64. 0x41000, 0x41001, 0x41008, 0x41009, 0x41040, 0x41041, 0x41048, 0x41049,
  65. 0x41200, 0x41201, 0x41208, 0x41209, 0x41240, 0x41241, 0x41248, 0x41249,
  66. 0x48000, 0x48001, 0x48008, 0x48009, 0x48040, 0x48041, 0x48048, 0x48049,
  67. 0x48200, 0x48201, 0x48208, 0x48209, 0x48240, 0x48241, 0x48248, 0x48249,
  68. 0x49000, 0x49001, 0x49008, 0x49009, 0x49040, 0x49041, 0x49048, 0x49049,
  69. 0x49200, 0x49201, 0x49208, 0x49209, 0x49240, 0x49241, 0x49248, 0x49249
  70. };
  71. /*
  72. * This expands 8 bit indices into 32 bit contents (high bit 28), by inserting 0s between bits.
  73. */
  74. private static readonly int[] INTERLEAVE4_TABLE = new int[]
  75. {
  76. 0x00000000, 0x00000001, 0x00000010, 0x00000011, 0x00000100, 0x00000101, 0x00000110, 0x00000111,
  77. 0x00001000, 0x00001001, 0x00001010, 0x00001011, 0x00001100, 0x00001101, 0x00001110, 0x00001111,
  78. 0x00010000, 0x00010001, 0x00010010, 0x00010011, 0x00010100, 0x00010101, 0x00010110, 0x00010111,
  79. 0x00011000, 0x00011001, 0x00011010, 0x00011011, 0x00011100, 0x00011101, 0x00011110, 0x00011111,
  80. 0x00100000, 0x00100001, 0x00100010, 0x00100011, 0x00100100, 0x00100101, 0x00100110, 0x00100111,
  81. 0x00101000, 0x00101001, 0x00101010, 0x00101011, 0x00101100, 0x00101101, 0x00101110, 0x00101111,
  82. 0x00110000, 0x00110001, 0x00110010, 0x00110011, 0x00110100, 0x00110101, 0x00110110, 0x00110111,
  83. 0x00111000, 0x00111001, 0x00111010, 0x00111011, 0x00111100, 0x00111101, 0x00111110, 0x00111111,
  84. 0x01000000, 0x01000001, 0x01000010, 0x01000011, 0x01000100, 0x01000101, 0x01000110, 0x01000111,
  85. 0x01001000, 0x01001001, 0x01001010, 0x01001011, 0x01001100, 0x01001101, 0x01001110, 0x01001111,
  86. 0x01010000, 0x01010001, 0x01010010, 0x01010011, 0x01010100, 0x01010101, 0x01010110, 0x01010111,
  87. 0x01011000, 0x01011001, 0x01011010, 0x01011011, 0x01011100, 0x01011101, 0x01011110, 0x01011111,
  88. 0x01100000, 0x01100001, 0x01100010, 0x01100011, 0x01100100, 0x01100101, 0x01100110, 0x01100111,
  89. 0x01101000, 0x01101001, 0x01101010, 0x01101011, 0x01101100, 0x01101101, 0x01101110, 0x01101111,
  90. 0x01110000, 0x01110001, 0x01110010, 0x01110011, 0x01110100, 0x01110101, 0x01110110, 0x01110111,
  91. 0x01111000, 0x01111001, 0x01111010, 0x01111011, 0x01111100, 0x01111101, 0x01111110, 0x01111111,
  92. 0x10000000, 0x10000001, 0x10000010, 0x10000011, 0x10000100, 0x10000101, 0x10000110, 0x10000111,
  93. 0x10001000, 0x10001001, 0x10001010, 0x10001011, 0x10001100, 0x10001101, 0x10001110, 0x10001111,
  94. 0x10010000, 0x10010001, 0x10010010, 0x10010011, 0x10010100, 0x10010101, 0x10010110, 0x10010111,
  95. 0x10011000, 0x10011001, 0x10011010, 0x10011011, 0x10011100, 0x10011101, 0x10011110, 0x10011111,
  96. 0x10100000, 0x10100001, 0x10100010, 0x10100011, 0x10100100, 0x10100101, 0x10100110, 0x10100111,
  97. 0x10101000, 0x10101001, 0x10101010, 0x10101011, 0x10101100, 0x10101101, 0x10101110, 0x10101111,
  98. 0x10110000, 0x10110001, 0x10110010, 0x10110011, 0x10110100, 0x10110101, 0x10110110, 0x10110111,
  99. 0x10111000, 0x10111001, 0x10111010, 0x10111011, 0x10111100, 0x10111101, 0x10111110, 0x10111111,
  100. 0x11000000, 0x11000001, 0x11000010, 0x11000011, 0x11000100, 0x11000101, 0x11000110, 0x11000111,
  101. 0x11001000, 0x11001001, 0x11001010, 0x11001011, 0x11001100, 0x11001101, 0x11001110, 0x11001111,
  102. 0x11010000, 0x11010001, 0x11010010, 0x11010011, 0x11010100, 0x11010101, 0x11010110, 0x11010111,
  103. 0x11011000, 0x11011001, 0x11011010, 0x11011011, 0x11011100, 0x11011101, 0x11011110, 0x11011111,
  104. 0x11100000, 0x11100001, 0x11100010, 0x11100011, 0x11100100, 0x11100101, 0x11100110, 0x11100111,
  105. 0x11101000, 0x11101001, 0x11101010, 0x11101011, 0x11101100, 0x11101101, 0x11101110, 0x11101111,
  106. 0x11110000, 0x11110001, 0x11110010, 0x11110011, 0x11110100, 0x11110101, 0x11110110, 0x11110111,
  107. 0x11111000, 0x11111001, 0x11111010, 0x11111011, 0x11111100, 0x11111101, 0x11111110, 0x11111111
  108. };
  109. /*
  110. * This expands 7 bit indices into 35 bit contents (high bit 30), by inserting 0s between bits.
  111. */
  112. private static readonly int[] INTERLEAVE5_TABLE = new int[] {
  113. 0x00000000, 0x00000001, 0x00000020, 0x00000021, 0x00000400, 0x00000401, 0x00000420, 0x00000421,
  114. 0x00008000, 0x00008001, 0x00008020, 0x00008021, 0x00008400, 0x00008401, 0x00008420, 0x00008421,
  115. 0x00100000, 0x00100001, 0x00100020, 0x00100021, 0x00100400, 0x00100401, 0x00100420, 0x00100421,
  116. 0x00108000, 0x00108001, 0x00108020, 0x00108021, 0x00108400, 0x00108401, 0x00108420, 0x00108421,
  117. 0x02000000, 0x02000001, 0x02000020, 0x02000021, 0x02000400, 0x02000401, 0x02000420, 0x02000421,
  118. 0x02008000, 0x02008001, 0x02008020, 0x02008021, 0x02008400, 0x02008401, 0x02008420, 0x02008421,
  119. 0x02100000, 0x02100001, 0x02100020, 0x02100021, 0x02100400, 0x02100401, 0x02100420, 0x02100421,
  120. 0x02108000, 0x02108001, 0x02108020, 0x02108021, 0x02108400, 0x02108401, 0x02108420, 0x02108421,
  121. 0x40000000, 0x40000001, 0x40000020, 0x40000021, 0x40000400, 0x40000401, 0x40000420, 0x40000421,
  122. 0x40008000, 0x40008001, 0x40008020, 0x40008021, 0x40008400, 0x40008401, 0x40008420, 0x40008421,
  123. 0x40100000, 0x40100001, 0x40100020, 0x40100021, 0x40100400, 0x40100401, 0x40100420, 0x40100421,
  124. 0x40108000, 0x40108001, 0x40108020, 0x40108021, 0x40108400, 0x40108401, 0x40108420, 0x40108421,
  125. 0x42000000, 0x42000001, 0x42000020, 0x42000021, 0x42000400, 0x42000401, 0x42000420, 0x42000421,
  126. 0x42008000, 0x42008001, 0x42008020, 0x42008021, 0x42008400, 0x42008401, 0x42008420, 0x42008421,
  127. 0x42100000, 0x42100001, 0x42100020, 0x42100021, 0x42100400, 0x42100401, 0x42100420, 0x42100421,
  128. 0x42108000, 0x42108001, 0x42108020, 0x42108021, 0x42108400, 0x42108401, 0x42108420, 0x42108421
  129. };
  130. /*
  131. * This expands 9 bit indices into 63 bit (long) contents (high bit 56), by inserting 0s between bits.
  132. */
  133. private static readonly long[] INTERLEAVE7_TABLE = new long[]
  134. {
  135. 0x0000000000000000L, 0x0000000000000001L, 0x0000000000000080L, 0x0000000000000081L,
  136. 0x0000000000004000L, 0x0000000000004001L, 0x0000000000004080L, 0x0000000000004081L,
  137. 0x0000000000200000L, 0x0000000000200001L, 0x0000000000200080L, 0x0000000000200081L,
  138. 0x0000000000204000L, 0x0000000000204001L, 0x0000000000204080L, 0x0000000000204081L,
  139. 0x0000000010000000L, 0x0000000010000001L, 0x0000000010000080L, 0x0000000010000081L,
  140. 0x0000000010004000L, 0x0000000010004001L, 0x0000000010004080L, 0x0000000010004081L,
  141. 0x0000000010200000L, 0x0000000010200001L, 0x0000000010200080L, 0x0000000010200081L,
  142. 0x0000000010204000L, 0x0000000010204001L, 0x0000000010204080L, 0x0000000010204081L,
  143. 0x0000000800000000L, 0x0000000800000001L, 0x0000000800000080L, 0x0000000800000081L,
  144. 0x0000000800004000L, 0x0000000800004001L, 0x0000000800004080L, 0x0000000800004081L,
  145. 0x0000000800200000L, 0x0000000800200001L, 0x0000000800200080L, 0x0000000800200081L,
  146. 0x0000000800204000L, 0x0000000800204001L, 0x0000000800204080L, 0x0000000800204081L,
  147. 0x0000000810000000L, 0x0000000810000001L, 0x0000000810000080L, 0x0000000810000081L,
  148. 0x0000000810004000L, 0x0000000810004001L, 0x0000000810004080L, 0x0000000810004081L,
  149. 0x0000000810200000L, 0x0000000810200001L, 0x0000000810200080L, 0x0000000810200081L,
  150. 0x0000000810204000L, 0x0000000810204001L, 0x0000000810204080L, 0x0000000810204081L,
  151. 0x0000040000000000L, 0x0000040000000001L, 0x0000040000000080L, 0x0000040000000081L,
  152. 0x0000040000004000L, 0x0000040000004001L, 0x0000040000004080L, 0x0000040000004081L,
  153. 0x0000040000200000L, 0x0000040000200001L, 0x0000040000200080L, 0x0000040000200081L,
  154. 0x0000040000204000L, 0x0000040000204001L, 0x0000040000204080L, 0x0000040000204081L,
  155. 0x0000040010000000L, 0x0000040010000001L, 0x0000040010000080L, 0x0000040010000081L,
  156. 0x0000040010004000L, 0x0000040010004001L, 0x0000040010004080L, 0x0000040010004081L,
  157. 0x0000040010200000L, 0x0000040010200001L, 0x0000040010200080L, 0x0000040010200081L,
  158. 0x0000040010204000L, 0x0000040010204001L, 0x0000040010204080L, 0x0000040010204081L,
  159. 0x0000040800000000L, 0x0000040800000001L, 0x0000040800000080L, 0x0000040800000081L,
  160. 0x0000040800004000L, 0x0000040800004001L, 0x0000040800004080L, 0x0000040800004081L,
  161. 0x0000040800200000L, 0x0000040800200001L, 0x0000040800200080L, 0x0000040800200081L,
  162. 0x0000040800204000L, 0x0000040800204001L, 0x0000040800204080L, 0x0000040800204081L,
  163. 0x0000040810000000L, 0x0000040810000001L, 0x0000040810000080L, 0x0000040810000081L,
  164. 0x0000040810004000L, 0x0000040810004001L, 0x0000040810004080L, 0x0000040810004081L,
  165. 0x0000040810200000L, 0x0000040810200001L, 0x0000040810200080L, 0x0000040810200081L,
  166. 0x0000040810204000L, 0x0000040810204001L, 0x0000040810204080L, 0x0000040810204081L,
  167. 0x0002000000000000L, 0x0002000000000001L, 0x0002000000000080L, 0x0002000000000081L,
  168. 0x0002000000004000L, 0x0002000000004001L, 0x0002000000004080L, 0x0002000000004081L,
  169. 0x0002000000200000L, 0x0002000000200001L, 0x0002000000200080L, 0x0002000000200081L,
  170. 0x0002000000204000L, 0x0002000000204001L, 0x0002000000204080L, 0x0002000000204081L,
  171. 0x0002000010000000L, 0x0002000010000001L, 0x0002000010000080L, 0x0002000010000081L,
  172. 0x0002000010004000L, 0x0002000010004001L, 0x0002000010004080L, 0x0002000010004081L,
  173. 0x0002000010200000L, 0x0002000010200001L, 0x0002000010200080L, 0x0002000010200081L,
  174. 0x0002000010204000L, 0x0002000010204001L, 0x0002000010204080L, 0x0002000010204081L,
  175. 0x0002000800000000L, 0x0002000800000001L, 0x0002000800000080L, 0x0002000800000081L,
  176. 0x0002000800004000L, 0x0002000800004001L, 0x0002000800004080L, 0x0002000800004081L,
  177. 0x0002000800200000L, 0x0002000800200001L, 0x0002000800200080L, 0x0002000800200081L,
  178. 0x0002000800204000L, 0x0002000800204001L, 0x0002000800204080L, 0x0002000800204081L,
  179. 0x0002000810000000L, 0x0002000810000001L, 0x0002000810000080L, 0x0002000810000081L,
  180. 0x0002000810004000L, 0x0002000810004001L, 0x0002000810004080L, 0x0002000810004081L,
  181. 0x0002000810200000L, 0x0002000810200001L, 0x0002000810200080L, 0x0002000810200081L,
  182. 0x0002000810204000L, 0x0002000810204001L, 0x0002000810204080L, 0x0002000810204081L,
  183. 0x0002040000000000L, 0x0002040000000001L, 0x0002040000000080L, 0x0002040000000081L,
  184. 0x0002040000004000L, 0x0002040000004001L, 0x0002040000004080L, 0x0002040000004081L,
  185. 0x0002040000200000L, 0x0002040000200001L, 0x0002040000200080L, 0x0002040000200081L,
  186. 0x0002040000204000L, 0x0002040000204001L, 0x0002040000204080L, 0x0002040000204081L,
  187. 0x0002040010000000L, 0x0002040010000001L, 0x0002040010000080L, 0x0002040010000081L,
  188. 0x0002040010004000L, 0x0002040010004001L, 0x0002040010004080L, 0x0002040010004081L,
  189. 0x0002040010200000L, 0x0002040010200001L, 0x0002040010200080L, 0x0002040010200081L,
  190. 0x0002040010204000L, 0x0002040010204001L, 0x0002040010204080L, 0x0002040010204081L,
  191. 0x0002040800000000L, 0x0002040800000001L, 0x0002040800000080L, 0x0002040800000081L,
  192. 0x0002040800004000L, 0x0002040800004001L, 0x0002040800004080L, 0x0002040800004081L,
  193. 0x0002040800200000L, 0x0002040800200001L, 0x0002040800200080L, 0x0002040800200081L,
  194. 0x0002040800204000L, 0x0002040800204001L, 0x0002040800204080L, 0x0002040800204081L,
  195. 0x0002040810000000L, 0x0002040810000001L, 0x0002040810000080L, 0x0002040810000081L,
  196. 0x0002040810004000L, 0x0002040810004001L, 0x0002040810004080L, 0x0002040810004081L,
  197. 0x0002040810200000L, 0x0002040810200001L, 0x0002040810200080L, 0x0002040810200081L,
  198. 0x0002040810204000L, 0x0002040810204001L, 0x0002040810204080L, 0x0002040810204081L,
  199. 0x0100000000000000L, 0x0100000000000001L, 0x0100000000000080L, 0x0100000000000081L,
  200. 0x0100000000004000L, 0x0100000000004001L, 0x0100000000004080L, 0x0100000000004081L,
  201. 0x0100000000200000L, 0x0100000000200001L, 0x0100000000200080L, 0x0100000000200081L,
  202. 0x0100000000204000L, 0x0100000000204001L, 0x0100000000204080L, 0x0100000000204081L,
  203. 0x0100000010000000L, 0x0100000010000001L, 0x0100000010000080L, 0x0100000010000081L,
  204. 0x0100000010004000L, 0x0100000010004001L, 0x0100000010004080L, 0x0100000010004081L,
  205. 0x0100000010200000L, 0x0100000010200001L, 0x0100000010200080L, 0x0100000010200081L,
  206. 0x0100000010204000L, 0x0100000010204001L, 0x0100000010204080L, 0x0100000010204081L,
  207. 0x0100000800000000L, 0x0100000800000001L, 0x0100000800000080L, 0x0100000800000081L,
  208. 0x0100000800004000L, 0x0100000800004001L, 0x0100000800004080L, 0x0100000800004081L,
  209. 0x0100000800200000L, 0x0100000800200001L, 0x0100000800200080L, 0x0100000800200081L,
  210. 0x0100000800204000L, 0x0100000800204001L, 0x0100000800204080L, 0x0100000800204081L,
  211. 0x0100000810000000L, 0x0100000810000001L, 0x0100000810000080L, 0x0100000810000081L,
  212. 0x0100000810004000L, 0x0100000810004001L, 0x0100000810004080L, 0x0100000810004081L,
  213. 0x0100000810200000L, 0x0100000810200001L, 0x0100000810200080L, 0x0100000810200081L,
  214. 0x0100000810204000L, 0x0100000810204001L, 0x0100000810204080L, 0x0100000810204081L,
  215. 0x0100040000000000L, 0x0100040000000001L, 0x0100040000000080L, 0x0100040000000081L,
  216. 0x0100040000004000L, 0x0100040000004001L, 0x0100040000004080L, 0x0100040000004081L,
  217. 0x0100040000200000L, 0x0100040000200001L, 0x0100040000200080L, 0x0100040000200081L,
  218. 0x0100040000204000L, 0x0100040000204001L, 0x0100040000204080L, 0x0100040000204081L,
  219. 0x0100040010000000L, 0x0100040010000001L, 0x0100040010000080L, 0x0100040010000081L,
  220. 0x0100040010004000L, 0x0100040010004001L, 0x0100040010004080L, 0x0100040010004081L,
  221. 0x0100040010200000L, 0x0100040010200001L, 0x0100040010200080L, 0x0100040010200081L,
  222. 0x0100040010204000L, 0x0100040010204001L, 0x0100040010204080L, 0x0100040010204081L,
  223. 0x0100040800000000L, 0x0100040800000001L, 0x0100040800000080L, 0x0100040800000081L,
  224. 0x0100040800004000L, 0x0100040800004001L, 0x0100040800004080L, 0x0100040800004081L,
  225. 0x0100040800200000L, 0x0100040800200001L, 0x0100040800200080L, 0x0100040800200081L,
  226. 0x0100040800204000L, 0x0100040800204001L, 0x0100040800204080L, 0x0100040800204081L,
  227. 0x0100040810000000L, 0x0100040810000001L, 0x0100040810000080L, 0x0100040810000081L,
  228. 0x0100040810004000L, 0x0100040810004001L, 0x0100040810004080L, 0x0100040810004081L,
  229. 0x0100040810200000L, 0x0100040810200001L, 0x0100040810200080L, 0x0100040810200081L,
  230. 0x0100040810204000L, 0x0100040810204001L, 0x0100040810204080L, 0x0100040810204081L,
  231. 0x0102000000000000L, 0x0102000000000001L, 0x0102000000000080L, 0x0102000000000081L,
  232. 0x0102000000004000L, 0x0102000000004001L, 0x0102000000004080L, 0x0102000000004081L,
  233. 0x0102000000200000L, 0x0102000000200001L, 0x0102000000200080L, 0x0102000000200081L,
  234. 0x0102000000204000L, 0x0102000000204001L, 0x0102000000204080L, 0x0102000000204081L,
  235. 0x0102000010000000L, 0x0102000010000001L, 0x0102000010000080L, 0x0102000010000081L,
  236. 0x0102000010004000L, 0x0102000010004001L, 0x0102000010004080L, 0x0102000010004081L,
  237. 0x0102000010200000L, 0x0102000010200001L, 0x0102000010200080L, 0x0102000010200081L,
  238. 0x0102000010204000L, 0x0102000010204001L, 0x0102000010204080L, 0x0102000010204081L,
  239. 0x0102000800000000L, 0x0102000800000001L, 0x0102000800000080L, 0x0102000800000081L,
  240. 0x0102000800004000L, 0x0102000800004001L, 0x0102000800004080L, 0x0102000800004081L,
  241. 0x0102000800200000L, 0x0102000800200001L, 0x0102000800200080L, 0x0102000800200081L,
  242. 0x0102000800204000L, 0x0102000800204001L, 0x0102000800204080L, 0x0102000800204081L,
  243. 0x0102000810000000L, 0x0102000810000001L, 0x0102000810000080L, 0x0102000810000081L,
  244. 0x0102000810004000L, 0x0102000810004001L, 0x0102000810004080L, 0x0102000810004081L,
  245. 0x0102000810200000L, 0x0102000810200001L, 0x0102000810200080L, 0x0102000810200081L,
  246. 0x0102000810204000L, 0x0102000810204001L, 0x0102000810204080L, 0x0102000810204081L,
  247. 0x0102040000000000L, 0x0102040000000001L, 0x0102040000000080L, 0x0102040000000081L,
  248. 0x0102040000004000L, 0x0102040000004001L, 0x0102040000004080L, 0x0102040000004081L,
  249. 0x0102040000200000L, 0x0102040000200001L, 0x0102040000200080L, 0x0102040000200081L,
  250. 0x0102040000204000L, 0x0102040000204001L, 0x0102040000204080L, 0x0102040000204081L,
  251. 0x0102040010000000L, 0x0102040010000001L, 0x0102040010000080L, 0x0102040010000081L,
  252. 0x0102040010004000L, 0x0102040010004001L, 0x0102040010004080L, 0x0102040010004081L,
  253. 0x0102040010200000L, 0x0102040010200001L, 0x0102040010200080L, 0x0102040010200081L,
  254. 0x0102040010204000L, 0x0102040010204001L, 0x0102040010204080L, 0x0102040010204081L,
  255. 0x0102040800000000L, 0x0102040800000001L, 0x0102040800000080L, 0x0102040800000081L,
  256. 0x0102040800004000L, 0x0102040800004001L, 0x0102040800004080L, 0x0102040800004081L,
  257. 0x0102040800200000L, 0x0102040800200001L, 0x0102040800200080L, 0x0102040800200081L,
  258. 0x0102040800204000L, 0x0102040800204001L, 0x0102040800204080L, 0x0102040800204081L,
  259. 0x0102040810000000L, 0x0102040810000001L, 0x0102040810000080L, 0x0102040810000081L,
  260. 0x0102040810004000L, 0x0102040810004001L, 0x0102040810004080L, 0x0102040810004081L,
  261. 0x0102040810200000L, 0x0102040810200001L, 0x0102040810200080L, 0x0102040810200081L,
  262. 0x0102040810204000L, 0x0102040810204001L, 0x0102040810204080L, 0x0102040810204081L
  263. };
  264. // For toString(); must have length 64
  265. private const string ZEROES = "0000000000000000000000000000000000000000000000000000000000000000";
  266. internal static readonly byte[] BitLengths =
  267. {
  268. 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
  269. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  270. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  271. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  272. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  273. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  274. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  275. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  276. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  277. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  278. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  279. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  280. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  281. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  282. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
  283. 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8
  284. };
  285. // TODO make m fixed for the LongArray, and hence compute T once and for all
  286. private long[] m_ints;
  287. public LongArray(int intLen)
  288. {
  289. m_ints = new long[intLen];
  290. }
  291. public LongArray(long[] ints)
  292. {
  293. m_ints = ints;
  294. }
  295. public LongArray(long[] ints, int off, int len)
  296. {
  297. if (off == 0 && len == ints.Length)
  298. {
  299. m_ints = ints;
  300. }
  301. else
  302. {
  303. m_ints = new long[len];
  304. Array.Copy(ints, off, m_ints, 0, len);
  305. }
  306. }
  307. public LongArray(BigInteger bigInt)
  308. {
  309. if (bigInt == null || bigInt.SignValue < 0)
  310. {
  311. throw new ArgumentException("invalid F2m field value", "bigInt");
  312. }
  313. if (bigInt.SignValue == 0)
  314. {
  315. m_ints = new long[] { 0L };
  316. return;
  317. }
  318. byte[] barr = bigInt.ToByteArray();
  319. int barrLen = barr.Length;
  320. int barrStart = 0;
  321. if (barr[0] == 0)
  322. {
  323. // First byte is 0 to enforce highest (=sign) bit is zero.
  324. // In this case ignore barr[0].
  325. barrLen--;
  326. barrStart = 1;
  327. }
  328. int intLen = (barrLen + 7) / 8;
  329. m_ints = new long[intLen];
  330. int iarrJ = intLen - 1;
  331. int rem = barrLen % 8 + barrStart;
  332. long temp = 0;
  333. int barrI = barrStart;
  334. if (barrStart < rem)
  335. {
  336. for (; barrI < rem; barrI++)
  337. {
  338. temp <<= 8;
  339. uint barrBarrI = barr[barrI];
  340. temp |= barrBarrI;
  341. }
  342. m_ints[iarrJ--] = temp;
  343. }
  344. for (; iarrJ >= 0; iarrJ--)
  345. {
  346. temp = 0;
  347. for (int i = 0; i < 8; i++)
  348. {
  349. temp <<= 8;
  350. uint barrBarrI = barr[barrI++];
  351. temp |= barrBarrI;
  352. }
  353. m_ints[iarrJ] = temp;
  354. }
  355. }
  356. public bool IsOne()
  357. {
  358. long[] a = m_ints;
  359. if (a[0] != 1L)
  360. {
  361. return false;
  362. }
  363. for (int i = 1; i < a.Length; ++i)
  364. {
  365. if (a[i] != 0L)
  366. {
  367. return false;
  368. }
  369. }
  370. return true;
  371. }
  372. public bool IsZero()
  373. {
  374. long[] a = m_ints;
  375. for (int i = 0; i < a.Length; ++i)
  376. {
  377. if (a[i] != 0L)
  378. {
  379. return false;
  380. }
  381. }
  382. return true;
  383. }
  384. public int GetUsedLength()
  385. {
  386. return GetUsedLengthFrom(m_ints.Length);
  387. }
  388. public int GetUsedLengthFrom(int from)
  389. {
  390. long[] a = m_ints;
  391. from = System.Math.Min(from, a.Length);
  392. if (from < 1)
  393. {
  394. return 0;
  395. }
  396. // Check if first element will act as sentinel
  397. if (a[0] != 0)
  398. {
  399. while (a[--from] == 0)
  400. {
  401. }
  402. return from + 1;
  403. }
  404. do
  405. {
  406. if (a[--from] != 0)
  407. {
  408. return from + 1;
  409. }
  410. }
  411. while (from > 0);
  412. return 0;
  413. }
  414. public int Degree()
  415. {
  416. int i = m_ints.Length;
  417. long w;
  418. do
  419. {
  420. if (i == 0)
  421. {
  422. return 0;
  423. }
  424. w = m_ints[--i];
  425. }
  426. while (w == 0);
  427. return (i << 6) + BitLength(w);
  428. }
  429. private int DegreeFrom(int limit)
  430. {
  431. int i = (int)(((uint)limit + 62) >> 6);
  432. long w;
  433. do
  434. {
  435. if (i == 0)
  436. {
  437. return 0;
  438. }
  439. w = m_ints[--i];
  440. }
  441. while (w == 0);
  442. return (i << 6) + BitLength(w);
  443. }
  444. // private int lowestCoefficient()
  445. // {
  446. // for (int i = 0; i < m_ints.Length; ++i)
  447. // {
  448. // long mi = m_ints[i];
  449. // if (mi != 0)
  450. // {
  451. // int j = 0;
  452. // while ((mi & 0xFFL) == 0)
  453. // {
  454. // j += 8;
  455. // mi >>>= 8;
  456. // }
  457. // while ((mi & 1L) == 0)
  458. // {
  459. // ++j;
  460. // mi >>>= 1;
  461. // }
  462. // return (i << 6) + j;
  463. // }
  464. // }
  465. // return -1;
  466. // }
  467. private static int BitLength(long w)
  468. {
  469. int u = (int)((ulong)w >> 32), b;
  470. if (u == 0)
  471. {
  472. u = (int)w;
  473. b = 0;
  474. }
  475. else
  476. {
  477. b = 32;
  478. }
  479. int t = (int)((uint)u >> 16), k;
  480. if (t == 0)
  481. {
  482. t = (int)((uint)u >> 8);
  483. k = (t == 0) ? BitLengths[u] : 8 + BitLengths[t];
  484. }
  485. else
  486. {
  487. int v = (int)((uint)t >> 8);
  488. k = (v == 0) ? 16 + BitLengths[t] : 24 + BitLengths[v];
  489. }
  490. return b + k;
  491. }
  492. private long[] ResizedInts(int newLen)
  493. {
  494. long[] newInts = new long[newLen];
  495. Array.Copy(m_ints, 0, newInts, 0, System.Math.Min(m_ints.Length, newLen));
  496. return newInts;
  497. }
  498. public BigInteger ToBigInteger()
  499. {
  500. int usedLen = GetUsedLength();
  501. if (usedLen == 0)
  502. {
  503. return BigInteger.Zero;
  504. }
  505. long highestInt = m_ints[usedLen - 1];
  506. byte[] temp = new byte[8];
  507. int barrI = 0;
  508. bool trailingZeroBytesDone = false;
  509. for (int j = 7; j >= 0; j--)
  510. {
  511. byte thisByte = (byte)((ulong)highestInt >> (8 * j));
  512. if (trailingZeroBytesDone || (thisByte != 0))
  513. {
  514. trailingZeroBytesDone = true;
  515. temp[barrI++] = thisByte;
  516. }
  517. }
  518. int barrLen = 8 * (usedLen - 1) + barrI;
  519. byte[] barr = new byte[barrLen];
  520. for (int j = 0; j < barrI; j++)
  521. {
  522. barr[j] = temp[j];
  523. }
  524. // Highest value int is done now
  525. for (int iarrJ = usedLen - 2; iarrJ >= 0; iarrJ--)
  526. {
  527. long mi = m_ints[iarrJ];
  528. for (int j = 7; j >= 0; j--)
  529. {
  530. barr[barrI++] = (byte)((ulong)mi >> (8 * j));
  531. }
  532. }
  533. return new BigInteger(1, barr);
  534. }
  535. // private static long shiftUp(long[] x, int xOff, int count)
  536. // {
  537. // long prev = 0;
  538. // for (int i = 0; i < count; ++i)
  539. // {
  540. // long next = x[xOff + i];
  541. // x[xOff + i] = (next << 1) | prev;
  542. // prev = next >>> 63;
  543. // }
  544. // return prev;
  545. // }
  546. private static long ShiftUp(long[] x, int xOff, int count, int shift)
  547. {
  548. int shiftInv = 64 - shift;
  549. long prev = 0;
  550. for (int i = 0; i < count; ++i)
  551. {
  552. long next = x[xOff + i];
  553. x[xOff + i] = (next << shift) | prev;
  554. prev = (long)((ulong)next >> shiftInv);
  555. }
  556. return prev;
  557. }
  558. private static long ShiftUp(long[] x, int xOff, long[] z, int zOff, int count, int shift)
  559. {
  560. int shiftInv = 64 - shift;
  561. long prev = 0;
  562. for (int i = 0; i < count; ++i)
  563. {
  564. long next = x[xOff + i];
  565. z[zOff + i] = (next << shift) | prev;
  566. prev = (long)((ulong)next >> shiftInv);
  567. }
  568. return prev;
  569. }
  570. public LongArray AddOne()
  571. {
  572. if (m_ints.Length == 0)
  573. {
  574. return new LongArray(new long[]{ 1L });
  575. }
  576. int resultLen = System.Math.Max(1, GetUsedLength());
  577. long[] ints = ResizedInts(resultLen);
  578. ints[0] ^= 1L;
  579. return new LongArray(ints);
  580. }
  581. // private void addShiftedByBits(LongArray other, int bits)
  582. // {
  583. // int words = bits >>> 6;
  584. // int shift = bits & 0x3F;
  585. //
  586. // if (shift == 0)
  587. // {
  588. // addShiftedByWords(other, words);
  589. // return;
  590. // }
  591. //
  592. // int otherUsedLen = other.GetUsedLength();
  593. // if (otherUsedLen == 0)
  594. // {
  595. // return;
  596. // }
  597. //
  598. // int minLen = otherUsedLen + words + 1;
  599. // if (minLen > m_ints.Length)
  600. // {
  601. // m_ints = resizedInts(minLen);
  602. // }
  603. //
  604. // long carry = addShiftedByBits(m_ints, words, other.m_ints, 0, otherUsedLen, shift);
  605. // m_ints[otherUsedLen + words] ^= carry;
  606. // }
  607. private void AddShiftedByBitsSafe(LongArray other, int otherDegree, int bits)
  608. {
  609. int otherLen = (int)((uint)(otherDegree + 63) >> 6);
  610. int words = (int)((uint)bits >> 6);
  611. int shift = bits & 0x3F;
  612. if (shift == 0)
  613. {
  614. Add(m_ints, words, other.m_ints, 0, otherLen);
  615. return;
  616. }
  617. long carry = AddShiftedUp(m_ints, words, other.m_ints, 0, otherLen, shift);
  618. if (carry != 0L)
  619. {
  620. m_ints[otherLen + words] ^= carry;
  621. }
  622. }
  623. private static long AddShiftedUp(long[] x, int xOff, long[] y, int yOff, int count, int shift)
  624. {
  625. int shiftInv = 64 - shift;
  626. long prev = 0;
  627. for (int i = 0; i < count; ++i)
  628. {
  629. long next = y[yOff + i];
  630. x[xOff + i] ^= (next << shift) | prev;
  631. prev = (long)((ulong)next >> shiftInv);
  632. }
  633. return prev;
  634. }
  635. private static long AddShiftedDown(long[] x, int xOff, long[] y, int yOff, int count, int shift)
  636. {
  637. int shiftInv = 64 - shift;
  638. long prev = 0;
  639. int i = count;
  640. while (--i >= 0)
  641. {
  642. long next = y[yOff + i];
  643. x[xOff + i] ^= (long)((ulong)next >> shift) | prev;
  644. prev = next << shiftInv;
  645. }
  646. return prev;
  647. }
  648. public void AddShiftedByWords(LongArray other, int words)
  649. {
  650. int otherUsedLen = other.GetUsedLength();
  651. if (otherUsedLen == 0)
  652. {
  653. return;
  654. }
  655. int minLen = otherUsedLen + words;
  656. if (minLen > m_ints.Length)
  657. {
  658. m_ints = ResizedInts(minLen);
  659. }
  660. Add(m_ints, words, other.m_ints, 0, otherUsedLen);
  661. }
  662. private static void Add(long[] x, int xOff, long[] y, int yOff, int count)
  663. {
  664. for (int i = 0; i < count; ++i)
  665. {
  666. x[xOff + i] ^= y[yOff + i];
  667. }
  668. }
  669. private static void Add(long[] x, int xOff, long[] y, int yOff, long[] z, int zOff, int count)
  670. {
  671. for (int i = 0; i < count; ++i)
  672. {
  673. z[zOff + i] = x[xOff + i] ^ y[yOff + i];
  674. }
  675. }
  676. private static void AddBoth(long[] x, int xOff, long[] y1, int y1Off, long[] y2, int y2Off, int count)
  677. {
  678. for (int i = 0; i < count; ++i)
  679. {
  680. x[xOff + i] ^= y1[y1Off + i] ^ y2[y2Off + i];
  681. }
  682. }
  683. private static void Distribute(long[] x, int src, int dst1, int dst2, int count)
  684. {
  685. for (int i = 0; i < count; ++i)
  686. {
  687. long v = x[src + i];
  688. x[dst1 + i] ^= v;
  689. x[dst2 + i] ^= v;
  690. }
  691. }
  692. public int Length
  693. {
  694. get { return m_ints.Length; }
  695. }
  696. private static void FlipWord(long[] buf, int off, int bit, long word)
  697. {
  698. int n = off + (int)((uint)bit >> 6);
  699. int shift = bit & 0x3F;
  700. if (shift == 0)
  701. {
  702. buf[n] ^= word;
  703. }
  704. else
  705. {
  706. buf[n] ^= word << shift;
  707. word = (long)((ulong)word >> (64 - shift));
  708. if (word != 0)
  709. {
  710. buf[++n] ^= word;
  711. }
  712. }
  713. }
  714. // private static long getWord(long[] buf, int off, int len, int bit)
  715. // {
  716. // int n = off + (bit >>> 6);
  717. // int shift = bit & 0x3F;
  718. // if (shift == 0)
  719. // {
  720. // return buf[n];
  721. // }
  722. // long result = buf[n] >>> shift;
  723. // if (++n < len)
  724. // {
  725. // result |= buf[n] << (64 - shift);
  726. // }
  727. // return result;
  728. // }
  729. public bool TestBitZero()
  730. {
  731. return m_ints.Length > 0 && (m_ints[0] & 1L) != 0;
  732. }
  733. private static bool TestBit(long[] buf, int off, int n)
  734. {
  735. // theInt = n / 64
  736. int theInt = (int)((uint)n >> 6);
  737. // theBit = n % 64
  738. int theBit = n & 0x3F;
  739. long tester = 1L << theBit;
  740. return (buf[off + theInt] & tester) != 0;
  741. }
  742. private static void FlipBit(long[] buf, int off, int n)
  743. {
  744. // theInt = n / 64
  745. int theInt = (int)((uint)n >> 6);
  746. // theBit = n % 64
  747. int theBit = n & 0x3F;
  748. long flipper = 1L << theBit;
  749. buf[off + theInt] ^= flipper;
  750. }
  751. // private static void SetBit(long[] buf, int off, int n)
  752. // {
  753. // // theInt = n / 64
  754. // int theInt = n >>> 6;
  755. // // theBit = n % 64
  756. // int theBit = n & 0x3F;
  757. // long setter = 1L << theBit;
  758. // buf[off + theInt] |= setter;
  759. // }
  760. //
  761. // private static void ClearBit(long[] buf, int off, int n)
  762. // {
  763. // // theInt = n / 64
  764. // int theInt = n >>> 6;
  765. // // theBit = n % 64
  766. // int theBit = n & 0x3F;
  767. // long setter = 1L << theBit;
  768. // buf[off + theInt] &= ~setter;
  769. // }
  770. private static void MultiplyWord(long a, long[] b, int bLen, long[] c, int cOff)
  771. {
  772. if ((a & 1L) != 0L)
  773. {
  774. Add(c, cOff, b, 0, bLen);
  775. }
  776. int k = 1;
  777. while ((a = (long)((ulong)a >> 1)) != 0L)
  778. {
  779. if ((a & 1L) != 0L)
  780. {
  781. long carry = AddShiftedUp(c, cOff, b, 0, bLen, k);
  782. if (carry != 0L)
  783. {
  784. c[cOff + bLen] ^= carry;
  785. }
  786. }
  787. ++k;
  788. }
  789. }
  790. public LongArray ModMultiplyLD(LongArray other, int m, int[] ks)
  791. {
  792. /*
  793. * Find out the degree of each argument and handle the zero cases
  794. */
  795. int aDeg = Degree();
  796. if (aDeg == 0)
  797. {
  798. return this;
  799. }
  800. int bDeg = other.Degree();
  801. if (bDeg == 0)
  802. {
  803. return other;
  804. }
  805. /*
  806. * Swap if necessary so that A is the smaller argument
  807. */
  808. LongArray A = this, B = other;
  809. if (aDeg > bDeg)
  810. {
  811. A = other; B = this;
  812. int tmp = aDeg; aDeg = bDeg; bDeg = tmp;
  813. }
  814. /*
  815. * Establish the word lengths of the arguments and result
  816. */
  817. int aLen = (int)((uint)(aDeg + 63) >> 6);
  818. int bLen = (int)((uint)(bDeg + 63) >> 6);
  819. int cLen = (int)((uint)(aDeg + bDeg + 62) >> 6);
  820. if (aLen == 1)
  821. {
  822. long a0 = A.m_ints[0];
  823. if (a0 == 1L)
  824. {
  825. return B;
  826. }
  827. /*
  828. * Fast path for small A, with performance dependent only on the number of set bits
  829. */
  830. long[] c0 = new long[cLen];
  831. MultiplyWord(a0, B.m_ints, bLen, c0, 0);
  832. /*
  833. * Reduce the raw answer against the reduction coefficients
  834. */
  835. return ReduceResult(c0, 0, cLen, m, ks);
  836. }
  837. /*
  838. * Determine if B will get bigger during shifting
  839. */
  840. int bMax = (int)((uint)(bDeg + 7 + 63) >> 6);
  841. /*
  842. * Lookup table for the offset of each B in the tables
  843. */
  844. int[] ti = new int[16];
  845. /*
  846. * Precompute table of all 4-bit products of B
  847. */
  848. long[] T0 = new long[bMax << 4];
  849. int tOff = bMax;
  850. ti[1] = tOff;
  851. Array.Copy(B.m_ints, 0, T0, tOff, bLen);
  852. for (int i = 2; i < 16; ++i)
  853. {
  854. ti[i] = (tOff += bMax);
  855. if ((i & 1) == 0)
  856. {
  857. ShiftUp(T0, (int)((uint)tOff >> 1), T0, tOff, bMax, 1);
  858. }
  859. else
  860. {
  861. Add(T0, bMax, T0, tOff - bMax, T0, tOff, bMax);
  862. }
  863. }
  864. /*
  865. * Second table with all 4-bit products of B shifted 4 bits
  866. */
  867. long[] T1 = new long[T0.Length];
  868. ShiftUp(T0, 0, T1, 0, T0.Length, 4);
  869. // shiftUp(T0, bMax, T1, bMax, tOff, 4);
  870. long[] a = A.m_ints;
  871. long[] c = new long[cLen];
  872. int MASK = 0xF;
  873. /*
  874. * Lopez-Dahab algorithm
  875. */
  876. for (int k = 56; k >= 0; k -= 8)
  877. {
  878. for (int j = 1; j < aLen; j += 2)
  879. {
  880. int aVal = (int)((ulong)a[j] >> k);
  881. int u = aVal & MASK;
  882. int v = (int)((uint)aVal >> 4) & MASK;
  883. AddBoth(c, j - 1, T0, ti[u], T1, ti[v], bMax);
  884. }
  885. ShiftUp(c, 0, cLen, 8);
  886. }
  887. for (int k = 56; k >= 0; k -= 8)
  888. {
  889. for (int j = 0; j < aLen; j += 2)
  890. {
  891. int aVal = (int)((ulong)a[j] >> k);
  892. int u = aVal & MASK;
  893. int v = (int)((uint)aVal >> 4) & MASK;
  894. AddBoth(c, j, T0, ti[u], T1, ti[v], bMax);
  895. }
  896. if (k > 0)
  897. {
  898. ShiftUp(c, 0, cLen, 8);
  899. }
  900. }
  901. /*
  902. * Finally the raw answer is collected, reduce it against the reduction coefficients
  903. */
  904. return ReduceResult(c, 0, cLen, m, ks);
  905. }
  906. public LongArray ModMultiply(LongArray other, int m, int[] ks)
  907. {
  908. /*
  909. * Find out the degree of each argument and handle the zero cases
  910. */
  911. int aDeg = Degree();
  912. if (aDeg == 0)
  913. {
  914. return this;
  915. }
  916. int bDeg = other.Degree();
  917. if (bDeg == 0)
  918. {
  919. return other;
  920. }
  921. /*
  922. * Swap if necessary so that A is the smaller argument
  923. */
  924. LongArray A = this, B = other;
  925. if (aDeg > bDeg)
  926. {
  927. A = other; B = this;
  928. int tmp = aDeg; aDeg = bDeg; bDeg = tmp;
  929. }
  930. /*
  931. * Establish the word lengths of the arguments and result
  932. */
  933. int aLen = (int)((uint)(aDeg + 63) >> 6);
  934. int bLen = (int)((uint)(bDeg + 63) >> 6);
  935. int cLen = (int)((uint)(aDeg + bDeg + 62) >> 6);
  936. if (aLen == 1)
  937. {
  938. long a0 = A.m_ints[0];
  939. if (a0 == 1L)
  940. {
  941. return B;
  942. }
  943. /*
  944. * Fast path for small A, with performance dependent only on the number of set bits
  945. */
  946. long[] c0 = new long[cLen];
  947. MultiplyWord(a0, B.m_ints, bLen, c0, 0);
  948. /*
  949. * Reduce the raw answer against the reduction coefficients
  950. */
  951. return ReduceResult(c0, 0, cLen, m, ks);
  952. }
  953. /*
  954. * Determine if B will get bigger during shifting
  955. */
  956. int bMax = (int)((uint)(bDeg + 7 + 63) >> 6);
  957. /*
  958. * Lookup table for the offset of each B in the tables
  959. */
  960. int[] ti = new int[16];
  961. /*
  962. * Precompute table of all 4-bit products of B
  963. */
  964. long[] T0 = new long[bMax << 4];
  965. int tOff = bMax;
  966. ti[1] = tOff;
  967. Array.Copy(B.m_ints, 0, T0, tOff, bLen);
  968. for (int i = 2; i < 16; ++i)
  969. {
  970. ti[i] = (tOff += bMax);
  971. if ((i & 1) == 0)
  972. {
  973. ShiftUp(T0, (int)((uint)tOff >> 1), T0, tOff, bMax, 1);
  974. }
  975. else
  976. {
  977. Add(T0, bMax, T0, tOff - bMax, T0, tOff, bMax);
  978. }
  979. }
  980. /*
  981. * Second table with all 4-bit products of B shifted 4 bits
  982. */
  983. long[] T1 = new long[T0.Length];
  984. ShiftUp(T0, 0, T1, 0, T0.Length, 4);
  985. // ShiftUp(T0, bMax, T1, bMax, tOff, 4);
  986. long[] a = A.m_ints;
  987. long[] c = new long[cLen << 3];
  988. int MASK = 0xF;
  989. /*
  990. * Lopez-Dahab (Modified) algorithm
  991. */
  992. for (int aPos = 0; aPos < aLen; ++aPos)
  993. {
  994. long aVal = a[aPos];
  995. int cOff = aPos;
  996. for (;;)
  997. {
  998. int u = (int)aVal & MASK;
  999. aVal = (long)((ulong)aVal >> 4);
  1000. int v = (int)aVal & MASK;
  1001. AddBoth(c, cOff, T0, ti[u], T1, ti[v], bMax);
  1002. aVal = (long)((ulong)aVal >> 4);
  1003. if (aVal == 0L)
  1004. {
  1005. break;
  1006. }
  1007. cOff += cLen;
  1008. }
  1009. }
  1010. {
  1011. int cOff = c.Length;
  1012. while ((cOff -= cLen) != 0)
  1013. {
  1014. AddShiftedUp(c, cOff - cLen, c, cOff, cLen, 8);
  1015. }
  1016. }
  1017. /*
  1018. * Finally the raw answer is collected, reduce it against the reduction coefficients
  1019. */
  1020. return ReduceResult(c, 0, cLen, m, ks);
  1021. }
  1022. public LongArray ModMultiplyAlt(LongArray other, int m, int[] ks)
  1023. {
  1024. /*
  1025. * Find out the degree of each argument and handle the zero cases
  1026. */
  1027. int aDeg = Degree();
  1028. if (aDeg == 0)
  1029. {
  1030. return this;
  1031. }
  1032. int bDeg = other.Degree();
  1033. if (bDeg == 0)
  1034. {
  1035. return other;
  1036. }
  1037. /*
  1038. * Swap if necessary so that A is the smaller argument
  1039. */
  1040. LongArray A = this, B = other;
  1041. if (aDeg > bDeg)
  1042. {
  1043. A = other; B = this;
  1044. int tmp = aDeg; aDeg = bDeg; bDeg = tmp;
  1045. }
  1046. /*
  1047. * Establish the word lengths of the arguments and result
  1048. */
  1049. int aLen = (int)((uint)(aDeg + 63) >> 6);
  1050. int bLen = (int)((uint)(bDeg + 63) >> 6);
  1051. int cLen = (int)((uint)(aDeg + bDeg + 62) >> 6);
  1052. if (aLen == 1)
  1053. {
  1054. long a0 = A.m_ints[0];
  1055. if (a0 == 1L)
  1056. {
  1057. return B;
  1058. }
  1059. /*
  1060. * Fast path for small A, with performance dependent only on the number of set bits
  1061. */
  1062. long[] c0 = new long[cLen];
  1063. MultiplyWord(a0, B.m_ints, bLen, c0, 0);
  1064. /*
  1065. * Reduce the raw answer against the reduction coefficients
  1066. */
  1067. return ReduceResult(c0, 0, cLen, m, ks);
  1068. }
  1069. // NOTE: This works, but is slower than width 4 processing
  1070. // if (aLen == 2)
  1071. // {
  1072. // /*
  1073. // * Use common-multiplicand optimization to save ~1/4 of the adds
  1074. // */
  1075. // long a1 = A.m_ints[0], a2 = A.m_ints[1];
  1076. // long aa = a1 & a2; a1 ^= aa; a2 ^= aa;
  1077. //
  1078. // long[] b = B.m_ints;
  1079. // long[] c = new long[cLen];
  1080. // multiplyWord(aa, b, bLen, c, 1);
  1081. // add(c, 0, c, 1, cLen - 1);
  1082. // multiplyWord(a1, b, bLen, c, 0);
  1083. // multiplyWord(a2, b, bLen, c, 1);
  1084. //
  1085. // /*
  1086. // * Reduce the raw answer against the reduction coefficients
  1087. // */
  1088. // return ReduceResult(c, 0, cLen, m, ks);
  1089. // }
  1090. /*
  1091. * Determine the parameters of the Interleaved window algorithm: the 'width' in bits to
  1092. * process together, the number of evaluation 'positions' implied by that width, and the
  1093. * 'top' position at which the regular window algorithm stops.
  1094. */
  1095. int width, positions, top, banks;
  1096. // NOTE: width 4 is the fastest over the entire range of sizes used in current crypto
  1097. // width = 1; positions = 64; top = 64; banks = 4;
  1098. // width = 2; positions = 32; top = 64; banks = 4;
  1099. // width = 3; positions = 21; top = 63; banks = 3;
  1100. width = 4; positions = 16; top = 64; banks = 8;
  1101. // width = 5; positions = 13; top = 65; banks = 7;
  1102. // width = 7; positions = 9; top = 63; banks = 9;
  1103. // width = 8; positions = 8; top = 64; banks = 8;
  1104. /*
  1105. * Determine if B will get bigger during shifting
  1106. */
  1107. int shifts = top < 64 ? positions : positions - 1;
  1108. int bMax = (int)((uint)(bDeg + shifts + 63) >> 6);
  1109. int bTotal = bMax * banks, stride = width * banks;
  1110. /*
  1111. * Create a single temporary buffer, with an offset table to find the positions of things in it
  1112. */
  1113. int[] ci = new int[1 << width];
  1114. int cTotal = aLen;
  1115. {
  1116. ci[0] = cTotal;
  1117. cTotal += bTotal;
  1118. ci[1] = cTotal;
  1119. for (int i = 2; i < ci.Length; ++i)
  1120. {
  1121. cTotal += cLen;
  1122. ci[i] = cTotal;
  1123. }
  1124. cTotal += cLen;
  1125. }
  1126. // NOTE: Provide a safe dump for "high zeroes" since we are adding 'bMax' and not 'bLen'
  1127. ++cTotal;
  1128. long[] c = new long[cTotal];
  1129. // Prepare A in Interleaved form, according to the chosen width
  1130. Interleave(A.m_ints, 0, c, 0, aLen, width);
  1131. // Make a working copy of B, since we will be shifting it
  1132. {
  1133. int bOff = aLen;
  1134. Array.Copy(B.m_ints, 0, c, bOff, bLen);
  1135. for (int bank = 1; bank < banks; ++bank)
  1136. {
  1137. ShiftUp(c, aLen, c, bOff += bMax, bMax, bank);
  1138. }
  1139. }
  1140. /*
  1141. * The main loop analyzes the Interleaved windows in A, and for each non-zero window
  1142. * a single word-array XOR is performed to a carefully selected slice of 'c'. The loop is
  1143. * breadth-first, checking the lowest window in each word, then looping again for the
  1144. * next higher window position.
  1145. */
  1146. int MASK = (1 << width) - 1;
  1147. int k = 0;
  1148. for (;;)
  1149. {
  1150. int aPos = 0;
  1151. do
  1152. {
  1153. long aVal = (long)((ulong)c[aPos] >> k);
  1154. int bank = 0, bOff = aLen;
  1155. for (;;)
  1156. {
  1157. int index = (int)(aVal) & MASK;
  1158. if (index != 0)
  1159. {
  1160. /*
  1161. * Add to a 'c' buffer based on the bit-pattern of 'index'. Since A is in
  1162. * Interleaved form, the bits represent the current B shifted by 0, 'positions',
  1163. * 'positions' * 2, ..., 'positions' * ('width' - 1)
  1164. */
  1165. Add(c, aPos + ci[index], c, bOff, bMax);
  1166. }
  1167. if (++bank == banks)
  1168. {
  1169. break;
  1170. }
  1171. bOff += bMax;
  1172. aVal = (long)((ulong)aVal >> width);
  1173. }
  1174. }
  1175. while (++aPos < aLen);
  1176. if ((k += stride) >= top)
  1177. {
  1178. if (k >= 64)
  1179. {
  1180. break;
  1181. }
  1182. /*
  1183. * Adjustment for window setups with top == 63, the final bit (if any) is processed
  1184. * as the top-bit of a window
  1185. */
  1186. k = 64 - width;
  1187. MASK &= MASK << (top - k);
  1188. }
  1189. /*
  1190. * After each position has been checked for all words of A, B is shifted up 1 place
  1191. */
  1192. ShiftUp(c, aLen, bTotal, banks);
  1193. }
  1194. int ciPos = ci.Length;
  1195. while (--ciPos > 1)
  1196. {
  1197. if ((ciPos & 1L) == 0L)
  1198. {
  1199. /*
  1200. * For even numbers, shift contents and add to the half-position
  1201. */
  1202. AddShiftedUp(c, ci[(uint)ciPos >> 1], c, ci[ciPos], cLen, positions);
  1203. }
  1204. else
  1205. {
  1206. /*
  1207. * For odd numbers, 'distribute' contents to the result and the next-lowest position
  1208. */
  1209. Distribute(c, ci[ciPos], ci[ciPos - 1], ci[1], cLen);
  1210. }
  1211. }
  1212. /*
  1213. * Finally the raw answer is collected, reduce it against the reduction coefficients
  1214. */
  1215. return ReduceResult(c, ci[1], cLen, m, ks);
  1216. }
  1217. public LongArray ModReduce(int m, int[] ks)
  1218. {
  1219. long[] buf = Arrays.Clone(m_ints);
  1220. int rLen = ReduceInPlace(buf, 0, buf.Length, m, ks);
  1221. return new LongArray(buf, 0, rLen);
  1222. }
  1223. public LongArray Multiply(LongArray other, int m, int[] ks)
  1224. {
  1225. /*
  1226. * Find out the degree of each argument and handle the zero cases
  1227. */
  1228. int aDeg = Degree();
  1229. if (aDeg == 0)
  1230. {
  1231. return this;
  1232. }
  1233. int bDeg = other.Degree();
  1234. if (bDeg == 0)
  1235. {
  1236. return other;
  1237. }
  1238. /*
  1239. * Swap if necessary so that A is the smaller argument
  1240. */
  1241. LongArray A = this, B = other;
  1242. if (aDeg > bDeg)
  1243. {
  1244. A = other; B = this;
  1245. int tmp = aDeg; aDeg = bDeg; bDeg = tmp;
  1246. }
  1247. /*
  1248. * Establish the word lengths of the arguments and result
  1249. */
  1250. int aLen = (int)((uint)(aDeg + 63) >> 6);
  1251. int bLen = (int)((uint)(bDeg + 63) >> 6);
  1252. int cLen = (int)((uint)(aDeg + bDeg + 62) >> 6);
  1253. if (aLen == 1)
  1254. {
  1255. long a0 = A.m_ints[0];
  1256. if (a0 == 1L)
  1257. {
  1258. return B;
  1259. }
  1260. /*
  1261. * Fast path for small A, with performance dependent only on the number of set bits
  1262. */
  1263. long[] c0 = new long[cLen];
  1264. MultiplyWord(a0, B.m_ints, bLen, c0, 0);
  1265. /*
  1266. * Reduce the raw answer against the reduction coefficients
  1267. */
  1268. //return ReduceResult(c0, 0, cLen, m, ks);
  1269. return new LongArray(c0, 0, cLen);
  1270. }
  1271. /*
  1272. * Determine if B will get bigger during shifting
  1273. */
  1274. int bMax = (int)((uint)(bDeg + 7 + 63) >> 6);
  1275. /*
  1276. * Lookup table for the offset of each B in the tables
  1277. */
  1278. int[] ti = new int[16];
  1279. /*
  1280. * Precompute table of all 4-bit products of B
  1281. */
  1282. long[] T0 = new long[bMax << 4];
  1283. int tOff = bMax;
  1284. ti[1] = tOff;
  1285. Array.Copy(B.m_ints, 0, T0, tOff, bLen);
  1286. for (int i = 2; i < 16; ++i)
  1287. {
  1288. ti[i] = (tOff += bMax);
  1289. if ((i & 1) == 0)
  1290. {
  1291. ShiftUp(T0, (int)((uint)tOff >> 1), T0, tOff, bMax, 1);
  1292. }
  1293. else
  1294. {
  1295. Add(T0, bMax, T0, tOff - bMax, T0, tOff, bMax);
  1296. }
  1297. }
  1298. /*
  1299. * Second table with all 4-bit products of B shifted 4 bits
  1300. */
  1301. long[] T1 = new long[T0.Length];
  1302. ShiftUp(T0, 0, T1, 0, T0.Length, 4);
  1303. // ShiftUp(T0, bMax, T1, bMax, tOff, 4);
  1304. long[] a = A.m_ints;
  1305. long[] c = new long[cLen << 3];
  1306. int MASK = 0xF;
  1307. /*
  1308. * Lopez-Dahab (Modified) algorithm
  1309. */
  1310. for (int aPos = 0; aPos < aLen; ++aPos)
  1311. {
  1312. long aVal = a[aPos];
  1313. int cOff = aPos;
  1314. for (; ; )
  1315. {
  1316. int u = (int)aVal & MASK;
  1317. aVal = (long)((ulong)aVal >> 4);
  1318. int v = (int)aVal & MASK;
  1319. AddBoth(c, cOff, T0, ti[u], T1, ti[v], bMax);
  1320. aVal = (long)((ulong)aVal >> 4);
  1321. if (aVal == 0L)
  1322. {
  1323. break;
  1324. }
  1325. cOff += cLen;
  1326. }
  1327. }
  1328. {
  1329. int cOff = c.Length;
  1330. while ((cOff -= cLen) != 0)
  1331. {
  1332. AddShiftedUp(c, cOff - cLen, c, cOff, cLen, 8);
  1333. }
  1334. }
  1335. /*
  1336. * Finally the raw answer is collected, reduce it against the reduction coefficients
  1337. */
  1338. //return ReduceResult(c, 0, cLen, m, ks);
  1339. return new LongArray(c, 0, cLen);
  1340. }
  1341. public void Reduce(int m, int[] ks)
  1342. {
  1343. long[] buf = m_ints;
  1344. int rLen = ReduceInPlace(buf, 0, buf.Length, m, ks);
  1345. if (rLen < buf.Length)
  1346. {
  1347. m_ints = new long[rLen];
  1348. Array.Copy(buf, 0, m_ints, 0, rLen);
  1349. }
  1350. }
  1351. private static LongArray ReduceResult(long[] buf, int off, int len, int m, int[] ks)
  1352. {
  1353. int rLen = ReduceInPlace(buf, off, len, m, ks);
  1354. return new LongArray(buf, off, rLen);
  1355. }
  1356. // private static void deInterleave(long[] x, int xOff, long[] z, int zOff, int count, int rounds)
  1357. // {
  1358. // for (int i = 0; i < count; ++i)
  1359. // {
  1360. // z[zOff + i] = deInterleave(x[zOff + i], rounds);
  1361. // }
  1362. // }
  1363. //
  1364. // private static long deInterleave(long x, int rounds)
  1365. // {
  1366. // while (--rounds >= 0)
  1367. // {
  1368. // x = deInterleave32(x & DEInterleave_MASK) | (deInterleave32((x >>> 1) & DEInterleave_MASK) << 32);
  1369. // }
  1370. // return x;
  1371. // }
  1372. //
  1373. // private static long deInterleave32(long x)
  1374. // {
  1375. // x = (x | (x >>> 1)) & 0x3333333333333333L;
  1376. // x = (x | (x >>> 2)) & 0x0F0F0F0F0F0F0F0FL;
  1377. // x = (x | (x >>> 4)) & 0x00FF00FF00FF00FFL;
  1378. // x = (x | (x >>> 8)) & 0x0000FFFF0000FFFFL;
  1379. // x = (x | (x >>> 16)) & 0x00000000FFFFFFFFL;
  1380. // return x;
  1381. // }
  1382. private static int ReduceInPlace(long[] buf, int off, int len, int m, int[] ks)
  1383. {
  1384. int mLen = (m + 63) >> 6;
  1385. if (len < mLen)
  1386. {
  1387. return len;
  1388. }
  1389. int numBits = System.Math.Min(len << 6, (m << 1) - 1); // TODO use actual degree?
  1390. int excessBits = (len << 6) - numBits;
  1391. while (excessBits >= 64)
  1392. {
  1393. --len;
  1394. excessBits -= 64;
  1395. }
  1396. int kLen = ks.Length, kMax = ks[kLen - 1], kNext = kLen > 1 ? ks[kLen - 2] : 0;
  1397. int wordWiseLimit = System.Math.Max(m, kMax + 64);
  1398. int vectorableWords = (excessBits + System.Math.Min(numBits - wordWiseLimit, m - kNext)) >> 6;
  1399. if (vectorableWords > 1)
  1400. {
  1401. int vectorWiseWords = len - vectorableWords;
  1402. ReduceVectorWise(buf, off, len, vectorWiseWords, m, ks);
  1403. while (len > vectorWiseWords)
  1404. {
  1405. buf[off + --len] = 0L;
  1406. }
  1407. numBits = vectorWiseWords << 6;
  1408. }
  1409. if (numBits > wordWiseLimit)
  1410. {
  1411. ReduceWordWise(buf, off, len, wordWiseLimit, m, ks);
  1412. numBits = wordWiseLimit;
  1413. }
  1414. if (numBits > m)
  1415. {
  1416. ReduceBitWise(buf, off, numBits, m, ks);
  1417. }
  1418. return mLen;
  1419. }
  1420. private static void ReduceBitWise(long[] buf, int off, int BitLength, int m, int[] ks)
  1421. {
  1422. while (--BitLength >= m)
  1423. {
  1424. if (TestBit(buf, off, BitLength))
  1425. {
  1426. ReduceBit(buf, off, BitLength, m, ks);
  1427. }
  1428. }
  1429. }
  1430. private static void ReduceBit(long[] buf, int off, int bit, int m, int[] ks)
  1431. {
  1432. FlipBit(buf, off, bit);
  1433. int n = bit - m;
  1434. int j = ks.Length;
  1435. while (--j >= 0)
  1436. {
  1437. FlipBit(buf, off, ks[j] + n);
  1438. }
  1439. FlipBit(buf, off, n);
  1440. }
  1441. private static void ReduceWordWise(long[] buf, int off, int len, int toBit, int m, int[] ks)
  1442. {
  1443. int toPos = (int)((uint)toBit >> 6);
  1444. while (--len > toPos)
  1445. {
  1446. long word = buf[off + len];
  1447. if (word != 0)
  1448. {
  1449. buf[off + len] = 0;
  1450. ReduceWord(buf, off, (len << 6), word, m, ks);
  1451. }
  1452. }
  1453. {
  1454. int partial = toBit & 0x3F;
  1455. long word = (long)((ulong)buf[off + toPos] >> partial);
  1456. if (word != 0)
  1457. {
  1458. buf[off + toPos] ^= word << partial;
  1459. ReduceWord(buf, off, toBit, word, m, ks);
  1460. }
  1461. }
  1462. }
  1463. private static void ReduceWord(long[] buf, int off, int bit, long word, int m, int[] ks)
  1464. {
  1465. int offset = bit - m;
  1466. int j = ks.Length;
  1467. while (--j >= 0)
  1468. {
  1469. FlipWord(buf, off, offset + ks[j], word);
  1470. }
  1471. FlipWord(buf, off, offset, word);
  1472. }
  1473. private static void ReduceVectorWise(long[] buf, int off, int len, int words, int m, int[] ks)
  1474. {
  1475. /*
  1476. * NOTE: It's important we go from highest coefficient to lowest, because for the highest
  1477. * one (only) we allow the ranges to partially overlap, and therefore any changes must take
  1478. * effect for the subsequent lower coefficients.
  1479. */
  1480. int baseBit = (words << 6) - m;
  1481. int j = ks.Length;
  1482. while (--j >= 0)
  1483. {
  1484. FlipVector(buf, off, buf, off + words, len - words, baseBit + ks[j]);
  1485. }
  1486. FlipVector(buf, off, buf, off + words, len - words, baseBit);
  1487. }
  1488. private static void FlipVector(long[] x, int xOff, long[] y, int yOff, int yLen, int bits)
  1489. {
  1490. xOff += (int)((uint)bits >> 6);
  1491. bits &= 0x3F;
  1492. if (bits == 0)
  1493. {
  1494. Add(x, xOff, y, yOff, yLen);
  1495. }
  1496. else
  1497. {
  1498. long carry = AddShiftedDown(x, xOff + 1, y, yOff, yLen, 64 - bits);
  1499. x[xOff] ^= carry;
  1500. }
  1501. }
  1502. public LongArray ModSquare(int m, int[] ks)
  1503. {
  1504. int len = GetUsedLength();
  1505. if (len == 0)
  1506. {
  1507. return this;
  1508. }
  1509. int _2len = len << 1;
  1510. long[] r = new long[_2len];
  1511. int pos = 0;
  1512. while (pos < _2len)
  1513. {
  1514. long mi = m_ints[(uint)pos >> 1];
  1515. r[pos++] = Interleave2_32to64((int)mi);
  1516. r[pos++] = Interleave2_32to64((int)((ulong)mi >> 32));
  1517. }
  1518. return new LongArray(r, 0, ReduceInPlace(r, 0, r.Length, m, ks));
  1519. }
  1520. public LongArray ModSquareN(int n, int m, int[] ks)
  1521. {
  1522. int len = GetUsedLength();
  1523. if (len == 0)
  1524. {
  1525. return this;
  1526. }
  1527. int mLen = (m + 63) >> 6;
  1528. long[] r = new long[mLen << 1];
  1529. Array.Copy(m_ints, 0, r, 0, len);
  1530. while (--n >= 0)
  1531. {
  1532. SquareInPlace(r, len, m, ks);
  1533. len = ReduceInPlace(r, 0, r.Length, m, ks);
  1534. }
  1535. return new LongArray(r, 0, len);
  1536. }
  1537. public LongArray Square(int m, int[] ks)
  1538. {
  1539. int len = GetUsedLength();
  1540. if (len == 0)
  1541. {
  1542. return this;
  1543. }
  1544. int _2len = len << 1;
  1545. long[] r = new long[_2len];
  1546. int pos = 0;
  1547. while (pos < _2len)
  1548. {
  1549. long mi = m_ints[(uint)pos >> 1];
  1550. r[pos++] = Interleave2_32to64((int)mi);
  1551. r[pos++] = Interleave2_32to64((int)((ulong)mi >> 32));
  1552. }
  1553. return new LongArray(r, 0, r.Length);
  1554. }
  1555. private static void SquareInPlace(long[] x, int xLen, int m, int[] ks)
  1556. {
  1557. int pos = xLen << 1;
  1558. while (--xLen >= 0)
  1559. {
  1560. long xVal = x[xLen];
  1561. x[--pos] = Interleave2_32to64((int)((ulong)xVal >> 32));
  1562. x[--pos] = Interleave2_32to64((int)xVal);
  1563. }
  1564. }
  1565. private static void Interleave(long[] x, int xOff, long[] z, int zOff, int count, int width)
  1566. {
  1567. switch (width)
  1568. {
  1569. case 3:
  1570. Interleave3(x, xOff, z, zOff, count);
  1571. break;
  1572. case 5:
  1573. Interleave5(x, xOff, z, zOff, count);
  1574. break;
  1575. case 7:
  1576. Interleave7(x, xOff, z, zOff, count);
  1577. break;
  1578. default:
  1579. Interleave2_n(x, xOff, z, zOff, count, BitLengths[width] - 1);
  1580. break;
  1581. }
  1582. }
  1583. private static void Interleave3(long[] x, int xOff, long[] z, int zOff, int count)
  1584. {
  1585. for (int i = 0; i < count; ++i)
  1586. {
  1587. z[zOff + i] = Interleave3(x[xOff + i]);
  1588. }
  1589. }
  1590. private static long Interleave3(long x)
  1591. {
  1592. long z = x & (1L << 63);
  1593. return z
  1594. | Interleave3_21to63((int)x & 0x1FFFFF)
  1595. | Interleave3_21to63((int)((ulong)x >> 21) & 0x1FFFFF) << 1
  1596. | Interleave3_21to63((int)((ulong)x >> 42) & 0x1FFFFF) << 2;
  1597. // int zPos = 0, wPos = 0, xPos = 0;
  1598. // for (;;)
  1599. // {
  1600. // z |= ((x >>> xPos) & 1L) << zPos;
  1601. // if (++zPos == 63)
  1602. // {
  1603. // String sz2 = Long.toBinaryString(z);
  1604. // return z;
  1605. // }
  1606. // if ((xPos += 21) >= 63)
  1607. // {
  1608. // xPos = ++wPos;
  1609. // }
  1610. // }
  1611. }
  1612. private static long Interleave3_21to63(int x)
  1613. {
  1614. int r00 = INTERLEAVE3_TABLE[x & 0x7F];
  1615. int r21 = INTERLEAVE3_TABLE[((uint)x >> 7) & 0x7F];
  1616. int r42 = INTERLEAVE3_TABLE[(uint)x >> 14];
  1617. return (r42 & 0xFFFFFFFFL) << 42 | (r21 & 0xFFFFFFFFL) << 21 | (r00 & 0xFFFFFFFFL);
  1618. }
  1619. private static void Interleave5(long[] x, int xOff, long[] z, int zOff, int count)
  1620. {
  1621. for (int i = 0; i < count; ++i)
  1622. {
  1623. z[zOff + i] = Interleave5(x[xOff + i]);
  1624. }
  1625. }
  1626. private static long Interleave5(long x)
  1627. {
  1628. return Interleave3_13to65((int)x & 0x1FFF)
  1629. | Interleave3_13to65((int)((ulong)x >> 13) & 0x1FFF) << 1
  1630. | Interleave3_13to65((int)((ulong)x >> 26) & 0x1FFF) << 2
  1631. | Interleave3_13to65((int)((ulong)x >> 39) & 0x1FFF) << 3
  1632. | Interleave3_13to65((int)((ulong)x >> 52) & 0x1FFF) << 4;
  1633. // long z = 0;
  1634. // int zPos = 0, wPos = 0, xPos = 0;
  1635. // for (;;)
  1636. // {
  1637. // z |= ((x >>> xPos) & 1L) << zPos;
  1638. // if (++zPos == 64)
  1639. // {
  1640. // return z;
  1641. // }
  1642. // if ((xPos += 13) >= 64)
  1643. // {
  1644. // xPos = ++wPos;
  1645. // }
  1646. // }
  1647. }
  1648. private static long Interleave3_13to65(int x)
  1649. {
  1650. int r00 = INTERLEAVE5_TABLE[x & 0x7F];
  1651. int r35 = INTERLEAVE5_TABLE[(uint)x >> 7];
  1652. return (r35 & 0xFFFFFFFFL) << 35 | (r00 & 0xFFFFFFFFL);
  1653. }
  1654. private static void Interleave7(long[] x, int xOff, long[] z, int zOff, int count)
  1655. {
  1656. for (int i = 0; i < count; ++i)
  1657. {
  1658. z[zOff + i] = Interleave7(x[xOff + i]);
  1659. }
  1660. }
  1661. private static long Interleave7(long x)
  1662. {
  1663. long z = x & (1L << 63);
  1664. return z
  1665. | INTERLEAVE7_TABLE[(int)x & 0x1FF]
  1666. | INTERLEAVE7_TABLE[(int)((ulong)x >> 9) & 0x1FF] << 1
  1667. | INTERLEAVE7_TABLE[(int)((ulong)x >> 18) & 0x1FF] << 2
  1668. | INTERLEAVE7_TABLE[(int)((ulong)x >> 27) & 0x1FF] << 3
  1669. | INTERLEAVE7_TABLE[(int)((ulong)x >> 36) & 0x1FF] << 4
  1670. | INTERLEAVE7_TABLE[(int)((ulong)x >> 45) & 0x1FF] << 5
  1671. | INTERLEAVE7_TABLE[(int)((ulong)x >> 54) & 0x1FF] << 6;
  1672. // int zPos = 0, wPos = 0, xPos = 0;
  1673. // for (;;)
  1674. // {
  1675. // z |= ((x >>> xPos) & 1L) << zPos;
  1676. // if (++zPos == 63)
  1677. // {
  1678. // return z;
  1679. // }
  1680. // if ((xPos += 9) >= 63)
  1681. // {
  1682. // xPos = ++wPos;
  1683. // }
  1684. // }
  1685. }
  1686. private static void Interleave2_n(long[] x, int xOff, long[] z, int zOff, int count, int rounds)
  1687. {
  1688. for (int i = 0; i < count; ++i)
  1689. {
  1690. z[zOff + i] = Interleave2_n(x[xOff + i], rounds);
  1691. }
  1692. }
  1693. private static long Interleave2_n(long x, int rounds)
  1694. {
  1695. while (rounds > 1)
  1696. {
  1697. rounds -= 2;
  1698. x = Interleave4_16to64((int)x & 0xFFFF)
  1699. | Interleave4_16to64((int)((ulong)x >> 16) & 0xFFFF) << 1
  1700. | Interleave4_16to64((int)((ulong)x >> 32) & 0xFFFF) << 2
  1701. | Interleave4_16to64((int)((ulong)x >> 48) & 0xFFFF) << 3;
  1702. }
  1703. if (rounds > 0)
  1704. {
  1705. x = Interleave2_32to64((int)x) | Interleave2_32to64((int)((ulong)x >> 32)) << 1;
  1706. }
  1707. return x;
  1708. }
  1709. private static long Interleave4_16to64(int x)
  1710. {
  1711. int r00 = INTERLEAVE4_TABLE[x & 0xFF];
  1712. int r32 = INTERLEAVE4_TABLE[(uint)x >> 8];
  1713. return (r32 & 0xFFFFFFFFL) << 32 | (r00 & 0xFFFFFFFFL);
  1714. }
  1715. private static long Interleave2_32to64(int x)
  1716. {
  1717. int r00 = INTERLEAVE2_TABLE[x & 0xFF] | INTERLEAVE2_TABLE[((uint)x >> 8) & 0xFF] << 16;
  1718. int r32 = INTERLEAVE2_TABLE[((uint)x >> 16) & 0xFF] | INTERLEAVE2_TABLE[(uint)x >> 24] << 16;
  1719. return (r32 & 0xFFFFFFFFL) << 32 | (r00 & 0xFFFFFFFFL);
  1720. }
  1721. // private static LongArray ExpItohTsujii2(LongArray B, int n, int m, int[] ks)
  1722. // {
  1723. // LongArray t1 = B, t3 = new LongArray(new long[]{ 1L });
  1724. // int scale = 1;
  1725. //
  1726. // int numTerms = n;
  1727. // while (numTerms > 1)
  1728. // {
  1729. // if ((numTerms & 1) != 0)
  1730. // {
  1731. // t3 = t3.ModMultiply(t1, m, ks);
  1732. // t1 = t1.modSquareN(scale, m, ks);
  1733. // }
  1734. //
  1735. // LongArray t2 = t1.modSquareN(scale, m, ks);
  1736. // t1 = t1.ModMultiply(t2, m, ks);
  1737. // numTerms >>>= 1; scale <<= 1;
  1738. // }
  1739. //
  1740. // return t3.ModMultiply(t1, m, ks);
  1741. // }
  1742. //
  1743. // private static LongArray ExpItohTsujii23(LongArray B, int n, int m, int[] ks)
  1744. // {
  1745. // LongArray t1 = B, t3 = new LongArray(new long[]{ 1L });
  1746. // int scale = 1;
  1747. //
  1748. // int numTerms = n;
  1749. // while (numTerms > 1)
  1750. // {
  1751. // bool m03 = numTerms % 3 == 0;
  1752. // bool m14 = !m03 && (numTerms & 1) != 0;
  1753. //
  1754. // if (m14)
  1755. // {
  1756. // t3 = t3.ModMultiply(t1, m, ks);
  1757. // t1 = t1.modSquareN(scale, m, ks);
  1758. // }
  1759. //
  1760. // LongArray t2 = t1.modSquareN(scale, m, ks);
  1761. // t1 = t1.ModMultiply(t2, m, ks);
  1762. //
  1763. // if (m03)
  1764. // {
  1765. // t2 = t2.modSquareN(scale, m, ks);
  1766. // t1 = t1.ModMultiply(t2, m, ks);
  1767. // numTerms /= 3; scale *= 3;
  1768. // }
  1769. // else
  1770. // {
  1771. // numTerms >>>= 1; scale <<= 1;
  1772. // }
  1773. // }
  1774. //
  1775. // return t3.ModMultiply(t1, m, ks);
  1776. // }
  1777. //
  1778. // private static LongArray ExpItohTsujii235(LongArray B, int n, int m, int[] ks)
  1779. // {
  1780. // LongArray t1 = B, t4 = new LongArray(new long[]{ 1L });
  1781. // int scale = 1;
  1782. //
  1783. // int numTerms = n;
  1784. // while (numTerms > 1)
  1785. // {
  1786. // if (numTerms % 5 == 0)
  1787. // {
  1788. //// t1 = ExpItohTsujii23(t1, 5, m, ks);
  1789. //
  1790. // LongArray t3 = t1;
  1791. // t1 = t1.modSquareN(scale, m, ks);
  1792. //
  1793. // LongArray t2 = t1.modSquareN(scale, m, ks);
  1794. // t1 = t1.ModMultiply(t2, m, ks);
  1795. // t2 = t1.modSquareN(scale << 1, m, ks);
  1796. // t1 = t1.ModMultiply(t2, m, ks);
  1797. //
  1798. // t1 = t1.ModMultiply(t3, m, ks);
  1799. //
  1800. // numTerms /= 5; scale *= 5;
  1801. // continue;
  1802. // }
  1803. //
  1804. // bool m03 = numTerms % 3 == 0;
  1805. // bool m14 = !m03 && (numTerms & 1) != 0;
  1806. //
  1807. // if (m14)
  1808. // {
  1809. // t4 = t4.ModMultiply(t1, m, ks);
  1810. // t1 = t1.modSquareN(scale, m, ks);
  1811. // }
  1812. //
  1813. // LongArray t2 = t1.modSquareN(scale, m, ks);
  1814. // t1 = t1.ModMultiply(t2, m, ks);
  1815. //
  1816. // if (m03)
  1817. // {
  1818. // t2 = t2.modSquareN(scale, m, ks);
  1819. // t1 = t1.ModMultiply(t2, m, ks);
  1820. // numTerms /= 3; scale *= 3;
  1821. // }
  1822. // else
  1823. // {
  1824. // numTerms >>>= 1; scale <<= 1;
  1825. // }
  1826. // }
  1827. //
  1828. // return t4.ModMultiply(t1, m, ks);
  1829. // }
  1830. public LongArray ModInverse(int m, int[] ks)
  1831. {
  1832. /*
  1833. * Fermat's Little Theorem
  1834. */
  1835. // LongArray A = this;
  1836. // LongArray B = A.modSquare(m, ks);
  1837. // LongArray R0 = B, R1 = B;
  1838. // for (int i = 2; i < m; ++i)
  1839. // {
  1840. // R1 = R1.modSquare(m, ks);
  1841. // R0 = R0.ModMultiply(R1, m, ks);
  1842. // }
  1843. //
  1844. // return R0;
  1845. /*
  1846. * Itoh-Tsujii
  1847. */
  1848. // LongArray B = modSquare(m, ks);
  1849. // switch (m)
  1850. // {
  1851. // case 409:
  1852. // return ExpItohTsujii23(B, m - 1, m, ks);
  1853. // case 571:
  1854. // return ExpItohTsujii235(B, m - 1, m, ks);
  1855. // case 163:
  1856. // case 233:
  1857. // case 283:
  1858. // default:
  1859. // return ExpItohTsujii2(B, m - 1, m, ks);
  1860. // }
  1861. /*
  1862. * Inversion in F2m using the extended Euclidean algorithm
  1863. *
  1864. * Input: A nonzero polynomial a(z) of degree at most m-1
  1865. * Output: a(z)^(-1) mod f(z)
  1866. */
  1867. int uzDegree = Degree();
  1868. if (uzDegree == 0)
  1869. {
  1870. throw new InvalidOperationException();
  1871. }
  1872. if (uzDegree == 1)
  1873. {
  1874. return this;
  1875. }
  1876. // u(z) := a(z)
  1877. LongArray uz = (LongArray)Copy();
  1878. int t = (m + 63) >> 6;
  1879. // v(z) := f(z)
  1880. LongArray vz = new LongArray(t);
  1881. ReduceBit(vz.m_ints, 0, m, m, ks);
  1882. // g1(z) := 1, g2(z) := 0
  1883. LongArray g1z = new LongArray(t);
  1884. g1z.m_ints[0] = 1L;
  1885. LongArray g2z = new LongArray(t);
  1886. int[] uvDeg = new int[]{ uzDegree, m + 1 };
  1887. LongArray[] uv = new LongArray[]{ uz, vz };
  1888. int[] ggDeg = new int[]{ 1, 0 };
  1889. LongArray[] gg = new LongArray[]{ g1z, g2z };
  1890. int b = 1;
  1891. int duv1 = uvDeg[b];
  1892. int dgg1 = ggDeg[b];
  1893. int j = duv1 - uvDeg[1 - b];
  1894. for (;;)
  1895. {
  1896. if (j < 0)
  1897. {
  1898. j = -j;
  1899. uvDeg[b] = duv1;
  1900. ggDeg[b] = dgg1;
  1901. b = 1 - b;
  1902. duv1 = uvDeg[b];
  1903. dgg1 = ggDeg[b];
  1904. }
  1905. uv[b].AddShiftedByBitsSafe(uv[1 - b], uvDeg[1 - b], j);
  1906. int duv2 = uv[b].DegreeFrom(duv1);
  1907. if (duv2 == 0)
  1908. {
  1909. return gg[1 - b];
  1910. }
  1911. {
  1912. int dgg2 = ggDeg[1 - b];
  1913. gg[b].AddShiftedByBitsSafe(gg[1 - b], dgg2, j);
  1914. dgg2 += j;
  1915. if (dgg2 > dgg1)
  1916. {
  1917. dgg1 = dgg2;
  1918. }
  1919. else if (dgg2 == dgg1)
  1920. {
  1921. dgg1 = gg[b].DegreeFrom(dgg1);
  1922. }
  1923. }
  1924. j += (duv2 - duv1);
  1925. duv1 = duv2;
  1926. }
  1927. }
  1928. public override bool Equals(object obj)
  1929. {
  1930. return Equals(obj as LongArray);
  1931. }
  1932. public virtual bool Equals(LongArray other)
  1933. {
  1934. if (this == other)
  1935. return true;
  1936. if (null == other)
  1937. return false;
  1938. int usedLen = GetUsedLength();
  1939. if (other.GetUsedLength() != usedLen)
  1940. {
  1941. return false;
  1942. }
  1943. for (int i = 0; i < usedLen; i++)
  1944. {
  1945. if (m_ints[i] != other.m_ints[i])
  1946. {
  1947. return false;
  1948. }
  1949. }
  1950. return true;
  1951. }
  1952. public override int GetHashCode()
  1953. {
  1954. int usedLen = GetUsedLength();
  1955. int hash = 1;
  1956. for (int i = 0; i < usedLen; i++)
  1957. {
  1958. long mi = m_ints[i];
  1959. hash *= 31;
  1960. hash ^= (int)mi;
  1961. hash *= 31;
  1962. hash ^= (int)((ulong)mi >> 32);
  1963. }
  1964. return hash;
  1965. }
  1966. public LongArray Copy()
  1967. {
  1968. return new LongArray(Arrays.Clone(m_ints));
  1969. }
  1970. public override string ToString()
  1971. {
  1972. int i = GetUsedLength();
  1973. if (i == 0)
  1974. {
  1975. return "0";
  1976. }
  1977. StringBuilder sb = new StringBuilder(Convert.ToString(m_ints[--i], 2));
  1978. while (--i >= 0)
  1979. {
  1980. string s = Convert.ToString(m_ints[i], 2);
  1981. // Add leading zeroes, except for highest significant word
  1982. int len = s.Length;
  1983. if (len < 64)
  1984. {
  1985. sb.Append(ZEROES.Substring(len));
  1986. }
  1987. sb.Append(s);
  1988. }
  1989. return sb.ToString();
  1990. }
  1991. }
  1992. }
  1993. #endif