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)