[C#] add Serialize/Deserialize

update-fork
MITSUNARI Shigeo 4 years ago
parent 0901e76cc2
commit ae670c82f7
  1. 405
      ffi/cs/mcl/mcl.cs
  2. 95
      ffi/cs/test/test.cs

@ -19,93 +19,93 @@ namespace mcl {
[DllImport(dllName)] public static extern void mclBnFr_clear(ref Fr x);
[DllImport(dllName)] public static extern void mclBnFr_setInt(ref Fr y, int x);
[DllImport(dllName)] public static extern int mclBnFr_setStr(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode);
[DllImport(dllName)] public static extern int mclBnFr_isValid(ref Fr x);
[DllImport(dllName)] public static extern int mclBnFr_isEqual(ref Fr x, ref Fr y);
[DllImport(dllName)] public static extern int mclBnFr_isZero(ref Fr x);
[DllImport(dllName)] public static extern int mclBnFr_isOne(ref Fr x);
[DllImport(dllName)] public static extern int mclBnFr_isValid(in Fr x);
[DllImport(dllName)] public static extern int mclBnFr_isEqual(in Fr x, in Fr y);
[DllImport(dllName)] public static extern int mclBnFr_isZero(in Fr x);
[DllImport(dllName)] public static extern int mclBnFr_isOne(in Fr x);
[DllImport(dllName)] public static extern void mclBnFr_setByCSPRNG(ref Fr x);
[DllImport(dllName)] public static extern int mclBnFr_setHashOf(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern int mclBnFr_getStr([Out] StringBuilder buf, long maxBufSize, ref Fr x, int ioMode);
[DllImport(dllName)] public static extern int mclBnFr_getStr([Out] StringBuilder buf, long maxBufSize, in Fr x, int ioMode);
[DllImport(dllName)] public static extern void mclBnFr_neg(ref Fr y, ref Fr x);
[DllImport(dllName)] public static extern void mclBnFr_inv(ref Fr y, ref Fr x);
[DllImport(dllName)] public static extern void mclBnFr_add(ref Fr z, ref Fr x, ref Fr y);
[DllImport(dllName)] public static extern void mclBnFr_sub(ref Fr z, ref Fr x, ref Fr y);
[DllImport(dllName)] public static extern void mclBnFr_mul(ref Fr z, ref Fr x, ref Fr y);
[DllImport(dllName)] public static extern void mclBnFr_div(ref Fr z, ref Fr x, ref Fr y);
[DllImport(dllName)] public static extern void mclBnFr_neg(ref Fr y, in Fr x);
[DllImport(dllName)] public static extern void mclBnFr_inv(ref Fr y, in Fr x);
[DllImport(dllName)] public static extern void mclBnFr_add(ref Fr z, in Fr x, in Fr y);
[DllImport(dllName)] public static extern void mclBnFr_sub(ref Fr z, in Fr x, in Fr y);
[DllImport(dllName)] public static extern void mclBnFr_mul(ref Fr z, in Fr x, in Fr y);
[DllImport(dllName)] public static extern void mclBnFr_div(ref Fr z, in Fr x, in Fr y);
[DllImport(dllName)] public static extern void mclBnFp_clear(ref Fp x);
[DllImport(dllName)] public static extern void mclBnFp_setInt(ref Fp y, int x);
[DllImport(dllName)] public static extern int mclBnFp_setStr(ref Fp x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode);
[DllImport(dllName)] public static extern int mclBnFp_isValid(ref Fp x);
[DllImport(dllName)] public static extern int mclBnFp_isEqual(ref Fp x, ref Fp y);
[DllImport(dllName)] public static extern int mclBnFp_isZero(ref Fp x);
[DllImport(dllName)] public static extern int mclBnFp_isOne(ref Fp x);
[DllImport(dllName)] public static extern int mclBnFp_isValid(in Fp x);
[DllImport(dllName)] public static extern int mclBnFp_isEqual(in Fp x, in Fp y);
[DllImport(dllName)] public static extern int mclBnFp_isZero(in Fp x);
[DllImport(dllName)] public static extern int mclBnFp_isOne(in Fp x);
[DllImport(dllName)] public static extern void mclBnFp_setByCSPRNG(ref Fp x);
[DllImport(dllName)] public static extern int mclBnFp_setHashOf(ref Fp x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern int mclBnFp_getStr([Out] StringBuilder buf, long maxBufSize, ref Fp x, int ioMode);
[DllImport(dllName)] public static extern int mclBnFp_getStr([Out] StringBuilder buf, long maxBufSize, in Fp x, int ioMode);
[DllImport(dllName)] public static extern void mclBnFp_neg(ref Fp y, ref Fp x);
[DllImport(dllName)] public static extern void mclBnFp_inv(ref Fp y, ref Fp x);
[DllImport(dllName)] public static extern void mclBnFp_add(ref Fp z, ref Fp x, ref Fp y);
[DllImport(dllName)] public static extern void mclBnFp_sub(ref Fp z, ref Fp x, ref Fp y);
[DllImport(dllName)] public static extern void mclBnFp_mul(ref Fp z, ref Fp x, ref Fp y);
[DllImport(dllName)] public static extern void mclBnFp_div(ref Fp z, ref Fp x, ref Fp y);
[DllImport(dllName)] public static extern void mclBnFp_neg(ref Fp y, in Fp x);
[DllImport(dllName)] public static extern void mclBnFp_inv(ref Fp y, in Fp x);
[DllImport(dllName)] public static extern void mclBnFp_add(ref Fp z, in Fp x, in Fp y);
[DllImport(dllName)] public static extern void mclBnFp_sub(ref Fp z, in Fp x, in Fp y);
[DllImport(dllName)] public static extern void mclBnFp_mul(ref Fp z, in Fp x, in Fp y);
[DllImport(dllName)] public static extern void mclBnFp_div(ref Fp z, in Fp x, in Fp y);
[DllImport(dllName)] public static extern void mclBnG1_clear(ref G1 x);
[DllImport(dllName)] public static extern int mclBnG1_setStr(ref G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode);
[DllImport(dllName)] public static extern int mclBnG1_isValid(ref G1 x);
[DllImport(dllName)] public static extern int mclBnG1_isEqual(ref G1 x, ref G1 y);
[DllImport(dllName)] public static extern int mclBnG1_isZero(ref G1 x);
[DllImport(dllName)] public static extern int mclBnG1_isValid(in G1 x);
[DllImport(dllName)] public static extern int mclBnG1_isEqual(in G1 x, in G1 y);
[DllImport(dllName)] public static extern int mclBnG1_isZero(in G1 x);
[DllImport(dllName)] public static extern int mclBnG1_hashAndMapTo(ref G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern long mclBnG1_getStr([Out] StringBuilder buf, long maxBufSize, ref G1 x, int ioMode);
[DllImport(dllName)] public static extern void mclBnG1_neg(ref G1 y, ref G1 x);
[DllImport(dllName)] public static extern void mclBnG1_dbl(ref G1 y, ref G1 x);
[DllImport(dllName)] public static extern void mclBnG1_add(ref G1 z, ref G1 x, ref G1 y);
[DllImport(dllName)] public static extern void mclBnG1_sub(ref G1 z, ref G1 x, ref G1 y);
[DllImport(dllName)] public static extern void mclBnG1_mul(ref G1 z, ref G1 x, ref Fr y);
[DllImport(dllName)] public static extern long mclBnG1_getStr([Out] StringBuilder buf, long maxBufSize, in G1 x, int ioMode);
[DllImport(dllName)] public static extern void mclBnG1_neg(ref G1 y, in G1 x);
[DllImport(dllName)] public static extern void mclBnG1_dbl(ref G1 y, in G1 x);
[DllImport(dllName)] public static extern void mclBnG1_add(ref G1 z, in G1 x, in G1 y);
[DllImport(dllName)] public static extern void mclBnG1_sub(ref G1 z, in G1 x, in G1 y);
[DllImport(dllName)] public static extern void mclBnG1_mul(ref G1 z, in G1 x, in Fr y);
[DllImport(dllName)] public static extern void mclBnG2_clear(ref G2 x);
[DllImport(dllName)] public static extern int mclBnG2_setStr(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode);
[DllImport(dllName)] public static extern int mclBnG2_isValid(ref G2 x);
[DllImport(dllName)] public static extern int mclBnG2_isEqual(ref G2 x, ref G2 y);
[DllImport(dllName)] public static extern int mclBnG2_isZero(ref G2 x);
[DllImport(dllName)] public static extern int mclBnG2_isValid(in G2 x);
[DllImport(dllName)] public static extern int mclBnG2_isEqual(in G2 x, in G2 y);
[DllImport(dllName)] public static extern int mclBnG2_isZero(in G2 x);
[DllImport(dllName)] public static extern int mclBnG2_hashAndMapTo(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern long mclBnG2_getStr([Out] StringBuilder buf, long maxBufSize, ref G2 x, int ioMode);
[DllImport(dllName)] public static extern void mclBnG2_neg(ref G2 y, ref G2 x);
[DllImport(dllName)] public static extern void mclBnG2_dbl(ref G2 y, ref G2 x);
[DllImport(dllName)] public static extern void mclBnG2_add(ref G2 z, ref G2 x, ref G2 y);
[DllImport(dllName)] public static extern void mclBnG2_sub(ref G2 z, ref G2 x, ref G2 y);
[DllImport(dllName)] public static extern void mclBnG2_mul(ref G2 z, ref G2 x, ref Fr y);
[DllImport(dllName)] public static extern long mclBnG2_getStr([Out] StringBuilder buf, long maxBufSize, in G2 x, int ioMode);
[DllImport(dllName)] public static extern void mclBnG2_neg(ref G2 y, in G2 x);
[DllImport(dllName)] public static extern void mclBnG2_dbl(ref G2 y, in G2 x);
[DllImport(dllName)] public static extern void mclBnG2_add(ref G2 z, in G2 x, in G2 y);
[DllImport(dllName)] public static extern void mclBnG2_sub(ref G2 z, in G2 x, in G2 y);
[DllImport(dllName)] public static extern void mclBnG2_mul(ref G2 z, in G2 x, in Fr y);
[DllImport(dllName)] public static extern void mclBnGT_clear(ref GT x);
[DllImport(dllName)] public static extern int mclBnGT_setStr(ref GT x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize, int ioMode);
[DllImport(dllName)] public static extern int mclBnGT_isEqual(ref GT x, ref GT y);
[DllImport(dllName)] public static extern int mclBnGT_isZero(ref GT x);
[DllImport(dllName)] public static extern int mclBnGT_isOne(ref GT x);
[DllImport(dllName)] public static extern long mclBnGT_getStr([Out] StringBuilder buf, long maxBufSize, ref GT x, int ioMode);
[DllImport(dllName)] public static extern void mclBnGT_neg(ref GT y, ref GT x);
[DllImport(dllName)] public static extern void mclBnGT_inv(ref GT y, ref GT x);
[DllImport(dllName)] public static extern void mclBnGT_add(ref GT z, ref GT x, ref GT y);
[DllImport(dllName)] public static extern void mclBnGT_sub(ref GT z, ref GT x, ref GT y);
[DllImport(dllName)] public static extern void mclBnGT_mul(ref GT z, ref GT x, ref GT y);
[DllImport(dllName)] public static extern void mclBnGT_div(ref GT z, ref GT x, ref GT y);
[DllImport(dllName)] public static extern void mclBnGT_pow(ref GT z, ref GT x, ref Fr y);
[DllImport(dllName)] public static extern void mclBn_pairing(ref GT z, ref G1 x, ref G2 y);
[DllImport(dllName)] public static extern void mclBn_finalExp(ref GT y, ref GT x);
[DllImport(dllName)] public static extern void mclBn_millerLoop(ref GT z, ref G1 x, ref G2 y);
[DllImport(dllName)] public static extern int mclBnGT_isEqual(in GT x, in GT y);
[DllImport(dllName)] public static extern int mclBnGT_isZero(in GT x);
[DllImport(dllName)] public static extern int mclBnGT_isOne(in GT x);
[DllImport(dllName)] public static extern long mclBnGT_getStr([Out] StringBuilder buf, long maxBufSize, in GT x, int ioMode);
[DllImport(dllName)] public static extern void mclBnGT_neg(ref GT y, in GT x);
[DllImport(dllName)] public static extern void mclBnGT_inv(ref GT y, in GT x);
[DllImport(dllName)] public static extern void mclBnGT_add(ref GT z, in GT x, in GT y);
[DllImport(dllName)] public static extern void mclBnGT_sub(ref GT z, in GT x, in GT y);
[DllImport(dllName)] public static extern void mclBnGT_mul(ref GT z, in GT x, in GT y);
[DllImport(dllName)] public static extern void mclBnGT_div(ref GT z, in GT x, in GT y);
[DllImport(dllName)] public static extern void mclBnGT_pow(ref GT z, in GT x, in Fr y);
[DllImport(dllName)] public static extern void mclBn_pairing(ref GT z, in G1 x, in G2 y);
[DllImport(dllName)] public static extern void mclBn_finalExp(ref GT y, in GT x);
[DllImport(dllName)] public static extern void mclBn_millerLoop(ref GT z, in G1 x, in G2 y);
[DllImport(dllName)] public static extern int mclBnFp_setLittleEndianMod(ref Fp y, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern int mclBnFr_setLittleEndianMod(ref Fr y, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern int mclBnFp_serialize([Out] StringBuilder buf, long maxBufSiz, ref Fr x);
[DllImport(dllName)] public static extern int mclBnFr_serialize([Out] StringBuilder buf, long maxBufSiz, ref Fr x);
[DllImport(dllName)] public static extern int mclBnG1_serialize([Out] StringBuilder buf, long maxBufSiz, ref G1 x);
[DllImport(dllName)] public static extern int mclBnG2_serialize([Out] StringBuilder buf, long maxBufSiz, ref G2 x);
[DllImport(dllName)] public static extern int mclBnGT_serialize([Out] StringBuilder buf, long maxBufSiz, ref GT x);
[DllImport(dllName)] public static extern int mclBnFr_deserialize(ref Fr x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern int mclBnFp_deserialize(ref Fp x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern int mclBnG1_deserialize(ref G1 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern int mclBnG2_deserialize(ref G2 x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern int mclBnGT_deserialize(ref GT x, [In][MarshalAs(UnmanagedType.LPStr)] string buf, long bufSize);
[DllImport(dllName)] public static extern int mclBn_getFrByteSize();
[DllImport(dllName)] public static extern int mclBn_getFpByteSize();
[DllImport(dllName)] public static extern ulong mclBnFp_serialize([Out] byte[] buf, ulong maxBufSize, in Fp x);
[DllImport(dllName)] public static extern ulong mclBnFr_serialize([Out] byte[] buf, ulong maxBufSize, in Fr x);
[DllImport(dllName)] public static extern ulong mclBnG1_serialize([Out]byte[] buf, ulong maxBufSize, in G1 x);
[DllImport(dllName)] public static extern ulong mclBnG2_serialize([Out]byte[] buf, ulong maxBufSize, in G2 x);
[DllImport(dllName)] public static extern ulong mclBnFr_deserialize(ref Fr x, [In]byte[] buf, ulong bufSize);
[DllImport(dllName)] public static extern ulong mclBnFp_deserialize(ref Fp x, [In]byte[] buf, ulong bufSize);
[DllImport(dllName)] public static extern ulong mclBnG1_deserialize(ref G1 x, [In]byte[] buf, ulong bufSize);
[DllImport(dllName)] public static extern ulong mclBnG2_deserialize(ref G2 x, [In]byte[] buf, ulong bufSize);
public static void Init(int curveType = BN254)
{
@ -141,19 +141,19 @@ namespace mcl {
}
public bool IsValid()
{
return mclBnFr_isValid(ref this) == 1;
return mclBnFr_isValid(this) == 1;
}
public bool Equals(Fr rhs)
public bool Equals(in Fr rhs)
{
return mclBnFr_isEqual(ref this, ref rhs) == 1;
return mclBnFr_isEqual(this, rhs) == 1;
}
public bool IsZero()
{
return mclBnFr_isZero(ref this) == 1;
return mclBnFr_isZero(this) == 1;
}
public bool IsOne()
{
return mclBnFr_isOne(ref this) == 1;
return mclBnFr_isOne(this) == 1;
}
public void SetByCSPRNG()
{
@ -168,61 +168,78 @@ namespace mcl {
public string GetStr(int ioMode)
{
StringBuilder sb = new StringBuilder(1024);
long size = mclBnFr_getStr(sb, sb.Capacity, ref this, ioMode);
long size = mclBnFr_getStr(sb, sb.Capacity, this, ioMode);
if (size == 0) {
throw new InvalidOperationException("mclBnFr_getStr:");
}
return sb.ToString();
}
public void Neg(Fr x)
public byte[] Serialize()
{
mclBnFr_neg(ref this, ref x);
byte[] buf = new byte[mclBn_getFrByteSize()];
ulong n = mclBnFr_serialize(buf, (ulong)buf.Length, this);
if (n != (ulong)buf.Length) {
throw new ArithmeticException("mclBnFr_serialize");
}
return buf;
}
public void Deserialize(byte[] buf)
{
ulong n = mclBnFr_deserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
throw new ArithmeticException("mclBnFr_deserialize");
}
}
public void Neg(in Fr x)
{
mclBnFr_neg(ref this, x);
}
public void Inv(Fr x)
public void Inv(in Fr x)
{
mclBnFr_inv(ref this, ref x);
mclBnFr_inv(ref this, x);
}
public void Add(Fr x, Fr y)
public void Add(in Fr x, in Fr y)
{
mclBnFr_add(ref this, ref x, ref y);
mclBnFr_add(ref this, x, y);
}
public void Sub(Fr x, Fr y)
public void Sub(in Fr x, in Fr y)
{
mclBnFr_sub(ref this, ref x, ref y);
mclBnFr_sub(ref this, x, y);
}
public void Mul(Fr x, Fr y)
public void Mul(in Fr x, in Fr y)
{
mclBnFr_mul(ref this, ref x, ref y);
mclBnFr_mul(ref this, x, y);
}
public void Div(Fr x, Fr y)
public void Div(in Fr x, in Fr y)
{
mclBnFr_div(ref this, ref x, ref y);
mclBnFr_div(ref this, x, y);
}
public static Fr operator -(Fr x)
public static Fr operator -(in Fr x)
{
Fr y = new Fr();
y.Neg(x);
return y;
}
public static Fr operator +(Fr x, Fr y)
public static Fr operator +(in Fr x, in Fr y)
{
Fr z = new Fr();
z.Add(x, y);
return z;
}
public static Fr operator -(Fr x, Fr y)
public static Fr operator -(in Fr x, in Fr y)
{
Fr z = new Fr();
z.Sub(x, y);
return z;
}
public static Fr operator *(Fr x, Fr y)
public static Fr operator *(in Fr x, in Fr y)
{
Fr z = new Fr();
z.Mul(x, y);
return z;
}
public static Fr operator /(Fr x, Fr y)
public static Fr operator /(in Fr x, in Fr y)
{
Fr z = new Fr();
z.Div(x, y);
@ -248,19 +265,19 @@ namespace mcl {
}
public bool IsValid()
{
return mclBnFp_isValid(ref this) == 1;
return mclBnFp_isValid(this) == 1;
}
public bool Equals(Fp rhs)
public bool Equals(in Fp rhs)
{
return mclBnFp_isEqual(ref this, ref rhs) == 1;
return mclBnFp_isEqual(this, rhs) == 1;
}
public bool IsZero()
{
return mclBnFp_isZero(ref this) == 1;
return mclBnFp_isZero(this) == 1;
}
public bool IsOne()
{
return mclBnFp_isOne(ref this) == 1;
return mclBnFp_isOne(this) == 1;
}
public void SetByCSPRNG()
{
@ -269,61 +286,77 @@ namespace mcl {
public string GetStr(int ioMode)
{
StringBuilder sb = new StringBuilder(1024);
long size = mclBnFp_getStr(sb, sb.Capacity, ref this, ioMode);
long size = mclBnFp_getStr(sb, sb.Capacity, this, ioMode);
if (size == 0) {
throw new InvalidOperationException("mclBnFp_getStr:");
}
return sb.ToString();
}
public void Neg(Fp x)
public byte[] Serialize()
{
byte[] buf = new byte[mclBn_getFpByteSize()];
ulong n = mclBnFp_serialize(buf, (ulong)buf.Length, this);
if (n != (ulong)buf.Length) {
throw new ArithmeticException("mclBnFp_serialize");
}
return buf;
}
public void Deserialize(byte[] buf)
{
ulong n = mclBnFp_deserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
throw new ArithmeticException("mclBnFp_deserialize");
}
}
public void Neg(in Fp x)
{
mclBnFp_neg(ref this, ref x);
mclBnFp_neg(ref this, x);
}
public void Inv(Fp x)
public void Inv(in Fp x)
{
mclBnFp_inv(ref this, ref x);
mclBnFp_inv(ref this, x);
}
public void Add(Fp x, Fp y)
public void Add(in Fp x, in Fp y)
{
mclBnFp_add(ref this, ref x, ref y);
mclBnFp_add(ref this, x, y);
}
public void Sub(Fp x, Fp y)
public void Sub(in Fp x, in Fp y)
{
mclBnFp_sub(ref this, ref x, ref y);
mclBnFp_sub(ref this, x, y);
}
public void Mul(Fp x, Fp y)
public void Mul(in Fp x, in Fp y)
{
mclBnFp_mul(ref this, ref x, ref y);
mclBnFp_mul(ref this, x, y);
}
public void Div(Fp x, Fp y)
public void Div(in Fp x, in Fp y)
{
mclBnFp_div(ref this, ref x, ref y);
mclBnFp_div(ref this, x, y);
}
public static Fp operator -(Fp x)
public static Fp operator -(in Fp x)
{
Fp y = new Fp();
y.Neg(x);
return y;
}
public static Fp operator +(Fp x, Fp y)
public static Fp operator +(in Fp x, in Fp y)
{
Fp z = new Fp();
z.Add(x, y);
return z;
}
public static Fp operator -(Fp x, Fp y)
public static Fp operator -(in Fp x, in Fp y)
{
Fp z = new Fp();
z.Sub(x, y);
return z;
}
public static Fp operator *(Fp x, Fp y)
public static Fp operator *(in Fp x, in Fp y)
{
Fp z = new Fp();
z.Mul(x, y);
return z;
}
public static Fp operator /(Fp x, Fp y)
public static Fp operator /(in Fp x, in Fp y)
{
Fp z = new Fp();
z.Div(x, y);
@ -349,15 +382,15 @@ namespace mcl {
}
public bool IsValid()
{
return mclBnG1_isValid(ref this) == 1;
return mclBnG1_isValid(this) == 1;
}
public bool Equals(G1 rhs)
public bool Equals(in G1 rhs)
{
return mclBnG1_isEqual(ref this, ref rhs) == 1;
return mclBnG1_isEqual(this, rhs) == 1;
}
public bool IsZero()
{
return mclBnG1_isZero(ref this) == 1;
return mclBnG1_isZero(this) == 1;
}
public void HashAndMapTo(String s)
{
@ -368,31 +401,47 @@ namespace mcl {
public string GetStr(int ioMode)
{
StringBuilder sb = new StringBuilder(1024);
long size = mclBnG1_getStr(sb, sb.Capacity, ref this, ioMode);
long size = mclBnG1_getStr(sb, sb.Capacity, this, ioMode);
if (size == 0) {
throw new InvalidOperationException("mclBnG1_getStr:");
}
return sb.ToString();
}
public void Neg(G1 x)
public byte[] Serialize()
{
mclBnG1_neg(ref this, ref x);
byte[] buf = new byte[mclBn_getFpByteSize()];
ulong n = mclBnG1_serialize(buf, (ulong)buf.Length, this);
if (n != (ulong)buf.Length) {
throw new ArithmeticException("mclBnG1_serialize");
}
return buf;
}
public void Deserialize(byte[] buf)
{
ulong n = mclBnG1_deserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
throw new ArithmeticException("mclBnG1_deserialize");
}
}
public void Neg(in G1 x)
{
mclBnG1_neg(ref this, x);
}
public void Dbl(G1 x)
public void Dbl(in G1 x)
{
mclBnG1_dbl(ref this, ref x);
mclBnG1_dbl(ref this, x);
}
public void Add(G1 x, G1 y)
public void Add(in G1 x, in G1 y)
{
mclBnG1_add(ref this, ref x, ref y);
mclBnG1_add(ref this, x, y);
}
public void Sub(G1 x, G1 y)
public void Sub(in G1 x, in G1 y)
{
mclBnG1_sub(ref this, ref x, ref y);
mclBnG1_sub(ref this, x, y);
}
public void Mul(G1 x, Fr y)
public void Mul(in G1 x, in Fr y)
{
mclBnG1_mul(ref this, ref x, ref y);
mclBnG1_mul(ref this, x, y);
}
}
[StructLayout(LayoutKind.Sequential)]
@ -410,15 +459,15 @@ namespace mcl {
}
public bool IsValid()
{
return mclBnG2_isValid(ref this) == 1;
return mclBnG2_isValid(this) == 1;
}
public bool Equals(G2 rhs)
public bool Equals(in G2 rhs)
{
return mclBnG2_isEqual(ref this, ref rhs) == 1;
return mclBnG2_isEqual(this, rhs) == 1;
}
public bool IsZero()
{
return mclBnG2_isZero(ref this) == 1;
return mclBnG2_isZero(this) == 1;
}
public void HashAndMapTo(String s)
{
@ -429,31 +478,47 @@ namespace mcl {
public string GetStr(int ioMode)
{
StringBuilder sb = new StringBuilder(1024);
long size = mclBnG2_getStr(sb, sb.Capacity, ref this, ioMode);
long size = mclBnG2_getStr(sb, sb.Capacity, this, ioMode);
if (size == 0) {
throw new InvalidOperationException("mclBnG2_getStr:");
}
return sb.ToString();
}
public void Neg(G2 x)
public byte[] Serialize()
{
byte[] buf = new byte[mclBn_getFpByteSize() * 2];
ulong n = mclBnG2_serialize(buf, (ulong)buf.Length, this);
if (n != (ulong)buf.Length) {
throw new ArithmeticException("mclBnG2_serialize");
}
return buf;
}
public void Deserialize(byte[] buf)
{
ulong n = mclBnG2_deserialize(ref this, buf, (ulong)buf.Length);
if (n == 0) {
throw new ArithmeticException("mclBnG2_deserialize");
}
}
public void Neg(in G2 x)
{
mclBnG2_neg(ref this, ref x);
mclBnG2_neg(ref this, x);
}
public void Dbl(G2 x)
public void Dbl(in G2 x)
{
mclBnG2_dbl(ref this, ref x);
mclBnG2_dbl(ref this, x);
}
public void Add(G2 x, G2 y)
public void Add(in G2 x, in G2 y)
{
mclBnG2_add(ref this, ref x, ref y);
mclBnG2_add(ref this, x, y);
}
public void Sub(G2 x, G2 y)
public void Sub(in G2 x, in G2 y)
{
mclBnG2_sub(ref this, ref x, ref y);
mclBnG2_sub(ref this, x, y);
}
public void Mul(G2 x, Fr y)
public void Mul(in G2 x, Fr y)
{
mclBnG2_mul(ref this, ref x, ref y);
mclBnG2_mul(ref this, x, y);
}
}
[StructLayout(LayoutKind.Sequential)]
@ -469,96 +534,96 @@ namespace mcl {
throw new ArgumentException("mclBnGT_setStr:" + s);
}
}
public bool Equals(GT rhs)
public bool Equals(in GT rhs)
{
return mclBnGT_isEqual(ref this, ref rhs) == 1;
return mclBnGT_isEqual(this, rhs) == 1;
}
public bool IsZero()
{
return mclBnGT_isZero(ref this) == 1;
return mclBnGT_isZero(this) == 1;
}
public bool IsOne()
{
return mclBnGT_isOne(ref this) == 1;
return mclBnGT_isOne(this) == 1;
}
public string GetStr(int ioMode)
{
StringBuilder sb = new StringBuilder(1024);
long size = mclBnGT_getStr(sb, sb.Capacity, ref this, ioMode);
long size = mclBnGT_getStr(sb, sb.Capacity, this, ioMode);
if (size == 0) {
throw new InvalidOperationException("mclBnGT_getStr:");
}
return sb.ToString();
}
public void Neg(GT x)
public void Neg(in GT x)
{
mclBnGT_neg(ref this, ref x);
mclBnGT_neg(ref this, x);
}
public void Inv(GT x)
public void Inv(in GT x)
{
mclBnGT_inv(ref this, ref x);
mclBnGT_inv(ref this, x);
}
public void Add(GT x, GT y)
public void Add(in GT x, in GT y)
{
mclBnGT_add(ref this, ref x, ref y);
mclBnGT_add(ref this, x, y);
}
public void Sub(GT x, GT y)
public void Sub(in GT x, in GT y)
{
mclBnGT_sub(ref this, ref x, ref y);
mclBnGT_sub(ref this, x, y);
}
public void Mul(GT x, GT y)
public void Mul(in GT x, in GT y)
{
mclBnGT_mul(ref this, ref x, ref y);
mclBnGT_mul(ref this, x, y);
}
public void Div(GT x, GT y)
public void Div(in GT x, in GT y)
{
mclBnGT_div(ref this, ref x, ref y);
mclBnGT_div(ref this, x, y);
}
public static GT operator -(GT x)
public static GT operator -(in GT x)
{
GT y = new GT();
y.Neg(x);
return y;
}
public static GT operator +(GT x, GT y)
public static GT operator +(in GT x, in GT y)
{
GT z = new GT();
z.Add(x, y);
return z;
}
public static GT operator -(GT x, GT y)
public static GT operator -(in GT x, in GT y)
{
GT z = new GT();
z.Sub(x, y);
return z;
}
public static GT operator *(GT x, GT y)
public static GT operator *(in GT x, in GT y)
{
GT z = new GT();
z.Mul(x, y);
return z;
}
public static GT operator /(GT x, GT y)
public static GT operator /(in GT x, in GT y)
{
GT z = new GT();
z.Div(x, y);
return z;
}
public void Pow(GT x, Fr y)
public void Pow(in GT x, in Fr y)
{
mclBnGT_pow(ref this, ref x, ref y);
mclBnGT_pow(ref this, x, y);
}
public void Pairing(G1 x, G2 y)
public void Pairing(in G1 x, in G2 y)
{
mclBn_pairing(ref this, ref x, ref y);
mclBn_pairing(ref this, x, y);
}
public void FinalExp(GT x)
public void FinalExp(in GT x)
{
mclBn_finalExp(ref this, ref x);
mclBn_finalExp(ref this, x);
}
public void MillerLoop(G1 x, G2 y)
public void MillerLoop(in G1 x, in G2 y)
{
mclBn_millerLoop(ref this, ref x, ref y);
mclBn_millerLoop(ref this, x, y);
}
}
}

@ -11,25 +11,15 @@ namespace mcl {
err++;
}
static void Main(string[] args)
{
Console.WriteLine("BN254");
TestCurve(BN254);
Console.WriteLine("BN_SNARK");
TestCurve(BN_SNARK);
Console.WriteLine("BLS12_381");
TestCurve(BLS12_381);
}
static void TestCurve(int curveType)
{
err = 0;
try {
Init(curveType);
TestFr();
TestG1();
TestG2();
TestPairing();
Console.WriteLine("BN254");
TestCurve(BN254);
Console.WriteLine("BN_SNARK");
TestCurve(BN_SNARK);
Console.WriteLine("BLS12_381");
TestCurve(BLS12_381);
if (err == 0) {
Console.WriteLine("all tests succeed");
} else {
@ -39,6 +29,17 @@ namespace mcl {
Console.WriteLine("ERR={0}", e);
}
}
static void TestCurve(int curveType)
{
Init(curveType);
TestFr();
TestFp();
TestG1();
TestG2();
TestPairing();
}
static void TestFr()
{
Console.WriteLine("TestFr");
@ -78,6 +79,56 @@ namespace mcl {
}
x.SetStr("1234567891234", 10);
assert("1234567891234", x.GetStr(10) == "1234567891234");
{
byte[] buf = x.Serialize();
y.Deserialize(buf);
assert("x == y", x.Equals(y));
}
}
static void TestFp()
{
Console.WriteLine("TestFp");
Fp x = new Fp();
x.Clear();
assert("0", x.GetStr(10) == "0");
assert("0.IzZero", x.IsZero());
assert("!0.IzOne", !x.IsOne());
x.SetInt(1);
assert("1", x.GetStr(10) == "1");
assert("!1.IzZero", !x.IsZero());
assert("1.IzOne", x.IsOne());
x.SetInt(3);
assert("3", x.GetStr(10) == "3");
assert("!3.IzZero", !x.IsZero());
assert("!3.IzOne", !x.IsOne());
x.SetInt(-5);
x = -x;
assert("5", x.GetStr(10) == "5");
x.SetInt(4);
x = x * x;
assert("16", x.GetStr(10) == "16");
assert("10", x.GetStr(16) == "10");
Fp y;
y = x;
assert("x == y", x.Equals(y));
x.SetInt(123);
assert("123", x.GetStr(10) == "123");
assert("7b", x.GetStr(16) == "7b");
assert("y != x", !x.Equals(y));
Console.WriteLine("exception test");
try {
x.SetStr("1234567891234x", 10);
Console.WriteLine("x = {0}", x);
} catch (Exception e) {
Console.WriteLine("OK ; expected exception: {0}", e);
}
x.SetStr("1234567891234", 10);
assert("1234567891234", x.GetStr(10) == "1234567891234");
{
byte[] buf = x.Serialize();
y.Deserialize(buf);
assert("x == y", x.Equals(y));
}
}
static void TestG1()
{
@ -104,6 +155,12 @@ namespace mcl {
R.Add(R, P);
Q.Mul(P, x);
assert("Q == R", Q.Equals(R));
{
byte[] buf = P.Serialize();
Q.Clear();
Q.Deserialize(buf);
assert("P == Q", P.Equals(Q));
}
}
static void TestG2()
{
@ -130,6 +187,12 @@ namespace mcl {
R.Add(R, P);
Q.Mul(P, x);
assert("Q == R", Q.Equals(R));
{
byte[] buf = P.Serialize();
Q.Clear();
Q.Deserialize(buf);
assert("P == Q", P.Equals(Q));
}
}
static void TestPairing()
{

Loading…
Cancel
Save