As others have said, yes you can update.
I did this on my 3GS a couple of weeks ago (it was also 3.1.3 jailbroken with Spirit). I will say this, though. When I upgraded mine, I went directly from the jailbroken 3.1.3 to iOS4 and there were some remnants of the jailbreak left over. In fact, and I don't know if this is normal or not, but none of my apps would work.
I ended up having to do a restore, set up as new phone, and then do the update to iOS4. That cleared everything up and it's worked perfectly since.
I didn't want to restore from backup just in case those remnants of the jailbreak would pop up again, but when you set up as new, you lose your contacts. I used my.memova.com BEFORE I restored the phone, while it was still jailbroken to save my contacts. You simply set up an account, download the app, and sync it with your contacts. After restoring, you put the app back on your phone via iTunes (remember to sync BEFORE restoring) and then run it. It'll put your contacts right back on the phone like they were before. It worked like a charm for me.