To do this, you'll need the following:
blackra1n
3.1.2 FW for 3GS (this isn't tested on any other device)
3.1.3 FW for 3GS
Pwnage Tool 3.1.5
Small disclaimer - I downgraded to 3.1.3 first to try and jailbreak and was unsuccessful, so I then downgraded to 3.1.2 from 3.1.3 and then did everything listed here. I have been able to downgrade straight to 3.1.2 from 4.0 so I'm just assuming you will be as well. Also, just to cover the bases, if anything should go wrong, simply put the phone into DFU mode, and upgrade back to 4.0. Then follow the instructions from the beginning.
1. Plus the device into your USB cable with iTunes open and put the device into DFU mode. Once in DFU mode, iTunes will tell you that the device needs to be restored - press okay, and then Hold Option + Click Restore and navigate to your 3.1.2 firmware.
2. After device restores, you'll be hit with a 1015 error. This is expected, just leave iTunes ...