diff --git a/src/Cryptography/FinitePoint`1.cs b/src/Cryptography/FinitePoint`1.cs index de6a39c..eb97fb7 100644 --- a/src/Cryptography/FinitePoint`1.cs +++ b/src/Cryptography/FinitePoint`1.cs @@ -154,39 +154,39 @@ public FinitePoint(Calculator x, Calculator y) /// /// The left operand /// The right operand - /// Returns if its operands are not equal, otherwise . + /// Returns if its operands aren't equal, otherwise . public static bool operator !=(FinitePoint left, FinitePoint right) => !left.Equals(right); /// /// Greater than operator /// - /// The 1st operand - /// The 2nd operand - /// Returns if its 1st operand is greater than its 2nd operand, otherwise . + /// The first operand + /// The second operand + /// Returns if its first operand is greater than its second operand, otherwise . public static bool operator >(FinitePoint left, FinitePoint right) => left.CompareTo(right) == 1; /// /// Less than operator /// - /// The 1st operand - /// The 2nd operand - /// Returns if its 1st operand is less than its 2nd operand, otherwise . + /// The first operand + /// The second operand + /// Returns if its first operand is less than its second operand, otherwise . public static bool operator <(FinitePoint left, FinitePoint right) => left.CompareTo(right) == -1; /// /// Greater than or equal operator /// /// The 1st operand - /// The 2nd operand - /// Returns if its 1st operand is greater than or equal to its 2nd operand, otherwise . + /// The second operand + /// Returns if its first operand is greater than or equal to its second operand, otherwise . public static bool operator >=(FinitePoint left, FinitePoint right) => left.CompareTo(right) >= 0; /// /// Less than or equal operator /// - /// The 1st operand - /// The 2nd operand - /// Returns if its 1st operand is less than or equal to its 2nd operand, otherwise . + /// The first operand + /// The second operand + /// Returns if its first operand is less than or equal to its second operand, otherwise . public static bool operator <=(FinitePoint left, FinitePoint right) => left.CompareTo(right) <= 0; /// @@ -251,7 +251,7 @@ private static Calculator Evaluate(IEnumerable> pol /// Converts a byte collection to hexadecimal string. /// /// - /// human readable / printable string + /// human-readable / printable string /// /// Based on discussion on stackoverflow /// @@ -287,8 +287,7 @@ private static string ToHexString(IEnumerable bytes) private static byte[] ToByteArray(string hexString) { byte[] bytes = new byte[hexString.Length / 2]; - var hexValues = Array.AsReadOnly(new[] - { + var hexValues = Array.AsReadOnly([ 0x00, 0x01, 0x02, @@ -312,7 +311,7 @@ private static byte[] ToByteArray(string hexString) 0x0D, 0x0E, 0x0F - }); + ]); for (int i = 0, j = 0; j < hexString.Length; j += 2, i += 1) { diff --git a/src/Cryptography/IMakeSharesUseCase.cs b/src/Cryptography/IMakeSharesUseCase.cs new file mode 100644 index 0000000..bfed74e --- /dev/null +++ b/src/Cryptography/IMakeSharesUseCase.cs @@ -0,0 +1,43 @@ +namespace SecretSharingDotNet.Cryptography; + +/// +/// Interface for the Shamir's Secret Sharing algorithm implementation for creating shared secrets. +/// +/// Numeric data type (An integer type) +public interface IMakeSharesUseCase +{ + /// + /// Generates a random shamir pool, returns the random secret and the share points. + /// + /// Minimum number of shared secrets for reconstruction + /// Maximum number of shared secrets + /// Security level (in number of bits). The minimum is 13. + /// A object + /// + /// The parameter is lower than 13 or greater than 43.112.609. OR The parameter is lower than 2 or greater than . + /// + Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberOfShares, int securityLevel); + + /// + /// Generates a random shamir pool, returns the specified and the share points. + /// + /// Minimum number of shared secrets for reconstruction + /// Maximum number of shared secrets + /// secret text as or see cref="string"/> + /// Security level (in number of bits). The minimum is 13. + /// A object + /// + /// The is lower than 13 or greater than 43.112.609. OR is lower than 2 or greater than . + /// + Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberOfShares, Secret secret, int securityLevel); + + /// + /// Generates a random shamir pool, returns the specified and the share points. + /// + /// Minimum number of shared secrets for reconstruction + /// Maximum number of shared secrets + /// secret text as or see cref="string"/> + /// A object + /// is lower than 2 or greater than . + Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberOfShares, Secret secret); +} \ No newline at end of file diff --git a/src/Cryptography/IReconstructionUseCase.cs b/src/Cryptography/IReconstructionUseCase.cs new file mode 100644 index 0000000..e4a70e6 --- /dev/null +++ b/src/Cryptography/IReconstructionUseCase.cs @@ -0,0 +1,39 @@ +namespace SecretSharingDotNet.Cryptography; + +/// +/// Interface for the Shamir's Secret Sharing algorithm implementation for reconstructing the secret. +/// +/// +public interface IReconstructionUseCase +{ + /// + /// Recovers the secret from the given (points with x and y on the polynomial) + /// + /// Shares represented by and separated by newline. + /// Re-constructed secret + Secret Reconstruction(string shares); + + /// + /// + /// Recovers the secret from the given (points with x and y on the polynomial) + /// + /// Shares represented by array. + /// Re-constructed secret + Secret Reconstruction(string[] shares); + + /// + /// Recovers the secret from the given (points with x and y on the polynomial) + /// + /// For details + /// Re-constructed secret + Secret Reconstruction(Shares shares); + + /// + /// Recovers the secret from the given (points with x and y on the polynomial) + /// + /// Two or more shares represented by a set of + /// Re-constructed secret + /// is . + /// The length of is lower than 2. + Secret Reconstruction(FinitePoint[] shares); +} \ No newline at end of file diff --git a/src/Cryptography/Secret`1.cs b/src/Cryptography/Secret`1.cs index df441f6..dcd5897 100644 --- a/src/Cryptography/Secret`1.cs +++ b/src/Cryptography/Secret`1.cs @@ -235,7 +235,7 @@ public static implicit operator Secret(ReadOnlySpan secretText) /// /// The left operand /// The right operand - /// Returns if its operands are not equal, otherwise . + /// Returns if its operands aren't equal, otherwise . public static bool operator !=(Secret left, Secret right) => !left.Equals(right); /// @@ -276,8 +276,8 @@ public static implicit operator Secret(ReadOnlySpan secretText) /// An instance to compare with this instance. /// A value that indicates the relative order of the instances being compared. public int CompareTo(Secret other) => this.secretNumber - .Subset(0, this.SecretByteSize - MarkByteCount).CompareTo(other.secretNumber - .Subset(0, other.SecretByteSize - MarkByteCount)); + .Subset(0, this.SecretByteSize - MarkByteCount) + .CompareTo(other.secretNumber.Subset(0, other.SecretByteSize - MarkByteCount)); /// /// Determines whether this instance and an specified instance are equal. diff --git a/src/Cryptography/ShamirsSecretSharing`3.cs b/src/Cryptography/ShamirsSecretSharing`3.cs index 2f5531c..c6edb10 100644 --- a/src/Cryptography/ShamirsSecretSharing`3.cs +++ b/src/Cryptography/ShamirsSecretSharing`3.cs @@ -41,9 +41,10 @@ namespace SecretSharingDotNet.Cryptography; /// Shamir's secret sharing algorithm /// /// Numeric data type -/// -/// -public class ShamirsSecretSharing : ShamirsSecretSharing +/// Extended greatest common divisor algorithm +/// Extended greatest common divisor result +public class ShamirsSecretSharing : ShamirsSecretSharing, + IMakeSharesUseCase, IReconstructionUseCase where TExtendedGcdAlgorithm : class, IExtendedGcdAlgorithm where TExtendedGcdResult : struct, IExtendedGcdResult { @@ -70,21 +71,21 @@ public class ShamirsSecretSharing /// Gets or sets the security level /// - /// Value is lower than 13 or greater than 43112609. - /// Value is lower than 13 or greater than 43112609. + /// The value is lower than 13 or greater than 43.112.609. + /// The value is lower than 13 or greater than 43.112.609. public int SecurityLevel { get => this.fixedSecurityLevel; set { - if (value < 13) + if (value < SecurityLevels[0]) { throw new ArgumentOutOfRangeException(nameof(value), value, ErrorMessages.MinimumSecurityLevelExceeded); } @@ -112,9 +113,11 @@ public int SecurityLevel /// /// Minimum number of shared secrets for reconstruction /// Maximum number of shared secrets - /// Security level (in number of bits). Minimum is 13. + /// Security level (in number of bits). The minimum is 13. /// - /// The parameter is lower than 13 or greater than 43112609. OR The parameter is lower than 2 or greater than . + /// + /// The parameter is lower than 13 or greater than 43.112.609. OR The parameter is lower than 2 or greater than . + /// public Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberOfShares, int securityLevel) { try @@ -156,10 +159,12 @@ public Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberO /// Minimum number of shared secrets for reconstruction /// Maximum number of shared secrets /// secret text as or see cref="string"/> - /// Security level (in number of bits). Minimum is 13. + /// Security level (in number of bits). The minimum is 13. /// /// This method can modify the based on the length. - /// The is lower than 13 or greater than 43112609. OR is lower than 2 or greater than . + /// + /// The is lower than 13 or greater than 43.112.609. OR is lower than 2 or greater than . + /// public Shares MakeShares(TNumber numberOfMinimumShares, TNumber numberOfShares, Secret secret, int securityLevel) { try @@ -220,13 +225,11 @@ private Calculator[] CreatePolynomial(int numberOfMinimumShares) var polynomial = new Calculator[numberOfMinimumShares]; polynomial[0] = Calculator.Zero; byte[] randomNumber = new byte[this.mersennePrime.ByteCount]; - using (var rng = RandomNumberGenerator.Create()) + using var rng = RandomNumberGenerator.Create(); + for (int i = 1; i < numberOfMinimumShares; i++) { - for (int i = 1; i < numberOfMinimumShares; i++) - { - rng.GetBytes(randomNumber); - polynomial[i] = (Calculator.Create(randomNumber, typeof(TNumber)) as Calculator)?.Abs() % this.mersennePrime; - } + rng.GetBytes(randomNumber); + polynomial[i] = (Calculator.Create(randomNumber, typeof(TNumber)) as Calculator)?.Abs() % this.mersennePrime; } return polynomial; @@ -293,7 +296,7 @@ private static Calculator Product(IReadOnlyList> va /// k points will define a polynomial of up to kth order /// /// The shares represented by a set of . - /// A prime number must be defined to avoid computation with real numbers. In fact it is finite field arithmetic. + /// A prime number must be defined to avoid computation with real numbers. In fact, it is finite field arithmetic. /// The prime number must be the same as used for the construction of shares. /// /// The re-constructed secret. diff --git a/src/Cryptography/SharedSeparator.cs b/src/Cryptography/SharedSeparator.cs index e86345d..06cd59e 100644 --- a/src/Cryptography/SharedSeparator.cs +++ b/src/Cryptography/SharedSeparator.cs @@ -41,5 +41,5 @@ internal static class SharedSeparator /// /// Separator array for method usage to avoid allocation of a new array. /// - internal static readonly char[] CoordinateSeparatorArray = { CoordinateSeparator }; + internal static readonly char[] CoordinateSeparatorArray = [CoordinateSeparator]; } \ No newline at end of file diff --git a/src/Cryptography/Shares.cs b/src/Cryptography/Shares.cs index f774a59..0ea67a9 100644 --- a/src/Cryptography/Shares.cs +++ b/src/Cryptography/Shares.cs @@ -95,12 +95,12 @@ internal Shares(Secret secret, IList> shares) public FinitePoint this[int i] => this.shareList[i]; /// - /// Gets a value indicating whether or not the original secret is available. + /// Gets a value indicating whether the original secret is available. /// public bool OriginalSecretExists => this.OriginalSecret != null; /// - /// Casts a object to a array of s. + /// Casts a object to an array of s. /// /// A object. public static implicit operator string[](Shares shares) => shares?.Select(s => s.ToString()).ToArray(); @@ -118,7 +118,7 @@ internal Shares(Secret secret, IList> shares) public static implicit operator Shares(string s) { var points = s - .Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) + .Split([Environment.NewLine], StringSplitOptions.RemoveEmptyEntries) .Select(line => new FinitePoint(line)) .ToArray(); return new Shares(points); @@ -146,7 +146,7 @@ public static explicit operator FinitePoint[](Shares shares) = /// /// Returns the string representation of the instance. /// - /// A human readable list of shares separated by newlines + /// A human-readable list of shares separated by newlines public override string ToString() { var stringBuilder = new StringBuilder(); @@ -198,7 +198,7 @@ public override string ToString() /// /// Removes all items from the collection. /// - /// This method is implemented. However this method does nothing as long as the property is + /// This method is implemented. However, this method does nothing as long as the property is /// set to . /// The collection is read-only. public void Clear() @@ -215,7 +215,7 @@ public void Clear() /// Adds an to the collection. /// /// The to add to the collection. - /// This method is implemented. However this method does nothing as long as the property is + /// This method is implemented. However, this method does nothing as long as the property is /// set to . /// The collection is read-only. public void Add(FinitePoint item) @@ -236,7 +236,7 @@ public void Add(FinitePoint item) /// /// The to remove from the collection. /// - /// This method is implemented. However this method does nothing as long as the property is + /// This method is implemented. However, this method does nothing as long as the property is /// set to . /// The collection is read-only. public bool Remove(FinitePoint item) diff --git a/src/Math/BigIntCalculator.cs b/src/Math/BigIntCalculator.cs index 10b04bc..f3bd1a9 100644 --- a/src/Math/BigIntCalculator.cs +++ b/src/Math/BigIntCalculator.cs @@ -66,7 +66,7 @@ public BigIntCalculator(byte[] data) : base(new BigInteger(data)) { } public override bool Equals(Calculator other) { var valueLeft = this.Value.ToByteArray(); - var valueRight = other?.Value.ToByteArray() ?? Array.Empty(); + var valueRight = other?.Value.ToByteArray() ?? []; var diff = (uint)valueLeft.Length ^ (uint)valueRight.Length; for (var i = 0; i < valueLeft.Length && i < valueRight.Length; i++) @@ -202,17 +202,17 @@ public override int CompareTo(Calculator other) public override IEnumerable ByteRepresentation => new ReadOnlyCollection(this.Value.ToByteArray()); /// - /// Gets a value indicating whether or not the current object is zero (0). + /// Gets a value indicating whether the current object is zero (0). /// public override bool IsZero => this.Value.IsZero; /// - /// Gets a value indicating whether or not the current object is one (1). + /// Gets a value indicating whether the current object is one (1). /// public override bool IsOne => this.Value.IsOne; /// - /// Gets a value indicating whether or not the current object is an even number. + /// Gets a value indicating whether the current object is an even number. /// public override bool IsEven => this.Value % 2 == 0; diff --git a/src/Math/Calculator.cs b/src/Math/Calculator.cs index 840fbeb..9dbf0e3 100644 --- a/src/Math/Calculator.cs +++ b/src/Math/Calculator.cs @@ -117,7 +117,7 @@ protected static Dictionary> GetDerivedCtors var parameterExpression = Expression.Parameter(paramType); foreach (var childType in ChildTypes) { - var ctorInfo = childType.Value.GetConstructor(new[] {paramType}); + var ctorInfo = childType.Value.GetConstructor([paramType]); if (ctorInfo == null) { continue; diff --git a/src/Math/Calculator`1.cs b/src/Math/Calculator`1.cs index 152076b..2fa503c 100644 --- a/src/Math/Calculator`1.cs +++ b/src/Math/Calculator`1.cs @@ -336,15 +336,12 @@ public override bool Equals(object obj) /// A signed integer that indicates the relationship of the current instance to the parameter public virtual int CompareTo(object obj) { - switch (obj) + return obj switch { - case null: - return 1; - case TNumber number: - return this.CompareTo(number); - default: - throw new ArgumentException(); - } + null => 1, + TNumber number => this.CompareTo(number), + _ => throw new ArgumentException() + }; } /// diff --git a/src/Math/IExtendedGcdAlgorithm`2.cs b/src/Math/IExtendedGcdAlgorithm`2.cs index 4d6da77..522e045 100644 --- a/src/Math/IExtendedGcdAlgorithm`2.cs +++ b/src/Math/IExtendedGcdAlgorithm`2.cs @@ -32,7 +32,7 @@ namespace SecretSharingDotNet.Math; /// -/// Provides mechanism to compute the extended greatest common divisor +/// Provides a mechanism to compute the extended greatest common divisor /// including Bézout coefficients. /// /// Numeric data type (An integer type)