Skip to content

Commit

Permalink
add precompute lines miller loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchhill committed Dec 10, 2024
1 parent 512f799 commit c30d27b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Nethermind.Crypto.Bls/Bls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,11 @@ static private partial ERROR blst_pairing_mul_n_aggregate_pk_in_g1(Span<long> fp
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
static private partial IntPtr blst_p2_generator();

//void blst_precompute_lines(blst_fp6 Qlines[68], const blst_p2_affine *Q);
[LibraryImport(LibraryName)]
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
static private partial void blst_precompute_lines(Span<long> qlines, ReadOnlySpan<long> q);

// [LibraryImport(LibraryName)]
// [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
// static private partial ERROR blst_core_verify_pk_in_g1(ReadOnlySpan<long> pk, ReadOnlySpan<long> sig,
Expand Down Expand Up @@ -1040,6 +1045,9 @@ public readonly bool IsInf()
public readonly bool IsEqual(P2Affine p)
=> blst_p2_affine_is_equal(_point, p._point);

public readonly void PrecomputeLines(Span<long> qlines)
=> blst_precompute_lines(qlines, _point);

// readonly ERROR core_verify(P1Affine pk, bool hash_or_encode,
// #pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
// byte[] msg, string DST = "", byte[] aug = null)
Expand Down Expand Up @@ -1461,6 +1469,13 @@ static private partial void blst_fp12_mul(Span<long> ret, ReadOnlySpan<long> a,
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
static private partial void blst_bendian_from_fp12(Span<byte> ret, ReadOnlySpan<long> a);

// void blst_miller_loop_lines(blst_fp12 *ret, const blst_fp6 Qlines[68],
// const blst_p1_affine *P);

[LibraryImport(LibraryName)]
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
static private partial void blst_miller_loop_lines(Span<long> ret, ReadOnlySpan<long> qlines, ReadOnlySpan<long> p);

public readonly ref struct PT
{
public readonly ReadOnlySpan<long> Fp12 { get => _fp12; }
Expand Down Expand Up @@ -1542,6 +1557,9 @@ public unsafe static PT One(Span<long> p)
}
return new(p);
}

public void MillerLoopLines(ReadOnlySpan<long> qlines, P1Affine p)
{ blst_miller_loop_lines(_fp12, qlines, p.Point); }
}

[LibraryImport(LibraryName)]
Expand Down
20 changes: 20 additions & 0 deletions src/Nethermind.Crypto.Test/BlsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Nethermind.Crypto.Test;

using G1 = Bls.P1;
using G2 = Bls.P2;
using G1Affine = Bls.P1Affine;
using G2Affine = Bls.P2Affine;
using GT = Bls.PT;

public class BlsTests
Expand Down Expand Up @@ -177,6 +179,24 @@ public void NewPointIsInfinity()
});
}

[Test]
public void MillerLoopPrecomputeLinesTest()
{
var p = G1Affine.Generator();
var q = G2Affine.Generator();

Span<long> lines = new long[68 * 6 * 6];
q.PrecomputeLines(lines);

GT resultWithLines = new(new long[GT.Sz]);
resultWithLines.MillerLoopLines(lines, p);

GT resultNormal = new(new long[GT.Sz]);
resultNormal.MillerLoop(q, p);

Assert.That(GT.FinalVerify(resultWithLines, resultNormal));
}

private static G1 G1FromUntrimmed(in ReadOnlyMemory<byte> untrimmed)
{
byte[] trimmed = new byte[LenG1Trimmed];
Expand Down

0 comments on commit c30d27b

Please sign in to comment.