Skip to content

Commit

Permalink
[cs] add sub method
Browse files Browse the repository at this point in the history
  • Loading branch information
herumi committed Jul 15, 2021
1 parent a526f51 commit 98140f3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ffi/cs/bls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class BLS
[DllImport(dllName)] public static extern void blsSecretKeyAdd(ref SecretKey sec, in SecretKey rhs);
[DllImport(dllName)] public static extern void blsPublicKeyAdd(ref PublicKey pub, in PublicKey rhs);
[DllImport(dllName)] public static extern void blsSignatureAdd(ref Signature sig, in Signature rhs);
// sub
[DllImport(dllName)] public static extern void blsSecretKeySub(ref SecretKey sec, in SecretKey rhs);
[DllImport(dllName)] public static extern void blsPublicKeySub(ref PublicKey pub, in PublicKey rhs);
[DllImport(dllName)] public static extern void blsSignatureSub(ref Signature sig, in Signature rhs);
// hash buf and set
[DllImport(dllName)] public static extern int blsHashToSecretKey(ref SecretKey sec, [In][MarshalAs(UnmanagedType.LPStr)] string buf, ulong bufSize);
/*
Expand Down Expand Up @@ -204,6 +208,10 @@ public unsafe struct SecretKey
public void Add(in SecretKey rhs) {
blsSecretKeyAdd(ref this, rhs);
}
public void Sub(in SecretKey rhs)
{
blsSecretKeySub(ref this, rhs);
}
public void SetByCSPRNG() {
blsSecretKeySetByCSPRNG(ref this);
}
Expand Down Expand Up @@ -280,6 +288,10 @@ public unsafe struct PublicKey
public void Add(in PublicKey rhs) {
blsPublicKeyAdd(ref this, rhs);
}
public void Sub(in PublicKey rhs)
{
blsPublicKeySub(ref this, rhs);
}
public bool Verify(in Signature sig, string m) {
return blsVerify(sig, this, m, (ulong)m.Length) == 1;
}
Expand Down Expand Up @@ -339,6 +351,10 @@ public unsafe struct Signature
public void Add(in Signature rhs) {
blsSignatureAdd(ref this, rhs);
}
public void Sub(in Signature rhs)
{
blsSignatureSub(ref this, rhs);
}
}
public static Signature RecoverSign(in Signature[] sigVec, in Id[] idVec) {
Signature sig;
Expand Down
15 changes: 15 additions & 0 deletions ffi/cs/bls_test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class BLSTest
sec2.SetHexStr("4000");
sec.Add(sec2);
assert("sec.Add", sec.GetHexStr() == "4321");
sec.Sub(sec2);
assert("sec.Sub", sec.GetHexStr() == "321");
sec.SetByCSPRNG();
Console.WriteLine("sec.Init={0}", sec.GetHexStr());
}
Expand Down Expand Up @@ -149,6 +151,19 @@ class BLSTest
}
assert("aggregate sec", secAgg.Sign(m).IsEqual(sigAgg));
assert("aggregate", pubAgg.Verify(sigAgg, m));
// sub
secAgg = secVec[0];
secAgg.Add(secVec[1]);
secAgg.Sub(secVec[1]);
assert("SecretKey.Sub", secAgg.IsEqual(secVec[0]));
pubAgg = pubVec[0];
pubAgg.Add(pubVec[1]);
pubAgg.Sub(pubVec[1]);
assert("PubretKey.Sub", pubAgg.IsEqual(pubVec[0]));
sigAgg = sigVec[0];
sigAgg.Add(sigVec[1]);
sigAgg.Sub(sigVec[1]);
assert("Signature.Sub", sigAgg.IsEqual(sigVec[0]));
}
static void Main(string[] args) {
try {
Expand Down

0 comments on commit 98140f3

Please sign in to comment.