Skip to content

Commit

Permalink
[C#] use static constructor to call Ini
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Aug 2, 2022
1 parent d6ae51d commit 4e7800c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions ffi/cs/bls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ public class BLS
[DllImport(dllName)] public static extern int blsFastAggregateVerify(in Signature sig, in PublicKey pubVec, ulong n, [In]byte[] msg, ulong msgSize);
[DllImport(dllName)] public static extern int blsAggregateVerifyNoCheck(in Signature sig, in PublicKey pubVec, in Msg msgVec, ulong msgSize, ulong n);

// don't call this if isETH = true, it calls in BLS()
public static void Init(int curveType = BLS12_381) {
if (isETH && isInit) return;
if (isETH && curveType != BLS12_381) {
throw new PlatformNotSupportedException("bad curveType");
}
Expand All @@ -157,6 +159,15 @@ public class BLS
throw new ArgumentException("blsInit");
}
}
static readonly bool isInit;
// call at once
static BLS()
{
if (isETH) {
Init(BLS12_381);
isInit = true;
}
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Id
{
Expand Down
11 changes: 11 additions & 0 deletions ffi/cs/bls_eth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ public class BLS
[DllImport(dllName)] public static extern int blsFastAggregateVerify(in Signature sig, in PublicKey pubVec, ulong n, [In]byte[] msg, ulong msgSize);
[DllImport(dllName)] public static extern int blsAggregateVerifyNoCheck(in Signature sig, in PublicKey pubVec, in Msg msgVec, ulong msgSize, ulong n);

// don't call this if isETH = true, it calls in BLS()
public static void Init(int curveType = BLS12_381) {
if (isETH && isInit) return;
if (isETH && curveType != BLS12_381) {
throw new PlatformNotSupportedException("bad curveType");
}
Expand All @@ -157,6 +159,15 @@ public class BLS
throw new ArgumentException("blsInit");
}
}
static readonly bool isInit;
// call at once
static BLS()
{
if (isETH) {
Init(BLS12_381);
isInit = true;
}
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Id
{
Expand Down
4 changes: 3 additions & 1 deletion ffi/cs/bls_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,9 @@ static void TestAreAllMsgDifferent()
continue;
}
Console.WriteLine("curveType={0}", curveType);
Init(curveType);
if (!isETH) {
Init(curveType);
}
TestId();
TestSecretKey();
TestPublicKey();
Expand Down

0 comments on commit 4e7800c

Please sign in to comment.