From 76b656ee3c85bfc9b30a26f1a565d5a4919bcc37 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sun, 14 May 2017 22:12:33 +0900 Subject: add TestId --- ffi/cs/bls256.cs | 105 ++++++++++++++++++++++++++++++++++++++++---------- ffi/cs/bls256_test.cs | 26 ++++++++++++- 2 files changed, 108 insertions(+), 23 deletions(-) (limited to 'ffi/cs') diff --git a/ffi/cs/bls256.cs b/ffi/cs/bls256.cs index ccc10e4..6e29c33 100644 --- a/ffi/cs/bls256.cs +++ b/ffi/cs/bls256.cs @@ -4,10 +4,8 @@ using System.Runtime.InteropServices; namespace mcl { class BLS256 { -// public static int IoBin = 2; -// public static int IoDec = 10; -// public static int IoHex = 16; -// public static int IoEcComp = 512; // fixed byte representation + const int IoEcComp = 512; // fixed byte representation + public const int maxUnitSize = 4; [DllImport("bls256.dll")] public static extern int blsInit(int curve, int maxUnitSize); [DllImport("bls256.dll")] @@ -22,10 +20,6 @@ namespace mcl { [DllImport("bls256.dll")] public static extern int blsSecretKeyIsSame(ref SecretKey lhs, ref SecretKey rhs); [DllImport("bls256.dll")] - public static extern void blsSecretKeyPut(ref SecretKey sec); - [DllImport("bls256.dll")] - public static extern void blsSecretKeyCopy(ref SecretKey dst, ref SecretKey src); - [DllImport("bls256.dll")] public static extern void blsSecretKeySetArray(ref SecretKey sec, ref ulong p); [DllImport("bls256.dll")] public static extern int blsSecretKeySetStr(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize, int ioMode); @@ -38,13 +32,13 @@ namespace mcl { [DllImport("bls256.dll")] public static extern void blsSecretKeyGetPublicKey(ref SecretKey sec, ref PublicKey pub); [DllImport("bls256.dll")] - public static extern void blsSecretKeySign(ref SecretKey sec, ref Sign sign, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size); + public static extern void blsSecretKeySign(ref SecretKey sec, ref Signature sign, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size); [DllImport("bls256.dll")] public static extern int blsSecretKeySet(ref SecretKey sec, ref SecretKey msk, ulong k, ref Id id); [DllImport("bls256.dll")] public static extern int blsSecretKeyRecover(ref SecretKey sec, ref SecretKey secVec, ref Id idVec, ulong n); [DllImport("bls256.dll")] - public static extern void blsSecretKeyGetPop(ref SecretKey sec, ref Sign sign); + public static extern void blsSecretKeyGetPop(ref SecretKey sec, ref Signature sign); [DllImport("bls256.dll")] public static extern int blsPublicKeyIsSame(ref PublicKey lhs, ref PublicKey rhs); [DllImport("bls256.dll")] @@ -62,28 +56,27 @@ namespace mcl { [DllImport("bls256.dll")] public static extern int blsPublicKeyRecover(ref PublicKey pub, ref PublicKey pubVec, ref Id idVec, ulong n); [DllImport("bls256.dll")] - public static extern int blsSignIsSame(ref Sign lhs, ref Sign rhs); + public static extern int blsSignIsSame(ref Signature lhs, ref Signature rhs); [DllImport("bls256.dll")] - public static extern void blsSignPut(ref Sign sign); + public static extern void blsSignPut(ref Signature sign); [DllImport("bls256.dll")] - public static extern void blsSignCopy(ref Sign dst, ref Sign src); + public static extern void blsSignCopy(ref Signature dst, ref Signature src); [DllImport("bls256.dll")] - public static extern int blsSignSetStr(ref Sign sign, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize, int ioMode); + public static extern int blsSignSetStr(ref Signature sign, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize, int ioMode); [DllImport("bls256.dll")] - public static extern ulong blsSignGetStr(ref Sign sign, [Out]StringBuilder buf, ulong maxBufSize, int ioMode); + public static extern ulong blsSignGetStr(ref Signature sign, [Out]StringBuilder buf, ulong maxBufSize, int ioMode); [DllImport("bls256.dll")] - public static extern void blsSignAdd(ref Sign sign, ref Sign rhs); + public static extern void blsSignAdd(ref Signature sign, ref Signature rhs); [DllImport("bls256.dll")] - public static extern int blsSignRecover(ref Sign sign, ref Sign signVec, ref Id idVec, ulong n); + public static extern int blsSignRecover(ref Signature sign, ref Signature signVec, ref Id idVec, ulong n); [DllImport("bls256.dll")] - public static extern int blsSignVerify(ref Sign sign, ref PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size); + public static extern int blsSignVerify(ref Signature sign, ref PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size); [DllImport("bls256.dll")] - public static extern int blsSignVerifyPop(ref Sign sign, ref PublicKey pub); + public static extern int blsSignVerifyPop(ref Signature sign, ref PublicKey pub); public static void Init() { const int CurveFp254BNb = 0; - const int maxUnitSize = 4; if (!System.Environment.Is64BitProcess) { throw new PlatformNotSupportedException("not 64-bit system"); } @@ -114,11 +107,81 @@ namespace mcl { } return sb.ToString(0, (int)size); } + public override string ToString() + { + return GetStr(16); + } + public void SetArray(ulong[] p) + { + int n = p.Length; + if (n != maxUnitSize) { + throw new InvalidOperationException("SetArray:" + n.ToString()); + } + blsIdSet(ref this, ref p[0]); + } } public struct SecretKey { private ulong v0, v1, v2, v3; + public bool IsSame(SecretKey rhs) + { + return blsSecretKeyIsSame(ref this, ref rhs) != 0; + } + public void SetStr(String s, int ioMode) + { + if (blsSecretKeySetStr(ref this, s, (ulong)s.Length, ioMode) != 0) { + throw new InvalidOperationException("blsSecretKeySetStr:" + s); + } + } + public string GetStr(int ioMode) + { + StringBuilder sb = new StringBuilder(1024); + ulong size = blsSecretKeyGetStr(ref this, sb, (ulong)sb.Capacity + 1, ioMode); + if (size == 0) { + throw new InvalidOperationException("blsSecretKeyGetStr"); + } + return sb.ToString(0, (int)size); + } + public override string ToString() + { + return GetStr(16); + } + public void SetArray(ulong[] p) + { + int n = p.Length; + if (n != maxUnitSize) { + throw new InvalidOperationException("SetArray:" + n.ToString()); + } + blsSecretKeySetArray(ref this, ref p[0]); + } + public void Add(SecretKey rhs) + { + blsSecretKeyAdd(ref this, ref rhs); + } + public void Init() + { + blsSecretKeyInit(ref this); + } + public PublicKey GetPublicKey() + { + PublicKey pub = new PublicKey(); + blsSecretKeyGetPublicKey(ref this, ref pub); + return pub; + } + public Signature Sign(String m) + { + Signature sign = new Signature(); + blsSecretKeySign(ref this, ref sign, m, (ulong)m.Length); + return sign; + } + // secretKey = sum_{i=0}^{msk.Length - 1} msk[i] * id^i + public void ShareById(SecretKey[] msk, Id id) + { + if (blsSecretKeySet(ref this, ref msk[0], (ulong)msk.Length, ref id) != 0) { + throw new InvalidOperationException("GetSecretKeyForId:" + id.ToString()); + } + } } - public struct Sign { + public struct Signature { private ulong v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11; } public struct PublicKey { diff --git a/ffi/cs/bls256_test.cs b/ffi/cs/bls256_test.cs index f649ddc..5c358fe 100644 --- a/ffi/cs/bls256_test.cs +++ b/ffi/cs/bls256_test.cs @@ -15,14 +15,36 @@ namespace mcl { Console.WriteLine("TestId"); Id id = new Id(); id.SetStr("255", 10); - Console.WriteLine("id={0}", id.GetStr(10)); - Console.WriteLine("id={0}", id.GetStr(16)); + assert("GetStr(10)", id.GetStr(10) == "255"); + assert("GetStr(16)", id.GetStr(16) == "ff"); + id.SetArray(new ulong[] { 1, 2, 3, 4 }); + assert("GetStr(16)", id.GetStr(16) == "4000000000000000300000000000000020000000000000001"); + } + static void TestSecretKey() + { + Console.WriteLine("TestSecretKey"); + SecretKey sec = new SecretKey(); + sec.SetStr("255", 10); + assert("GetStr(10)", sec.GetStr(10) == "255"); + assert("GetStr(16)", sec.GetStr(16) == "ff"); + sec.SetArray(new ulong[] { 1, 2, 3, 4 }); + assert("GetStr(16)", sec.GetStr(16) == "4000000000000000300000000000000020000000000000001"); + { + SecretKey sec2 = new SecretKey(); + sec.SetStr("321", 10); + sec2.SetStr("4000", 10); + sec.Add(sec2); + assert("sec.Add", sec.GetStr(10) == "4321"); + sec.Init(); + Console.WriteLine("sec.Init={0}", sec); + } } static void Main(string[] args) { try { Init(); TestId(); + TestSecretKey(); if (err == 0) { Console.WriteLine("all tests succeed"); } else { -- cgit v1.2.3