OKlibrary  0.2.1.6
Conversions.mac
Go to the documentation of this file.
00001 /* Matthew Gwynne, 2.3.2007 (Swansea) */
00002 /* Copyright 2008, 2010 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00023 oklib_include("OKlib/ComputerAlgebra/DataStructures/Lisp/Lists.mac")$
00024 oklib_include("OKlib/ComputerAlgebra/NumberTheory/Lisp/Auxiliary.mac")$
00025 
00026 /* Pads a string s to the left with Maxima-character c until it is at least
00027    n characters in length (n is an arbitrary integer): */
00028 lpad(st, c, n) := simplode([smake(max(n-slength(st),0),c), st])$
00029 /* Pad a list of numbers with leading zeros to reach the
00030    full block-length 16:
00031 */
00032 pad_block_16(L) := append(create_list(0,i,1,16-length(L)), L)$
00033 
00034 /* Converts an integer to a polynomial with coefficients in the given base by
00035    converting the integer to a number in that base and taking each digit as a
00036    coefficient of increasing powers of "x". */
00037 /* MG: Should this handle coeff_base = 0? */
00038 /* MG: Should this take the variable the polynomial should be in? */
00039 int2poly(n,coeff_base) := 
00040   expand(lreduce(lambda([a,b], a * x + b),
00041       map(digit2int,str2chrl(int2polyadicstr(n,coeff_base))),0))$ 
00042 
00043 /* Converts the given polynomial to an integer, assuming that the polynomial
00044    in "poly_x" represents an integer in the given base "poly_b". */
00045 /* MG : Should this return an error expression if "poly" isn't univariate? */
00046 poly2int(poly, poly_b, poly_x) := subst(poly_b,poly_x,poly)$
00047 
00048 /* Converts a hexidecimal string to a list of GF(2^8) elements:
00049 
00050    Note in particular that it is assumed in this function that the hexidecimal
00051    string amounts to a representation of a matrix of bytes, which is given
00052    column by column and is transformed into a list of polynomials over GF(2^8)
00053    which represents the matrix column by column. */
00054 hex2gf2t8l(hex) :=
00055   map(lambda([a],int2poly(polyadicstr2int(simplode(a),16),2)),
00056     partition_elements(str2chrl(hex),2))$
00057   
00058 /* Converts a list of GF(2^8) elements to a hexidecimal string:
00059 
00060    Note in particular that it is assumed in this function that list of 
00061    polynomials over GF(2^8) represents the matrix of polynomials given 
00062    column by column in the list and the returned hex string is a hexidecimal
00063    representation of the matrix given column by column. */
00064 gf2t8l2hex(lst) :=
00065     simplode(flatten(map(lambda([a], lpad(simplode([int2polyadicstr(a,16)]),"0",2)),  
00066       subst(2,x,lst))))$
00067 
00068 /* Converts a hexidecimal string in the typical notation associated with
00069    Rijndael to a list of GF(2^8) elements represented by use of integers. 
00070    The result list is used in the CryptoSystems AES implementation. */
00071 hex2il(h) := 
00072   map(lambda([b], hex2int(simplode(b))), partition_elements(charlist(h),2))$
00073 
00074 /* Converts a list of GF(2^8) elements represented by use of integers to a 
00075    hexidecimal string in the typical notation associated with Rijndael . 
00076    The input list is used in the CryptoSystems AES implementation. */
00077 il2hex(lst) := uaapply(sconcat, map(lambda([s],lpad(int2hex(s),"0",2)),lst))$
00078 
00079