Google is your friend http://www.ihackintosh.com/2010/04/downgrade-iphone-3g-os-4-0-to-3-1-3-or-lower/
You will struggle mate, OS4.0 was designed to bulk up the protection i.e. the baseband etc. It was the same with 3.1.3, if you upgraded you were screwed!