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

Commit

Permalink
Remove Interface static (#11)
Browse files Browse the repository at this point in the history
* Remove Interface static

* Create math instance methods

* Fix name

* Remove statics from IMillerLoopDriver

* Avoid losing different public extensions

* Add AggressiveInlining

* Prevent to lose some public static methods
  • Loading branch information
shargon authored Jan 9, 2024
1 parent 3f0726b commit 144d8c7
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 27 deletions.
28 changes: 25 additions & 3 deletions src/Neo.Cryptography.BLS12_381/Bls12.Adder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using static Neo.Cryptography.BLS12_381.MillerLoopUtility;

namespace Neo.Cryptography.BLS12_381;
Expand Down Expand Up @@ -29,10 +30,31 @@ Fp12 IMillerLoopDriver<Fp12>.AdditionStep(in Fp12 f)
return Ell(in f, in coeffs, in P);
}

static Fp12 IMillerLoopDriver<Fp12>.SquareOutput(in Fp12 f) => f.Square();
#region IMillerLoopDriver<T>

static Fp12 IMillerLoopDriver<Fp12>.Conjugate(in Fp12 f) => f.Conjugate();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fp12 Square(in Fp12 f) => f.Square();

static Fp12 IMillerLoopDriver<Fp12>.One => Fp12.One;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fp12 Conjugate(in Fp12 f) => f.Conjugate();

public static Fp12 One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Fp12.One;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Fp12 IMillerLoopDriver<Fp12>.Square(in Fp12 f) => Adder.Square(f);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Fp12 IMillerLoopDriver<Fp12>.Conjugate(in Fp12 f) => Adder.Conjugate(f);
Fp12 IMillerLoopDriver<Fp12>.One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Adder.One;
}

#endregion
}
}
10 changes: 9 additions & 1 deletion src/Neo.Cryptography.BLS12_381/Fp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Neo.Cryptography.BLS12_381;

private static readonly Fp _zero = new();

static int INumber<Fp>.Size => Size;
public static ref readonly Fp Zero => ref _zero;
public static ref readonly Fp One => ref R;

Expand Down Expand Up @@ -465,4 +464,13 @@ public Fp Square()

return MontgomeryReduce(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11);
}

#region Instance math methods

public Fp Negate() => -this;
public Fp Multiply(in Fp value) => this * value;
public Fp Sum(in Fp value) => this + value;
public Fp Subtract(in Fp value) => this - value;

#endregion
}
10 changes: 9 additions & 1 deletion src/Neo.Cryptography.BLS12_381/Fp12.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Neo.Cryptography.BLS12_381;
private static readonly Fp12 _zero = new();
private static readonly Fp12 _one = new(in Fp6.One);

static int INumber<Fp12>.Size => Size;
public static ref readonly Fp12 Zero => ref _zero;
public static ref readonly Fp12 One => ref _one;

Expand Down Expand Up @@ -196,4 +195,13 @@ public Fp12 Invert()

return new Fp12(in c0, in c1);
}

#region Instance math methods

public Fp12 Negate() => -this;
public Fp12 Multiply(in Fp12 value) => this * value;
public Fp12 Sum(in Fp12 value) => this + value;
public Fp12 Subtract(in Fp12 value) => this - value;

#endregion
}
10 changes: 9 additions & 1 deletion src/Neo.Cryptography.BLS12_381/Fp2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace Neo.Cryptography.BLS12_381;
private static readonly Fp2 _zero = new();
private static readonly Fp2 _one = new(in Fp.One);

static int INumber<Fp2>.Size => Size;
public static ref readonly Fp2 Zero => ref _zero;
public static ref readonly Fp2 One => ref _one;

Expand Down Expand Up @@ -252,4 +251,13 @@ public bool TryInvert(out Fp2 result)
result = new Fp2(C0 * t, C1 * -t);
return s;
}

#region Instance math methods

public Fp2 Negate() => -this;
public Fp2 Multiply(in Fp2 value) => this * value;
public Fp2 Sum(in Fp2 value) => this + value;
public Fp2 Subtract(in Fp2 value) => this - value;

#endregion
}
10 changes: 9 additions & 1 deletion src/Neo.Cryptography.BLS12_381/Fp6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace Neo.Cryptography.BLS12_381;
private static readonly Fp6 _zero = new();
private static readonly Fp6 _one = new(in Fp2.One);

static int INumber<Fp6>.Size => Size;
public static ref readonly Fp6 Zero => ref _zero;
public static ref readonly Fp6 One => ref _one;

Expand Down Expand Up @@ -286,4 +285,13 @@ public Fp6 Invert()
))
);
}

#region Instance math methods

public Fp6 Negate() => -this;
public Fp6 Multiply(in Fp6 value) => this * value;
public Fp6 Sum(in Fp6 value) => this + value;
public Fp6 Subtract(in Fp6 value) => this - value;

#endregion
}
29 changes: 26 additions & 3 deletions src/Neo.Cryptography.BLS12_381/G2Prepared.Adder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using static Neo.Cryptography.BLS12_381.MillerLoopUtility;

namespace Neo.Cryptography.BLS12_381;
Expand Down Expand Up @@ -31,10 +32,32 @@ public Adder(in G2Affine q)
return null;
}

