From 9ee82069bf0c12083ff5bbb6dbb3f9d2de59f0ab Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Mon, 15 Mar 2021 21:13:04 +0900 Subject: [PATCH] [cs] add MulVec for G1 --- ffi/cs/mcl/mcl.cs | 12 ++++++++++-- ffi/cs/test/test.cs | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ffi/cs/mcl/mcl.cs b/ffi/cs/mcl/mcl.cs index 133186d..24fc55d 100644 --- a/ffi/cs/mcl/mcl.cs +++ b/ffi/cs/mcl/mcl.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text; using System.Runtime.InteropServices; @@ -73,7 +73,7 @@ namespace mcl { [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 mclBnG1_mulVec(ref G1 x, [In]G1[] vec1, [In]Fr[] vec2, long bufSize); + [DllImport(dllName)] public static extern void mclBnG1_mulVec(ref G1 z, [In]G1[] x, [In]Fr[] y, long n); [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); @@ -217,6 +217,14 @@ namespace mcl { { mclBnG1_normalize(ref y, x); } + public static void MulVec(ref G1 z, in G1[] x, in Fr[] y) + { + int n = x.Length; + if (n <= 0 || n != y.Length) { + throw new ArgumentException("bad length"); + } + mclBnG1_mulVec(ref z, x, y, (long)n); + } public static void Add(ref G2 z, in G2 x, in G2 y) { mclBnG2_add(ref z, x, y); diff --git a/ffi/cs/test/test.cs b/ffi/cs/test/test.cs index c9393e3..2972cc8 100644 --- a/ffi/cs/test/test.cs +++ b/ffi/cs/test/test.cs @@ -164,6 +164,20 @@ namespace mcl { Q.Deserialize(buf); assert("P == Q", P.Equals(Q)); } + { + const int n = 5; + G1[] xVec = new G1[n]; + Fr[] yVec = new Fr[n]; + P.Clear(); + for (int i = 0; i < n; i++) { + xVec[i].HashAndMapTo(i.ToString()); + yVec[i].SetByCSPRNG(); + Q.Mul(xVec[i], yVec[i]); + P.Add(P, Q); + } + MulVec(ref Q, xVec, yVec); + assert("mulVecG1", P.Equals(Q)); + } } static void TestG2() {