Ticket #7010: trac_7010-key-check.patch

File trac_7010-key-check.patch, 3.3 KB (added by mvngu, 12 years ago)

apply on top of previous patch

  • sage/crypto/classical.py

    # HG changeset patch
    # User Minh Van Nguyen <nguyenminh2@gmail.com>
    # Date 1254673371 25200
    # Node ID 454c2fcbbe79057f27ce3f49aa2eb3f458d4fbb9
    # Parent  c518b1d9f5f047fb07eb87ba91ffc44734268aa9
    trac 7010: sanity check the key value of the shift cryptosystem
    
    diff -r c518b1d9f5f0 -r 454c2fcbbe79 sage/crypto/classical.py
    a b  
    977977            sage: K; invK
    978978            0
    979979            0
     980
     981        TESTS:
     982
     983        The key ``K`` must satisfy the inequality `0 \leq K < n` with `n`
     984        being the size of the plaintext, ciphertext, and key spaces. For the
     985        shift cryptosystem, all these spaces are the same alphabet. This
     986        inequality must be satisfied for each of the supported alphabets.
     987        The capital letters of the English alphabet::
     988
     989            sage: S = ShiftCryptosystem(AlphabeticStrings())
     990            sage: S.inverse_key(S.alphabet_size())
     991            Traceback (most recent call last):
     992            ...
     993            ValueError: K (=26) is outside the range of acceptable values for a key of this shift cryptosystem.
     994            sage: S.inverse_key(-1)
     995            Traceback (most recent call last):
     996            ...
     997            ValueError: K (=-1) is outside the range of acceptable values for a key of this shift cryptosystem.
     998
     999        The hexadecimal number system::
     1000
     1001            sage: S = ShiftCryptosystem(HexadecimalStrings())
     1002            sage: S.inverse_key(S.alphabet_size())
     1003            Traceback (most recent call last):
     1004            ...
     1005            ValueError: K (=16) is outside the range of acceptable values for a key of this shift cryptosystem.
     1006            sage: S.inverse_key(-1)
     1007            Traceback (most recent call last):
     1008            ...
     1009            ValueError: K (=-1) is outside the range of acceptable values for a key of this shift cryptosystem.
     1010
     1011        The binary number system::
     1012
     1013            sage: S = ShiftCryptosystem(BinaryStrings())
     1014            sage: S.inverse_key(S.alphabet_size())
     1015            Traceback (most recent call last):
     1016            ...
     1017            ValueError: K (=2) is outside the range of acceptable values for a key of this shift cryptosystem.
     1018            sage: S.inverse_key(-1)
     1019            Traceback (most recent call last):
     1020            ...
     1021            ValueError: K (=-1) is outside the range of acceptable values for a key of this shift cryptosystem.
    9801022        """
    981         # Let A be the alphabet of this cryptosystem and let n be the number
    982         # of elements in A. If k is a key, then the corresponding inverse
    983         # key is -k mod n.
    984         return self.key_space()(-Integer(K)).lift()
     1023        # Sanity check: the key K must satisfy the inequality
     1024        # 0 <= K < n with n being the size of the plaintext, ciphertext, and
     1025        # key spaces. For the shift cryptosystem, all these spaces are the
     1026        # same alphabet.
     1027        if 0 <= K < self.alphabet_size():
     1028            # Let A be the alphabet of this cryptosystem and let n be the
     1029            # number of elements in A. If k is a key, then the corresponding
     1030            # inverse key is -k mod n.
     1031            return self.key_space()(-Integer(K)).lift()
     1032        else:
     1033            raise ValueError("K (=%s) is outside the range of acceptable values for a key of this shift cryptosystem." % K)
    9851034
    9861035    def random_key(self):
    9871036        r"""