Skip to content

Commit e6c70f7

Browse files
committed
Adapted OpenSSL part to changes to IsElement
1 parent 5fc60dc commit e6c70f7

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

CompactCryptoGroupAlgebra.OpenSsl.Tests/EllipticCurves/EllipticCurveAlgebraTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,17 @@ public void TestIsElement()
167167
var point = new ECPoint(groupHandle);
168168
ECPointHandle.Multiply(groupHandle, point.Handle, new BigNumber(index).Handle, ECPointHandle.Null, BigNumberHandle.Null, ctx);
169169

170-
Assert.That(algebra.IsElement(point), "valid point not accepted!");
170+
Assert.That(algebra.IsPotentialElement(point), "valid point not accepted by IsPotentialElement!");
171+
Assert.That(algebra.IsSafeElement(point), "valid point not accepted by IsSafeElement!");
172+
}
173+
174+
[Test]
175+
public void TestIsElementForPointAtInfinity()
176+
{
177+
var algebra = new EllipticCurveAlgebra(EllipticCurveID.Prime256v1);
178+
179+
Assert.That(algebra.IsPotentialElement(algebra.NeutralElement), "point at infinity not accepted by IsPotentialElement!");
180+
Assert.That(!algebra.IsSafeElement(algebra.NeutralElement), "point at infinity accepted by IsSafeElement!");
171181
}
172182

173183
[Test]

CompactCryptoGroupAlgebra.OpenSsl.Tests/Multiplicative/MultiplicativeGroupAlgebraTests.cs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public void TestInvalidElementRejectedAsGenerator(int generatorInt)
113113
public void TestIsElementAcceptsValidElements(int elementInt)
114114
{
115115
var element = new BigNumber(elementInt);
116-
Assert.That(groupAlgebra!.IsElement(element));
116+
Assert.That(groupAlgebra!.IsPotentialElement(element));
117+
Assert.That(groupAlgebra!.IsSafeElement(element));
117118
}
118119

119120
[Test]
@@ -124,16 +125,26 @@ public void TestIsElementAcceptsValidElements(int elementInt)
124125
public void TestIsElementRejectsInvalidElementsOutOfBounds(int elementInt)
125126
{
126127
var element = new BigNumber(elementInt);
127-
Assert.That(!groupAlgebra!.IsElement(element));
128+
Assert.That(!groupAlgebra!.IsPotentialElement(element));
129+
Assert.That(!groupAlgebra!.IsSafeElement(element));
128130
}
129131

130132
[Test]
131133
[TestCase(1)]
132134
[TestCase(22)]
133-
public void TestIsElementRejectsUnsafeElements(int elementInt)
135+
public void TestIsElementForUnsafeElements(int elementInt)
134136
{
135137
var element = new BigNumber(elementInt);
136-
Assert.That(!groupAlgebra!.IsElement(element));
138+
Assert.That(groupAlgebra!.IsPotentialElement(element));
139+
Assert.That(!groupAlgebra!.IsSafeElement(element));
140+
}
141+
142+
[Test]
143+
public void TestIsElementForNeutralElementAndNoCofactor()
144+
{
145+
var groupAlgebra = new MultiplicativeGroupAlgebra(BigPrime.CreateWithoutChecks(11), BigPrime.CreateWithoutChecks(10), 2);
146+
Assert.That(groupAlgebra.IsPotentialElement(groupAlgebra.NeutralElement));
147+
Assert.That(!groupAlgebra.IsSafeElement(groupAlgebra.NeutralElement));
137148
}
138149

139150
[Test]
@@ -222,27 +233,14 @@ public void TestEqualsFalseForUnrelatedObject()
222233
public void TestEqualsFalseForOtherAlgebra()
223234
{
224235
var otherAlgebra = new MultiplicativeGroupAlgebra(
225-
BigPrime.CreateWithoutChecks(59),
226-
BigPrime.CreateWithoutChecks(29),
227-
5
228-
);
229-
Assert.That(!groupAlgebra!.Equals(otherAlgebra));
230-
231-
otherAlgebra = new MultiplicativeGroupAlgebra(
232-
BigPrime.CreateWithoutChecks(23),
233-
BigPrime.CreateWithoutChecks(11),
234-
8
235-
);
236-
Assert.That(!groupAlgebra!.Equals(otherAlgebra));
237-
238-
otherAlgebra = new MultiplicativeGroupAlgebra(
239-
BigPrime.CreateWithoutChecks(23),
240-
BigPrime.CreateWithoutChecks(23),
241-
5
236+
BigPrime.CreateWithoutChecks(2*53+1),
237+
BigPrime.CreateWithoutChecks(53),
238+
4
242239
);
243-
Assert.That(!groupAlgebra!.Equals(otherAlgebra));
240+
Assert.IsFalse(groupAlgebra!.Equals(otherAlgebra));
244241
}
245242

243+
246244
[Test]
247245
public void TestGetHashCodeSameForEqual()
248246
{

CompactCryptoGroupAlgebra.OpenSsl/EllipticCurves/EllipticCurveAlgebra.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,17 @@ public bool IsPotentialElement(ECPoint element)
190190
/// <inheritdocs />
191191
public bool IsSafeElement(ECPoint element)
192192
{
193-
return IsPotentialElement(element);
193+
if (!IsPotentialElement(element)) return false;
194+
195+
// verifying that the point is not from a small subgroup of the whole curve (and thus outside
196+
// of the safe subgroup over which operations are considered)
197+
using (var cofactorBignum = new BigNumber(Cofactor))
198+
{
199+
var check = MultiplyScalar(element, SecureBigNumber.FromBigNumber(new BigNumber(Cofactor)));
200+
if (check.IsAtInfinity)
201+
return false;
202+
}
203+
return true;
194204
}
195205

196206
/// <inheritdocs />

CompactCryptoGroupAlgebra.OpenSsl/Multiplicative/MultiplicativeGroupAlgebra.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,18 @@ public bool IsPotentialElement(BigNumber element)
109109
return true;
110110
}
111111

112+
/// <inheritdocs />
112113
public bool IsSafeElement(BigNumber element)
113114
{
114115
if (!IsPotentialElement(element)) return false;
115-
116+
116117
// verifying that the point is not from a small subgroup of the whole curve (and thus outside
117118
// of the safe subgroup over which operations are considered)
118-
if (Cofactor > 1)
119+
using (var cofactorBignum = new BigNumber(Cofactor))
119120
{
120-
using (var cofactorBignum = new BigNumber(Cofactor))
121-
{
122-
var check = element.ModExp(cofactorBignum, _modulo);
123-
if (check.Equals(NeutralElement))
124-
return false;
125-
}
121+
var check = element.ModExp(cofactorBignum, _modulo);
122+
if (check.Equals(NeutralElement))
123+
return false;
126124
}
127125
return true;
128126
}

0 commit comments

Comments
 (0)