PwnageTool 4.0 (4.01 released) can easily jailbreak your iPhone 3G iOS 4.0 final version without upgrading your baseband to 05.13.04. Remember you can also use Redsn0w 0.9.5b5-3 which will jailbreak iOS 4.0 on your iPhone 3G but it will upgrade your baseband to 05.13.04 which still can be unlocked via Ultrasn0w 0.93. We recommend you to use PwnageTool instead of Redsn0w as PwnageTool preserves your baseband version from being upgraded to 05.13.04.
Before you may start jailbreaking iPhone 3G with PwnageTool 4.0, please read the official note from iPhone Dev Team below:
As said in the above note, if you are on iPhone OS 3.1.2 and you haven't used Spirit to jailbreak your iPhone 3G then you can easily restore the custom IPSW file cooked by PwnageTool 4.0 / 4.01 via recovery or DFU mode. But if you have jailbroken iPhone OS 3.1.3 then you have to use DFU mode as recovery mode might throw you with some unknown errors while restoring your custom IPSW file.
You can follow our step-by-step guide here to jailbreak your iPhone 3G iOS 4.0 with PwnageTool 4.0. Once jailbreaked, you can use Ultrasn0w 0.93 to unlock your iPhone 3G. You can find detailed step-by-step unlock instructions here.