-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CsCheck for property based testing (#371)
- Remove FsCheck. - Add CsCheck. - Add a first property based test. Partially closes #370.
- Loading branch information
1 parent
c9106e5
commit b0870e9
Showing
6 changed files
with
61 additions
and
215 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
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
48 changes: 48 additions & 0 deletions
48
test/Verifiable.Tests/EllipticCurveUtilitiesPropertyTests.cs
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,48 @@ | ||
using CsCheck; | ||
using Verifiable.Core.Cryptography; | ||
using Xunit; | ||
|
||
namespace Verifiable.Tests.Cryptography | ||
{ | ||
/// <summary> | ||
/// Property based tests on elliptic curve utilities. | ||
/// </summary> | ||
public class EllipticCurveUtilitiesPropertyTests | ||
{ | ||
[Fact] | ||
public void CompressionDecompressionShouldBeInverseIfOnTheCurve() | ||
{ | ||
var curveTypeGen = Gen.Enum<EllipticCurveTypes>().Where(curve => curve != EllipticCurveTypes.None && curve != EllipticCurveTypes.Curve25519); | ||
|
||
//Generator for random elliptic curve points. | ||
var pointGen = Gen.Byte.Array[32].SelectMany(x => Gen.Byte.Array[32], (x, y) => (X: x, Y: y)); | ||
|
||
//Combines the curve type generator with the point generator. | ||
var testDataGen = curveTypeGen.SelectMany(curveType => pointGen, | ||
(curveType, point) => | ||
{ | ||
return (curveType, point.X, point.Y); | ||
}); | ||
|
||
testDataGen.Sample(testData => | ||
{ | ||
var curveType = testData.curveType; | ||
byte[] publicKeyX = testData.X; | ||
byte[] publicKeyY = testData.Y; | ||
|
||
//Check the pont is on the curve. If not, there is no point in testing compression/decompression. | ||
if(EllipticCurveUtilities.CheckPointOnCurve(publicKeyX, publicKeyY, curveType)) | ||
{ | ||
//Compress the elliptic curve point. | ||
byte[] compressedPoint = EllipticCurveUtilities.Compress(publicKeyX, publicKeyY); | ||
|
||
//Decompress the point. | ||
byte[] decompressedY = EllipticCurveUtilities.Decompress(compressedPoint, curveType); | ||
|
||
//Ensure the decompressed Y matches the original Y. | ||
Assert.Equal(publicKeyY, decompressedY); | ||
} | ||
}); | ||
} | ||
} | ||
} |
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
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