-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ZKP class to use ProofProvider for HMAC and salt generation;…
… remove old ZKP implementation
- Loading branch information
1 parent
fe55d9b
commit c7f4b82
Showing
5 changed files
with
176 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Changelog | ||
|
||
## [Unreleased] | ||
|
||
### Added | ||
- New method for range proofs (`ProveRange` and `VerifyRange`). | ||
- Support for time-based proofs (`ProveTimestamp` and `VerifyTimestamp`). | ||
- Support for proving set membership (`ProveSetMembership` and `VerifySetMembership`). | ||
|
||
### Changed | ||
- Updated HMAC implementation to use more secure random salts. | ||
|
||
### Fixed | ||
- Bug in age verification logic that caused incorrect validation for dates close to the required age. | ||
|
||
--- | ||
|
||
## [1.1.0] - 2025-01-01 | ||
|
||
### Added | ||
- Proof of Balance feature (`ProveBalance` and `VerifyBalance`). | ||
- Salt generation improvements for stronger proofs. | ||
|
||
### Changed | ||
- Refactored `ZKP` class to improve performance and modularity. | ||
|
||
### Fixed | ||
- Fixed an issue where incorrect salt generation would sometimes lead to hash mismatches. | ||
|
||
--- | ||
|
||
## [1.0.0] - 2025-01-01 | ||
|
||
### Initial release | ||
- Proof of Age feature (`ProveAge` and `VerifyAge`). | ||
- Proof of Balance feature (`ProveBalance` and `VerifyBalance`). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using ZkpSharp.Security; | ||
|
||
namespace ZkpSharp.Core | ||
{ | ||
public class ZKP | ||
{ | ||
private const int RequiredAge = 18; | ||
private readonly IProofProvider _proofProvider; | ||
|
||
public ZKP(IProofProvider proofProvider) | ||
{ | ||
_proofProvider = proofProvider; | ||
} | ||
|
||
// Proof of Age | ||
public (string Proof, string Salt) ProveAge(DateTime dateOfBirth) | ||
{ | ||
if (CalculateAge(dateOfBirth) < RequiredAge) | ||
{ | ||
throw new ArgumentException("Insufficient age"); | ||
} | ||
|
||
string salt = _proofProvider.GenerateSalt(); | ||
string proof = _proofProvider.GenerateHMAC(dateOfBirth.ToString("yyyy-MM-dd") + salt); | ||
return (proof, salt); | ||
} | ||
|
||
public bool VerifyAge(string proof, DateTime dateOfBirth, string salt) | ||
{ | ||
int age = CalculateAge(dateOfBirth); | ||
string calculatedProof = _proofProvider.GenerateHMAC(dateOfBirth.ToString("yyyy-MM-dd") + salt); | ||
return age >= RequiredAge && _proofProvider.SecureEqual(calculatedProof, proof); | ||
} | ||
|
||
private int CalculateAge(DateTime dateOfBirth) | ||
{ | ||
DateTime today = DateTime.UtcNow; | ||
int age = today.Year - dateOfBirth.Year; | ||
if (dateOfBirth > today.AddYears(-age)) age--; | ||
return age; | ||
} | ||
|
||
// Proof of Balance | ||
public (string Proof, string Salt) ProveBalance(double balance, double requestedAmount) | ||
{ | ||
if (balance < requestedAmount) | ||
{ | ||
throw new ArgumentException("Insufficient balance"); | ||
} | ||
|
||
string salt = _proofProvider.GenerateSalt(); | ||
string proof = _proofProvider.GenerateHMAC(balance.ToString() + salt); | ||
return (proof, salt); | ||
} | ||
|
||
public bool VerifyBalance(string proof, double requestedAmount, string salt, double balance) | ||
{ | ||
string calculatedProof = _proofProvider.GenerateHMAC(balance.ToString() + salt); | ||
return _proofProvider.SecureEqual(calculatedProof, proof) && balance >= requestedAmount; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace ZkpSharp.Security | ||
{ | ||
public interface IProofProvider | ||
{ | ||
string GenerateSalt(); | ||
string GenerateHMAC(string input); | ||
bool SecureEqual(string a, string b); | ||
} | ||
|
||
public class ProofProvider : IProofProvider | ||
{ | ||
private readonly byte[] _hmacKey; | ||
|
||
public ProofProvider() | ||
{ | ||
_hmacKey = LoadHmacKey(); | ||
} | ||
|
||
public string GenerateSalt() | ||
{ | ||
byte[] saltBytes = new byte[16]; | ||
using (var rng = RandomNumberGenerator.Create()) | ||
{ | ||
rng.GetBytes(saltBytes); | ||
} | ||
return Convert.ToBase64String(saltBytes); | ||
} | ||
|
||
public string GenerateHMAC(string input) | ||
{ | ||
using (var hmac = new HMACSHA256(_hmacKey)) | ||
{ | ||
byte[] hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(input)); | ||
return Convert.ToBase64String(hashBytes); | ||
} | ||
} | ||
|
||
public bool SecureEqual(string a, string b) | ||
{ | ||
if (a.Length != b.Length) return false; | ||
|
||
int diff = 0; | ||
for (int i = 0; i < a.Length; i++) | ||
{ | ||
diff |= a[i] ^ b[i]; | ||
} | ||
|
||
return diff == 0; | ||
} | ||
|
||
private byte[] LoadHmacKey() | ||
{ | ||
// Load HMAC key from secure storage | ||
return new byte[32]; // Here we just return a dummy key | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.