Closed kwantam closed 3 years ago
I did a bit more pseudocode cleanup, both for sqrt_ratio
and for the constant-time Tonelli-Shanks impl in a different appendix (and I back-ported the changed code to the codebase, and tested it).
The goal was to remove expressions from CMOVs, just to emphasize that every expression needs to be evaluated in a straight-line impl.
The argument for why it's sufficient to choose S = Z looks correct.
(cc @chris-wood @daira @armfazh)
This PR does the following:
S
, which does not appear necessary (discussion below)The generic
sqrt_ratio
routine has a constantS
that was previously defined as a primitive element of the field. This certainly works, but there are two issues with it:S != Z
needs an extra "cleanup" multiplication and a corresponding constant in the sswu routineq - 1
, and I'm not aware of another method. This is infeasible for BLS12-381 G2, for example.Fortunately, we don't actually need
S
to be a primitive element of F. Definingq - 1 = 2^l * o
witho
odd, what we actually need is thatS^o
generates the multiplicative subgroup of order2^l
. This is satisfied by any non-square in F: ifS
is non-square, thenS^o
is also non-square (becauseo
is odd), and is also an element of the order-2^l
subgroup by construction. Moreover, any element of this subgroup that is a non-square in F must generate the subgroup, and likewise any generator must be non-square in F. Since we require the constantZ
to be non-square already, and since the genericsqrt_ratio
routine already computesS^o
, settingS = Z
appears to suffice.But @daira I'd appreciate if you'd double check me on the above!