1

I am using Solidity to write an Ethereum smart contract.

I have a specific use case that requires a piece of data to be encrypted on the chain, despite the fact that both the plaintext and the ciphertext will be visible to everyone.

So far all the answers I have read have simply pointed out, correctly, that encryption on the chain is not a good idea. As mentioned, this is a very specific use case.

Are there any libaries or functions that could make this work?

  • Yes, since both the plaintext and the ciphertext will be visible to everyone, a good enough function would be function encrypt(string plaintext) public pure returns (string) {return plaintext;} – goodvibration May 26 at 6:48
  • The use case is this: I need to check that a message submitted is the same as a message that has been encrypted with a public key. The only way to do this is to also encrypt the submitted message with same public key, and then hash both messages. – Burrough Clarke May 26 at 6:54
  • 1
    Well that does not require encryption, that requires verification, and for that you can use Solidity's built-in function keccak256. – goodvibration May 26 at 7:00
  • So how are you suggesting you compare the hashes entirely via a smart contract when one is encrypted and the other isn't? – Burrough Clarke May 26 at 7:05
  • I cannot answer this, because I am not sure what exactly your use-case is (you haven't posted a single line of code). – goodvibration May 26 at 7:32
1

@goodvibration is right. From the comment, this appears to be more a signature verification problem. It would be much lighter to use ecrecover:

  • You want to verify the encryption result is generated by a particular public key.
  • One solution, as you have suggested, is to let every node re-execute the encryption and compare. (note the gas cost is unlikely to be cheap)
  • An alternative solution:
    1. Encrypt as usual.
    2. Use the same public key's paired private key to generate a signature for the encryption output. (As suggested by the above-linked post, you may need to hash the encryption output to make it 32 bytes)
    3. The resulting r, s, v are saved in the blockchain.
    4. That is it. Now everyone can verify with the saved info. First, hash the encryption output (to get the 32 bytes). Second, use ecrecovery with the saved r, s, v, the result is an address, which can be compared against by the public key's associated address (see more here)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.