When the statistics calculations were first implemented a long time ago, one of my colleagues tested them very carefully.
Also, Moodle is open source. If you really want to know what is going on, you can read the code and check it. It's not the easiest code to follow (because it was written with an eye to efficiency of the calculation as well as clarity) but this seems to be where it computes the discrimination index: https://github.com/moodle/moodle/blob/87afa4d7595eeafdf9e3f83d53272ee61fb66780/question/classes/statistics/questions/calculator.php#L415
The key difference you can get with calculations of discrimination index is whether you are computing the covariance between
- the mark for this question and the total for all questions, or
- the mark for this question and the total for the other questions that are not this one.
Moodle uses the second options (if my memory is right). That bit of code seems to be here: https://github.com/moodle/moodle/blob/87afa4d7595eeafdf9e3f83d53272ee61fb66780/question/classes/statistics/questions/calculator.php#L387