# Smart contract datum encryption

Is there any way to make datum data only visible to a smart contract that owns the UTXO?

For example, lets say I wanted to make a guessing game. In this game, person A selects a number and creates an output at the smart contract with a prize (some amount of ADA) and a number in the datum of the UTXO. A second player B can then try to guess the number. For each guess, the player has to pay a certain amount of ADA to the contract. If he is right, all the ADA in the contract is given to him. If he is wrong, the ADA he provided is added to the prize pool. In order for the contract to determine if the guess is correct, it should evaluate the number provided by player B to the number stored in the datum UTXO, provided by player A.

However, datum data is public. It can be retrieved by everyone which means player B can look at the datum of the UTXO at the contract, see what number it holds and guarantee a win. Is there a way (in the smart contract) to enshure the number at the UTXO can only be evaluated under the pre-determined conditions of the smart contract code?

For example, could player A encrypt the number using the public key of the smart contract address, where the smart contract could decrypt the data using its private key on evaluation? This isn't possible since a smart contract doesn't have a private key, therefore, is there another way to achieve this?

• Perhaps you could store a hash of the number instead of the number directly, then in the script check that the hash of the guess matches? Jun 13, 2021 at 15:49
• If its just a hash of a number, user B could check a lot of numbers and look for a corresponding hash. What could work is letting user A pick a nonce which is added to the number before hashing. After user B shows his guess user A could then provide the nonce to prove that the guess of user B was incorrect. This is the same as what is done in plutus pioneer lesson 7 but it adds extra transactions. Also, once player A reveals the nonce, the game has to stop since it again becomes possible to brute force a matching hash. Jun 13, 2021 at 18:50
• The game could continue if player A changes the hash to a new hash where the new hash is generated using the old hash and a new nonce. This enshures that the original number is still the same while preventing a brute force attack. Jun 13, 2021 at 19:02
• "If its just a hash of a number, user B could check a lot of numbers and look for a corresponding hash". Isn't this mostly true for the encryption too? The script could easily be run without needing to submit a real transaction to the blockchain, so it could be brute forced? Jun 14, 2021 at 6:38
• That depends on the "encryption". Hashing a number using sha will result in the same hash every time, the hash only changes if the content that is hashed changes. This way, if player A only hashes the number, player B could hash all the numbers that are supported by the 64? bit representation of the number and search for a matching hash. When you talk about encryption you probably mean public-key encryption which takes a know public key and uses it to encrypt some data. The data can then only be decrypted with the matching private key. Jun 14, 2021 at 14:58