P751
using Org.BouncyCastle.Utilities.IO.Compression;
using System.Collections.Generic;
using System.IO;
namespace Org.BouncyCastle.Pqc.Crypto.Sike
{
internal class P751 : Internal
{
internal P751(bool isCompressed)
{
COMPRESS = isCompressed;
CRYPTO_SECRETKEYBYTES = 644;
CRYPTO_PUBLICKEYBYTES = 564;
CRYPTO_BYTES = 32;
CRYPTO_CIPHERTEXTBYTES = 596;
if (isCompressed) {
CRYPTO_SECRETKEYBYTES = 602;
CRYPTO_PUBLICKEYBYTES = 335;
CRYPTO_CIPHERTEXTBYTES = 410;
}
NWORDS_FIELD = 12;
PRIME_ZERO_WORDS = 5;
NBITS_FIELD = 751;
MAXBITS_FIELD = 768;
MAXWORDS_FIELD = (MAXBITS_FIELD + Internal.RADIX - 1) / Internal.RADIX;
NWORDS64_FIELD = (NBITS_FIELD + 63) / 64;
NBITS_ORDER = 384;
NWORDS_ORDER = (NBITS_ORDER + Internal.RADIX - 1) / Internal.RADIX;
NWORDS64_ORDER = (NBITS_ORDER + 63) / 64;
MAXBITS_ORDER = NBITS_ORDER;
ALICE = 0;
BOB = 1;
OALICE_BITS = 372;
OBOB_BITS = 379;
OBOB_EXPON = 239;
MASK_ALICE = 15;
MASK_BOB = 3;
PARAM_A = 6;
PARAM_C = 1;
MAX_INT_POINTS_ALICE = 8;
MAX_INT_POINTS_BOB = 10;
MAX_Alice = 186;
MAX_Bob = 239;
MSG_BYTES = 32;
SECRETKEY_A_BYTES = (OALICE_BITS + 7) / 8;
SECRETKEY_B_BYTES = (OBOB_BITS - 1 + 7) / 8;
FP2_ENCODED_BYTES = 2 * ((NBITS_FIELD + 7) / 8);
PRIME = new ulong[12] {
ulong.MaxValue,
ulong.MaxValue,
ulong.MaxValue,
ulong.MaxValue,
ulong.MaxValue,
17199246976927924223,
16423667440329193640,
15750665808104639606,
598583372241692790,
9611443585101748040,
1014031881231588454,
123032916064028
};
PRIMEx2 = new ulong[12] {
18446744073709551614,
ulong.MaxValue,
ulong.MaxValue,
ulong.MaxValue,
ulong.MaxValue,
15951749880146296831,
14400590806948835665,
13054587542499727597,
1197166744483385581,
776143096493944464,
2028063762463176909,
246065832128056
};
PRIMEx4 = new ulong[12] {
18446744073709551612,
ulong.MaxValue,
ulong.MaxValue,
ulong.MaxValue,
ulong.MaxValue,
13456755686583042047,
10354437540188119715,
7662431011289903579,
2394333488966771163,
1552286192987888928,
4056127524926353818,
492131664256112
};
PRIMEp1 = new ulong[12] {
0,
0,
0,
0,
0,
17199246976927924224,
16423667440329193640,
15750665808104639606,
598583372241692790,
9611443585101748040,
1014031881231588454,
123032916064028
};
PRIMEx16p = new ulong[24] {
16,
0,
0,
0,
0,
3026418949592973312,
9398220047042800354,
12487528204518977827,
17738820235684933924,
6028454529806440190,
4444467948008272687,
6389925372342901886,
9183714343363691506,
5885816994991374139,
15511269745733968757,
6605506351970878676,
11826827898049043624,
2354645367770068943,
4229001520684072827,
8116152847571104894,
5904732737952813393,
12541849493931687641,
16092533092944000694,
13129340006
};
Alice_order = new ulong[6] {
0,
0,
0,
0,
0,
4503599627370496
};
Bob_order = new ulong[6] {
14512942843351961323,
6463124234301828670,
16827274972312858025,
3121071280576823428,
2957168939937196118,
503942824198258913
};
A_gen = new ulong[72] {
9822147065185090216,
13425902357697129504,
13938563312470237261,
586935199814300635,
1231476659462315650,
15200349552625419408,
7720484030924475341,
10594672674827951252,
16258160073680417295,
9900115913593791836,
2594594101592586405,
16460334914570,
12589684371389518740,
12332659108360031092,
16510385560356170993,
9730777839585202459,
17903424488311508735,
5769280992065803964,
5532188670625076987,
16579944219273134793,
11273833143932675593,
14491979851476136262,
9207655709386969385,
36625983307955,
1122465274781142185,
8515870630345178839,
16522000615137270631,
10711565273514878189,
15314004285403374021,
4840703288083784924,
13590004812123635944,
12882217991877249059,
17191071062736800731,
8865126078528016748,
3693150086021936691,
36658777259884,
17077429180071124812,
18158079048006766323,
1672925350903708369,
13862967679991471090,
2382796300166624212,
13977248558483123863,
2278800419424555458,
118108782222142818,
1476285485934066847,
16034197662737970158,
2038060998052304781,
107394058694173,
1585483835096717809,
11931875557452383223,
7781638337591394101,
6664888135375272208,
2840823194606539793,
12795682130246575520,
2611282525231902794,
6070226513535983789,
14818409522906275305,
15252419596163003285,
7824800416788242299,
58455551134839,
11203073095899037416,
15794937577346159166,
2745271787383434087,
11641405899540905191,
10870594127288654381,
11105192472711036386,
14727745195886210467,
1375411507907286301,
14178291107140140658,
5435466916003927824,
8796316040578972826,
31328095521215
};
B_gen = new ulong[72] {
9613244219595815052,
8392675302948378161,
9147551389475264226,
10681340829511687623,
1891166766906289442,
8726784965380087145,
278069042628663236,
321051512772386179,
4418794039415165171,
811238939992429693,
11875834369919409703,
56397746590099,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
10267279138215760704,
15455480150741085920,
18292544765444947000,
601370936378187550,
15472156581712037259,
5596913087184264637,
5895422123728360424,
16546858821940167717,
10412066657013232056,
5132030994927125788,
3098590566816827475,
44009130331453,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
13070667682173250844,
278885298517689489,
16120934674390684754,
12236545707274815619,
9611450916420723852,
2637748658071451261,
3544807444712948572,
16213618612546618337,
16912673257488186033,
13195093391319151071,
17886130922126146806,
59505860712722,
6397758586429206260,
9338639848856234571,
4403427314326644353,
10612695944165988144,
11149562808784569047,
16467442628418687666,
15521226430153318,
3328537178486072741,
17900197353359942647,
10368784128223943932,
18031568085834724987,
115645459333053
};
Montgomery_R2 = new ulong[12] {
2535603850726686808,
15780896088201250090,
6788776303855402382,
17585428585582356230,
5274503137951975249,
2266259624764636289,
11695651972693921304,
13072885652150159301,
4908312795585420432,
6229583484603254826,
488927695601805643,
72213483953973
};
Montgomery_one = new ulong[12] {
149933,
0,
0,
0,
0,
9444048418595930112,
6136068611055053926,
7599709743867700432,
14455912356952952366,
5522737203492907350,
1222606818372667369,
49869481633250
};
strat_Alice = new uint[185] {
80,
48,
27,
15,
8,
4,
2,
1,
1,
2,
1,
1,
4,
2,
1,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
2,
1,
1,
4,
2,
1,
1,
2,
1,
1,
7,
4,
2,
1,
1,
2,
1,
1,
3,
2,
1,
1,
1,
1,
21,
12,
8,
4,
2,
1,
1,
2,
1,
1,
4,
2,
1,
1,
2,
1,
1,
5,
3,
2,
1,
1,
1,
1,
2,
1,
1,
1,
9,
5,
3,
2,
1,
1,
1,
1,
2,
1,
1,
1,
4,
2,
1,
1,
1,
2,
1,
1
};
if (COMPRESS) {
MASK2_BOB = 0;
MASK3_BOB = 255;
ORDER_A_ENCODED_BYTES = SECRETKEY_A_BYTES;
ORDER_B_ENCODED_BYTES = SECRETKEY_B_BYTES;
PARTIALLY_COMPRESSED_CHUNK_CT = 4 * ORDER_A_ENCODED_BYTES + FP2_ENCODED_BYTES + 2;
COMPRESSED_CHUNK_CT = 3 * ORDER_A_ENCODED_BYTES + FP2_ENCODED_BYTES + 2;
UNCOMPRESSEDPK_BYTES = 564;
TABLE_R_LEN = 17;
TABLE_V_LEN = 34;
TABLE_V3_LEN = 20;
W_2 = 4;
W_3 = 3;
ELL2_W = (uint)(1 << (int)W_2);
ELL3_W = 27;
ELL2_EMODW = (uint)(1 << (int)(OALICE_BITS % W_2));
ELL3_EMODW = 9;
DLEN_2 = (OALICE_BITS + W_2 - 1) / W_2;
DLEN_3 = (OBOB_EXPON + W_3 - 1) / W_3;
PLEN_2 = 94;
PLEN_3 = 81;
Dictionary<string, string> dictionary = new Dictionary<string, string>();
using (Stream stream = typeof(P751).Assembly.GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p751.bz2"))
using (StreamReader streamReader = new StreamReader(Bzip2.DecompressInput(stream, false))) {
string text = streamReader.ReadLine();
int num = 0;
while (text != null) {
string text2 = text;
if (text2 != "") {
if (num > 1)
text2 = text2.Replace(",", "");
int num2 = text2.IndexOf('=');
string key = text2.Substring(0, num2).Trim();
string value = text2.Substring(num2 + 1).Trim();
dictionary.Add(key, value);
}
text = streamReader.ReadLine();
num++;
}
}
ph2_path = Internal.ReadIntsFromProperty(dictionary, "ph2_path", PLEN_2);
ph3_path = Internal.ReadIntsFromProperty(dictionary, "ph3_path", PLEN_3);
A_gen = Internal.ReadFromProperty(dictionary, "A_gen", 6 * NWORDS64_FIELD);
B_gen = Internal.ReadFromProperty(dictionary, "B_gen", 6 * NWORDS64_FIELD);
XQB3 = Internal.ReadFromProperty(dictionary, "XQB3", 2 * NWORDS64_FIELD);
A_basis_zero = Internal.ReadFromProperty(dictionary, "A_basis_zero", 8 * NWORDS64_FIELD);
B_basis_zero = Internal.ReadFromProperty(dictionary, "B_basis_zero", 8 * NWORDS64_FIELD);
B_gen_3_tors = Internal.ReadFromProperty(dictionary, "B_gen_3_tors", 16 * NWORDS64_FIELD);
g_R_S_im = Internal.ReadFromProperty(dictionary, "g_R_S_im", NWORDS64_FIELD);
Montgomery_R2 = Internal.ReadFromProperty(dictionary, "Montgomery_R2", NWORDS64_FIELD);
Montgomery_RB1 = Internal.ReadFromProperty(dictionary, "Montgomery_RB1", NWORDS64_FIELD);
Montgomery_RB2 = Internal.ReadFromProperty(dictionary, "Montgomery_RB2", NWORDS64_FIELD);
Montgomery_one = Internal.ReadFromProperty(dictionary, "Montgomery_one", NWORDS64_FIELD);
threeinv = Internal.ReadFromProperty(dictionary, "threeinv", NWORDS64_FIELD);
u_entang = Internal.ReadFromProperty(dictionary, "u_entang", 2 * NWORDS64_FIELD);
u0_entang = Internal.ReadFromProperty(dictionary, "u0_entang", 2 * NWORDS64_FIELD);
table_r_qr = Internal.ReadFromProperty(dictionary, "table_r_qr", TABLE_R_LEN, NWORDS64_FIELD);
table_r_qnr = Internal.ReadFromProperty(dictionary, "table_r_qnr", TABLE_R_LEN, NWORDS64_FIELD);
table_v_qr = Internal.ReadFromProperty(dictionary, "table_v_qr", TABLE_V_LEN, NWORDS64_FIELD);
table_v_qnr = Internal.ReadFromProperty(dictionary, "table_v_qnr", TABLE_V_LEN, NWORDS64_FIELD);
v_3_torsion = Internal.ReadFromProperty(dictionary, "v_3_torsion", TABLE_V3_LEN, 2, NWORDS64_FIELD);
T_tate3 = Internal.ReadFromProperty(dictionary, "T_tate3", (6 * (OBOB_EXPON - 1) + 4) * NWORDS64_FIELD);
T_tate2_firststep_P = Internal.ReadFromProperty(dictionary, "T_tate2_firststep_P", 4 * NWORDS64_FIELD);
T_tate2_P = Internal.ReadFromProperty(dictionary, "T_tate2_P", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD);
T_tate2_firststep_Q = Internal.ReadFromProperty(dictionary, "T_tate2_firststep_Q", 4 * NWORDS64_FIELD);
T_tate2_Q = Internal.ReadFromProperty(dictionary, "T_tate2_Q", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD);
ph2_T = Internal.ReadFromProperty(dictionary, "ph2_T", DLEN_2 * (ELL2_W >> 1) * 2 * NWORDS64_FIELD);
ph3_T1 = Internal.ReadFromProperty(dictionary, "ph3_T1", DLEN_3 * (ELL3_W >> 1) * 2 * NWORDS64_FIELD);
ph3_T2 = Internal.ReadFromProperty(dictionary, "ph3_T2", DLEN_3 * (ELL3_W >> 1) * 2 * NWORDS64_FIELD);
Montgomery_R = new ulong[NWORDS64_FIELD];
ph2_T1 = new ulong[2 * ((DLEN_2 - 1) * (ELL2_W / 2) + (ph2_path[PLEN_2 - 1] - 1))];
ph2_T2 = new ulong[2 * ((DLEN_2 - 1) * (ELL2_W / 2) + (ph2_path[PLEN_2 - 1] - 1))];
ph3_T = new ulong[DLEN_3 * (ELL3_W >> 1) * 2 * NWORDS64_FIELD];
}
}
}
}