Using the ETAS branching model of triggered seismicity, we apply the formalism of generating probability functions to calculate exactly the average difference between the magnitude of a mainshock and the magnitude of its largest aftershock over all generations. This average magnitude difference is found empirically to be independent of the mainshock magnitude and equal to 1.2, a universal behavior known as Baths law. Our theory shows that Baths law holds only sufficiently close to the critical regime of the ETAS branching process. Allowing for error bars +- 0.1 for Baths constant value around 1.2, our exact analytical treatment of Baths law provides new constraints on the productivity exponent alpha and the branching ratio n: $0.9 <= alpha <= 1$ and 0.8 <= n <= 1. We propose a novel method for measuring alpha based on the predicted renormalization of the Gutenberg-Richter distribution of the magnitudes of the largest aftershock. We also introduce the ``second Baths law for foreshocks: the probability that a main earthquake turns out to be the foreshock does not depend on its magnitude.