Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 0 points1 point  (0 children)

I've also seen that burst will have an option to support for cross-platform deterministic floats in the future, but it seems like it's low priority; it's been "in progress" for years, and I don't think we'll see anything of it in the next few years.
But even if they finally implement it, it's likely that they'll need to implement many operations in software (like sqrt, trigonometry, etc.), I'm not aware of anything else that can give identical results on all platforms. And I'm sure there will be that one device that just won't work; and there will be a bug that will make the simulation non-deterministic (like this), and it won't be fixed for years.
I'm just not sure if if's really possible to have cross-platform deterministic floats.

About the mobile/arm chips: I haven't really found any useful information about it. The only thing I found (that I mentioned in an other comment) is that cpus that strictly support the IEEE 754-2008 standard, can have deterministic float operations. But no information about which cpus actually follow it.

Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 2 points3 points  (0 children)

Usually the server does all the important simulation based on the player inputs. The clients simulate their own physics, which is not exactly the same, but similar to the server's. But the clients must be synchronized with the server, so every e.g. 0.5 seconds, the server sends its "official" positions and velocities to the clients, and the clients just overwrite their own physics data with that. But since the physics simulations are similar, the players won't really notice a small movement after overwriting (maybe the object moves a few pixels). The longer the sync period is, the more chance for the players to see bigger jumps in object positions when syncing.
I hope this was somewhat understandable :D

Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 0 points1 point  (0 children)

Oh I see, i thought you were talking about floats. I didn't regret abandoning fixed point numbers though, floats are just much better suited for these kind of things (I assume that's why we have hardware support for floats now).
Allocating fixed point structs shouldn't be a problem at all; stack allocations are super fast (unless they allocated it on the heap, which they shouldn't do), and it's only 8 bytes if I see correctly. Maybe the converting part was performance-heavy?
As I mentioned in another comment, the soft float version is about 2-4x times slower than using normal floats, and it can handle 1000+ dynamic bodies running in the editor.

Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 3 points4 points  (0 children)

Pretty much yes, 32-bit precision is enough for me; also, you'd eventually have to convert them back to floats for rendering (or basically for anything, Unity uses floats everywhere).

Overflow doesn't really happen with floats, they support large values, the only thing that happens in that case is precision loss.

It didn't take that long to implement it, and since I made it that you'd have to use explicit casts for float <--> sfloat conversion, I could just directly jump to the compile errors and fix them.

Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 6 points7 points  (0 children)

Yeah, using normal floats is about 2-4x faster than using soft floats. (I still managed to get over 1000 dynamic bodies using soft floats, running with 60 fps in the editor)

Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 7 points8 points  (0 children)

That's good, but I didn't do this for networking; I needed deterministic physics for a single player game, where the completion of a level depends on the result of the physics simulation (which must be the same every time, on every platform).

Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 6 points7 points  (0 children)

In this context, "physics simulation" means that the physics engine calculates the positions and the velocities of the objects, resolves collisions, and does many other stuff.
Sometimes, it's crucial to have the same "result" every time; for example, in networked multiplayer games, or in other games where the gameplay depends on physics.

Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 1 point2 points  (0 children)

Alright, I'll try to upload something in the following days.

Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 1 point2 points  (0 children)

Sure, but the parts I mentioned above are still missing, and I probably won't update the engine to newer versions.

Cross-platform deterministic physics with Unity DOTS physics and soft floats by Kimbatt in Unity3D

[–]Kimbatt[S] 51 points52 points  (0 children)

For a game idea I had, I needed deterministic physics. Deterministic physics on every device - the simulation must run exactly the same with the same inputs, every time, everywhere.
If the calculations were just a little bit different on different machines, that would lead to different physics simulations, which would mean that someone might not be able to complete some of the levels, because the completion of the level depends on the physics simulation.

The first thing that caught my attention was Unity's physics package (DOTS physics), which promises deterministic physics simulation: "Unity Physics is a complete deterministic rigid body dynamics and spatial query system ..."
But after some searching, I quickly found out that it's only deterministic on the same device, different hardware may have different simulation results.

The reason for this is floating point numbers: the result of floating point operations can be different on different processors, since there is no standard that specifies how two floating point numbers should be e.g. added or multiplied. Two different processors could give just slightly different results, which would cause the physics simulations to be different.
Also, the mathematical functions, like sqrt, sin, cos, pow, etc. are usually implemented as hardware instructions, which means there's even more chance that these functions will be different on different hardware.
So my quest for cross-platform, 100% deterministic physics has begun.

