# HG changeset patch
# User Martin Geisler <[EMAIL PROTECTED]>
# Date 1214169868 -7200
# Node ID 48f52da60f334d77732e73cb3af2c04c7a07d91d
# Parent  33f8fbf147a76ce6aec9c36ddea41d6920084968
Basic implementation of the ElGamal crypto system.

diff --git a/viff/elgamal.py b/viff/elgamal.py
new file mode 100644
--- /dev/null
+++ b/viff/elgamal.py
@@ -0,0 +1,48 @@
+# Copyright 2008 VIFF Development Team.
+#
+# This file is part of VIFF, the Virtual Ideal Functionality Framework.
+#
+# VIFF is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License (LGPL) as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# VIFF is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+# Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
+
+"""The ElGamal crypto system."""
+
+from viff.util import rand
+from viff.field import GF
+
+# Common generator for all keys.
+G = 3
+
+def generate_key_pair(p):
+    """Generate a ``(pk, sk)`` key pair for the group Zp^*."""
+    # We calculate with normal Python (long) integers to ensure that
+    # we can easily save/load keys.
+    a = rand.randint(1, p - 2)
+    g_a = pow(G, a, p)
+    pk = (p, g_a)
+    sk = (p, a)
+    return (pk, sk)
+
+def encrypt(m, pk):
+    """ElGamal encryption."""
+    p, g_a = pk
+    k = rand.randint(1, p - 2)
+    gamma = pow(G, k, p)
+    delta = (m * pow(g_a, k, p)) % p
+    return (gamma, delta)
+
+def decrypt(c, sk):
+    """ElGamal decryption."""
+    gamma, delta = c
+    p, a = sk
+    return (pow(gamma, p - 1 - a, p) * delta) % p
_______________________________________________
viff-patches mailing list
[email protected]
http://lists.viff.dk/listinfo.cgi/viff-patches-viff.dk

Reply via email to