The rinnegan originated with Kaguya. It was the eye of the ten tails. The gene was passed to Hagaromo who was able to unlock it as a natural progression of the mangekyou sharingan.
In order for Madara to unlock rinnegan he needed senju and uchiha dna.
Sasuke also had Hashirama cells when he...