Skip to content

Commit 8d22c11

Browse files
committed
feat: make ChameleonHashHelper to ChameleonHashService
1 parent 3a4b05b commit 8d22c11

File tree

7 files changed

+129
-42
lines changed

7 files changed

+129
-42
lines changed

EccSDK/ChameleonHashHelper.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

EccSDK/Models/ChameleonHash/ChameleonHashRequest.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ namespace EccSDK.Models.ChameleonHash;
55

66
public class ChameleonHashRequest
77
{
8-
public KeyPairDomain KeyPairDomain { get; set; }
98
public string Message { get; set; }
10-
public BigInteger SessionKey { get; set; }
11-
public BigInteger Order { get; set; }
9+
public KeyPairDomain KeyPairDomain { get; set; }
1210
public BigInteger Signature { get; set; }
1311
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using EccSDK.models.Keys;
2+
using Org.BouncyCastle.Math;
3+
4+
namespace EccSDK.Models.ChameleonHash;
5+
6+
public class ChameleonHashVerifyRequest
7+
{
8+
public KeyPairDomain KeyPairDomain { get; set; }
9+
public string Message { get; set; }
10+
public string StrSignature { get; set; }
11+
}

EccSDK/Models/ChameleonHash/ChameleonSignature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ namespace EccSDK.models.ChameleonHash;
44

55
public class ChameleonSignature
66
{
7-
public BigInteger Value { get; set; }
7+
public string Value { get; set; }
88
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using EccSDK.models.ChameleonHash;
2+
using EccSDK.Models.ChameleonHash;
3+
using EccSDK.models.Keys;
4+
using EccSDK.Services.Interfaces;
5+
using Org.BouncyCastle.Math;
6+
7+
namespace EccSDK.Services;
8+
9+
public class ChameleonHashService : IChameleonHashService
10+
{
11+
private readonly KeyPairDomain _keyPairDomain;
12+
private readonly ChameleonHash _chameleonHash;
13+
14+
// Kn = public key
15+
// kn = private key
16+
public ChameleonHashService(KeyPairDomain keyPairDomain)
17+
{
18+
_keyPairDomain = keyPairDomain;
19+
_chameleonHash = GetChameleonHash(keyPairDomain);
20+
}
21+
22+
23+
24+
public ChameleonSignature Sign(string message)
25+
{
26+
// sign = (sessionKey - dn) mod n
27+
// dn = H(m) * kn
28+
var msgHash = HashHelper.Sha256(message);
29+
var dn = msgHash.Multiply(_keyPairDomain.PrivateKey.D);
30+
31+
var signature = _keyPairDomain.SessionKey.Add(dn).Mod(_keyPairDomain.PublicKey.Parameters.N);
32+
33+
return new ChameleonSignature
34+
{
35+
Value = signature.ToString(16)
36+
};
37+
}
38+
39+
public bool Verify(ChameleonHashVerifyRequest verifyRequest)
40+
{
41+
var chameleonHashRequest = new ChameleonHashRequest()
42+
{
43+
Message = verifyRequest.Message,
44+
KeyPairDomain = verifyRequest.KeyPairDomain,
45+
Signature = new BigInteger(verifyRequest.StrSignature, 16)
46+
};
47+
48+
var chameleonHashCalculated = GetChameleonHashBy(chameleonHashRequest);
49+
50+
return chameleonHashCalculated.Value.Equals(_chameleonHash.Value);
51+
}
52+
53+
public ChameleonHash GetChameleonHashBy(ChameleonHashRequest request)
54+
{
55+
// chameleonHash = [Kn x H(m)] + [P x signature]
56+
var hashedMessage = HashHelper.Sha256(request.Message);
57+
var knHash = request.KeyPairDomain.PublicKey.Q.Multiply(hashedMessage);
58+
var pSignature =request.KeyPairDomain.PublicKey.Parameters.G.Multiply(request.Signature);
59+
60+
return new ChameleonHash()
61+
{
62+
Value = knHash.Add(pSignature)
63+
};
64+
}
65+
66+
private ChameleonHash GetChameleonHash(KeyPairDomain keyPairDomain)
67+
{
68+
var signature = Sign("init chameleon hash");
69+
return GetChameleonHashBy(new ChameleonHashRequest()
70+
{
71+
Message = "init chameleon hash",
72+
KeyPairDomain = keyPairDomain,
73+
Signature = new BigInteger(signature.Value, 16)
74+
});
75+
}
76+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using EccSDK.models.ChameleonHash;
2+
using EccSDK.Models.ChameleonHash;
3+
using Org.BouncyCastle.Math.EC;
4+
5+
namespace EccSDK.Services.Interfaces;
6+
7+
public interface IChameleonHashService
8+
{
9+
ChameleonSignature Sign(string message);
10+
bool Verify(ChameleonHashVerifyRequest verifyRequest);
11+
ChameleonHash GetChameleonHashBy(ChameleonHashRequest request);
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using EccSDK;
2+
using EccSDK.Models.ChameleonHash;
3+
using EccSDK.Services;
4+
using NUnit.Framework;
5+
6+
namespace EccSdkUnitTest;
7+
8+
[TestFixture]
9+
public class ChameleonHashServiceTests
10+
{
11+
[Test]
12+
public void should_be_pass()
13+
{
14+
var keyPairDomain = EccGenerator.GetKeyDomain();
15+
16+
var chameleonHashService = new ChameleonHashService(keyPairDomain);
17+
18+
var chameleonSignature = chameleonHashService.Sign("Hello world");
19+
20+
chameleonHashService.Verify(new ChameleonHashVerifyRequest()
21+
{
22+
KeyPairDomain = keyPairDomain,
23+
Message = "Hello world",
24+
StrSignature = chameleonSignature.Value
25+
});
26+
}
27+
28+
}

0 commit comments

Comments
 (0)