A differentiable N-body code for transit timing and dynamical modeling – I. Algorithm and derivatives