I'm writting DApp on Ethereum (Solidity) and I need to find a solution how to store a private data on blockchain, when I also need to proceed them somehow. If it is only about storing, I can use some normal encrypting, but the problem is that I need to read the data IN the smart contract and proceed it somehow too.

Let's say:

1) I want to send some private number to a blockchain.

2) I need to check if the privete number is bigger than the last stored private number and smaller than the second last stored number.

if (storage[n] < y < storage[n-1]) storage.push(y);

3) If yes, I want to store it privately.

Any ideas? Thank you.

3 Answers 3


All contract execution is public on Ethereum. After all, that is how a node can compute the next state. So you cannot have private computation.

If you wish to stay in the Ethereum ecosystem, you can have a look at Quorum and its private transactions.

This is not your use case, but it is possible to (only) store private data in Ethereum. Just store the hash of it, and keep the real content private. You can later prove you had it, because you knew its hash. But that's about it.

  • Thanks, unfortunately I need to proceed the dada from a contract somehow and let them interact together, so the "only storing" is not my use case. Jan 10, 2019 at 13:36

As Xavier already said, with plain Ethereum you can not shield the data used in smart contracts. However, you could use zero knowledge proofs for this. Basically, with ZK-SNARKs, you can offload the smart contract code and private data from the chain, but let the chain validate that it's running according to a predefined algorithm.

If you store your numbers onchain as salted hashes for instance, you could have a ZK-SNARKs Programm (here written in ZoKrates DSL) of the form:

def (private field salt, private field y, private n, private n_1, field yHashA, yHashB, field nHashA, field nHashB, field n_1HashA, field n_1HashB) -> (field)
    y0, y1 = sha256packed(0, 0, salt, y)
    y0 == yHashA
    y1 == yHashB
    // ... same here for n and nHashA/B
    // ... same here for n_1 and n_1HashA/B
    // at this points you proved that you private fields match their corresponding public values (salted hashes)
    // now you can check the constraint on y
    field ok = if n < y && y < n_1 then 1 else 0 fi
    return ok

Now onchain, you can store salted hashes of your numbers and let the veifier for the program above verify that provided proof for the plain value satisfies the constraint storage[n] < y < storage[n-1]

See ZoKrates for tutorials and examples - https://zokrates.github.io

The interesting thing is that you can write additional zk-snarks for doing some other processing on the same values.

Also interesting to read: https://blog.decentriq.ch/zk-snarks-primer-part-one/


Given your very particular needing, may be you could:

1) define/found a pseudo-cryptographic reversible transformation F which maintains the property that

 if a > b then F(a) > F(b)

2) You could apply that transformation off-chain (I.e. before to use the smart contract) and you should keep it strictly secret;

3) you could then do your comparations between keys in the smart contract and store the winning key in that encoded way in blockchain, nobody who do not know your coding can understand it

4) when you should need to decrypt, you could read the winning value from blockchain and you should decode it off chain

A simple transformation of the required above mentioned type can be any monotonic math function, defined in any way, i.e. any suitable y=f(x)

Of course someone can work on it trying to decode the whole, but if you maintain it secret and DO NOT give out information useful to understand (i.e. a series of input/output couples) if not when “the game” is over, it can work.

In the case your application permits it, you can change quite often the codifying function. At every new “game” run, for instance.

Hope this can help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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