Tezos
$1.12 0.64%
XTZ · 3w

Proving Knowledge of a Hash Pre-Image with ZoKrates

In this blog we implement a problem that's very typical in blockchain use-cases: proving the knowledge of a pre-image for a given SHA-256 digest.

We will begin demystifying this machinery by computing the SHA-256 hash of the number 5. Towards this end, we will navigate through multiple implementations of this hash function using different programming languages: Python (accessible and easy to debug), ZoKrates (to compile the zkSNARKs program) and Solidity (to close the loop with the Ethereum blockchain).

In the second part, we show how ZoKrates and the Ethereum blockchain can be used to allow a prover, let’s call her Peggy, to demonstrate beyond any reasonable doubt to a verifier, let’s call him Victor, that she knows a hash pre-image for a chosen digest by Victor, without revealing what the pre-image is.

Pre-requisites

Make sure you have followed the instructions in the Getting Started ZoKrates documentation and are able to run the "Hello World" example described there.

Note that you also need a working Python environment to reproduce the full tutorial. The Python section, however, is optional and for educational purpose only.

Python Implementation

We will start by implementing our example hash problem using Python.

Have a look at the following Python code:

import hashlib preimage = bytes.fromhex('00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05') bin(int(preimage.hex(), 16)) //binary representation of pre-image //output is '0b101' hashlib.sha256(preimage).hexdigest() //compute hash //output is //'c6481e22c5ff4164af680b8cfaa5e8ed3120eeff89c4f307c4a6faaae059ce10'

Let's go through this example line-by-line.

Clearly, the first line imports the hashlib module which implements most of the common secure hash and message digest algorithms.

Continue on blog.decentriq.ch
Recent news
XTZ +0.64% · medium.com · 10h

Introducing Kiln – Obsidian Systems – Medium

Bread, pastries, and lots of other delicious foods are baked in an oven. If you are baking bricks or other building blocks, you need a kiln. Presently, Obsidian Systems’ Kiln is an application to…