Skip to content

Commit 77a37b9

Browse files
committed
feat: new ChameleonHash Helper
1 parent cc14127 commit 77a37b9

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

EccSDK/ChameleonHashHelper.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using EccGrpcSDK.models;
2+
using Org.BouncyCastle.Math;
3+
using Org.BouncyCastle.Math.EC;
4+
5+
namespace EccGrpcSDK;
6+
7+
public class ChameleonHashHelper
8+
{
9+
// Kn = public key
10+
// kn = private key
11+
12+
public static BigInteger Sign(ChameleonHashRequest request)
13+
{
14+
// sign = (sessionKey - dn) mod n
15+
// dn = H(m) * kn
16+
var msgHash = HashHelper.Sha256(request.Message);
17+
var dn = msgHash.Multiply(request.KeyPair.PrivateKey);
18+
return request.SessionKey.Add(dn).Mod(request.Order);
19+
}
20+
21+
public static bool Verify(ChameleonHashRequest request, ECPoint rightChameleonHash)
22+
{
23+
return GetChameleonHash(request).Equals(rightChameleonHash);
24+
}
25+
26+
private static ECPoint GetChameleonHash(ChameleonHashRequest request)
27+
{
28+
// chameleonHash = [Kn x H(m)] + [P x sessionKey]
29+
var msgHash = HashHelper.Sha256(request.Message);
30+
var rP = request.KeyPair.BasePoint.Multiply(request.Signature);
31+
var chameleonHash = request.KeyPair.PublicKey.Multiply(msgHash).Add(rP).Normalize();
32+
return chameleonHash;
33+
}
34+
}

EccSDK/EccGenerator.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public KeyPair GenerateKeyPair(int keySize)
1818
var privateKey = (ECPrivateKeyParameters) keyGen.Private;
1919
var publicKey = (ECPublicKeyParameters) keyGen.Public;
2020

21-
return new KeyPair()
21+
var generateKeyPair = new KeyPair()
2222
{
2323
PublicKey = publicKey.Q,
2424
PrivateKey = privateKey.D,
2525
BasePoint = publicKey.Parameters.G
2626
};
27+
28+
return generateKeyPair;
2729
}
2830
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Org.BouncyCastle.Math;
2+
3+
namespace EccGrpcSDK.models;
4+
5+
public class ChameleonHashRequest
6+
{
7+
public KeyPair KeyPair { get; set; }
8+
public string Message { get; set; }
9+
public BigInteger SessionKey { get; set; }
10+
public BigInteger Order { get; set; }
11+
public BigInteger Signature { get; set; }
12+
}

0 commit comments

Comments
 (0)