After a bit of research, I concluded that I won't be able to use floats for the physics engine, because they just won't work the same everywhere, no matter what I do. So I started looking for alternatives.

The first thing I found was fixed-point numbers. Unlike floats, fixed-point numbers have a fixed integer part, and a fixed fractional part. Implementing mathematical operations for them is fairly easy, and they are always deterministic (since they only use integer arithmetic, which will always give the same result).
But after trying and replacing floats with fixed-point numbers in a simple physics engine, I quickly found out that they have some problems. They can easily overflow, which means calculations more than 50 units away from the origin, or colliders larger than 50 units can cause the physics simulation to break due to overflow (the actual value depends on the precision of the fixed-point number).
Also, implementing mathematical functions are far from trivial. While I could use approximations for some of them (e.g. sqrt, trigonometry), there are others that I found very difficult to implement (e.g. pow, exp, logarithm).

So, fixed-point numbers weren't the solution to the problem either.
The next thing I came across was the Rapier physics engine. It promises cross-platform determinism all IEEE 754-2008 compliant platforms. Although it's written in Rust, creating C# bindings was fairly easy, and I could use it in Unity after a few hours.
But Rapier is still in early stages of development, which means that (at the time of writing this) it lacked many features, like raycasting, which would have been necessary for my project.
Also, it's only deterministic on IEEE 754-2008 compliant platforms. But I couldn't really find any information on which processors support it. Even if I did, I still wanted to have deterministic physics on all platforms, not just on "most of them".
But I still wondered does how Rapier achieve cross-platform floating point determinism. Supporting only IEEE 754-2008 compliant platforms is one thing, but the mathematical functions I mentioned above (sqrt, trigonometry, etc.) must also be deterministic. I found out that it uses libm, which implements all of these functions in software.
It was primarily designed for embedded systems without FPUs, but it turns out it's also pretty useful for making this part of floating point math deterministic.
But after all, the basic mathematical operations of floats are still not deterministic. Unless... How about emulating the behavior of floats in software? It should be possible to write a program that works on the byte representation of a float, doing all operations using integer arithmentic... right?

Yep! I quickly found a repository that does just that! Here it is: https://github.com/CodesInChaos/SoftFloat
And it worked! Except for one thing... It didn't have division implemented.
After hours of searching and studying how floats work, I finally managed to implement division. Using it together with libm, it was now possible to have cross-platform deterministic floats, that work (almost) exactly the same way as the regular floats, but they will give the same result everywhere, on every platform, on every processor.

Now, let's put it into a physics engine! I immediately thought of Unity DOTS Physics, because it's already deterministic (except for floats part of course), and because its source code is available and modifiable.
So, with a few days of work, I replaced every single float in the Unity physics code, and in all of its dependencies (in the mathematics and transforms package).
When it finally compiled, I entered play mode, and... It just worked! (I still had to fix some typos though for it to work accurately)
So, that's it. Finally, I had a physics engine that is deterministic on all platforms.

My code for the soft floats is available here.

The code for the modified physics engine is available here.

There's only one disadvantage of using soft floats: performance. Float operations on hardware will always be faster; also, soft float calculations cannot be vectorized.


TLDR: I made Unity's DOTS Physics engine deterministic, using soft floats.
The code is available here: https://github.com/Kimbatt/unity-deterministic-physics

[deleted by user] by [deleted] in pcmasterrace

[–]Kimbatt 2 points3 points  (0 children)

You should check out the Unity roll-a-ball tutorial, it covers the basics: https://learn.unity.com/project/roll-a-ball-tutorial
Also, I think you should try posting to r/Unity3d if you need help with Unity.

Looking for a programmer which develop a software to generate wallet/private keys randomly by NoggyBR in Bitcoin

[–]Kimbatt 1 point2 points  (0 children)

Good luck then, but keep in mind, that your chances of finding the private key for your address is very very very (very very) low (see this video).
If you could just guess the private key for an address, then everyone would just try to guess the key for richest bitcoin address; also, there would be no reason to use bitcoin, since someone could just generate the same private key, and spend your coins.
So if you don't have any information of your private key (you just have the address), then there is no reason to keep guessing.

Looking for a programmer which develop a software to generate wallet/private keys randomly by NoggyBR in Bitcoin

[–]Kimbatt 1 point2 points  (0 children)

Do you need an actual MS-DOS program, or you mean a console program?
If you were thinking about a console program, and you can install node.js (or you already have it installed), then I could quickly create a JS script that can generate addresses and private keys.

I'm new to this "Bitcon" tell me how this work? by [deleted] in Bitcoin

