Not medieval actually and more likely came into use, primarily in England with the opening of India and "fast" ships. And not actually a form of torture per se.

Figging is thought to be first used in corporal punishment. Because clenching down on an inserted finger of ginger increases the effects, it is an excellent disincentive against the clenching of buttocks during a caning.

(Some will claim it has to do with the showing of horses... but I like my version better.)

PS. Give the search a try. You'll find lots of conversations and theories.