If I hire a freelancer to work on project A and charges me X$/h, but instead work on project B and still charge me for that time, it's illegal. Of course, illegal is a fuzzy term, it really depends of how the contract is crafted and, more importantly, how much one would be willing to take legal action against the freelancer (which will most likely turns out more expensive than just letting it go).
I'm no lawyer, but pretty sure it's not written in standard contracts "Employees can work on whatever he/she wants, get to keep all the IP of it, pending that employee doesn't get caught."
Employers with exempt full-time employees can mandate you stay for 40 hours/week regardless if you've finished your work for the week in the first 4 hours or not.
With physical labor, it's relatively easy to find the line where it's impossible to go faster.
With software, you can take a 3 month project and solve it in 10 minutes if it happens to be close enough to something available off-the-shelf. I get nothing extra if I deliver before the deadline by knowing about that solution (especially within 10 minutes), and that's the problem. Worst case, you have to write it all yourself and then you're still on-par with physical labor jobs, so you aren't entitled to anything extra for delivering before the deadline.
With that being said, OP is probably doing the work from his employer's office, which is definitely a breach of any employment contract I've ever signed.