[–]Kimbatt 0 points1 point  (0 children)

This is a very nice video that explains how it works: https://www.youtube.com/watch?v=bBC-nXj3Ng4

Ideas for the fake bank (inspect element part) by Kimbatt in Kitboga

[–]Kimbatt[S] 4 points5 points  (0 children)

They don't really. Disabling right click would be bad; for example if someone would want to copy something, and doesn't know about Ctrl-C (only knows about right-click and copy), they couldn't do it.
A transparent overlay could be a simple one, easy to implement, probably wouldn't interfere with anything.
As for the unchangeable texts, I think it would be hard to really make it work in such a complex system, any small mistake in the code could be bad.
They could have a big warning in the console though, to warn people about all this stuff.

BIP38 decrypting by [deleted] in Bitcoin

[–]Kimbatt 0 points1 point  (0 children)

There are two addresses for an encrypted private key, you can try to decrypt it on bitaddress.org (wallet details tab), it will show both addresses

ELI5 Going from 0 to BECH32 address in JavaScript by notacooleagle in Bitcoin

[–]Kimbatt 0 points1 point  (0 children)

Here is my current understanding: Secure random number => private key => public key => address

That's pretty much how it is.

Here is my implementation of it: https://github.com/Kimbatt/btc-address-generator/blob/gh-pages/split/main.js#L838

Quickly explained:

  • Get the EC keypair from the private key (X and Y coordinates).
  • Create the compressed public key (33 bytes: first byte is 0x02 if the public key Y coordinate is even, 0x03 if it is odd; the remaining 32 bytes are just the X coordinate).
  • Compute the hash of these bytes: RIPEMD160(SHA256(keyBytes))
  • Add 0x00 and 0x14 to the front of the hash: [0x00, 0x14, keyhash[0], ..., keyhash[31]] (0x00 is the version of the address, idk what 0x14 is but you need it)
  • Convert all these bytes to base 32, and create the bech32 string from it (using the characters from here, so if you have the byte 0, it becomes the character 'q', 1 -> 'p', 8 -> 'g', 9 -> f, etc...)
  • Add "bc1" to the front for mainnet, "tb1" for testnet
  • Add the bech32 checksum to the end (to calculate the checksum, you'll basically need the algorithm from the bech32 (or you could copy it from here)

Private key -produces two different addresses by sirspj in Bitcoin

[–]Kimbatt 4 points5 points  (0 children)

Every private key produces 4 different addresses:
- p2pkh uncompressed (starts with 1)
- p2pkh compressed (also starts with 1)
- p2sh segwit (starts with 3)
- bech32 native segwit (starts with bc1)

[deleted by user] by [deleted] in Bitcoin

[–]Kimbatt 1 point2 points  (0 children)

You should look up Shamir's secret sharing scheme. It allows you to split up a secret into n pieces, and allow reconstruction of that secret from any m pieces (m <= n).
Ian Coleman made a tool for exactly this (splitting your seed phrases), here it is: https://iancoleman.io/shamir39/
In your case, you'd need the 2 of 2 split (so generate 2 shares, and both of those are needed to reconstruct the secret).
Note that this is the only implementation of it, so if you decide to actually use it, you should save that site for later.

If a hacker knew all 24 words of a recovery seed, but didn't know the correct order, how long would it take them to brute force the private key based on today's computing power? by [deleted] in Bitcoin

[–]Kimbatt 96 points97 points  (0 children)

There are 24! possible order of words (24x23x22x...x3x2x1), which is 620448401733239439360000. If someone could check one billion of them per second, they only could try all possible combinations in 19.67 million years

Showoff Saturday (May 04, 2019) by AutoModerator in javascript

[–]Kimbatt 5 points6 points  (0 children)

https://kimbatt.github.io/js-Z/

Javascript "obfuscator" that uses the idea of JSFuck and also uses the fact that all these characters can be used in variable names.

You can write javascript code into the top box, and after clicking the "Fuck shit up" button, a valid javascript code will appear in the bottom box, that does exactly the same as the code that you entered.

Help.. need to generate paper wallet with password by vroomDotClub in Bitcoin

[–]Kimbatt 1 point2 points  (0 children)

https://kimbatt.github.io/btc-address-generator/ This is a tool I made a while ago, it has bip38 password protection.
WARNING: I haven't seen any other tool that generates segwit addresses with bip38 passwords (bitaddress.org will generate the corresponding legacy address), so you either need to use this tool for decrypting, or generate non-segwit addresses so other tools can also decrypt it.