Many of you have already upgraded your precious iPhone 3GS to the new OS  released by apple, iOS 4.0. You are probably here because you could not  wait to try iOS4 for yourself or you upgraded by accident. Whatever the  reason i will show you how to successfully downgrade your iPhone 3GS to  3.1.3 with jailbreak and unlock, even if you have the new bootrom.
First  off, let me warn you that this is a complicated process and failure to  follow the instructions may cause your iPhone or your computer problems.  So be warned. Oh and i am not responsible for any problems caused by  following this guide. I have personally done what i will ask of you in  this guide and have not suffered from any inconveniences other than a  headache :)
Please follow the following steps to downgrade your 3GS to 3.1.3 from 4.0 and to also jailbreak and unlock it.
Step 1:
first  step requires you to create save SHSH file on saurik server to allow  you to downgrade to 3.1.3. This will be done by using a software called  tiny umbrella which can be downloaded 
here. Install and run it (WIN 7 & Vista users right click and run as administrator).

Before  launching the app plug in your iPhone 3GS to your computer's USB port.  Then launch the app and click on "Start TSS Server" button then check  advanced options on the left. the program should have automatically  loaded your ECID (If not you need to manually find it your self and  paste it. i am not going to go into details about that, google would be  your friend in that case). After the ECID is loaded/entered please  select iPhone3GS 3.1.3 (7e18) from the device/version option. 
IMPORTANT: you must select 3.1.3 and 
NOT 4.0.
After you have selected the correct options click on save my SHSH button. your shsh will be submitted. That's all for step one.
note:  if umbrella cannot connect to servers you may need to make some read  only files writable. For windows users these files are located in  "C:\Windows\System32\drivers\etc" select and right click the files and uncheck the read-only box. Step 2:For this step you need to follow a short guide from hackintosh website 
here. just follow the steps on that page and come back for step 3.
Step 3:In  steps 1 & 2 you made your iPhone 3GS "downgradeable". what this  means is that iTunes will no longer bit$@ at you and say that your  iPhone is not eligble for a downgrade. Now comes the part where you  start downgrading your iPhone. you will need the latest version of 
iTunes and the 3.1.3 firmware iPSW file which can be downloaded by clicking 
here. Place the iPSW file on your desktop and launch itunes.
Step 4:Place iPhone in DFU mode. if you don't know how to do this go 
here.  While in DFU mode with iPhone connected to the PC run iTunes. iTunes  will inform you that the iPhone is in recovery mode. click ok and then  while holding shift click on "restore" button. then browse for the 3.1.3  ipsw file that you downloaded in step3. select it and let it do its  work.
Step 5:after a  few minutes in which iPhone is being downgraded you will recieve a 1015  error message. This is GOOD. this means iPhone has been downgraded but  not activated for whatever reason. we can do this ourselves. so close  itunes and leave iphone connected while displaying the itunes usb logo.
 Step 6:
Step 6:to activate iPhone we first need to reset it. we cannot do this by hand will need 
quickpwn to assist us. after you have downloaded and installed quickpwn make sure you do 
NOT run it. go to step 7 instead.
Step 7:while  iphone is displaying the itunes logo and connected open your "my  computer" or "windows explorer" and brows to "C:\Program Files\Common  Files\Apple\Mobile Device Support" or for 64-bit users "C:\Program Files  (x86)\Common Files\Apple\Mobile Device Support". In there create a new  folder called "bin". then cut everything in mobile device folder  excluding the bin folder and paste it in the newly create bin folder.  you might get an access denied error. in that case terminate the running  apple mobile device and any other itunes process using task manager and  try again. now with the files in the bin folder run quickpwn.
Step 8: When your run quickpwn follow its instructions on reseting your iPhone. 

After  you do this your iphone should boot up properly with a 3.1.3 OS. if not  you probably didn't follow steps 4-5 or 7 properly. now close quickpwn  (you do not jailbreak using quickpwn here). also close itunes if it  opened.
Step 9:Now  that your iphone is running 3.1.3 properly it is time to jailbreak.  before you do that you need to undo what you did in step 7
. go back to the "C:\Program Files\Common Files\Apple\Mobile Device  Support" directory and cut everything from the bin folder back to the  mobile device support. you might have to end some processes again here  if you get errors in moving. Finally delete the bin folder you create in  step 7.
Step 10:
This is the jailbreaking part. you will need spirit to jailbreak your 3.1.3 iPhone 3GS. Go 
here to  have your iPhone jailbroken using Spirit. follow the instructions and  after you are done you will have a jailbroken 3.1.3 iPhone.
Step 11:Unlocking: To do this run cydia and have any available upgrades done.
- In cydia go to “add sources”.
- Now  type the following repo in the URL field : http://repo666.ultraSn0w.com.
- Now   search for ultrasn0w 0.93 using Cydia search bar. Once located,  install  by tapping the button on the top left corner of the display.
- Let  the installation complete and finally you will have an unlocked iPhone  3G / 3GS running jailbroken iOS 4.
thanks for following my guide i hope this helped. you can follow "ali2000" (me) on twitter if you want.