so what exactly are you trying to accomplish with this code?
Verifying signature I guess. But stwenhao pointed the error: R-value can have 02 or 03 prefix. And if you add those two points, then you will reach zero. So, what should be done instead, is calculating points on two sides, and comparing only x-values.
s=(z+rd)/k
sk=z+rd
sk-z=rd
(s/r)k-(z/r)=d
(s/r)R-(z/r)=Q
Q+(z/r)=(s/r)R
(Q+(z/r))*(r/s)=R
Q*(r/s)+(z/s)=R
That means, you should take your public key Q, multiply it by (r/s), then add (z/s), and then take x-value of that result, and compare it with r-value. If it is identical, then the signature is valid.
And if you compare it to the script used by OP, it is almost correct:
w=1/s
u1=z/s
u2=r/s
D=(z/s)+(r/s)Q
And then, we have D=(0,0). So, what should be done then? Of course, in that case, the whole signature should be marked as invalid. Not to mention that it should not pass malleability check, because s-value is in the upper half, so it should be rejected from the start, unless there are additional bytes, informing about the sign of each component, as it is for example in Bitcoin Message.