From a83aea07afcb55eb06bb3b118a8aa473ac761304 Mon Sep 17 00:00:00 2001 From: brainpower Date: Fri, 22 Nov 2024 12:46:27 +0100 Subject: [PATCH 1/2] ext/gmp: add test for some uses of gmp_pow in ECC crypto with common number sizes used there --- ext/gmp/tests/gmp_cryptography.phpt | 36 +++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ext/gmp/tests/gmp_cryptography.phpt diff --git a/ext/gmp/tests/gmp_cryptography.phpt b/ext/gmp/tests/gmp_cryptography.phpt new file mode 100644 index 0000000000000..771c557e5425a --- /dev/null +++ b/ext/gmp/tests/gmp_cryptography.phpt @@ -0,0 +1,36 @@ +--TEST-- +test some of the simple operations done in ECC cryptography (GH-16870) +--EXTENSIONS-- +gmp +--FILE-- + +--EXPECT-- +string(77) "75877646180287003845291692588996321992008024509271171205840397374930023621264" +string(116) "20901178542000013443295783507452967008255111311111391381095411786512605029527130998551663048238767676593252458334912" +string(155) "11214254709783046066761897074581165564696209875788503860615296581570239055173596662994580613114615553617959320367624022951770022252133452732995602203876100" +string(232) "1187560172240926986956555551805946700914014146395850601682624192167183140821546786142042010581624015945796617035803644080855311724420346850512769661608026325318823192181186500090109111010574371273491473985103578154963883352347509000" +string(230) "41175244239511227932271803271789465275501438128816738161335879819867157609061333189838579416997011220296835767015204168726569753974321567200401024149587401209307771709029048946370759582286096704404540466667318675170269947865547236" +string(345) "264213056979683026715444213001489498112251992104549535125497320782388835160296854596249682279610208850530256165562505337646325742090947380083481042994421218556776724906795900034870043631716997303728488772811762108912277655229349137086580494553471889656657230883768271890897138746582039089128053875620299138149315547875444447674573987934196165016" +string(313) "3168892355445512933444812965909472020409957119791476182178991646344151155563236535370283312345943041041662641584330401473731788344553589640556705580180081371688996848117690101021660395072221488243400947129794119144961728431002781350889740682623487619845390287149216977767858293453242551076767446272230208078076521" +string(469) "5641066640108537808257411937508162073054596465771071759059984994432846497317882144255197676537588304873835452821393566559424565939427398239568910381599042586683472720047480341060077571873252225062218300704671408283921205864218742798766467986541832811143938893251282757214673131758780892167922492222473153470483402146144945253685265421614344082690235633775622262137908096304889066587289890823326962594240368957840634699139830536223598719285051876381571976198029149478069" From 0e1af55b6118598e454fb8c9fba377838e25dd49 Mon Sep 17 00:00:00 2001 From: Florian Moser Date: Thu, 28 Nov 2024 09:57:42 +0100 Subject: [PATCH 2/2] ext/gmp: add test with examples of the usage of gmp for cryptography --- ext/gmp/tests/gmp_cryptography_ffc.phpt | 108 ++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 ext/gmp/tests/gmp_cryptography_ffc.phpt diff --git a/ext/gmp/tests/gmp_cryptography_ffc.phpt b/ext/gmp/tests/gmp_cryptography_ffc.phpt new file mode 100644 index 0000000000000..f869f055a4329 --- /dev/null +++ b/ext/gmp/tests/gmp_cryptography_ffc.phpt @@ -0,0 +1,108 @@ +--TEST-- +Examples of the usage of gmp for finite field cryptography. +--DESCRIPTION-- +This executes basic operations (addition, multiplication, inverse, exponentiation) as the "base operations". +Then, it performs a primality check, and finally diffie-hellman as the "application". +All operations are done in the 4096-bit MODP Group from RFC 3526: https://www.ietf.org/rfc/rfc3526.txt + +Omitted are calculations on elliptic curves, which are also common, because of the complexity of these algorithms. +Elliptic curves generally operate on smaller values, so their use-case is somewhat covered here, +but curve calculations may need additional operations not used here. + +Further, omitted is explicit demonstration of (public-key) encryption, commitments, zero-knowledge proofs or similar common applications. +However, the operation used in the diffie-hellman is at the core of all these other applications, hence these use-cases are implicitly covered, too. + +$a, $b, and $c generated with +$random = gmp_random_range(0, $prime); +$randomHex = strtoupper(gmp_strval($random, 16)); +echo chunk_split(chunk_split($randomHex, 8, " "), 54); +--EXTENSIONS-- +gmp +--FILE-- + 0); + +// diffie-hellman key exchange (g^a)^b = (g^b)^a +$generator = gmp_init(2); +$factorA = gmp_random_range(1, $primeP); +$factorB = gmp_random_range(1, $primeP); +$left = gmp_powm(gmp_powm($generator, $factorA, $primeP), $factorB, $primeP); +$right = gmp_powm(gmp_powm($generator, $factorB, $primeP), $factorA, $primeP); +var_dump(gmp_cmp($left, $right) === 0); + +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true)