From 2c24f40b842d41aa7e523042585794abada4cc9e Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 6 Mar 2023 15:46:26 +0700 Subject: Refactor resource loading --- crypto/src/pqc/crypto/picnic/LowmcConstantsL1.cs | 8 ++-- crypto/src/pqc/crypto/picnic/LowmcConstantsL3.cs | 8 ++-- crypto/src/pqc/crypto/picnic/LowmcConstantsL5.cs | 8 ++-- crypto/src/pqc/crypto/sike/P434.cs | 10 ++--- crypto/src/pqc/crypto/sike/P503.cs | 10 ++--- crypto/src/pqc/crypto/sike/P610.cs | 50 ++++++++++++------------ crypto/src/pqc/crypto/sike/P751.cs | 50 ++++++++++++------------ 7 files changed, 65 insertions(+), 79 deletions(-) diff --git a/crypto/src/pqc/crypto/picnic/LowmcConstantsL1.cs b/crypto/src/pqc/crypto/picnic/LowmcConstantsL1.cs index 7c779ab2d..d50651f4b 100644 --- a/crypto/src/pqc/crypto/picnic/LowmcConstantsL1.cs +++ b/crypto/src/pqc/crypto/picnic/LowmcConstantsL1.cs @@ -10,11 +10,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Picnic public LowmcConstantsL1() { _matrixToHex = new Dictionary(); - Stream input = typeof(LowmcConstants).Assembly - .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.picnic.lowmcL1.bz2"); - input = Bzip2.DecompressInput(input); - - using (StreamReader sr = new StreamReader(input)) + using (var input = typeof(LowmcConstants).Assembly.GetManifestResourceStream( + "Org.BouncyCastle.pqc.crypto.picnic.lowmcL1.bz2")) + using (var sr = new StreamReader(Bzip2.DecompressInput(input))) { // load a properties file string line = sr.ReadLine(); diff --git a/crypto/src/pqc/crypto/picnic/LowmcConstantsL3.cs b/crypto/src/pqc/crypto/picnic/LowmcConstantsL3.cs index b35e727a5..026081fcd 100644 --- a/crypto/src/pqc/crypto/picnic/LowmcConstantsL3.cs +++ b/crypto/src/pqc/crypto/picnic/LowmcConstantsL3.cs @@ -10,11 +10,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Picnic public LowmcConstantsL3() { _matrixToHex = new Dictionary(); - Stream input = typeof(LowmcConstants).Assembly - .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.picnic.lowmcL3.bz2"); - input = Bzip2.DecompressInput(input); - - using (StreamReader sr = new StreamReader(input)) + using (var input = typeof(LowmcConstants).Assembly.GetManifestResourceStream( + "Org.BouncyCastle.pqc.crypto.picnic.lowmcL3.bz2")) + using (var sr = new StreamReader(Bzip2.DecompressInput(input))) { // load a properties file string line = sr.ReadLine(); diff --git a/crypto/src/pqc/crypto/picnic/LowmcConstantsL5.cs b/crypto/src/pqc/crypto/picnic/LowmcConstantsL5.cs index 2fcab262f..55edf6c5c 100644 --- a/crypto/src/pqc/crypto/picnic/LowmcConstantsL5.cs +++ b/crypto/src/pqc/crypto/picnic/LowmcConstantsL5.cs @@ -11,11 +11,9 @@ namespace Org.BouncyCastle.Pqc.Crypto.Picnic public LowmcConstantsL5() { _matrixToHex = new Dictionary(); - Stream input = typeof(LowmcConstants).Assembly - .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.picnic.lowmcL5.bz2"); - input = Bzip2.DecompressInput(input); - - using (StreamReader sr = new StreamReader(input)) + using (var input = typeof(LowmcConstants).Assembly.GetManifestResourceStream( + "Org.BouncyCastle.pqc.crypto.picnic.lowmcL5.bz2")) + using (var sr = new StreamReader(Bzip2.DecompressInput(input))) { // load a properties file string line = sr.ReadLine(); diff --git a/crypto/src/pqc/crypto/sike/P434.cs b/crypto/src/pqc/crypto/sike/P434.cs index b55b092c0..81bc16d07 100644 --- a/crypto/src/pqc/crypto/sike/P434.cs +++ b/crypto/src/pqc/crypto/sike/P434.cs @@ -111,12 +111,10 @@ internal class P434 this.PLEN_3 = 47; // Import compression tables from properties - var props = new Dictionary(); - Stream input = typeof(P434).Assembly - .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p434.bz2"); - input = Bzip2.DecompressInput(input); - - using (StreamReader sr = new StreamReader(input)) + var props = new Dictionary(); + using (var input = typeof(P434).Assembly + .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p434.bz2")) + using (var sr = new StreamReader(Bzip2.DecompressInput(input))) { int i = 0; // load a properties file diff --git a/crypto/src/pqc/crypto/sike/P503.cs b/crypto/src/pqc/crypto/sike/P503.cs index 32085f41a..8e5bfbe58 100644 --- a/crypto/src/pqc/crypto/sike/P503.cs +++ b/crypto/src/pqc/crypto/sike/P503.cs @@ -147,12 +147,10 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike this.PLEN_3 = 54; // Import compression tables from properties - var props = new Dictionary(); - Stream input = typeof(P503).Assembly - .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p503.bz2"); - input = Bzip2.DecompressInput(input); - - using (StreamReader sr = new StreamReader(input)) + var props = new Dictionary(); + using (var input = typeof(P503).Assembly + .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p503.bz2")) + using (var sr = new StreamReader(Bzip2.DecompressInput(input))) { // load a properties file string line = sr.ReadLine(); diff --git a/crypto/src/pqc/crypto/sike/P610.cs b/crypto/src/pqc/crypto/sike/P610.cs index 38e180f6e..f7681feb6 100644 --- a/crypto/src/pqc/crypto/sike/P610.cs +++ b/crypto/src/pqc/crypto/sike/P610.cs @@ -147,35 +147,33 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike this.PLEN_3 = 65; // Import compression tables from properties - var props = new Dictionary(); - Stream input = typeof(P610).Assembly - .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p610.bz2"); - input = Bzip2.DecompressInput(input); - - using (StreamReader sr = new StreamReader(input)) + var props = new Dictionary(); + using (var input = typeof(P610).Assembly.GetManifestResourceStream( + "Org.BouncyCastle.pqc.crypto.sike.p610.bz2")) + using (var sr = new StreamReader(Bzip2.DecompressInput(input))) { - // load a properties file - string line = sr.ReadLine(); - string matrix, hexString; - int i = 0; - while (line != null) + // load a properties file + string line = sr.ReadLine(); + string matrix, hexString; + int i = 0; + while (line != null) + { + string header = line; + if (header != "") { - string header = line; - if (header != "") - { - if (i > 1) - { - header = header.Replace(",", ""); - } - int index = header.IndexOf('='); - matrix = header.Substring(0, index).Trim(); - hexString = header.Substring(index + 1).Trim(); - props.Add(matrix, hexString); - i++; - } - - line = sr.ReadLine(); + if (i > 1) + { + header = header.Replace(",", ""); + } + int index = header.IndexOf('='); + matrix = header.Substring(0, index).Trim(); + hexString = header.Substring(index + 1).Trim(); + props.Add(matrix, hexString); + i++; } + + line = sr.ReadLine(); + } } ph2_path = ReadIntsFromProperty(props, "ph2_path", PLEN_2); ph3_path = ReadIntsFromProperty(props, "ph3_path", PLEN_3); diff --git a/crypto/src/pqc/crypto/sike/P751.cs b/crypto/src/pqc/crypto/sike/P751.cs index 9604a6ede..9fc704e76 100644 --- a/crypto/src/pqc/crypto/sike/P751.cs +++ b/crypto/src/pqc/crypto/sike/P751.cs @@ -160,35 +160,33 @@ namespace Org.BouncyCastle.Pqc.Crypto.Sike PLEN_3 = 81; // Import compression tables from properties - var props = new Dictionary(); - Stream input = typeof(P751).Assembly - .GetManifestResourceStream("Org.BouncyCastle.pqc.crypto.sike.p751.bz2"); - input = Bzip2.DecompressInput(input); - - using (StreamReader sr = new StreamReader(input)) + var props = new Dictionary(); + using (var input = typeof(P751).Assembly.GetManifestResourceStream( + "Org.BouncyCastle.pqc.crypto.sike.p751.bz2")) + using (var sr = new StreamReader(Bzip2.DecompressInput(input))) { - // load a properties file - string line = sr.ReadLine(); - string matrix, hexString; - int i = 0; - while (line != null) + // load a properties file + string line = sr.ReadLine(); + string matrix, hexString; + int i = 0; + while (line != null) + { + string header = line; + if (header != "") { - string header = line; - if (header != "") - { - if (i > 1) - { - header = header.Replace(",", ""); - } - int index = header.IndexOf('='); - matrix = header.Substring(0, index).Trim(); - hexString = header.Substring(index + 1).Trim(); - props.Add(matrix, hexString); - } - - line = sr.ReadLine(); - i++; + if (i > 1) + { + header = header.Replace(",", ""); + } + int index = header.IndexOf('='); + matrix = header.Substring(0, index).Trim(); + hexString = header.Substring(index + 1).Trim(); + props.Add(matrix, hexString); } + + line = sr.ReadLine(); + i++; + } } ph2_path = ReadIntsFromProperty(props, "ph2_path", PLEN_2); ph3_path = ReadIntsFromProperty(props, "ph3_path", PLEN_3); -- cgit 1.4.1