Skip to content

Commit 3cd3c27

Browse files
botovqrhenium
authored andcommitted
Fix modular square root test with LibreSSL >= 3.8
[ This is a backport to the 3.1 branch. ] If x is a modular square root of a (mod p) then so is (p - x). Both answers are valid. In particular, both 2 and 3 are valid square roots of 4 (mod 5). Do not assume that a particular square root is chosen by the algorithm. Indeed, the algorithm in OpenSSL and LibreSSL <= 3.7 returns a non-deterministic answer in many cases. LibreSSL 3.8 and later will always return the smaller of the two possible answers. This breaks the current test case. Instead of checking for a particular square root, check that the square of the claimed square root is the given value. This is always true. Add the simplest test case where the answer is indeed non-deterministic. (cherry picked from commit 93548ae)
1 parent 09122c5 commit 3cd3c27

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

test/openssl/test_bn.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ def test_mod_sqr
175175
end
176176

177177
def test_mod_sqrt
178-
assert_equal(3, 4.to_bn.mod_sqrt(5))
178+
assert_equal(4, 4.to_bn.mod_sqrt(5).mod_sqr(5))
179+
# One of 189484 or 326277 is returned as a square root of 2 (mod 515761).
180+
assert_equal(2, 2.to_bn.mod_sqrt(515761).mod_sqr(515761))
179181
assert_equal(0, 5.to_bn.mod_sqrt(5))
180182
assert_raise(OpenSSL::BNError) { 3.to_bn.mod_sqrt(5) }
181183
end

0 commit comments

Comments
 (0)