Skip to content

Commit

Permalink
Clear Memory
Browse files Browse the repository at this point in the history
Resolves: No entry
  • Loading branch information
shinji-san committed Mar 7, 2024
1 parent 5d76a79 commit 5226b7c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/Cryptography/ShamirsSecretSharing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace SecretSharingDotNet.Cryptography;
/// </summary>
public abstract class ShamirsSecretSharing
{
/// <summary>
/// The minimum number of shares required to reconstruct the secret
/// </summary>
protected const int MinimumShareLimit = 2;

/// <summary>
/// Saves the known security levels (Mersenne prime exponents)
/// </summary>
Expand Down
36 changes: 28 additions & 8 deletions src/Cryptography/ShamirsSecretSharing`3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class ShamirsSecretSharing<TNumber, TExtendedGcdAlgorithm, TExtendedGcdRe
public ShamirsSecretSharing(TExtendedGcdAlgorithm extendedGcd)
{
this.extendedGcd = extendedGcd ?? throw new ArgumentNullException(nameof(extendedGcd));
this.SecurityLevel = 13;
this.SecurityLevel = SecurityLevels[0];
}

/// <summary>
Expand All @@ -84,7 +84,7 @@ public int SecurityLevel

set
{
if (value < 13)
if (value < SecurityLevels[0])
{
throw new ArgumentOutOfRangeException(nameof(value), value, ErrorMessages.MinimumSecurityLevelExceeded);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ public Shares<TNumber> MakeShares(TNumber numberOfMinimumShares, TNumber numberO

int min = ((Calculator<TNumber>)numberOfMinimumShares).ToInt32();
int max = ((Calculator<TNumber>)numberOfShares).ToInt32();
if (min < 2)
if (min < MinimumShareLimit)
{
throw new ArgumentOutOfRangeException(nameof(numberOfMinimumShares), numberOfMinimumShares, ErrorMessages.MinNumberOfSharesLowerThanTwo);
}
Expand Down Expand Up @@ -187,7 +187,7 @@ public Shares<TNumber> MakeShares(TNumber numberOfMinimumShares, TNumber numberO
{
int min = ((Calculator<TNumber>)numberOfMinimumShares).ToInt32();
int max = ((Calculator<TNumber>)numberOfShares).ToInt32();
if (min < 2)
if (min < MinimumShareLimit)
{
throw new ArgumentOutOfRangeException(nameof(numberOfMinimumShares), numberOfMinimumShares, ErrorMessages.MinNumberOfSharesLowerThanTwo);
}
Expand Down Expand Up @@ -215,20 +215,40 @@ public Shares<TNumber> MakeShares(TNumber numberOfMinimumShares, TNumber numberO
/// </summary>
/// <param name="numberOfMinimumShares">Minimum number of shared secrets for reconstruction</param>
/// <returns></returns>
#if NET6_0_OR_GREATER
private unsafe Calculator<TNumber>[] CreatePolynomial(int numberOfMinimumShares)
#else
private Calculator<TNumber>[] CreatePolynomial(int numberOfMinimumShares)
#endif
{
var polynomial = new Calculator<TNumber>[numberOfMinimumShares];
polynomial[0] = Calculator<TNumber>.Zero;
byte[] randomNumber = new byte[this.mersennePrime.ByteCount];
using (var rng = RandomNumberGenerator.Create())
#if NET6_0_OR_GREATER
fixed (byte* pointer = randomNumber)
{
var span = new Span<byte>(pointer, this.mersennePrime.ByteCount);
using var rng = RandomNumberGenerator.Create();
for (int i = 1; i < numberOfMinimumShares; i++)
{
rng.GetBytes(randomNumber);
polynomial[i] = (Calculator.Create(randomNumber, typeof(TNumber)) as Calculator<TNumber>)?.Abs() % this.mersennePrime;
rng.GetBytes(span);
polynomial[i] = (Calculator.Create(randomNumber, typeof(TNumber)) as Calculator<TNumber>)?.Abs() %
this.mersennePrime;
}
}

span.Clear();
}
#else
using var rng = RandomNumberGenerator.Create();
for (int i = 1; i < numberOfMinimumShares; i++)
{
rng.GetBytes(randomNumber);
polynomial[i] = (Calculator.Create(randomNumber, typeof(TNumber)) as Calculator<TNumber>)?.Abs() %
this.mersennePrime;
}

Array.Clear(randomNumber, 0, randomNumber.Length);
#endif
return polynomial;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Cryptography/SharedSeparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ internal static class SharedSeparator
/// <summary>
/// Separator array for <see cref="string.Split(char[])"/> method usage to avoid allocation of a new array.
/// </summary>
internal static readonly char[] CoordinateSeparatorArray = { CoordinateSeparator };
}
internal static readonly char[] CoordinateSeparatorArray = [CoordinateSeparator];
}
2 changes: 1 addition & 1 deletion src/Math/Calculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected static Dictionary<Type, Func<TParameter, TCalculator>> 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;
Expand Down
8 changes: 8 additions & 0 deletions src/SecretSharingDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Update="Resources\ErrorMessages.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand Down

0 comments on commit 5226b7c

Please sign in to comment.