diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 7b7ed0a..c99b30a 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -41,24 +41,19 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Setup .NET 6 - uses: actions/setup-dotnet@v4.0.0 - with: - dotnet-version: 6.0.416 - - - name: Setup .NET 7 - uses: actions/setup-dotnet@v4.0.0 + - name: Setup .NET 8 + uses: actions/setup-dotnet@v4.1.0 with: - dotnet-version: 7.0.403 + dotnet-version: 8.0.404 - - name: Setup .NET 8 - uses: actions/setup-dotnet@v4.0.0 + - name: Setup .NET 9 + uses: actions/setup-dotnet@v4.1.0 with: - dotnet-version: 8.0.100 + dotnet-version: 9.0.100 - name: .NET Core SxS run: | - rsync -a ${DOTNET_ROOT/8.0.100/7.0.403/6.0.416}/* $DOTNET_ROOT/ + rsync -a ${DOTNET_ROOT/8.0.404/9.0.100}/* $DOTNET_ROOT/ # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL diff --git a/.github/workflows/dotnetall.yml b/.github/workflows/dotnetall.yml index f41ac11..3b9c522 100644 --- a/.github/workflows/dotnetall.yml +++ b/.github/workflows/dotnetall.yml @@ -18,20 +18,15 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Setup .NET 6 - uses: actions/setup-dotnet@v4.0.0 - with: - dotnet-version: 6.0.416 - - - name: Setup .NET 7 - uses: actions/setup-dotnet@v4.0.0 + - name: Setup .NET 8 + uses: actions/setup-dotnet@v4.1.0 with: - dotnet-version: 7.0.403 + dotnet-version: 8.0.404 - - name: Setup .NET 8 - uses: actions/setup-dotnet@v4.0.0 + - name: Setup .NET 9 + uses: actions/setup-dotnet@v4.1.0 with: - dotnet-version: 8.0.100 + dotnet-version: 9.0.100 - name: Restore run: dotnet restore SecretSharingDotNet.sln diff --git a/.github/workflows/publishing.yml b/.github/workflows/publishing.yml index 0f2b11d..78f9eca 100644 --- a/.github/workflows/publishing.yml +++ b/.github/workflows/publishing.yml @@ -16,20 +16,15 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Setup .NET 6 - uses: actions/setup-dotnet@v4.0.0 - with: - dotnet-version: 6.0.416 - - - name: Setup .NET 7 - uses: actions/setup-dotnet@v4.0.0 + - name: Setup .NET 8 + uses: actions/setup-dotnet@v4.1.0 with: - dotnet-version: 7.0.403 + dotnet-version: 8.0.404 - - name: Setup .NET 8 - uses: actions/setup-dotnet@v4.0.0 + - name: Setup .NET 9 + uses: actions/setup-dotnet@v4.1.0 with: - dotnet-version: 8.0.100 + dotnet-version: 9.0.100 - name: Decrypt large secret run: ./.github/secrets/decrypt_publisher_snk.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fa7bb5..ae34c92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,20 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.12.0] - 2024-11-17 +### Added +- Added .NET 9.0 support +- Added use-case interfaces `IMakeSharesUseCase` and `IReconstructUseCase` to the `SecretSharingDotNet` project. +- Added a guide to the `README.md` file on how to use the use-case interfaces with dependency injection. + +### Changed +- Changed the `Sqrt` signature in the `BigIntCalculator` class from property to method. +- Changed method visibility from `public` to `protected` in the `BigIntCalculator`and `Calculator` classes to restrict access. + +### Removed +- Removed .NET 7 support, because it retires on May 14, 2024. See [.NET and .NET Core Support Policy](https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core). +- Removed .NET 6 support, because it retires on November 12, 2024. See [.NET and .NET Core Support Policy](https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core). + ## [0.11.0] - 2023-12-30 ### Added - Added support for .NET FX v4.8.1 @@ -206,7 +220,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `LICENSE.md` - Added `README.md` -[Unreleased]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.11.0...develop +[Unreleased]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.12.0...develop +[0.12.0]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.11.0...v0.12.0 [0.11.0]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.10.2...v0.11.0 [0.10.2]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.10.1...v0.10.2 [0.10.1]: https://github.com/shinji-san/SecretSharingDotNet/compare/v0.10.0...v0.10.1 diff --git a/README.md b/README.md index b7bd551..5faa531 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,9 @@ An C# implementation of Shamir's Secret Sharing. - Build status - SecretSharingDotNet.sln - SDK + Build status + SecretSharingDotNet.sln + SDK Standard 2.0 @@ -37,13 +37,10 @@ An C# implementation of Shamir's Secret Sharing. FX 4.8 - .NET 6 - - - .NET 7 + .NET 8 - .NET 8 + .NET 9 @@ -61,9 +58,9 @@ An C# implementation of Shamir's Secret Sharing. - SecretSharingDotNet NuGet - NuGet Version 0.11.0 - Tag + SecretSharingDotNet NuGet + NuGet Version 0.12.0 + Tag Standard 2.0 @@ -85,13 +82,10 @@ An C# implementation of Shamir's Secret Sharing. FX 4.8 - .NET 6 + .NET 8 - .NET 7 - - - .NET 8 + .NET 9 @@ -100,10 +94,10 @@ An C# implementation of Shamir's Secret Sharing. 1. Open a console and switch to the directory, containing your project file. -2. Use the following command to install version 0.11.0 of the SecretSharingDotNet package: +2. Use the following command to install version 0.12.0 of the SecretSharingDotNet package: ```dotnetcli - dotnet add package SecretSharingDotNet -v 0.11.0 -f + dotnet add package SecretSharingDotNet -v 0.12.0 -f ``` 3. After the completion of the command, look at the project file to make sure that the package is successfully installed. @@ -112,7 +106,7 @@ An C# implementation of Shamir's Secret Sharing. ```xml - + ``` ## Remove SecretSharingDotNet package @@ -134,7 +128,45 @@ An C# implementation of Shamir's Secret Sharing. Use the function `MakeShares` to generate the shares, based on a random or pre-defined secret. Afterwards, use the function `Reconstruction` to re-construct the original secret. -The length of the shares is based on the security level. It's possible to pre-define a security level by `ctor` or the `SecurityLevel` property. The pre-defined security level will be overriden, if the secret size is greater than the Mersenne prime, which is calculated by means of the security level. It is not necessary to define a security level for a re-construction. +The length of the shares is based on the security level. It is possible to pre-define a security level by `ctor` or the `SecurityLevel` property. The pre-defined security level will be overriden, if the secret size is greater than the Mersenne prime, which is calculated by means of the security level. It is not necessary to define a security level for a re-construction. + +## Using the SecretSharingDotNet library with DI in a .NET project. +This guide will demonstrate how to use the SecretSharingDotNet library with Dependency Injection (DI) in a .NET project. + +Firstly, add the following dependencies: +```csharp +using Microsoft.Extensions.DependencyInjection; +using SecretSharingDotNet.Cryptography; +using SecretSharingDotNet.Math; +using System.Numerics; +``` +Next, initialize a `ServiceCollection` instance and add dependencies to the DI container: +```csharp +var serviceCollection = new ServiceCollection(); +serviceCollection.AddSingleton, ExtendedEuclideanAlgorithm>(); +serviceCollection.AddSingleton, ShamirsSecretSharing>(); +serviceCollection.AddSingleton, ShamirsSecretSharing>(); +using var serviceProvider = serviceCollection.BuildServiceProvider(); +``` +In the code above, the `ServiceCollection` registers an implementation for each of the main components of the SecretSharingDotNet library. + +Next, create an instance of the `IMakeSharesUseCase`: +```csharp +var makeSharesUseCase = serviceProvider.GetRequiredService>(); +``` +Using this instance, it is possible to create shares from a secret: +```csharp +var shares = makeSharesUseCase.MakeShares(3, 7, "Hello!"); +Console.WriteLine(shares); +``` +Similarly, an instance of `IReconstructionUseCase` can be created to rebuild the original secret: +```csharp +var reconstructionUseCase = serviceProvider.GetRequiredService>(); +var reconstruction = reconstructionUseCase.Reconstruction(shares.Where(p => p.X.IsEven).ToArray()); +Console.WriteLine(reconstruction); +``` + +The code above reconstructs the original secret from the shares, and then outputs it. ## Random secret Create a random secret in conjunction with the generation of shares. The length of the generated shares and of the secret are based on the security level. Here is an example with a pre-defined security level of 127: @@ -409,7 +441,7 @@ You can find the Mono installation instructions [here](https://www.mono-project. The .NET Frameworks 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 and 4.8.1 can be found [here](https://dotnet.microsoft.com/download/dotnet-framework). -The .NET SDKs 6.0, 7.0 and 8.0 can be found [here](https://dotnet.microsoft.com/download/dotnet). +The .NET SDKs 8.0 and 9.0 can be found [here](https://dotnet.microsoft.com/download/dotnet). ## Build and test the solution You can use the `SecretSharingDotNet.sln` solution file with the `dotnet` command to build the [SecretSharingDotNet](#secretsharingdotnet) library in the `Debug` or `Release` configuration. You can also use the `dotnet` command to start the unit tests. diff --git a/src/Cryptography/FinitePoint`1.cs b/src/Cryptography/FinitePoint`1.cs index de6a39c..354cfe1 100644 --- a/src/Cryptography/FinitePoint`1.cs +++ b/src/Cryptography/FinitePoint`1.cs @@ -154,45 +154,45 @@ 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; /// public int CompareTo(FinitePoint other) { - return ((this.X.Pow(2) + this.Y.Pow(2)).Sqrt - (other.X.Pow(2) + other.Y.Pow(2)).Sqrt).Sign; + return ((this.X.Pow(2) + this.Y.Pow(2)).Sqrt() - (other.X.Pow(2) + other.Y.Pow(2)).Sqrt()).Sign; } /// @@ -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..21c6efc --- /dev/null +++ b/src/Cryptography/IReconstructionUseCase.cs @@ -0,0 +1,38 @@ +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..f89d561 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. @@ -307,7 +307,7 @@ public bool Equals(Secret other) /// /// Converts the value of structure to its equivalent representation - /// that is unicode encoded. + /// that is Unicode encoded. /// /// representation of public override string ToString() 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..0820e97 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++) @@ -82,28 +82,28 @@ public override bool Equals(Calculator other) /// /// right-hand operand /// This method returns if this instance is greater than the instance, otherwise. - public override bool GreaterThan(BigInteger right) => this.Value > right; + protected override bool GreaterThan(BigInteger right) => this.Value > right; /// /// This method represents the Lower Than operator. /// /// right-hand operand /// This method returns if this instance is less than the instance, otherwise. - public override bool LowerThan(BigInteger right) => this.Value < right; + protected override bool LowerThan(BigInteger right) => this.Value < right; /// /// This method represents the Greater Than Or Equal To operator. /// /// right-hand operand /// This method returns if this instance is greater than or equal to the instance, otherwise. - public override bool EqualOrGreaterThan(BigInteger right) => this.Value >= right; + protected override bool EqualOrGreaterThan(BigInteger right) => this.Value >= right; /// /// This method represents the Lower Than Or Equal To operator. /// /// right-hand operand /// This method returns if this instance is less than or equal to the instance, otherwise. - public override bool EqualOrLowerThan(BigInteger right) => this.Value <= right; + protected override bool EqualOrLowerThan(BigInteger right) => this.Value <= right; /// /// Unable to convert the current instance of class to . @@ -127,7 +127,7 @@ public override int CompareTo(Calculator other) /// Right value to add (right summand). /// The sum of the current instance and the /// instance. - public override Calculator Add(BigInteger right) => this.Value + right; + protected override Calculator Add(BigInteger right) => this.Value + right; /// /// Subtracts the current instance with the @@ -136,7 +136,7 @@ public override int CompareTo(Calculator other) /// Right value to subtract (subtrahend). /// The difference of the current instance and the /// instance. - public override Calculator Subtract(BigInteger right) => this.Value - right; + protected override Calculator Subtract(BigInteger right) => this.Value - right; /// /// Multiplies the current instance with the @@ -145,7 +145,7 @@ public override int CompareTo(Calculator other) /// multiplicand /// The product of the current instance and the /// instance. - public override Calculator Multiply(BigInteger right) => this.Value * right; + protected override Calculator Multiply(BigInteger right) => this.Value * right; /// /// Divides the current instance with the @@ -154,26 +154,26 @@ public override int CompareTo(Calculator other) /// divisor /// The quotient of the current instance and the /// instance. - public override Calculator Divide(BigInteger right) => this.Value / right; + protected override Calculator Divide(BigInteger right) => this.Value / right; /// /// The modulo operation /// /// divisor /// The remainder as instance. - public override Calculator Modulo(BigInteger right) => this.Value % right; + protected override Calculator Modulo(BigInteger right) => this.Value % right; /// /// The unary increment method increments this instance by 1. /// /// This instance plus - public override Calculator Increment() => ++this.Clone().Value; + protected override Calculator Increment() => ++this.Clone().Value; /// /// The unary decrement method decrements this instance by 1. /// /// This instance minus - public override Calculator Decrement() => --this.Clone().Value; + protected override Calculator Decrement() => --this.Clone().Value; /// /// Returns the absolute value of the current object. @@ -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; @@ -225,30 +225,27 @@ public override int CompareTo(Calculator other) /// Returns the square root of the current object. /// /// NaN (value is lower than zero) - public override Calculator Sqrt + public override Calculator Sqrt() { - get + if (this.Value == BigInteger.Zero) { - if (this.Value == BigInteger.Zero) - { - return Zero; - } - - if (this.Value < BigInteger.Zero) - { - throw new ArithmeticException("NaN"); - } - - int bitLength = Convert.ToInt32(Math.Ceiling(BigInteger.Log(this.Value, 2))); - var root = BigInteger.One << (bitLength >> 1); - bool IsSqrt(BigInteger n, BigInteger r) => n >= r * r && n < (r + 1) * (r + 1); - while (!IsSqrt(this.Value, root)) - { - root = root + this.Value / root >> 1; - } - - return root; + return Zero; } + + if (this.Value < BigInteger.Zero) + { + throw new ArithmeticException("NaN"); + } + + int bitLength = Convert.ToInt32(Math.Ceiling(BigInteger.Log(this.Value, 2))); + var root = BigInteger.One << (bitLength >> 1); + bool IsSqrt(BigInteger n, BigInteger r) => n >= r * r && n < (r + 1) * (r + 1); + while (!IsSqrt(this.Value, root)) + { + root = root + this.Value / root >> 1; + } + + return root; } /// 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..a5a773a 100644 --- a/src/Math/Calculator`1.cs +++ b/src/Math/Calculator`1.cs @@ -39,7 +39,11 @@ namespace SecretSharingDotNet.Math; /// implementation from the concrete numeric data type like BigInteger. /// /// Numeric data type -public abstract class Calculator : Calculator, IEquatable>, IComparable, IComparable> +public abstract class Calculator : + Calculator, + IEquatable>, + IComparable, + IComparable> { /// /// Saves a dictionary of constructors of number data types derived from the class. @@ -59,7 +63,7 @@ public abstract class Calculator : Calculator, IEquatableRight value to add (right summand). /// The sum of the current instance and the /// instance. - public abstract Calculator Add(TNumber right); + protected abstract Calculator Add(TNumber right); /// /// Subtracts the current instance with the @@ -68,7 +72,7 @@ public abstract class Calculator : Calculator, IEquatableRight value to subtract (subtrahend). /// The difference of the current instance and the /// instance. - public abstract Calculator Subtract(TNumber right); + protected abstract Calculator Subtract(TNumber right); /// /// Multiplies the current instance with the @@ -77,7 +81,7 @@ public abstract class Calculator : Calculator, IEquatablemultiplicand /// The product of the current instance and the /// instance. - public abstract Calculator Multiply(TNumber right); + protected abstract Calculator Multiply(TNumber right); /// /// Divides the current instance with the @@ -86,26 +90,26 @@ public abstract class Calculator : Calculator, IEquatabledivisor /// The quotient of the current instance and the /// instance. - public abstract Calculator Divide(TNumber right); + protected abstract Calculator Divide(TNumber right); /// /// The modulo operation /// /// divisor /// The remainder - public abstract Calculator Modulo(TNumber right); + protected abstract Calculator Modulo(TNumber right); /// /// The unary increment method increments this instance by 1. /// /// This instance plus - public abstract Calculator Increment(); + protected abstract Calculator Increment(); /// /// The unary decrement method decrements this instance by 1. /// /// This instance minus - public abstract Calculator Decrement(); + protected abstract Calculator Decrement(); /// /// Returns the absolute value of the current object. @@ -123,35 +127,35 @@ public abstract class Calculator : Calculator, IEquatable /// Returns the square root of the current . /// - public abstract Calculator Sqrt { get; } + public abstract Calculator Sqrt(); /// /// This method represents the Greater Than operator. /// /// right-hand operand /// This method returns if this instance is greater than the instance, otherwise. - public abstract bool GreaterThan(TNumber right); + protected abstract bool GreaterThan(TNumber right); /// /// This method represents the Greater Than Or Equal To operator. /// /// right-hand operand /// This method returns if this instance is greater than or equal to the instance, otherwise. - public abstract bool EqualOrGreaterThan(TNumber right); + protected abstract bool EqualOrGreaterThan(TNumber right); /// /// This method represents the Lower Than operator. /// /// right-hand operand /// This method returns if this instance is less than the instance, otherwise. - public abstract bool LowerThan(TNumber right); + protected abstract bool LowerThan(TNumber right); /// /// This method represents the Lower Than Or Equal To operator. /// /// right-hand operand /// This method returns if this instance is less than or equal to the instance, otherwise. - public abstract bool EqualOrLowerThan(TNumber right); + protected abstract bool EqualOrLowerThan(TNumber right); /// /// Greater than operator @@ -159,7 +163,7 @@ public abstract class Calculator : Calculator, IEquatableThe 1st operand /// The 2nd operand /// Returns if its 1st operand is greater than its 2nd operand, otherwise . - public static bool operator >(Calculator left, Calculator right) => !(left is null) && !(right is null) && left.GreaterThan(right.Value); + public static bool operator >(Calculator left, Calculator right) => left is not null && right is not null && left.GreaterThan(right.Value); /// /// Less than operator @@ -167,7 +171,7 @@ public abstract class Calculator : Calculator, IEquatableThe 1st operand /// The 2nd operand /// Returns if its 1st operand is less than its 2nd operand, otherwise . - public static bool operator <(Calculator left, Calculator right) => !(left is null) && !(right is null) && left.LowerThan(right.Value); + public static bool operator <(Calculator left, Calculator right) => left is not null && right is not null && left.LowerThan(right.Value); /// /// Greater than or equal operator @@ -175,7 +179,7 @@ public abstract class Calculator : Calculator, IEquatableThe 1st operand /// The 2nd operand /// Returns if its 1st operand is greater than or equal to its 2nd operand, otherwise . - public static bool operator >=(Calculator left, Calculator right) => !(left is null) && !(right is null) && left.EqualOrGreaterThan(right.Value); + public static bool operator >=(Calculator left, Calculator right) => left is not null && right is not null && left.EqualOrGreaterThan(right.Value); /// /// Less than or equal operator @@ -183,7 +187,7 @@ public abstract class Calculator : Calculator, IEquatableThe 1st operand /// The 2nd operand /// Returns if its 1st operand is less than or equal to its 2nd operand, otherwise . - public static bool operator <=(Calculator left, Calculator right) => !(left is null) && !(right is null) && left.EqualOrLowerThan(right.Value); + public static bool operator <=(Calculator left, Calculator right) => left is not null && right is not null && left.EqualOrLowerThan(right.Value); /// /// Addition operation @@ -191,7 +195,7 @@ public abstract class Calculator : Calculator, IEquatableThe 1st summand /// The 2nd summand /// The sum - public static Calculator operator +(Calculator left, Calculator right) => !(right is null) ? left?.Add(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); + public static Calculator operator +(Calculator left, Calculator right) => right is not null ? left?.Add(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); /// /// Subtraction operation @@ -199,7 +203,7 @@ public abstract class Calculator : Calculator, IEquatableThe minuend /// The subtrahend /// The difference - public static Calculator operator -(Calculator left, Calculator right) => !(right is null) ? left?.Subtract(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); + public static Calculator operator -(Calculator left, Calculator right) => right is not null ? left?.Subtract(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); /// /// Multiplication operation @@ -207,7 +211,7 @@ public abstract class Calculator : Calculator, IEquatablemultiplier /// multiplicand /// The product - public static Calculator operator *(Calculator left, Calculator right) => !(right is null) ? left?.Multiply(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); + public static Calculator operator *(Calculator left, Calculator right) => right is not null ? left?.Multiply(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); /// /// Divide operation @@ -215,7 +219,7 @@ public abstract class Calculator : Calculator, IEquatabledividend /// divisor /// The quotient - public static Calculator operator /(Calculator left, Calculator right) => !(right is null) ? left?.Divide(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); + public static Calculator operator /(Calculator left, Calculator right) => right is not null ? left?.Divide(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); /// /// Modulo operation @@ -223,7 +227,7 @@ public abstract class Calculator : Calculator, IEquatabledividend /// divisor /// The remainder - public static Calculator operator %(Calculator left, Calculator right) => !(right is null) ? left?.Modulo(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); + public static Calculator operator %(Calculator left, Calculator right) => right is not null ? left?.Modulo(right.Value) ?? throw new ArgumentNullException(nameof(left)) : throw new ArgumentNullException(nameof(right)); /// /// Increment operator @@ -275,7 +279,7 @@ public abstract class Calculator : Calculator, IEquatable instance to an instance. /// /// A data type from basic class . - public static implicit operator TNumber(Calculator calculatorInstance) => !(calculatorInstance is null) ? calculatorInstance.Value : default; + public static implicit operator TNumber(Calculator calculatorInstance) => calculatorInstance is not null ? calculatorInstance.Value : default; /// /// Casts the instance to an instance. @@ -336,15 +340,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/ExtendedGcdResult.cs b/src/Math/ExtendedGcdResult.cs index a4d15b6..f98e6c5 100644 --- a/src/Math/ExtendedGcdResult.cs +++ b/src/Math/ExtendedGcdResult.cs @@ -96,7 +96,7 @@ public bool Equals(ExtendedGcdResult other) => this.Quotients.SequenceE /// if the specified object is equal to the current structure; otherwise . public override bool Equals(object obj) { - return obj != null && this.Equals((ExtendedGcdResult)obj); + return obj is ExtendedGcdResult other && this.Equals(other); } /// diff --git a/src/Math/IExtendedGcdAlgorithm`2.cs b/src/Math/IExtendedGcdAlgorithm`2.cs index 4d6da77..d8346ab 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) @@ -40,10 +40,10 @@ namespace SecretSharingDotNet.Math; public interface IExtendedGcdAlgorithm where TExtendedGcdResult : struct, IExtendedGcdResult { /// - /// Computes, in addition to the greatest common divisor of integers and , also the coefficients of Bézout's identity. + /// Computes, in addition to the greatest common divisor of elements of type and , also the coefficients of Bézout's identity. /// - /// An integer - /// An integer + /// An element of type + /// An element of type /// For details: [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "a")] [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "b")] diff --git a/src/Properties/AssemblyInfo.cs b/src/Properties/AssemblyInfo.cs index 23ebac5..f581d62 100644 --- a/src/Properties/AssemblyInfo.cs +++ b/src/Properties/AssemblyInfo.cs @@ -15,8 +15,8 @@ [assembly: Guid("1c21b99c-2de4-4ca5-b4ce-bc95cf89369e")] -[assembly: AssemblyVersion("0.11.0")] -[assembly: AssemblyFileVersion("0.11.0")] +[assembly: AssemblyVersion("0.12.0")] +[assembly: AssemblyFileVersion("0.12.0")] [assembly: NeutralResourcesLanguage("en")] [assembly: System.CLSCompliant(true)] diff --git a/src/SecretSharingDotNet.csproj b/src/SecretSharingDotNet.csproj index a196c0b..ee9ab1e 100644 --- a/src/SecretSharingDotNet.csproj +++ b/src/SecretSharingDotNet.csproj @@ -5,20 +5,20 @@ SecretSharingDotNet Library latest - netstandard2.0;netstandard2.1;net462;net47;net471;net472;net48;net481;net6.0;net7.0;net8.0 + netstandard2.0;netstandard2.1;net462;net47;net471;net472;net48;net481;net8.0;net9.0 SecretSharingDotNet.snk True false SecretSharingDotNet MIT - Changelog: https://github.com/shinji-san/SecretSharingDotNet/blob/v0.11.0/CHANGELOG.md + Changelog: https://github.com/shinji-san/SecretSharingDotNet/blob/v0.12.0/CHANGELOG.md An C# implementation of Shamir's Secret Sharing README.md secret sharing;shamir secret sharing;cryptography https://github.com/shinji-san/SecretSharingDotNet https://github.com/shinji-san/SecretSharingDotNet git - 0.11.0 + 0.12.0 Sebastian Walther Private Person true diff --git a/tests/SecretSharingDotNetTest.csproj b/tests/SecretSharingDotNetTest.csproj index 7047b06..f32a2f9 100644 --- a/tests/SecretSharingDotNetTest.csproj +++ b/tests/SecretSharingDotNetTest.csproj @@ -2,7 +2,7 @@ latest - net462;net47;net471;net472;net48;net481;net6.0;net7.0;net8.0 + net462;net47;net471;net472;net48;net481;net8.0;net9.0 ..\src\SecretSharingDotNet.snk True false @@ -11,10 +11,10 @@ - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all