From 2bb58edf474dc6e320e3ea82f4112240be60121d Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 11 Nov 2022 16:41:25 +0700 Subject: SIKE: deferred static intializers --- crypto/src/pqc/crypto/sike/Internal.cs | 20 +++--- crypto/src/pqc/crypto/sike/P434.cs | 65 +++++++++--------- crypto/src/pqc/crypto/sike/P503.cs | 69 +++++++++---------- crypto/src/pqc/crypto/sike/P610.cs | 65 +++++++++--------- crypto/src/pqc/crypto/sike/P751.cs | 67 +++++++++---------- crypto/src/pqc/crypto/sike/SIKEKEMExtractor.cs | 2 +- crypto/src/pqc/crypto/sike/SIKEKEMGenerator.cs | 4 +- crypto/src/pqc/crypto/sike/SIKEKeyPairGenerator.cs | 2 +- crypto/src/pqc/crypto/sike/SIKEParameters.cs | 77 ++++++++++++++++++++-- 9 files changed, 212 insertions(+), 159 deletions(-) diff --git a/crypto/src/pqc/crypto/sike/Internal.cs b/crypto/src/pqc/crypto/sike/Internal.cs index 35b1a46e8..fce5f6f91 100644 --- a/crypto/src/pqc/crypto/sike/Internal.cs +++ b/crypto/src/pqc/crypto/sike/Internal.cs @@ -8,8 +8,6 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike { internal abstract class Internal { - protected static Dictionary _props; - protected internal static uint RADIX = 64; protected internal static uint LOG2RADIX = 6; @@ -139,10 +137,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike protected internal ulong[] ph3_T2; - static protected uint[] ReadIntsFromProperty(string key, uint intSize) + internal static uint[] ReadIntsFromProperty(IDictionary props, string key, uint intSize) { uint[] ints = new uint[intSize]; - string s = _props[key]; + string s = props[key]; uint i = 0; foreach (string number in s.Split(',')) { @@ -152,9 +150,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike return ints; } - static protected ulong[] ReadFromProperty(string key, uint ulongSize) + internal static ulong[] ReadFromProperty(IDictionary props, string key, uint ulongSize) { - string s = _props[key]; + string s = props[key]; s = s.Replace(",", ""); byte[] bytes = Hex.Decode(s); ulong[] ulongs = new ulong[ulongSize]; @@ -165,9 +163,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike return ulongs; } - static protected ulong[][] ReadFromProperty(string key, uint d1Size, uint d2Size) + internal static ulong[][] ReadFromProperty(IDictionary props, string key, uint d1Size, + uint d2Size) { - string s = _props[key]; + string s = props[key]; s = s.Replace(",", ""); byte[] bytes = Hex.Decode(s); ulong[][] ulongs = new ulong[d1Size][]; //[d2Size]; @@ -185,9 +184,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike return ulongs; } - static protected ulong[][][] ReadFromProperty(string key, uint d1Size, uint d2Size, uint d3Size) + internal static ulong[][][] ReadFromProperty(IDictionary props, string key, uint d1Size, + uint d2Size, uint d3Size) { - string s = _props[key]; + string s = props[key]; s = s.Replace(",", ""); byte[] bytes = Hex.Decode(s); ulong[][][] ulongs = new ulong[d1Size][][]; //[d2Size][d3Size]; diff --git a/crypto/src/pqc/crypto/sike/P434.cs b/crypto/src/pqc/crypto/sike/P434.cs index 98c77aa85..bcad9e970 100644 --- a/crypto/src/pqc/crypto/sike/P434.cs +++ b/crypto/src/pqc/crypto/sike/P434.cs @@ -110,7 +110,7 @@ internal class P434 this.PLEN_3 = 47; // Import compression tables from properties - _props = new Dictionary(); + var props = new Dictionary(); Stream input = typeof(P434).Assembly .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p434.properties"); @@ -134,42 +134,42 @@ internal class P434 int index = header.IndexOf('='); matrix = header.Substring(0, index).Trim(); hexString = header.Substring(index + 1).Trim(); - _props.Add(matrix, hexString); + props.Add(matrix, hexString); i++; } line = sr.ReadLine(); } } - ph2_path = ReadIntsFromProperty("ph2_path", PLEN_2); - ph3_path = ReadIntsFromProperty("ph3_path", PLEN_3); - A_gen = ReadFromProperty("A_gen", 6 * NWORDS64_FIELD); - B_gen = ReadFromProperty("B_gen", 6 * NWORDS64_FIELD); - XQB3 = ReadFromProperty("XQB3", 2 * NWORDS64_FIELD); - A_basis_zero = ReadFromProperty("A_basis_zero", 8 * NWORDS64_FIELD); - B_basis_zero = ReadFromProperty("B_basis_zero", 8 * NWORDS64_FIELD); - B_gen_3_tors = ReadFromProperty("B_gen_3_tors", 16 * NWORDS64_FIELD); - g_R_S_im = ReadFromProperty("g_R_S_im", NWORDS64_FIELD ); - g_phiR_phiS_re = ReadFromProperty("g_phiR_phiS_re", NWORDS64_FIELD); - g_phiR_phiS_im = ReadFromProperty("g_phiR_phiS_im", NWORDS64_FIELD); - Montgomery_RB1 = ReadFromProperty("Montgomery_RB1", NWORDS64_FIELD); - Montgomery_RB2 = ReadFromProperty("Montgomery_RB2", NWORDS64_FIELD); - threeinv = ReadFromProperty("threeinv", NWORDS64_FIELD); - u_entang = ReadFromProperty("u_entang", 2 * NWORDS64_FIELD); - u0_entang = ReadFromProperty("u0_entang", 2 * NWORDS64_FIELD); - table_r_qr = ReadFromProperty("table_r_qr", TABLE_R_LEN, NWORDS64_FIELD); - table_r_qnr = ReadFromProperty("table_r_qnr", TABLE_R_LEN, NWORDS64_FIELD); - table_v_qr = ReadFromProperty("table_v_qr", TABLE_V_LEN, NWORDS64_FIELD); - table_v_qnr = ReadFromProperty("table_v_qnr", TABLE_V_LEN, NWORDS64_FIELD); - v_3_torsion = ReadFromProperty("v_3_torsion", TABLE_V3_LEN, 2, NWORDS64_FIELD); - T_tate3 = ReadFromProperty("T_tate3", (6 * (OBOB_EXPON - 1) + 4) * NWORDS64_FIELD); - T_tate2_firststep_P = ReadFromProperty("T_tate2_firststep_P", 4 * NWORDS64_FIELD); - T_tate2_P = ReadFromProperty("T_tate2_P", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); - T_tate2_firststep_Q = ReadFromProperty("T_tate2_firststep_Q", 4 * NWORDS64_FIELD); - T_tate2_Q = ReadFromProperty("T_tate2_Q", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); - ph2_T = ReadFromProperty("ph2_T",DLEN_2*(ELL2_W >> 1)*2*NWORDS64_FIELD); - ph3_T1 = ReadFromProperty("ph3_T1",DLEN_3*(ELL3_W >> 1)*2*NWORDS64_FIELD); - ph3_T2 = ReadFromProperty("ph3_T2",DLEN_3*(ELL3_W >> 1)*2*NWORDS64_FIELD); + ph2_path = ReadIntsFromProperty(props, "ph2_path", PLEN_2); + ph3_path = ReadIntsFromProperty(props, "ph3_path", PLEN_3); + A_gen = ReadFromProperty(props, "A_gen", 6 * NWORDS64_FIELD); + B_gen = ReadFromProperty(props, "B_gen", 6 * NWORDS64_FIELD); + XQB3 = ReadFromProperty(props, "XQB3", 2 * NWORDS64_FIELD); + A_basis_zero = ReadFromProperty(props, "A_basis_zero", 8 * NWORDS64_FIELD); + B_basis_zero = ReadFromProperty(props, "B_basis_zero", 8 * NWORDS64_FIELD); + B_gen_3_tors = ReadFromProperty(props, "B_gen_3_tors", 16 * NWORDS64_FIELD); + g_R_S_im = ReadFromProperty(props, "g_R_S_im", NWORDS64_FIELD ); + g_phiR_phiS_re = ReadFromProperty(props, "g_phiR_phiS_re", NWORDS64_FIELD); + g_phiR_phiS_im = ReadFromProperty(props, "g_phiR_phiS_im", NWORDS64_FIELD); + Montgomery_RB1 = ReadFromProperty(props, "Montgomery_RB1", NWORDS64_FIELD); + Montgomery_RB2 = ReadFromProperty(props, "Montgomery_RB2", NWORDS64_FIELD); + threeinv = ReadFromProperty(props, "threeinv", NWORDS64_FIELD); + u_entang = ReadFromProperty(props, "u_entang", 2 * NWORDS64_FIELD); + u0_entang = ReadFromProperty(props, "u0_entang", 2 * NWORDS64_FIELD); + table_r_qr = ReadFromProperty(props, "table_r_qr", TABLE_R_LEN, NWORDS64_FIELD); + table_r_qnr = ReadFromProperty(props, "table_r_qnr", TABLE_R_LEN, NWORDS64_FIELD); + table_v_qr = ReadFromProperty(props, "table_v_qr", TABLE_V_LEN, NWORDS64_FIELD); + table_v_qnr = ReadFromProperty(props, "table_v_qnr", TABLE_V_LEN, NWORDS64_FIELD); + v_3_torsion = ReadFromProperty(props, "v_3_torsion", TABLE_V3_LEN, 2, NWORDS64_FIELD); + T_tate3 = ReadFromProperty(props, "T_tate3", (6 * (OBOB_EXPON - 1) + 4) * NWORDS64_FIELD); + T_tate2_firststep_P = ReadFromProperty(props, "T_tate2_firststep_P", 4 * NWORDS64_FIELD); + T_tate2_P = ReadFromProperty(props, "T_tate2_P", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); + T_tate2_firststep_Q = ReadFromProperty(props, "T_tate2_firststep_Q", 4 * NWORDS64_FIELD); + T_tate2_Q = ReadFromProperty(props, "T_tate2_Q", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); + ph2_T = ReadFromProperty(props, "ph2_T",DLEN_2*(ELL2_W >> 1)*2*NWORDS64_FIELD); + ph3_T1 = ReadFromProperty(props, "ph3_T1",DLEN_3*(ELL3_W >> 1)*2*NWORDS64_FIELD); + ph3_T2 = ReadFromProperty(props, "ph3_T2",DLEN_3*(ELL3_W >> 1)*2*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))]; @@ -177,5 +177,4 @@ internal class P434 } } } - -} \ No newline at end of file +} diff --git a/crypto/src/pqc/crypto/sike/P503.cs b/crypto/src/pqc/crypto/sike/P503.cs index fb0cb27c8..893dfd272 100644 --- a/crypto/src/pqc/crypto/sike/P503.cs +++ b/crypto/src/pqc/crypto/sike/P503.cs @@ -146,7 +146,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike this.PLEN_3 = 54; // Import compression tables from properties - _props = new Dictionary(); + var props = new Dictionary(); Stream input = typeof(P503).Assembly .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p503.properties"); @@ -168,55 +168,48 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike int index = header.IndexOf('='); matrix = header.Substring(0, index).Trim(); hexString = header.Substring(index + 1).Trim(); - _props.Add(matrix, hexString); + props.Add(matrix, hexString); i++; } line = sr.ReadLine(); } } - ph2_path = ReadIntsFromProperty("ph2_path", PLEN_2); - ph3_path = ReadIntsFromProperty("ph3_path", PLEN_3); - A_gen = ReadFromProperty("A_gen", 6 * NWORDS64_FIELD); - B_gen = ReadFromProperty("B_gen", 6 * NWORDS64_FIELD); - XQB3 = ReadFromProperty("XQB3", 2 * NWORDS64_FIELD); - A_basis_zero = ReadFromProperty("A_basis_zero", 8 * NWORDS64_FIELD); - B_basis_zero = ReadFromProperty("B_basis_zero", 8 * NWORDS64_FIELD); - B_gen_3_tors = ReadFromProperty("B_gen_3_tors", 16 * NWORDS64_FIELD); - g_R_S_im = ReadFromProperty("g_R_S_im", NWORDS64_FIELD ); - Montgomery_R2 = ReadFromProperty("Montgomery_R2", NWORDS64_FIELD); - Montgomery_RB1 = ReadFromProperty("Montgomery_RB1", NWORDS64_FIELD); - Montgomery_RB2 = ReadFromProperty("Montgomery_RB2", NWORDS64_FIELD); - Montgomery_one = ReadFromProperty( "Montgomery_one", NWORDS64_FIELD); - threeinv = ReadFromProperty("threeinv", NWORDS64_FIELD); - u_entang = ReadFromProperty("u_entang", 2 * NWORDS64_FIELD); - u0_entang = ReadFromProperty("u0_entang", 2 * NWORDS64_FIELD); - table_r_qr = ReadFromProperty("table_r_qr", TABLE_R_LEN, NWORDS64_FIELD); - table_r_qnr = ReadFromProperty("table_r_qnr", TABLE_R_LEN, NWORDS64_FIELD); - table_v_qr = ReadFromProperty("table_v_qr", TABLE_V_LEN, NWORDS64_FIELD); - table_v_qnr = ReadFromProperty("table_v_qnr", TABLE_V_LEN, NWORDS64_FIELD); - v_3_torsion = ReadFromProperty("v_3_torsion", TABLE_V3_LEN, 2, NWORDS64_FIELD); - T_tate3 = ReadFromProperty("T_tate3", (6 * (OBOB_EXPON - 1) + 4) * NWORDS64_FIELD); - T_tate2_firststep_P = ReadFromProperty("T_tate2_firststep_P", 4 * NWORDS64_FIELD); - T_tate2_P = ReadFromProperty("T_tate2_P", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); - T_tate2_firststep_Q = ReadFromProperty("T_tate2_firststep_Q", 4 * NWORDS64_FIELD); - T_tate2_Q = ReadFromProperty("T_tate2_Q", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); - ph2_T = ReadFromProperty("ph2_T",DLEN_2*(ELL2_W >> 1)*2*NWORDS64_FIELD); - ph3_T = ReadFromProperty( "ph3_T", DLEN_3*(ELL3_W>>1)*2*NWORDS64_FIELD); + ph2_path = ReadIntsFromProperty(props, "ph2_path", PLEN_2); + ph3_path = ReadIntsFromProperty(props, "ph3_path", PLEN_3); + A_gen = ReadFromProperty(props, "A_gen", 6 * NWORDS64_FIELD); + B_gen = ReadFromProperty(props, "B_gen", 6 * NWORDS64_FIELD); + XQB3 = ReadFromProperty(props, "XQB3", 2 * NWORDS64_FIELD); + A_basis_zero = ReadFromProperty(props, "A_basis_zero", 8 * NWORDS64_FIELD); + B_basis_zero = ReadFromProperty(props, "B_basis_zero", 8 * NWORDS64_FIELD); + B_gen_3_tors = ReadFromProperty(props, "B_gen_3_tors", 16 * NWORDS64_FIELD); + g_R_S_im = ReadFromProperty(props, "g_R_S_im", NWORDS64_FIELD ); + Montgomery_R2 = ReadFromProperty(props, "Montgomery_R2", NWORDS64_FIELD); + Montgomery_RB1 = ReadFromProperty(props, "Montgomery_RB1", NWORDS64_FIELD); + Montgomery_RB2 = ReadFromProperty(props, "Montgomery_RB2", NWORDS64_FIELD); + Montgomery_one = ReadFromProperty(props, "Montgomery_one", NWORDS64_FIELD); + threeinv = ReadFromProperty(props, "threeinv", NWORDS64_FIELD); + u_entang = ReadFromProperty(props, "u_entang", 2 * NWORDS64_FIELD); + u0_entang = ReadFromProperty(props, "u0_entang", 2 * NWORDS64_FIELD); + table_r_qr = ReadFromProperty(props, "table_r_qr", TABLE_R_LEN, NWORDS64_FIELD); + table_r_qnr = ReadFromProperty(props, "table_r_qnr", TABLE_R_LEN, NWORDS64_FIELD); + table_v_qr = ReadFromProperty(props, "table_v_qr", TABLE_V_LEN, NWORDS64_FIELD); + table_v_qnr = ReadFromProperty(props, "table_v_qnr", TABLE_V_LEN, NWORDS64_FIELD); + v_3_torsion = ReadFromProperty(props, "v_3_torsion", TABLE_V3_LEN, 2, NWORDS64_FIELD); + T_tate3 = ReadFromProperty(props, "T_tate3", (6 * (OBOB_EXPON - 1) + 4) * NWORDS64_FIELD); + T_tate2_firststep_P = ReadFromProperty(props, "T_tate2_firststep_P", 4 * NWORDS64_FIELD); + T_tate2_P = ReadFromProperty(props, "T_tate2_P", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); + T_tate2_firststep_Q = ReadFromProperty(props, "T_tate2_firststep_Q", 4 * NWORDS64_FIELD); + T_tate2_Q = ReadFromProperty(props, "T_tate2_Q", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); + ph2_T = ReadFromProperty(props, "ph2_T",DLEN_2*(ELL2_W >> 1)*2*NWORDS64_FIELD); + ph3_T = ReadFromProperty(props, "ph3_T", DLEN_3*(ELL3_W>>1)*2*NWORDS64_FIELD); Montgomery_R = new ulong[NWORDS64_FIELD]; ph3_T1 = new ulong[DLEN_3*(ELL3_W >> 1)*2*NWORDS64_FIELD]; ph3_T2 = new ulong[DLEN_3*(ELL3_W >> 1)*2*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))]; - - } - - - - } } - -} \ No newline at end of file +} diff --git a/crypto/src/pqc/crypto/sike/P610.cs b/crypto/src/pqc/crypto/sike/P610.cs index ac3f0deec..5ad57827c 100644 --- a/crypto/src/pqc/crypto/sike/P610.cs +++ b/crypto/src/pqc/crypto/sike/P610.cs @@ -146,7 +146,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike this.PLEN_3 = 65; // Import compression tables from properties - _props = new Dictionary(); + var props = new Dictionary(); Stream input = typeof(P610).Assembly .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p610.properties"); @@ -168,51 +168,48 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike int index = header.IndexOf('='); matrix = header.Substring(0, index).Trim(); hexString = header.Substring(index + 1).Trim(); - _props.Add(matrix, hexString); + props.Add(matrix, hexString); i++; } line = sr.ReadLine(); } } - ph2_path = ReadIntsFromProperty("ph2_path", PLEN_2); - ph3_path = ReadIntsFromProperty("ph3_path", PLEN_3); - A_gen = ReadFromProperty("A_gen", 6 * NWORDS64_FIELD); - B_gen = ReadFromProperty("B_gen", 6 * NWORDS64_FIELD); - XQB3 = ReadFromProperty("XQB3", 2 * NWORDS64_FIELD); - A_basis_zero = ReadFromProperty("A_basis_zero", 8 * NWORDS64_FIELD); - B_basis_zero = ReadFromProperty("B_basis_zero", 8 * NWORDS64_FIELD); - B_gen_3_tors = ReadFromProperty("B_gen_3_tors", 16 * NWORDS64_FIELD); - g_R_S_im = ReadFromProperty("g_R_S_im", NWORDS64_FIELD ); - Montgomery_R2 = ReadFromProperty("Montgomery_R2", NWORDS64_FIELD); - Montgomery_RB1 = ReadFromProperty("Montgomery_RB1", NWORDS64_FIELD); - Montgomery_RB2 = ReadFromProperty("Montgomery_RB2", NWORDS64_FIELD); - Montgomery_one = ReadFromProperty( "Montgomery_one", NWORDS64_FIELD); - threeinv = ReadFromProperty("threeinv", NWORDS64_FIELD); - u_entang = ReadFromProperty("u_entang", 2 * NWORDS64_FIELD); - u0_entang = ReadFromProperty("u0_entang", 2 * NWORDS64_FIELD); - table_r_qr = ReadFromProperty("table_r_qr", TABLE_R_LEN, NWORDS64_FIELD); - table_r_qnr = ReadFromProperty("table_r_qnr", TABLE_R_LEN, NWORDS64_FIELD); - table_v_qr = ReadFromProperty("table_v_qr", TABLE_V_LEN, NWORDS64_FIELD); - table_v_qnr = ReadFromProperty("table_v_qnr", TABLE_V_LEN, NWORDS64_FIELD); - v_3_torsion = ReadFromProperty("v_3_torsion", TABLE_V3_LEN, 2, NWORDS64_FIELD); - T_tate3 = ReadFromProperty("T_tate3", (6 * (OBOB_EXPON - 1) + 4) * NWORDS64_FIELD); - T_tate2_firststep_P = ReadFromProperty("T_tate2_firststep_P", 4 * NWORDS64_FIELD); - T_tate2_P = ReadFromProperty("T_tate2_P", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); - T_tate2_firststep_Q = ReadFromProperty("T_tate2_firststep_Q", 4 * NWORDS64_FIELD); - T_tate2_Q = ReadFromProperty("T_tate2_Q", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); - ph2_T = ReadFromProperty("ph2_T",DLEN_2*(ELL2_W >> 1)*2*NWORDS64_FIELD); - ph3_T = ReadFromProperty( "ph3_T", DLEN_3*(ELL3_W >> 1)*2*NWORDS64_FIELD); + ph2_path = ReadIntsFromProperty(props, "ph2_path", PLEN_2); + ph3_path = ReadIntsFromProperty(props, "ph3_path", PLEN_3); + A_gen = ReadFromProperty(props, "A_gen", 6 * NWORDS64_FIELD); + B_gen = ReadFromProperty(props, "B_gen", 6 * NWORDS64_FIELD); + XQB3 = ReadFromProperty(props, "XQB3", 2 * NWORDS64_FIELD); + A_basis_zero = ReadFromProperty(props, "A_basis_zero", 8 * NWORDS64_FIELD); + B_basis_zero = ReadFromProperty(props, "B_basis_zero", 8 * NWORDS64_FIELD); + B_gen_3_tors = ReadFromProperty(props, "B_gen_3_tors", 16 * NWORDS64_FIELD); + g_R_S_im = ReadFromProperty(props, "g_R_S_im", NWORDS64_FIELD ); + Montgomery_R2 = ReadFromProperty(props, "Montgomery_R2", NWORDS64_FIELD); + Montgomery_RB1 = ReadFromProperty(props, "Montgomery_RB1", NWORDS64_FIELD); + Montgomery_RB2 = ReadFromProperty(props, "Montgomery_RB2", NWORDS64_FIELD); + Montgomery_one = ReadFromProperty(props, "Montgomery_one", NWORDS64_FIELD); + threeinv = ReadFromProperty(props, "threeinv", NWORDS64_FIELD); + u_entang = ReadFromProperty(props, "u_entang", 2 * NWORDS64_FIELD); + u0_entang = ReadFromProperty(props, "u0_entang", 2 * NWORDS64_FIELD); + table_r_qr = ReadFromProperty(props, "table_r_qr", TABLE_R_LEN, NWORDS64_FIELD); + table_r_qnr = ReadFromProperty(props, "table_r_qnr", TABLE_R_LEN, NWORDS64_FIELD); + table_v_qr = ReadFromProperty(props, "table_v_qr", TABLE_V_LEN, NWORDS64_FIELD); + table_v_qnr = ReadFromProperty(props, "table_v_qnr", TABLE_V_LEN, NWORDS64_FIELD); + v_3_torsion = ReadFromProperty(props, "v_3_torsion", TABLE_V3_LEN, 2, NWORDS64_FIELD); + T_tate3 = ReadFromProperty(props, "T_tate3", (6 * (OBOB_EXPON - 1) + 4) * NWORDS64_FIELD); + T_tate2_firststep_P = ReadFromProperty(props, "T_tate2_firststep_P", 4 * NWORDS64_FIELD); + T_tate2_P = ReadFromProperty(props, "T_tate2_P", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); + T_tate2_firststep_Q = ReadFromProperty(props, "T_tate2_firststep_Q", 4 * NWORDS64_FIELD); + T_tate2_Q = ReadFromProperty(props, "T_tate2_Q", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); + ph2_T = ReadFromProperty(props, "ph2_T",DLEN_2*(ELL2_W >> 1)*2*NWORDS64_FIELD); + ph3_T = ReadFromProperty(props, "ph3_T", DLEN_3*(ELL3_W >> 1)*2*NWORDS64_FIELD); Montgomery_R = new ulong[NWORDS64_FIELD]; ph3_T1 = new ulong[DLEN_3*(ELL3_W >> 1)*2*NWORDS64_FIELD]; ph3_T2 = new ulong[DLEN_3*(ELL3_W >> 1)*2*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))]; - } } - } - -} \ No newline at end of file +} diff --git a/crypto/src/pqc/crypto/sike/P751.cs b/crypto/src/pqc/crypto/sike/P751.cs index ab281c3a6..e4d802ad5 100644 --- a/crypto/src/pqc/crypto/sike/P751.cs +++ b/crypto/src/pqc/crypto/sike/P751.cs @@ -159,7 +159,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike PLEN_3 = 81; // Import compression tables from properties - _props = new Dictionary(); + var props = new Dictionary(); Stream input = typeof(P751).Assembly .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p751.properties"); @@ -181,51 +181,48 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike int index = header.IndexOf('='); matrix = header.Substring(0, index).Trim(); hexString = header.Substring(index + 1).Trim(); - _props.Add(matrix, hexString); + props.Add(matrix, hexString); } line = sr.ReadLine(); i++; } } - ph2_path = ReadIntsFromProperty("ph2_path", PLEN_2); - ph3_path = ReadIntsFromProperty("ph3_path", PLEN_3); - A_gen = ReadFromProperty("A_gen", 6 * NWORDS64_FIELD); - B_gen = ReadFromProperty("B_gen", 6 * NWORDS64_FIELD); - XQB3 = ReadFromProperty("XQB3", 2 * NWORDS64_FIELD); - A_basis_zero = ReadFromProperty("A_basis_zero", 8 * NWORDS64_FIELD); - B_basis_zero = ReadFromProperty("B_basis_zero", 8 * NWORDS64_FIELD); - B_gen_3_tors = ReadFromProperty("B_gen_3_tors", 16 * NWORDS64_FIELD); - g_R_S_im = ReadFromProperty("g_R_S_im", NWORDS64_FIELD ); - Montgomery_R2 = ReadFromProperty("Montgomery_R2", NWORDS64_FIELD); - Montgomery_RB1 = ReadFromProperty("Montgomery_RB1", NWORDS64_FIELD); - Montgomery_RB2 = ReadFromProperty("Montgomery_RB2", NWORDS64_FIELD); - Montgomery_one = ReadFromProperty( "Montgomery_one", NWORDS64_FIELD); - threeinv = ReadFromProperty("threeinv", NWORDS64_FIELD); - u_entang = ReadFromProperty("u_entang", 2 * NWORDS64_FIELD); - u0_entang = ReadFromProperty("u0_entang", 2 * NWORDS64_FIELD); - table_r_qr = ReadFromProperty("table_r_qr", TABLE_R_LEN, NWORDS64_FIELD); - table_r_qnr = ReadFromProperty("table_r_qnr", TABLE_R_LEN, NWORDS64_FIELD); - table_v_qr = ReadFromProperty("table_v_qr", TABLE_V_LEN, NWORDS64_FIELD); - table_v_qnr = ReadFromProperty("table_v_qnr", TABLE_V_LEN, NWORDS64_FIELD); - v_3_torsion = ReadFromProperty("v_3_torsion", TABLE_V3_LEN, 2, NWORDS64_FIELD); - T_tate3 = ReadFromProperty("T_tate3", (6 * (OBOB_EXPON - 1) + 4) * NWORDS64_FIELD); - T_tate2_firststep_P = ReadFromProperty("T_tate2_firststep_P", 4 * NWORDS64_FIELD); - T_tate2_P = ReadFromProperty("T_tate2_P", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); - T_tate2_firststep_Q = ReadFromProperty("T_tate2_firststep_Q", 4 * NWORDS64_FIELD); - T_tate2_Q = ReadFromProperty("T_tate2_Q", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); - ph2_T = ReadFromProperty("ph2_T",DLEN_2*(ELL2_W >> 1)*2*NWORDS64_FIELD); - ph3_T1 = ReadFromProperty( "ph3_T1", DLEN_3*(ELL3_W>>1)*2*NWORDS64_FIELD); - ph3_T2 = ReadFromProperty( "ph3_T2", DLEN_3*(ELL3_W>>1)*2*NWORDS64_FIELD); + ph2_path = ReadIntsFromProperty(props, "ph2_path", PLEN_2); + ph3_path = ReadIntsFromProperty(props, "ph3_path", PLEN_3); + A_gen = ReadFromProperty(props, "A_gen", 6 * NWORDS64_FIELD); + B_gen = ReadFromProperty(props, "B_gen", 6 * NWORDS64_FIELD); + XQB3 = ReadFromProperty(props, "XQB3", 2 * NWORDS64_FIELD); + A_basis_zero = ReadFromProperty(props, "A_basis_zero", 8 * NWORDS64_FIELD); + B_basis_zero = ReadFromProperty(props, "B_basis_zero", 8 * NWORDS64_FIELD); + B_gen_3_tors = ReadFromProperty(props, "B_gen_3_tors", 16 * NWORDS64_FIELD); + g_R_S_im = ReadFromProperty(props, "g_R_S_im", NWORDS64_FIELD ); + Montgomery_R2 = ReadFromProperty(props, "Montgomery_R2", NWORDS64_FIELD); + Montgomery_RB1 = ReadFromProperty(props, "Montgomery_RB1", NWORDS64_FIELD); + Montgomery_RB2 = ReadFromProperty(props, "Montgomery_RB2", NWORDS64_FIELD); + Montgomery_one = ReadFromProperty(props, "Montgomery_one", NWORDS64_FIELD); + threeinv = ReadFromProperty(props, "threeinv", NWORDS64_FIELD); + u_entang = ReadFromProperty(props, "u_entang", 2 * NWORDS64_FIELD); + u0_entang = ReadFromProperty(props, "u0_entang", 2 * NWORDS64_FIELD); + table_r_qr = ReadFromProperty(props, "table_r_qr", TABLE_R_LEN, NWORDS64_FIELD); + table_r_qnr = ReadFromProperty(props, "table_r_qnr", TABLE_R_LEN, NWORDS64_FIELD); + table_v_qr = ReadFromProperty(props, "table_v_qr", TABLE_V_LEN, NWORDS64_FIELD); + table_v_qnr = ReadFromProperty(props, "table_v_qnr", TABLE_V_LEN, NWORDS64_FIELD); + v_3_torsion = ReadFromProperty(props, "v_3_torsion", TABLE_V3_LEN, 2, NWORDS64_FIELD); + T_tate3 = ReadFromProperty(props, "T_tate3", (6 * (OBOB_EXPON - 1) + 4) * NWORDS64_FIELD); + T_tate2_firststep_P = ReadFromProperty(props, "T_tate2_firststep_P", 4 * NWORDS64_FIELD); + T_tate2_P = ReadFromProperty(props, "T_tate2_P", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); + T_tate2_firststep_Q = ReadFromProperty(props, "T_tate2_firststep_Q", 4 * NWORDS64_FIELD); + T_tate2_Q = ReadFromProperty(props, "T_tate2_Q", 3 * (OALICE_BITS - 2) * NWORDS64_FIELD); + ph2_T = ReadFromProperty(props, "ph2_T",DLEN_2*(ELL2_W >> 1)*2*NWORDS64_FIELD); + ph3_T1 = ReadFromProperty(props, "ph3_T1", DLEN_3*(ELL3_W>>1)*2*NWORDS64_FIELD); + ph3_T2 = ReadFromProperty(props, "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]; } - - } } - -} \ No newline at end of file +} diff --git a/crypto/src/pqc/crypto/sike/SIKEKEMExtractor.cs b/crypto/src/pqc/crypto/sike/SIKEKEMExtractor.cs index 879f1d8ef..66bc0b632 100644 --- a/crypto/src/pqc/crypto/sike/SIKEKEMExtractor.cs +++ b/crypto/src/pqc/crypto/sike/SIKEKEMExtractor.cs @@ -20,7 +20,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike private void InitCipher(SikeParameters param) { - engine = param.Engine; + engine = param.GetEngine(); SikePrivateKeyParameters privateParams = (SikePrivateKeyParameters)key; //todo: add compression check } diff --git a/crypto/src/pqc/crypto/sike/SIKEKEMGenerator.cs b/crypto/src/pqc/crypto/sike/SIKEKEMGenerator.cs index 5e4bd41eb..c9f68dcd0 100644 --- a/crypto/src/pqc/crypto/sike/SIKEKEMGenerator.cs +++ b/crypto/src/pqc/crypto/sike/SIKEKEMGenerator.cs @@ -21,7 +21,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike public ISecretWithEncapsulation GenerateEncapsulated(AsymmetricKeyParameter recipientKey) { SikePublicKeyParameters key = (SikePublicKeyParameters)recipientKey; - SikeEngine engine = key.Parameters.Engine; + SikeEngine engine = key.Parameters.GetEngine(); return GenerateEncapsulated(recipientKey, (int)engine.GetDefaultSessionKeySize()); } @@ -31,7 +31,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike { Console.Error.WriteLine("WARNING: the SIKE algorithm is only for research purposes, insecure"); SikePublicKeyParameters key = (SikePublicKeyParameters)recipientKey; - SikeEngine engine = key.Parameters.Engine; + SikeEngine engine = key.Parameters.GetEngine(); byte[] cipher_text = new byte[engine.GetCipherTextSize()]; byte[] sessionKey = new byte[sessionKeySizeInBits / 8]; engine.crypto_kem_enc(cipher_text, sessionKey, key.GetPublicKey(), sr); diff --git a/crypto/src/pqc/crypto/sike/SIKEKeyPairGenerator.cs b/crypto/src/pqc/crypto/sike/SIKEKeyPairGenerator.cs index 20def8a32..7bfb1ee06 100644 --- a/crypto/src/pqc/crypto/sike/SIKEKeyPairGenerator.cs +++ b/crypto/src/pqc/crypto/sike/SIKEKeyPairGenerator.cs @@ -21,7 +21,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike private AsymmetricCipherKeyPair GenKeyPair() { - SikeEngine engine = sikeParams.Parameters.Engine; + SikeEngine engine = sikeParams.Parameters.GetEngine(); byte[] sk = new byte[engine.GetPrivateKeySize()]; byte[] pk = new byte[engine.GetPublicKeySize()]; diff --git a/crypto/src/pqc/crypto/sike/SIKEParameters.cs b/crypto/src/pqc/crypto/sike/SIKEParameters.cs index d18797067..07d2d77ef 100644 --- a/crypto/src/pqc/crypto/sike/SIKEParameters.cs +++ b/crypto/src/pqc/crypto/sike/SIKEParameters.cs @@ -1,10 +1,51 @@ using System; +using System.Runtime.ConstrainedExecution; namespace Org.BouncyCastle.Pqc.Crypto.Sike { [Obsolete("Will be removed")] public sealed class SikeParameters { + private class SikeP434Engine + { + internal static readonly SikeEngine Instance = new SikeEngine(434, false, null); + } + + private class SikeP503Engine + { + internal static readonly SikeEngine Instance = new SikeEngine(503, false, null); + } + + private class SikeP610Engine + { + internal static readonly SikeEngine Instance = new SikeEngine(610, false, null); + } + + private class SikeP751Engine + { + internal static readonly SikeEngine Instance = new SikeEngine(751, false, null); + } + + private class SikeP434CompressedEngine + { + internal static readonly SikeEngine Instance = new SikeEngine(434, true, null); + } + + private class SikeP503CompressedEngine + { + internal static readonly SikeEngine Instance = new SikeEngine(503, true, null); + } + + private class SikeP610CompressedEngine + { + internal static readonly SikeEngine Instance = new SikeEngine(610, true, null); + } + + private class SikeP751CompressedEngine + { + internal static readonly SikeEngine Instance = new SikeEngine(751, true, null); + } + public static readonly SikeParameters sikep434 = new SikeParameters(434, false, "sikep434"); public static readonly SikeParameters sikep503 = new SikeParameters(503, false, "sikep503"); public static readonly SikeParameters sikep610 = new SikeParameters(610, false, "sikep610"); @@ -15,19 +56,45 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike public static readonly SikeParameters sikep610_compressed = new SikeParameters(610, true, "sikep610_compressed"); public static readonly SikeParameters sikep751_compressed = new SikeParameters(751, true, "sikep751_compressed"); + private readonly int ver; + private readonly bool isCompressed; private readonly string name; - private readonly SikeEngine engine; - public SikeParameters(int ver, bool isCompressed, string name) + private SikeParameters(int ver, bool isCompressed, string name) { + this.ver = ver; + this.isCompressed = isCompressed; this.name = name; - this.engine = new SikeEngine(ver, isCompressed, null); } - internal SikeEngine Engine => engine; + internal SikeEngine GetEngine() + { + if (isCompressed) + { + switch (ver) + { + case 434: return SikeP434CompressedEngine.Instance; + case 503: return SikeP503CompressedEngine.Instance; + case 610: return SikeP610CompressedEngine.Instance; + case 751: return SikeP751CompressedEngine.Instance; + default: throw new InvalidOperationException(); + } + } + else + { + switch (ver) + { + case 434: return SikeP434Engine.Instance; + case 503: return SikeP503Engine.Instance; + case 610: return SikeP610Engine.Instance; + case 751: return SikeP751Engine.Instance; + default: throw new InvalidOperationException(); + } + } + } public string Name => name; - public int DefaultKeySize => (int)this.engine.GetDefaultSessionKeySize(); + public int DefaultKeySize => (int)GetEngine().GetDefaultSessionKeySize(); } } -- cgit 1.4.1