Skip to content

Commit

Permalink
[C#] fix serialize size
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Jul 26, 2022
1 parent 55c1ce8 commit 6b0f6fe
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 46 deletions.
4 changes: 4 additions & 0 deletions ffi/cs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
all: bls_eth.cs

bls_eth.cs: bls.cs
sed 's/public const bool isETH = false;/public const bool isETH = true;/' < bls.cs > bls_eth.cs
55 changes: 32 additions & 23 deletions ffi/cs/bls.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
þ½Ž¿using System;
using System;
using System.Text;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -27,8 +27,9 @@ class BLS
public const int SIGNATURE_SERIALIZE_SIZE = SIGNATURE_UNIT_SIZE * 8;

public const string dllName = FP_UNIT_SIZE == 6 ? "bls384_256.dll" : "bls256.dll";
[DllImport(dllName)]
public static extern int blsInit(int curveType, int compiledTimeVar);
[DllImport(dllName)] public static extern int blsInit(int curveType, int compiledTimeVar);
[DllImport(dllName)] public static extern int blsGetFrByteSize();
[DllImport(dllName)] public static extern int blsGetG1ByteSize();

[DllImport(dllName)] public static extern void blsIdSetInt(ref Id id, int x);
[DllImport(dllName)] public static extern int blsIdSetDecStr(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
Expand Down Expand Up @@ -147,16 +148,18 @@ public unsafe struct Id
{
private fixed ulong v[ID_UNIT_SIZE];
public byte[] Serialize() {
byte[] buf = new byte[ID_SERIALIZE_SIZE];
ulong n = blsIdSerialize(buf, (ulong)buf.Length, this);
if (n == 0) {
ulong bufSize = (ulong)blsGetFrByteSize();
byte[] buf = new byte[bufSize];
ulong n = blsIdSerialize(buf, bufSize, this);
if (n != bufSize) {
throw new ArithmeticException("blsIdSerialize");
}
return buf;
}
public void Deserialize(byte[] buf) {
ulong n = blsIdDeserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
ulong bufSize = (ulong)buf.Length;
ulong n = blsIdDeserialize(ref this, buf, bufSize);
if (n == 0 || n != bufSize) {
throw new ArithmeticException("blsIdDeserialize");
}
}
Expand Down Expand Up @@ -198,16 +201,18 @@ public unsafe struct SecretKey
{
private fixed ulong v[SECRETKEY_UNIT_SIZE];
public byte[] Serialize() {
byte[] buf = new byte[SECRETKEY_SERIALIZE_SIZE];
ulong n = blsSecretKeySerialize(buf, (ulong)buf.Length, this);
if (n == 0) {
ulong bufSize = (ulong)blsGetFrByteSize();
byte[] buf = new byte[bufSize];
ulong n = blsSecretKeySerialize(buf, bufSize, this);
if (n != bufSize) {
throw new ArithmeticException("blsSecretKeySerialize");
}
return buf;
}
public void Deserialize(byte[] buf) {
ulong n = blsSecretKeyDeserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
ulong bufSize = (ulong)buf.Length;
ulong n = blsSecretKeyDeserialize(ref this, buf, bufSize);
if (n == 0 || n != bufSize) {
throw new ArithmeticException("blsSecretKeyDeserialize");
}
}
Expand Down Expand Up @@ -290,16 +295,18 @@ public unsafe struct PublicKey
{
private fixed ulong v[PUBLICKEY_UNIT_SIZE];
public byte[] Serialize() {
byte[] buf = new byte[PUBLICKEY_SERIALIZE_SIZE];
ulong n = blsPublicKeySerialize(buf, (ulong)buf.Length, this);
if (n == 0) {
ulong bufSize = (ulong)blsGetG1ByteSize() * (isETH ? 1 : 2);
byte[] buf = new byte[bufSize];
ulong n = blsPublicKeySerialize(buf, bufSize, this);
if (n != bufSize) {
throw new ArithmeticException("blsPublicKeySerialize");
}
return buf;
}
public void Deserialize(byte[] buf) {
ulong n = blsPublicKeyDeserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
ulong bufSize = (ulong)buf.Length;
ulong n = blsPublicKeyDeserialize(ref this, buf, bufSize);
if (n == 0 || n != bufSize) {
throw new ArithmeticException("blsPublicKeyDeserialize");
}
}
Expand Down Expand Up @@ -365,16 +372,18 @@ public unsafe struct Signature
{
private fixed ulong v[SIGNATURE_UNIT_SIZE];
public byte[] Serialize() {
byte[] buf = new byte[SIGNATURE_SERIALIZE_SIZE];
ulong n = blsSignatureSerialize(buf, (ulong)buf.Length, this);
if (n == 0) {
ulong bufSize = (ulong)blsGetG1ByteSize() * (isETH ? 2 : 1);
byte[] buf = new byte[bufSize];
ulong n = blsSignatureSerialize(buf, bufSize, this);
if (n != bufSize) {
throw new ArithmeticException("blsSignatureSerialize");
}
return buf;
}
public void Deserialize(byte[] buf) {
ulong n = blsSignatureDeserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
ulong bufSize = (ulong)buf.Length;
ulong n = blsSignatureDeserialize(ref this, buf, bufSize);
if (n == 0 || n != bufSize) {
throw new ArithmeticException("blsSignatureDeserialize");
}
}
Expand Down
55 changes: 32 additions & 23 deletions ffi/cs/bls_eth.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
þ½Ž¿using System;
using System;
using System.Text;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -27,8 +27,9 @@ class BLS
public const int SIGNATURE_SERIALIZE_SIZE = SIGNATURE_UNIT_SIZE * 8;

public const string dllName = FP_UNIT_SIZE == 6 ? "bls384_256.dll" : "bls256.dll";
[DllImport(dllName)]
public static extern int blsInit(int curveType, int compiledTimeVar);
[DllImport(dllName)] public static extern int blsInit(int curveType, int compiledTimeVar);
[DllImport(dllName)] public static extern int blsGetFrByteSize();
[DllImport(dllName)] public static extern int blsGetG1ByteSize();

[DllImport(dllName)] public static extern void blsIdSetInt(ref Id id, int x);
[DllImport(dllName)] public static extern int blsIdSetDecStr(ref Id id, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
Expand Down Expand Up @@ -147,16 +148,18 @@ public unsafe struct Id
{
private fixed ulong v[ID_UNIT_SIZE];
public byte[] Serialize() {
byte[] buf = new byte[ID_SERIALIZE_SIZE];
ulong n = blsIdSerialize(buf, (ulong)buf.Length, this);
if (n == 0) {
ulong bufSize = (ulong)blsGetFrByteSize();
byte[] buf = new byte[bufSize];
ulong n = blsIdSerialize(buf, bufSize, this);
if (n != bufSize) {
throw new ArithmeticException("blsIdSerialize");
}
return buf;
}
public void Deserialize(byte[] buf) {
ulong n = blsIdDeserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
ulong bufSize = (ulong)buf.Length;
ulong n = blsIdDeserialize(ref this, buf, bufSize);
if (n == 0 || n != bufSize) {
throw new ArithmeticException("blsIdDeserialize");
}
}
Expand Down Expand Up @@ -198,16 +201,18 @@ public unsafe struct SecretKey
{
private fixed ulong v[SECRETKEY_UNIT_SIZE];
public byte[] Serialize() {
byte[] buf = new byte[SECRETKEY_SERIALIZE_SIZE];
ulong n = blsSecretKeySerialize(buf, (ulong)buf.Length, this);
if (n == 0) {
ulong bufSize = (ulong)blsGetFrByteSize();
byte[] buf = new byte[bufSize];
ulong n = blsSecretKeySerialize(buf, bufSize, this);
if (n != bufSize) {
throw new ArithmeticException("blsSecretKeySerialize");
}
return buf;
}
public void Deserialize(byte[] buf) {
ulong n = blsSecretKeyDeserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
ulong bufSize = (ulong)buf.Length;
ulong n = blsSecretKeyDeserialize(ref this, buf, bufSize);
if (n == 0 || n != bufSize) {
throw new ArithmeticException("blsSecretKeyDeserialize");
}
}
Expand Down Expand Up @@ -290,16 +295,18 @@ public unsafe struct PublicKey
{
private fixed ulong v[PUBLICKEY_UNIT_SIZE];
public byte[] Serialize() {
byte[] buf = new byte[PUBLICKEY_SERIALIZE_SIZE];
ulong n = blsPublicKeySerialize(buf, (ulong)buf.Length, this);
if (n == 0) {
ulong bufSize = (ulong)blsGetG1ByteSize() * (isETH ? 1 : 2);
byte[] buf = new byte[bufSize];
ulong n = blsPublicKeySerialize(buf, bufSize, this);
if (n != bufSize) {
throw new ArithmeticException("blsPublicKeySerialize");
}
return buf;
}
public void Deserialize(byte[] buf) {
ulong n = blsPublicKeyDeserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
ulong bufSize = (ulong)buf.Length;
ulong n = blsPublicKeyDeserialize(ref this, buf, bufSize);
if (n == 0 || n != bufSize) {
throw new ArithmeticException("blsPublicKeyDeserialize");
}
}
Expand Down Expand Up @@ -365,16 +372,18 @@ public unsafe struct Signature
{
private fixed ulong v[SIGNATURE_UNIT_SIZE];
public byte[] Serialize() {
byte[] buf = new byte[SIGNATURE_SERIALIZE_SIZE];
ulong n = blsSignatureSerialize(buf, (ulong)buf.Length, this);
if (n == 0) {
ulong bufSize = (ulong)blsGetG1ByteSize() * (isETH ? 2 : 1);
byte[] buf = new byte[bufSize];
ulong n = blsSignatureSerialize(buf, bufSize, this);
if (n != bufSize) {
throw new ArithmeticException("blsSignatureSerialize");
}
return buf;
}
public void Deserialize(byte[] buf) {
ulong n = blsSignatureDeserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
ulong bufSize = (ulong)buf.Length;
ulong n = blsSignatureDeserialize(ref this, buf, bufSize);
if (n == 0 || n != bufSize) {
throw new ArithmeticException("blsSignatureDeserialize");
}
}
Expand Down

0 comments on commit 6b0f6fe

Please sign in to comment.