Skip to content

Commit

Permalink
Changed BouncyCastle to PortableBouncyCastle (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx authored Jun 23, 2024
1 parent 2cefd0e commit e6c30a0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion dkgLibrary/dkgLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.3.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
</ItemGroup>

</Project>
15 changes: 8 additions & 7 deletions dkgLibrary/group/Sec256k1Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public BigInteger GetValue()
{
return _value;
}
public int GetLength()
public static int GetLength()
{
return _length;
}
Expand Down Expand Up @@ -153,15 +153,16 @@ public IScalar Mul(IScalar s2)
if (s2 is not Secp256k1Scalar other2)
throw new InvalidCastException("s2 is not a Secp256k1Scalar");

return new Secp256k1Scalar(_value.ModMultiply(other2._value, _order));
return new Secp256k1Scalar(_value.Multiply(other2._value).Mod(_order));
}

public IScalar Div(IScalar s2)
{
if (s2 is not Secp256k1Scalar other2)
throw new InvalidCastException("s2 is not a Secp256k1Scalar");

return new Secp256k1Scalar(_value.ModDivide(other2._value, _order));
BigInteger inverse = other2._value.ModInverse(_order);
return new Secp256k1Scalar(_value.Multiply(inverse).Mod(_order));
}

public IScalar Inv()
Expand Down Expand Up @@ -224,7 +225,7 @@ public class Secp256k1Point : IPoint, IEquatable<Secp256k1Point>
{
public ECPoint _point;
private static readonly X9ECParameters _ecP = ECNamedCurveTable.GetByName("secp256k1");
private static readonly ECCurve _curve = _ecP.Curve;
// private static readonly ECCurve _curve = _ecP.Curve;

public Secp256k1Point()
{
Expand Down Expand Up @@ -408,7 +409,7 @@ public byte[] ExtractData()
public class Secp256k1Group : IGroup, IEquatable<Secp256k1Group>
{
private static readonly X9ECParameters _ecP = ECNamedCurveTable.GetByName("secp256k1");
private static readonly Org.BouncyCastle.Math.EC.ECCurve _curve = _ecP.Curve;
// JFYI private static readonly Org.BouncyCastle.Math.EC.ECCurve _curve = _ecP.Curve;

private readonly RandomStream _strm = new();
public override bool Equals(object? obj)
Expand All @@ -435,7 +436,7 @@ public override string ToString()
}
public static int SScalarLen()
{
return new Secp256k1Scalar().GetLength();
return Secp256k1Scalar.GetLength();
}
public int ScalarLen()
{
Expand Down Expand Up @@ -485,7 +486,7 @@ public IPoint EmbedData(byte[] message)
// Check if the message is null or empty
if (message == null || message.Length == 0)
{
throw new ArgumentException(nameof(message), "EmbedMessage: Message cannot be null or empty");
throw new ArgumentException("EmbedMessage: Message cannot be null or empty", nameof(message));
}

int dataLength = Math.Min(message.Length, EmbedLen());
Expand Down
2 changes: 1 addition & 1 deletion dkgLibraryTests/dkgLibraryTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.Cryptography" Version="2.3.0" />
<PackageReference Include="coverlet.msbuild" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -20,6 +19,7 @@
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 5 additions & 5 deletions dkgLibraryTests/testsSec256k1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void TestGetSetBytes()
byte[] bytes = _scalar.GetBytes();
Secp256k1Scalar scalar2 = new();
scalar2.SetBytes(bytes);
Assert.That(_scalar.Equals(scalar2));
Assert.That(_scalar, Is.EqualTo(scalar2));
}

[Test]
Expand All @@ -172,7 +172,7 @@ public void TestMarshalUnmarshalBinary()
stream.Position = 0;
Secp256k1Scalar scalar2 = new();
scalar2.UnmarshalBinary(stream);
Assert.That(_scalar.Equals(scalar2));
Assert.That(_scalar, Is.EqualTo(scalar2));
}
}

Expand Down Expand Up @@ -266,7 +266,7 @@ public void TestGetSetBytes()
Secp256k1Point point2 = new();
point2.Pick(new RandomStream());
point2.SetBytes(bytes);
Assert.That(_point.Equals(point2));
Assert.That(_point, Is.EqualTo(point2));
}

[Test]
Expand All @@ -279,7 +279,7 @@ public void TestMarshalUnmarshalBinary()
stream.Position = 0;
Secp256k1Point point2 = new();
point2.UnmarshalBinary(stream);
Assert.That(_point.Equals(point2));
Assert.That(_point, Is.EqualTo(point2));
}
}

Expand Down Expand Up @@ -344,7 +344,7 @@ public void TestEmbedTooLong()
var embedded = _group.EmbedData(plainData);

var extractedData = embedded.ExtractData();
Assert.That(message.Substring(0, _group.EmbedLen()) , Is.EqualTo(Encoding.UTF8.GetString(extractedData)));
Assert.That(message[.._group.EmbedLen()] , Is.EqualTo(Encoding.UTF8.GetString(extractedData)));
}
}
}
5 changes: 3 additions & 2 deletions dkgSample/dkgSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.25.3" />
<PackageReference Include="Google.Protobuf" Version="3.27.1" />
<PackageReference Include="Grpc" Version="2.46.6" />
<PackageReference Include="Grpc.Tools" Version="2.62.0">
<PackageReference Include="Grpc.Tools" Version="2.64.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e6c30a0

Please sign in to comment.