Skip to content

Commit

Permalink
[C#] Sign/Verify supports byte[] message
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Jul 29, 2022
1 parent 582f176 commit 3af40c7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
31 changes: 22 additions & 9 deletions ffi/cs/bls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class BLS
[DllImport(dllName)] public static extern int blsPublicKeyIsZero(in PublicKey x);
[DllImport(dllName)] public static extern int blsSignatureIsZero(in Signature x);
// hash buf and set
[DllImport(dllName)] public static extern int blsHashToSecretKey(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
[DllImport(dllName)] public static extern int blsHashToSecretKey(ref SecretKey sec, [In]byte[] buf, ulong bufSize);
/*
set secretKey if system has /dev/urandom or CryptGenRandom
return 0 if success else -1
Expand All @@ -95,10 +95,10 @@ 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[] buf, 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[] buf, ulong size);
[DllImport(dllName)] public static extern int blsVerifyPop(in Signature sig, in PublicKey pub);

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -254,21 +254,30 @@ public void Mul(in SecretKey rhs)
public void SetByCSPRNG() {
blsSecretKeySetByCSPRNG(ref this);
}
public void SetHashOf(string s) {
if (blsHashToSecretKey(ref this, s, (ulong)s.Length) != 0) {
public void SetHashOf(byte[] buf)
{
if (blsHashToSecretKey(ref this, buf, (ulong)buf.Length) != 0) {
throw new ArgumentException("blsHashToSecretKey");
}
}
public void SetHashOf(string s) {
SetHashOf(Encoding.UTF8.GetBytes(s));
}
public PublicKey GetPublicKey() {
PublicKey pub;
blsGetPublicKey(ref pub, this);
return pub;
}
public Signature Sign(string m) {
public Signature Sign(byte[] buf)
{
Signature sig;
blsSign(ref sig, this, m, (ulong)m.Length);
blsSign(ref sig, this, buf, (ulong)buf.Length);
return sig;
}
public Signature Sign(string s)
{
return Sign(Encoding.UTF8.GetBytes(s));
}
public Signature GetPop() {
Signature sig;
blsGetPop(ref sig, this);
Expand Down Expand Up @@ -345,8 +354,12 @@ public void Mul(in SecretKey rhs)
{
blsPublicKeyMul(ref this, rhs);
}
public bool Verify(in Signature sig, string m) {
return blsVerify(sig, this, m, (ulong)m.Length) == 1;
public bool Verify(in Signature sig, byte[] buf)
{
return blsVerify(sig, this, buf, (ulong)buf.Length) == 1;
}
public bool Verify(in Signature sig, string s) {
return Verify(sig, Encoding.UTF8.GetBytes(s));
}
public bool VerifyPop(in Signature pop) {
return blsVerifyPop(pop, this) == 1;
Expand Down
31 changes: 22 additions & 9 deletions ffi/cs/bls_eth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class BLS
[DllImport(dllName)] public static extern int blsPublicKeyIsZero(in PublicKey x);
[DllImport(dllName)] public static extern int blsSignatureIsZero(in Signature x);
// hash buf and set
[DllImport(dllName)] public static extern int blsHashToSecretKey(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
[DllImport(dllName)] public static extern int blsHashToSecretKey(ref SecretKey sec, [In]byte[] buf, ulong bufSize);
/*
set secretKey if system has /dev/urandom or CryptGenRandom
return 0 if success else -1
Expand All @@ -95,10 +95,10 @@ 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[] buf, 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[] buf, ulong size);
[DllImport(dllName)] public static extern int blsVerifyPop(in Signature sig, in PublicKey pub);

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -254,21 +254,30 @@ public void Mul(in SecretKey rhs)
public void SetByCSPRNG() {
blsSecretKeySetByCSPRNG(ref this);
}
public void SetHashOf(string s) {
if (blsHashToSecretKey(ref this, s, (ulong)s.Length) != 0) {
public void SetHashOf(byte[] buf)
{
if (blsHashToSecretKey(ref this, buf, (ulong)buf.Length) != 0) {
throw new ArgumentException("blsHashToSecretKey");
}
}
public void SetHashOf(string s) {
SetHashOf(Encoding.UTF8.GetBytes(s));
}
public PublicKey GetPublicKey() {
PublicKey pub;
blsGetPublicKey(ref pub, this);
return pub;
}
public Signature Sign(string m) {
public Signature Sign(byte[] buf)
{
Signature sig;
blsSign(ref sig, this, m, (ulong)m.Length);
blsSign(ref sig, this, buf, (ulong)buf.Length);
return sig;
}
public Signature Sign(string s)
{
return Sign(Encoding.UTF8.GetBytes(s));
}
public Signature GetPop() {
Signature sig;
blsGetPop(ref sig, this);
Expand Down Expand Up @@ -345,8 +354,12 @@ public void Mul(in SecretKey rhs)
{
blsPublicKeyMul(ref this, rhs);
}
public bool Verify(in Signature sig, string m) {
return blsVerify(sig, this, m, (ulong)m.Length) == 1;
public bool Verify(in Signature sig, byte[] buf)
{
return blsVerify(sig, this, buf, (ulong)buf.Length) == 1;
}
public bool Verify(in Signature sig, string s) {
return Verify(sig, Encoding.UTF8.GetBytes(s));
}
public bool VerifyPop(in Signature pop) {
return blsVerifyPop(pop, this) == 1;
Expand Down

0 comments on commit 3af40c7

Please sign in to comment.