What is the math behind a private key BASE58 (P2PKH)
Taking a private key like this, I would like to understand how I can verify if it is valid, what the procedures are, because I would like to write a script to play around with this.


Check if the string has valid Base-58 characters (123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz)
Decode the string to get the byte array using Base-58 encoding while removing and validating its checksum (last 4 bytes). Refer to the wiki link you posted for details.
Take the first byte and check if it is using the expected (version) value (128 for MainNet, 239 for TestNet).
Check the remaining data length (without first byte):
4.1. If it is 32 bytes, that is the key
4.2. If it is 33 bytes, remove last byte and check if it is equal to 1, the rest is the key
4.3. If the length is anything else, it is invalid
Convert the 32 bytes you have from step 4 to a 256-bit integer using big-endian definition
The value of step 5 has to be &gt;= 1 and &lt;= 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140