-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ecdsa example, and is_symbol_referenced in internalcompiler
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 26, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Private Key: 1\n", | ||
"Public Key: 2\n", | ||
"Message: 0\n", | ||
"Signature: (2, 2)\n", | ||
"Signature valid: True\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import random\n", | ||
"\n", | ||
"# Extremely simplified parameters\n", | ||
"P = 3 # Prime field\n", | ||
"G = 2 # Generator\n", | ||
"N = 3 # Order of generator\n", | ||
"\n", | ||
"\n", | ||
"def mod_inv(a, m):\n", | ||
" return 1 if a == 1 else 2 # Only valid for P = 3\n", | ||
"\n", | ||
"\n", | ||
"def generate_keypair():\n", | ||
" private_key = random.randint(1, 3) # 2-bit key (1 to 3)\n", | ||
" public_key = pow(G, private_key, P)\n", | ||
" return private_key, public_key\n", | ||
"\n", | ||
"\n", | ||
"def sign(message, private_key):\n", | ||
" z = message % N\n", | ||
" k = random.randint(1, 3) # 2-bit nonce\n", | ||
" r = pow(G, k, P)\n", | ||
" s = (mod_inv(k, N) * (z + r * private_key)) % N\n", | ||
" return (r, s)\n", | ||
"\n", | ||
"\n", | ||
"def verify(message, signature, public_key):\n", | ||
" r, s = signature\n", | ||
" z = message % N\n", | ||
" w = mod_inv(s, N)\n", | ||
" u1 = (z * w) % N\n", | ||
" u2 = (r * w) % N\n", | ||
" v = (pow(G, u1, P) * pow(public_key, u2, P)) % P\n", | ||
" return v == r\n", | ||
"\n", | ||
"\n", | ||
"# Example usage\n", | ||
"private_key, public_key = generate_keypair()\n", | ||
"message = int(\"0b0\", 2) # Single character message\n", | ||
"signature = sign(message, private_key)\n", | ||
"is_valid = verify(message, signature, public_key)\n", | ||
"\n", | ||
"print(f\"Private Key: {private_key}\")\n", | ||
"print(f\"Public Key: {public_key}\")\n", | ||
"print(f\"Message: {message}\")\n", | ||
"print(f\"Signature: {signature}\")\n", | ||
"print(f\"Signature valid: {is_valid}\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "qlasskit_310-env", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters