Skip to content

Commit

Permalink
feat: take message as byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
colibrishin committed Jul 25, 2022
1 parent d35648d commit da82c1f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 4 additions & 4 deletions ffi/cs/bls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public class BLS
[DllImport(dllName)] public static extern int blsPublicKeyRecover(ref PublicKey pub, in PublicKey pubVec, in Id idVec, ulong n);
[DllImport(dllName)] public static extern int blsSignatureRecover(ref Signature sig, in Signature sigVec, in Id idVec, ulong n);

[DllImport(dllName)] public static extern void blsSign(ref Signature sig, in SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size);
[DllImport(dllName)] public static extern void blsSign(ref Signature sig, in SecretKey sec, [In]byte[] m, ulong size);

// return 1 if valid
[DllImport(dllName)] public static extern int blsVerify(in Signature sig, in PublicKey pub, [In][MarshalAs(UnmanagedType.LPStr)] string m, ulong size);
[DllImport(dllName)] public static extern int blsVerify(in Signature sig, in PublicKey pub, [In]byte[] m, ulong size);
[DllImport(dllName)] public static extern int blsVerifyPop(in Signature sig, in PublicKey pub);

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -255,7 +255,7 @@ public void Mul(in SecretKey rhs)
blsGetPublicKey(ref pub, this);
return pub;
}
public Signature Sign(string m) {
public Signature Sign(byte[] m) {
Signature sig;
blsSign(ref sig, this, m, (ulong)m.Length);
return sig;
Expand Down Expand Up @@ -334,7 +334,7 @@ public void Mul(in SecretKey rhs)
{
blsPublicKeyMul(ref this, rhs);
}
public bool Verify(in Signature sig, string m) {
public bool Verify(in Signature sig, byte[] m) {
return blsVerify(sig, this, m, (ulong)m.Length) == 1;
}
public bool VerifyPop(in Signature pop) {
Expand Down
12 changes: 7 additions & 5 deletions ffi/cs/bls_test.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Text;

namespace mcl
{
Expand Down Expand Up @@ -98,11 +100,11 @@ class BLSTest
SecretKey sec;
sec.SetByCSPRNG();
PublicKey pub = sec.GetPublicKey();
string m = "abc";
byte[] m = Encoding.UTF8.GetBytes("abc");
Signature sig = sec.Sign(m);
Console.WriteLine("sig={0}", sig.GetHexStr());
assert("verify", pub.Verify(sig, m));
assert("not verify", !pub.Verify(sig, m + "a"));
assert("not verify", !pub.Verify(sig, m.Append((byte)0x61) as byte[]));
{
Signature sig2;
byte[] buf = sig.Serialize();
Expand Down Expand Up @@ -147,7 +149,7 @@ class BLSTest
pubs[i] = SharePublicKey(mpk, ids[i]);
assert("share publicKey", secs[i].GetPublicKey().IsEqual(pubs[i]));
}
string m = "doremi";
byte[] m = Encoding.UTF8.GetBytes("doremi");
for (int i = 0; i < n; i++) {
Signature Signature = secs[i].Sign(m);
assert("Signature.Verify", pubs[i].Verify(Signature, m));
Expand Down Expand Up @@ -176,7 +178,7 @@ class BLSTest
static void TestAggregate() {
Console.WriteLine("TestAggregate");
const int n = 10;
const string m = "abc";
byte[] m = Encoding.UTF8.GetBytes("abc");
SecretKey[] secVec = new SecretKey[n];
PublicKey[] pubVec = new PublicKey[n];
Signature[] popVec = new Signature[n];
Expand Down Expand Up @@ -216,7 +218,7 @@ static void TestMulVec()
{
Console.WriteLine("TestMulVec");
int n = 10;
const string m = "abc";
byte[] m = Encoding.UTF8.GetBytes("abc");
SecretKey[] secVec = new SecretKey[n];
PublicKey[] pubVec = new PublicKey[n];
Signature[] sigVec = new Signature[n];
Expand Down

0 comments on commit da82c1f

Please sign in to comment.