Difference between revisions of "Sage Sandbox"

From YobiWiki
Jump to navigation Jump to search
m (New page: Back to SAGE & cryptology ==Helper functions== Conversion from int or long to raw string and back is [http://bugs.python.org/issue1023290 discussed on the Python bug tracking list] bu...)
 
Line 2: Line 2:
   
 
==Helper functions==
 
==Helper functions==
Conversion from int or long to raw string and back is [http://bugs.python.org/issue1023290 discussed on the Python bug tracking list] but since the problem is not yet solved, let's see how to make our own helper functions in a clean and fast way:
+
Conversion from int or long to raw string and back is [http://bugs.python.org/issue1023290 discussed on the Python bug tracking list] but since the problem is not yet solved, let's see how to make our own helper functions in a clean and fast way: (measures with %timeit of ipython)
 
<source lang=python>
 
<source lang=python>
 
############################### long2string ########################################
 
############################### long2string ########################################
Line 26: Line 26:
 
long2string(1234567890123456789012345)
 
long2string(1234567890123456789012345)
 
#16.4 µs per loop
 
#16.4 µs per loop
  +
###################################################################################
  +
def long2string(i):
  +
return ''.join([chr(i>>j & 0xFF) for j in range(int(math.log(i,2)//8)<<3,-1,-8)])
  +
long2string(123456789012345678901234)
  +
#15.5 µs per loop
  +
long2string(1234567890123456789012345)
  +
#16.7 µs per loop
 
###################################################################################
 
###################################################################################
 
import math
 
import math

Revision as of 18:00, 20 August 2008

Back to SAGE & cryptology

Helper functions

Conversion from int or long to raw string and back is discussed on the Python bug tracking list but since the problem is not yet solved, let's see how to make our own helper functions in a clean and fast way: (measures with %timeit of ipython)

###############################  long2string  ########################################
def long2string(i):
	l=[]
	while i:
		l[0:0]=chr(i&0xff)
		i=i>>8
	return ''.join(l)
long2string(123456789012345678901234)
#46 µs per loop
long2string(1234567890123456789012345)
#51.8 µs per loop
###################################################################################
def long2string(i):
	l=[]
	while i:
		l.append(chr(i&0xff))
		i=i>>8
	return ''.join(l[::-1])
long2string(123456789012345678901234)
#15.1 µs per loop
long2string(1234567890123456789012345)
#16.4 µs per loop
###################################################################################
def long2string(i): 
        return ''.join([chr(i>>j & 0xFF) for j in range(int(math.log(i,2)//8)<<3,-1,-8)])
long2string(123456789012345678901234)
#15.5 µs per loop
long2string(1234567890123456789012345)
#16.7 µs per loop
###################################################################################
import math
def long2string(i):
	return binascii.unhexlify('%0*x' % (int(math.ceil(math.log(i,2)/8)*2) , i))
long2string(123456789012345678901234)
#4.77 µs per loop
long2string(1234567890123456789012345)
#4.83 µs per loop
###################################################################################
def long2string(i):
	return binascii.unhexlify('%0*x' % (len(hex(i)) & -2 , i)).lstrip('\x00')
long2string(123456789012345678901234)
#3.64 µs per loop
long2string(1234567890123456789012345)
#3.78 µs per loop
###################################################################################
binascii.unhexlify('%0*x' % (32 , 123456789012345678901234))
#2.06 µs per loop
binascii.unhexlify('%0*x' % (32 , 1234567890123456789012345))
#2.01 µs per loop
#Ok if you want fixed length buffer, e.g. 32 bytes
###################################################################################
def long2string(i):
    s=hex(i)[2:].rstrip('L')
    if len(s) % 2:
        s='0'+s
    return binascii.unhexlify(s)
long2string(123456789012345678901234)
#2.88 µs per loop
long2string(1234567890123456789012345)
#3.22 µs per loop
###################################################################################
def long2string(i):
    s='0'+hex(long(i))[2:-1]
    return binascii.unhexlify(s[len(s) % 2:])
long2string(123456789012345678901234)
#3.04 µs per loop
long2string(1234567890123456789012345)
#3.03 µs per loop
###################################################################################
def long2string(i):
    s=hex(long(i))[2:-1]
    if len(s) % 2:
        s='0'+s
    return binascii.unhexlify(s)
long2string(123456789012345678901234)
#2.75 µs per loop
long2string(1234567890123456789012345)
#3.06 µs per loop
###################################################################################
#If we are sure our argument is a long we can remove the cast in the 2 previous codes:
def long2string(i):
    s='0'+hex(i)[2:-1]
    return binascii.unhexlify(s[len(s) % 2:])
long2string(123456789012345678901234)
#2.66 µs per loop
long2string(1234567890123456789012345)
#2.62 µs per loop
###################################################################################
def long2string(i):
    s=hex(i)[2:-1]
    if len(s) % 2:
        s='0'+s
    return binascii.unhexlify(s)
long2string(123456789012345678901234)
#2.42 µs per loop
long2string(1234567890123456789012345)
#2.76 µs per loop
###################################################################################
# There is still room ;-)
binascii.unhexlify('1a249b1f10a06c96aff2')
#990 ns per loop
###################################################################################
###############################  string2long  ########################################
def string2long(s):
	i=0
	for c in s:
		i=(i<<8)+ord(c)
	return i
string2long('\x01\x05n\x0f6\xa6D=\xe2\xdfy')
#8.91 µs per loop
###################################################################################
int('\x01\x05n\x0f6\xa6D=\xe2\xdfy'.encode('hex'),16)
#3.15 µs per loop
###################################################################################
int(binascii.hexlify('\x01\x05n\x0f6\xa6D=\xe2\xdfy'),16)
#1.64 µs per loop
###################################################################################
long(binascii.hexlify('\x01\x05n\x0f6\xa6D=\xe2\xdfy'),16)
#1.38 µs per loop