static object? IMillerLoopDriver<object?>.SquareOutput(in object? f) => null;
#region IMillerLoopDriver<T>

static object? IMillerLoopDriver<object?>.Conjugate(in object? f) => null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object? Square(in object? f) => null;

static object? IMillerLoopDriver<object?>.One => null;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static object? Conjugate(in object? f) => null;

public static object? One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => null;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
object? IMillerLoopDriver<object?>.Square(in object? f) => null;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
object? IMillerLoopDriver<object?>.Conjugate(in object? f) => null;

object? IMillerLoopDriver<object?>.One
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => null;
}

#endregion
}
}
6 changes: 3 additions & 3 deletions src/Neo.Cryptography.BLS12_381/IMillerLoopDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface IMillerLoopDriver<T>
{
public T DoublingStep(in T f);
public T AdditionStep(in T f);
public static abstract T SquareOutput(in T f);
public static abstract T Conjugate(in T f);
public static abstract T One { get; }
public T Square(in T f);
public T Conjugate(in T f);
public T One { get; }
}
42 changes: 32 additions & 10 deletions src/Neo.Cryptography.BLS12_381/INumber.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
using System.Runtime.CompilerServices;

namespace Neo.Cryptography.BLS12_381;

interface INumber<T> where T : unmanaged, INumber<T>
{
static abstract int Size { get; }
static abstract ref readonly T Zero { get; }
static abstract ref readonly T One { get; }
//static abstract int Size { get; }
//static abstract ref readonly T Zero { get; }
//static abstract ref readonly T One { get; }

//static abstract T operator -(in T x);
//static abstract T operator +(in T x, in T y);
//static abstract T operator -(in T x, in T y);
//static abstract T operator *(in T x, in T y);

static abstract T operator -(in T x);
static abstract T operator +(in T x, in T y);
static abstract T operator -(in T x, in T y);
static abstract T operator *(in T x, in T y);
T Negate();
T Sum(in T value);
T Subtract(in T value);
T Multiply(in T value);

abstract T Square();
}

static class NumberExtensions
{
public static T PowVartime<T>(this T self, ulong[] by) where T : unmanaged, INumber<T>
private static T PowVartime<T>(T one, T self, ulong[] by) where T : unmanaged, INumber<T>
{
// Although this is labeled "vartime", it is only
// variable time with respect to the exponent.
var res = T.One;
var res = one;
for (int j = by.Length - 1; j >= 0; j--)
{
for (int i = 63; i >= 0; i--)
{
res = res.Square();
if (((by[j] >> i) & 1) == 1)
{
res *= self;
res = res.Multiply(self);
}
}
}
return res;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fp PowVartime(this Fp self, ulong[] by) => PowVartime(Fp.One, self, by);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fp2 PowVartime(this Fp2 self, ulong[] by) => PowVartime(Fp2.One, self, by);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fp6 PowVartime(this Fp6 self, ulong[] by) => PowVartime(Fp6.One, self, by);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fp12 PowVartime(this Fp12 self, ulong[] by) => PowVartime(Fp12.One, self, by);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Scalar PowVartime(this Scalar self, ulong[] by) => PowVartime(Scalar.One, self, by);
}
6 changes: 3 additions & 3 deletions src/Neo.Cryptography.BLS12_381/MillerLoopUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ static class MillerLoopUtility
{
public static T MillerLoop<T, D>(D driver) where D : IMillerLoopDriver<T>
{
var f = D.One;
var f = driver.One;

var found_one = false;
foreach (var i in Enumerable.Range(0, 64).Reverse().Select(b => ((BLS_X >> 1 >> b) & 1) == 1))
Expand All @@ -22,13 +22,13 @@ public static T MillerLoop<T, D>(D driver) where D : IMillerLoopDriver<T>
if (i)
f = driver.AdditionStep(f);

f = D.SquareOutput(f);
f = driver.Square(f);
}

f = driver.DoublingStep(f);

if (BLS_X_IS_NEGATIVE)
f = D.Conjugate(f);
f = driver.Conjugate(f);

return f;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Neo.Cryptography.BLS12_381/Scalar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace Neo.Cryptography.BLS12_381;
public const int SizeL = Size / sizeof(ulong);
public static readonly Scalar Default = new();

static int INumber<Scalar>.Size => Size;
public static ref readonly Scalar Zero => ref Default;
public static ref readonly Scalar One => ref R;

Expand Down Expand Up @@ -491,4 +490,13 @@ private static Scalar MontgomeryReduce(ulong r0, ulong r1, ulong r2, ulong r3, u
ReadOnlySpan<ulong> tmp = stackalloc[] { d0 & mask, d1 & mask, d2 & mask, d3 & mask };
return MemoryMarshal.Cast<ulong, Scalar>(tmp)[0];
}

#region Instance math methods

public Scalar Negate() => -this;
public Scalar Multiply(in Scalar value) => this * value;
public Scalar Sum(in Scalar value) => this + value;
public Scalar Subtract(in Scalar value) => this - value;

#endregion
}

0 comments on commit 144d8c7

Please sign in to comment.