Skip to content

Instantly share code, notes, and snippets.

@JacobEberhardt
JacobEberhardt / demo.py
Created March 19, 2019 15:12
Python example of EdDSA signature generation to be used with ZoKrates EdDSA verification.
import hashlib
from zokrates.eddsa import PrivateKey, PublicKey
from zokrates.field import FQ
from zokrates.utils import write_for_zokrates_cli
if __name__ == "__main__":
raw_msg = "This is my secret message"
msg = hashlib.sha512(raw_msg.encode("utf-8")).digest()
# sk = PrivateKey.from_rand()
# Seeded for debug purpose
key = FQ(1997011358982923168928344992199991480689546837621580239342656433234255379025)
import "hashes/sha256/1024bitPadded.code" as sha256
import "ecc/edwardsScalarMult.code" as scalarMult
import "ecc/edwardsAdd.code" as add
import "utils/pack/unpack256.code" as unpack256
import "ecc/edwardsOnCurve.code" as onCurve
import "ecc/edwardsOrderCheck.code" as orderCheck
/// Verifies an EdDSA Signature.
///
/// Checks the correctness of a given EdDSA Signature (R,S) for the provided
import "ecc/edwardsAdd.code" as add
import "ecc/edwardsScalarMult.code" as multiply
import "utils/pack/unpack256.code" as unpack256
/// Verifies match of a given public/private keypair.
///
/// Checks if the following equation holds for the provided keypair:
/// pk = sk*G
/// where G is the chosen base point of the subgroup
/// and * denotes scalar multiplication in the subgroup
/**
* ZoKrates Grammar
* Author: Jacob Eberhardt
*/
// TODO:
// exclude language keywords as identifiers
// Ignore linebreak after \
// Skip Whitespaces and Block Comments and Line Comments
// associativit and precedence table for operators
<prog> ::= <functions>
<functions> ::= <function> <functions>
<function> ::= `def' <ide> `(' <arguments> `):\\n' <stat-list>
<arguments> ::= <ide> <more-args> | $\varepsilon$
<more-args> ::= `,' <ide> <more-args> | $\varepsilon$
<stat-list> ::= <statement> <stat-list> | `return' <expressions> `\\n'
<expressions> ::= <expr> <more-expr> | $\varepsilon$
<more-exprs> ::= `,' <ide> <more-args> | $\varepsilon$
<statement> ::= <ide> <statement'>
| `if' <expr> <comparator> <expr> `then' <expr> `else' <expr> `fi' <expr'> `==' <expr> `\\n'
// Sudoku of format
// | a11 | a12 || b11 | b12 |
// --------------------------
// | a21 | a22 || b21 | b22 |
// ==========================
// | c11 | c12 || d11 | d12 |
// --------------------------
// | c21 | c22 || d21 | d22 |