Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

Commit

Permalink
Converted to dotnet standard 2.1 (#6)
Browse files Browse the repository at this point in the history
* Converted to dotnet standard 2.1

* increase version number

* Fixed for netstandard2.1

* Updated packages

* Remove ToLowerInvariant (always lowercase)

* Remove reflection

* Add comments

---------

Co-authored-by: Fernando Diaz Toledano <shargon@gmail.com>
  • Loading branch information
cschuchardt88 and shargon committed Jan 9, 2024
1 parent 144d8c7 commit 58e4e45
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/Neo.Cryptography.BLS12_381/Fp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public override int GetHashCode()

public byte[] ToArray()
{
byte[] result = GC.AllocateUninitializedArray<byte>(Size);
byte[] result = new byte[Size];
TryWrite(result);
return result;
}
Expand All @@ -141,7 +141,11 @@ public bool TryWrite(Span<byte> buffer)

public override string ToString()
{
return "0x" + Convert.ToHexString(ToArray()).ToLowerInvariant();
var output = string.Empty;
foreach (var b in ToArray())
output += b.ToString("x2");

return "0x" + output;
}

public bool LexicographicallyLargest()
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Cryptography.BLS12_381/Fp12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public override int GetHashCode()

public byte[] ToArray()
{
byte[] result = GC.AllocateUninitializedArray<byte>(Size);
byte[] result = new byte[Size];
TryWrite(result);
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Cryptography.BLS12_381/Fp2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public override int GetHashCode()

public byte[] ToArray()
{
byte[] result = GC.AllocateUninitializedArray<byte>(Size);
byte[] result = new byte[Size];
TryWrite(result);
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Cryptography.BLS12_381/Fp6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override int GetHashCode()

public byte[] ToArray()
{
byte[] result = GC.AllocateUninitializedArray<byte>(Size);
byte[] result = new byte[Size];
TryWrite(result);
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.Cryptography.BLS12_381/G1Affine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public byte[] ToCompressed()

public byte[] ToUncompressed()
{
byte[] res = GC.AllocateUninitializedArray<byte>(96);
byte[] res = new byte[96];

ConditionalSelect(in X, in Fp.Zero, Infinity).TryWrite(res.AsSpan(0..48));
ConditionalSelect(in Y, in Fp.Zero, Infinity).TryWrite(res.AsSpan(48..96));
Expand Down
4 changes: 2 additions & 2 deletions src/Neo.Cryptography.BLS12_381/G2Affine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public byte[] ToCompressed()
// to guard against implementation mistakes we do not assume this.
var x = ConditionalSelect(in X, in Fp2.Zero, Infinity);

var res = GC.AllocateUninitializedArray<byte>(96);
var res = new byte[96];

x.C1.TryWrite(res.AsSpan(0..48));
x.C0.TryWrite(res.AsSpan(48..96));
Expand All @@ -124,7 +124,7 @@ public byte[] ToCompressed()

public byte[] ToUncompressed()
{
var res = GC.AllocateUninitializedArray<byte>(192);
var res = new byte[192];

var x = ConditionalSelect(in X, in Fp2.Zero, Infinity);
var y = ConditionalSelect(in Y, in Fp2.Zero, Infinity);
Expand Down
30 changes: 29 additions & 1 deletion src/Neo.Cryptography.BLS12_381/MathUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,39 @@ public static (ulong result, ulong borrow) Sbb(ulong a, ulong b, ulong borrow)

public static (ulong low, ulong high) Mac(ulong z, ulong x, ulong y, ulong carry)
{
ulong high = Math.BigMul(x, y, out ulong low);
ulong high = BigMul(x, y, out ulong low);
(low, carry) = Adc(low, carry, 0);
(high, _) = Adc(high, 0, carry);
(low, carry) = Adc(low, z, 0);
(high, _) = Adc(high, 0, carry);
return (low, high);
}

/// <summary>Produces the full product of two unsigned 64-bit numbers.</summary>
/// <param name="a">The first number to multiply.</param>
/// <param name="b">The second number to multiply.</param>
/// <param name="low">The low 64-bit of the product of the specified numbers.</param>
/// <returns>The high 64-bit of the product of the specified numbers.</returns>
public static ulong BigMul(ulong a, ulong b, out ulong low)
{
// Adaptation of algorithm for multiplication
// of 32-bit unsigned integers described
// in Hacker's Delight by Henry S. Warren, Jr. (ISBN 0-201-91465-4), Chapter 8
// Basically, it's an optimized version of FOIL method applied to
// low and high dwords of each operand

// Use 32-bit uints to optimize the fallback for 32-bit platforms.
uint al = (uint)a;
uint ah = (uint)(a >> 32);
uint bl = (uint)b;
uint bh = (uint)(b >> 32);

ulong mull = ((ulong)al) * bl;
ulong t = ((ulong)ah) * bl + (mull >> 32);
ulong tl = ((ulong)al) * bh + (uint)t;

low = tl << 32 | (uint)mull;

return ((ulong)ah) * bh + (t >> 32) + (tl >> 32);
}
}
11 changes: 8 additions & 3 deletions src/Neo.Cryptography.BLS12_381/Neo.Cryptography.BLS12_381.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>0.2.0</VersionPrefix>
<TargetFramework>net7.0</TargetFramework>
<VersionPrefix>0.3.0</VersionPrefix>
<TargetFrameworks>netstandard2.1;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Nullable>enable</Nullable>
<LangVersion>11.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/Neo.Cryptography.BLS12_381/Scalar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal Scalar(ulong[] values)
// This internal method is only used by the constants classes.
// The data must be in the correct format.
// So, there is no need to do any additional checks.
this = MemoryMarshal.AsRef<Scalar>(MemoryMarshal.Cast<ulong, byte>(values));
this = Unsafe.As<byte, Scalar>(ref MemoryMarshal.GetReference(MemoryMarshal.Cast<ulong, byte>(values)));
}

public Scalar(ulong value)
Expand All @@ -51,7 +51,7 @@ public static Scalar FromBytes(ReadOnlySpan<byte> data)
if (data.Length != Size)
throw new FormatException($"The argument `{nameof(data)}` must contain {Size} bytes.");

ref readonly Scalar ref_ = ref MemoryMarshal.AsRef<Scalar>(data);
ref readonly Scalar ref_ = ref Unsafe.As<byte, Scalar>(ref MemoryMarshal.GetReference(data));

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 58e4e45

Please sign in to comment.