root / trunk / samples / blinding.py

Revision 312, 3.9 kB (checked in by ocnils, 3 years ago)

splitted ring functions and blinding test

Line 
1""" ########################################################################
2    Short introduction to RSA encryption, signature and blinding.
3    License:    GPL
4    Copyright:  Nils Toedtmann 2007 <nils@opencoin.net>
5                Joerg Baach <mail1@baach.de>
6    Version:    2007-11-25-04
7    ########################################################################
8
9    Preliminary note you may or may not skip:
10
11    All calculations will be executed modulo some natural number N. Maths
12    calls the set {0,..,N-1} together with the operations +, - and * the
13    "ring of residues modulo N". These operations are the same as for
14    integers plus the "mod" operation (ok, i simplify a bit ;-)
15
16    A number is invertible modulo N (= you can divide by it modulo N) iff
17    it is coprime to N (= y and N have no common divisor). But in contrast
18    to +, - and *, the division is quite different. So telling python
19   
20        x_divided_by_y_modulo_N = x / y % N
21
22    does not work. We use a slightly altered "extended euclidian algorithm"
23    (usually used to compute the GCD of two numbers) for division mod N
24    (see devide below)
25   
26    ########################################################################
27    RSA 1: KEY GENERATION
28
29    Bob (key owner) selects two random primes (~1024bits) and calculates
30    their product (~2048bits, "RSA key size") which will be our modulus N.
31    Except key generation *all* computations will be %N.
32   
33    >>> p = 37
34    >>> q = 61
35    >>> N = p* q
36
37    The key pair are two numbers <N such that
38
39        public_key * secret_key == 1  modulo   (p-1)*(q-1)
40
41    It is unfeasible without knowing the factorization of N, but knowing
42    p and q just select a random public_key and invert it. If it turns out
43    to be not coprime to (p-1)(q-1), select an other public_key.
44   
45    >>> public_key  = 101
46    >>> secret_key  = divide(1,public_key,(p-1)*(q-1))
47
48    ########################################################################
49    RSA 2: ENCRYPTION
50
51    Alice (sender) select's Message <N to send. Fetches Bob's public key
52    (including the modulus N) and encrypts with it.
53
54    >>> cleartext  = 1465
55    >>> ciphertext = cleartext**public_key % N
56
57    Bob (receiver) decrypts using his secret key. Should gain ClearText.
58   
59    >>> decryptedtext = ciphertext**secret_key  %N
60    >>> decryptedtext == cleartext
61    True
62
63    ########################################################################
64    RSA 3: SIGNATURE
65
66    Bob (signer) select's Message <N (usually the hash of a longer text) and
67    signs it using his secret key.
68   
69    >>> message = 755
70    >>> signature = message**secret_key %N
71
72    Alice (verifier) verifies using Bob's public key. Should gain Message.
73
74    >>> verified_signature = signature**public_key %N
75    >>> verified_signature == message
76    True
77
78
79    ########################################################################
80    RSA 4: BLIND SIGNATURE
81
82    Alice (requester) selects a blank <N she wants the issuer to sign and a
83    random number ("blinder") for blinding. She creates the blind and keeps
84    the blinder secret:
85
86    >>> blank = 12
87    >>> blinder = 101
88    >>> blind = blank * (blinder**public_key) %N
89
90    Bob (issuer) signs the blind with his secret key
91   
92    >>> signature_of_blind = blind**secret_key %N
93
94    Alice unblinds the signed blind
95   
96    >>> unblinded_signature_of_blind= divide( signature_of_blind, blinder, N )
97
98    If somebody had the requester's blank *and* the issuer's secret key,
99    he could have directly computed this:
100   
101    >>> signature_of_blank = blank**secret_key %N
102    >>> unblinded_signature_of_blind == signature_of_blank
103    True
104
105    Alice uses the ususal signature verification algorithm:
106   
107    >>> (unblinded_signature_of_blind**public_key %N) == blank
108    True
109"""
110
111
112from eea import divide
113
114
115def _test():
116    import doctest
117    doctest.testmod()
118
119if __name__ == "__main__":
120    _test()
Note: See TracBrowser for help on using the browser.