CoCalc Public FilesSage Procedures / Final Exam Legendre.sagews
Views : 106
Compute Environment: Ubuntu 20.04 (Default)

######################################################################################################################
#                                                                                                                    #
#                                FINAL EXAM: LEGENDRE COVERT CHANNEL ATACK FOR 40-BITS KEY                           #
#                                                                                                                    #
######################################################################################################################

# Given 8 primes
# First five code block contents (5 bits per block)
# Last 3 code block number  (for 8 blocks)
# 40 bits is one block
# The "y" values carry information
# Legendre of a y value against the first five primes gives the bit-content, and the legendre value against
#the last three primes gives the block number in binary (0, 1, 2, 3, 4, 5, 6, 7) and we convert to 1, 2, 3, 4, 5, 6, 7, 8 by adding 1.

import numpy
import itertools
import sympy.ntheory
import math

def NewLegendre(a,p):
if is_prime(p):
return (1+kronecker(a,p))/2
else:
print("Second Parameter Must Be Prime")

# Last three primes code block number (for 8 blocks)

def BlockNumber(y,primelist):
m=0
for j in range(3):
m=2*m + NewLegendre(y,primelist[j+5])
return m+1

# First five primes code block contents from y (5 bits per block)

def BlockContents(y,primelist):
k=0
for j in range(5):
#print(j)
k=10*k+NewLegendre(y,primelist[j])
return k

# Then the blocks are pasted together in correct order to give a 40-bit quantity.

def KeyDiscover(ylist,primelist):
c=10^5
k=0
for j in range(8):
#print(j)
d = 8-BlockNumber(ylist[j],primelist)
k = k+(BlockContents(ylist[j],primelist))*(c^d)
return Integer("0b"+str(k))