SlideShare a Scribd company logo
1 of 12
For any help regarding Python Homework Help
visit : - https://www.pythonhomeworkhelp.com/,
Email :- support@pythonhomeworkhelp.com or
call us at :- +1 (315) 557-6473
Problem 1. Kalns
Ben Bitdiddle has designed a new cryptosystem called Kalns, but we suspect it might not be as strong
as we would like to be. Therefore we ask your help to break it.
In this problem we will be working with a finite field GF16. The elements of our field are all 4-bit strings.
The field addition is computed as xor: GF16(x) + GF16(y) = GF16(x ⊕
y). We provide the two tables describing addition and multiplication laws on the course web page.
If you are curious, these tables are obtained by interpreting 4-bit field elements as degree≤
4 polynomials over GF2 and performing addition and multiplication modulo the irreducible polynomial
x4 + x + 1.
However, for the purposes of this problem you do not need to understand how our GF16 is constructed;
the solutions we know assume black-box access to GF16. We have provided a GF16 implementation for
you.
Kalns is a 64-bit block cipher. The secret key consists of three parts:
•an invertible 16-by-16 matrix A over GF16;
•a 16-element vector b over GF16; and
•a permutation (bijection) S that maps GF16 one-to-one and onto GF16.
https://www.pythonhomeworkhelp.com/
To encrypt a 64-bit block B we first break it up in sixteen 4-bit chunks and interpret each of them as a
GF16 element. So block B corresponds to length 16 vector x = (x0, . . . , x15) over GF16.
The encryptions consists of the following: y = S(Ax + b), where the permutation S is individually applied
to each of 16 elements of v = Ax + b. The 16-element vector y is later re-interpreted as 64-bit integer to
obtain the encrypted block B'.
(a) Ben suspects that his cryptosystem is very secure. After all it has around 16162 1616 16! 21132.25
possible keys. However, we suspect that there are many equivalent keys. These keys have different
values for (A, b, S), but produce the same ciphertext for any given plaintext. Is our suspicion well-
founded?
(b) Describe a chosen-ciphertext attack on Kalns that recovers the unknown key (A, b, S) or an
equivalent key.
https://www.pythonhomeworkhelp.com/
Solution:
class GF16(object):
"""Implementation of GF(2^4) as degree 4 polynomials over GF(2) modulo
x^4 + x + 1.
"""
def __init__(self, val):
if not (isinstance(val, int) or isinstance(val, long)) or val < 0 or val > 15:
raise ValueError("GF16 elements must be constructed from integers from 0 to 15.")
self.val = val
def __repr__(self):
return "GF16(%d)" % self.val
def __add__(self, other):
if not isinstance(other, GF16):
raise ValueError("Addition is only defined between a GF16 element and GF16 element")
return GF16(self.val ^ other.val)
def __sub__(self, other):
if not isinstance(other, GF16):
raise ValueError("Subtraction is only defined between a GF16 element and GF16 element")
# +1 equals -1 (mod 2), so both operations are XOR
return GF16(self.val ^ other.val)
https://www.pythonhomeworkhelp.com/
def __neg__(self):
# +1 equals -1 (mod 2), so negation is the same element
return GF16(self.val)
def __mul__(self, other):
if not isinstance(other, GF16):
raise ValueError("Multiplication is only defined between a GF16 element and GF16 element")
a, b = self.val, other.val
r = 0
for c in xrange(4):
if b & 1:
r = r^a
a <<= 1
if a & 16 == 16:
a ^= (16 + 2 + 1) # subtract x^4 + x + 1
b >>= 1
return GF16(r)
def __div__(self, other):
if not isinstance(other, GF16):
raise ValueError("Division is only defined between a GF16 element and GF16 element")
return self * other.inverse()
__truediv__ = __div__
https://www.pythonhomeworkhelp.com/
def __eq__(self, other):
if isinstance(other, GF16):
return self.val == other.val
else:
return False
def __ne__(self, other):
if isinstance(other, GF16):
return self.val != other.val
else:
return True
def inverse(self):
if self.val == 0:
raise ValueError("Zero is not invertible.")
# We know that x^15 = x^14 * x = 1, so x^14 = x^{-1}
s2 = self * self
s4 = s2 * s2
s7 = self * s2 * s4
s14 = s7 * s7
return s14
def test_GF16():
# associativity
https://www.pythonhomeworkhelp.com/
for a in xrange(16):
for b in xrange(16):
for c in xrange(16):
assert GF16(a) * (GF16(b) * GF16(c)) == (GF16(a) * GF16(b)) * GF16(c)
assert GF16(a) + (GF16(b) + GF16(c)) == (GF16(a) + GF16(b)) + GF16(c)
# commutativity
for a in xrange(16):
for b in xrange(16):
assert GF16(a) * GF16(b) == GF16(b) * GF16(a)
assert GF16(a) + GF16(b) == GF16(b) + GF16(a)
# distributivity
for a in xrange(16):
for b in xrange(16):
for c in xrange(16):
assert GF16(a) * (GF16(b) + GF16(c)) == GF16(a) * GF16(b) + GF16(a) * GF16(c)
# inverses
for a in xrange(16):
assert GF16(a) + (-GF16(a)) == GF16(0)
for a in xrange(1, 16):
assert GF16(a) * (GF16(a).inverse()) == GF16(1)
https://www.pythonhomeworkhelp.com/
def matrix_by_vector(A, x):
zero = type(x[0])(0)
result = []
for row in A:
assert len(row) == len(x)
c = sum((rowi * xi for rowi, xi in zip(row, x)), zero)
result.append(c)
return result
def matrix_by_matrix(A, B):
zero = type(A[0][0])(0)
C = []
for i in xrange(len(A)):
Ci = []
for j in xrange(len(A[i])):
Cij = sum((A[i][k] * B[k][j] for k in xrange(len(A[i]))), zero)
Ci.append(Cij)
C.append(Ci)
return C
def matrix_inverse(A):
# O(n^3) inversion using Gauss-Jordan algorithm
https://www.pythonhomeworkhelp.com/
zero = type(A[0][0])(0)
one = type(A[0][0])(1)
n = len(A)
M = [A[i] + [zero] * i + [one] + [zero] * (n-1-i) for i in xrange(n)] # augment with identity matrix
# first make the matrix in upper-triangular form
for i in xrange(n):
pivot = None
for j in xrange(i, n):
if M[j][i] != zero:
pivot = j
break
if pivot is None:
raise ValueError, "Matrix is not invertible?"
# swap rows i and pivot
if pivot != i:
M[pivot], M[i] = M[i], M[pivot]
# divide out the row itself by the diagonal element
multiple = M[i][i]
for k in xrange(2*n):
M[i][k] /= multiple
https://www.pythonhomeworkhelp.com/
# subtract this row appropriate number of times for every row below it
for j in xrange(i+1, n):
multiple = M[j][i]
for k in xrange(2*n):
M[j][k] -= M[i][k] * multiple
# then work our way back up
for i in xrange(n-1, -1, -1):
for j in xrange(i-1, -1, -1):
multiple = M[j][i]
for k in xrange(2*n):
M[j][k] -= M[i][k] * multiple
# return the augmented part
return [M[i][n:] for i in xrange(n)]
def test_matrix_inverse(n):
r = range(n)
random.shuffle(r)
P = [[GF16(0)] * n for i in xrange(n)]
L = [[GF16(0)] * n for i in xrange(n)]
U = [[GF16(0)] * n for i in xrange(n)]
for i in xrange(n):
https://www.pythonhomeworkhelp.com/
P[i][r[i]] = GF16(1)
L[i][i] = GF16(random.randint(1, 15))
U[i][i] = GF16(random.randint(1, 15))
for j in xrange(i):
L[i][j] = GF16(random.randint(0, 15))
for j in xrange(i+1, n):
U[i][j] = GF16(random.randint(0, 15))
M = matrix_by_matrix(matrix_by_matrix(L, U), P)
Minv = matrix_inverse(M)
MMinv = matrix_by_matrix(M, Minv)
for i in xrange(n):
for j in xrange(n):
assert MMinv[i][j] == (GF16(1) if i == j else GF16(0))
def vector_add(u, v):
assert len(u) == len(v)
return [ui + vi for ui, vi in zip(u, v)]
def vector_sub(u, v):
assert len(u) == len(v)
return [ui - vi for ui, vi in zip(u, v)]
def random_GF16_vector(n):
return [GF16(random.randint(0, 15)) for i in xrange(n)]
https://www.pythonhomeworkhelp.com/
def random_GF16_matrix(n):
return [random_GF16_vector(n) for i in xrange(n)]
def random_invertible_GF16_matrix(n):
while True:
M = random_GF16_matrix(n)
try:
Minv = matrix_inverse(M)
except ValueError, e:
continue
return M
def int64_to_GF16_vec(x):
v = [None] * 16
for i in xrange(16):
v[15-i] = GF16(x & 15)
x >>= 4
assert x == 0
return v
def GF16_vec_to_int64(v):
assert len(v) == 16
x = 0
for el in v:
x = (x << 4) | el.val
return x
https://www.pythonhomeworkhelp.com/

More Related Content

Similar to Introduction to Python Programming.pptx

High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance HaskellJohan Tibell
 
Reed solomon Encoder and Decoder
Reed solomon Encoder and DecoderReed solomon Encoder and Decoder
Reed solomon Encoder and DecoderAmeer H Ali
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture gVlad Patryshev
 
AIOU Code 803 Mathematics for Economists Semester Spring 2022 Assignment 2.pptx
AIOU Code 803 Mathematics for Economists Semester Spring 2022 Assignment 2.pptxAIOU Code 803 Mathematics for Economists Semester Spring 2022 Assignment 2.pptx
AIOU Code 803 Mathematics for Economists Semester Spring 2022 Assignment 2.pptxZawarali786
 
Minerva_lib - fuzzing tool
Minerva_lib - fuzzing toolMinerva_lib - fuzzing tool
Minerva_lib - fuzzing toolLogicaltrust pl
 
Understanding the "Chain Rule" for Derivatives by Deriving Your Own Version
Understanding the "Chain Rule" for Derivatives by Deriving Your Own VersionUnderstanding the "Chain Rule" for Derivatives by Deriving Your Own Version
Understanding the "Chain Rule" for Derivatives by Deriving Your Own VersionJames Smith
 
AIOU Code 1349 Solved Assignment 2 Autumn 2022.pptx
AIOU Code 1349 Solved Assignment 2 Autumn 2022.pptxAIOU Code 1349 Solved Assignment 2 Autumn 2022.pptx
AIOU Code 1349 Solved Assignment 2 Autumn 2022.pptxZawarali786
 
Go: It's Not Just For Google
Go: It's Not Just For GoogleGo: It's Not Just For Google
Go: It's Not Just For GoogleEleanor McHugh
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
Introduction to Elliptic Curve Cryptography
Introduction to Elliptic Curve CryptographyIntroduction to Elliptic Curve Cryptography
Introduction to Elliptic Curve CryptographyDavid Evans
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryMeetu Maltiar
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheoryKnoldus Inc.
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
11 - Programming languages
11 - Programming languages11 - Programming languages
11 - Programming languagesTudor Girba
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptxGuy Komari
 

Similar to Introduction to Python Programming.pptx (20)

High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
Reed solomon Encoder and Decoder
Reed solomon Encoder and DecoderReed solomon Encoder and Decoder
Reed solomon Encoder and Decoder
 
Truth, deduction, computation lecture g
Truth, deduction, computation   lecture gTruth, deduction, computation   lecture g
Truth, deduction, computation lecture g
 
AIOU Code 803 Mathematics for Economists Semester Spring 2022 Assignment 2.pptx
AIOU Code 803 Mathematics for Economists Semester Spring 2022 Assignment 2.pptxAIOU Code 803 Mathematics for Economists Semester Spring 2022 Assignment 2.pptx
AIOU Code 803 Mathematics for Economists Semester Spring 2022 Assignment 2.pptx
 
Algorithms Design Homework Help
Algorithms Design Homework HelpAlgorithms Design Homework Help
Algorithms Design Homework Help
 
Minerva_lib - fuzzing tool
Minerva_lib - fuzzing toolMinerva_lib - fuzzing tool
Minerva_lib - fuzzing tool
 
Algorithms Design Exam Help
Algorithms Design Exam HelpAlgorithms Design Exam Help
Algorithms Design Exam Help
 
Understanding the "Chain Rule" for Derivatives by Deriving Your Own Version
Understanding the "Chain Rule" for Derivatives by Deriving Your Own VersionUnderstanding the "Chain Rule" for Derivatives by Deriving Your Own Version
Understanding the "Chain Rule" for Derivatives by Deriving Your Own Version
 
Algorithms DM
Algorithms DMAlgorithms DM
Algorithms DM
 
AIOU Code 1349 Solved Assignment 2 Autumn 2022.pptx
AIOU Code 1349 Solved Assignment 2 Autumn 2022.pptxAIOU Code 1349 Solved Assignment 2 Autumn 2022.pptx
AIOU Code 1349 Solved Assignment 2 Autumn 2022.pptx
 
Go: It's Not Just For Google
Go: It's Not Just For GoogleGo: It's Not Just For Google
Go: It's Not Just For Google
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
Introduction to Elliptic Curve Cryptography
Introduction to Elliptic Curve CryptographyIntroduction to Elliptic Curve Cryptography
Introduction to Elliptic Curve Cryptography
 
Programming Hp33s talk v3
Programming Hp33s talk v3Programming Hp33s talk v3
Programming Hp33s talk v3
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Scala categorytheory
Scala categorytheoryScala categorytheory
Scala categorytheory
 
Monadologie
MonadologieMonadologie
Monadologie
 
Ecc2
Ecc2Ecc2
Ecc2
 
11 - Programming languages
11 - Programming languages11 - Programming languages
11 - Programming languages
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 

More from Python Homework Help

Introduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxIntroduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptxPython Homework Help
 
Python Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Homework Help
 

More from Python Homework Help (20)

Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Complete my Python Homework
Complete my Python HomeworkComplete my Python Homework
Complete my Python Homework
 
Introduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptxIntroduction to Python Dictionary.pptx
Introduction to Python Dictionary.pptx
 
Basic Python Programming.pptx
Basic Python Programming.pptxBasic Python Programming.pptx
Basic Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Introduction to Python Programming.pptx
Introduction to Python Programming.pptxIntroduction to Python Programming.pptx
Introduction to Python Programming.pptx
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 
Python Programming Homework Help.pptx
Python Programming Homework Help.pptxPython Programming Homework Help.pptx
Python Programming Homework Help.pptx
 
Quality Python Homework Help
Quality Python Homework HelpQuality Python Homework Help
Quality Python Homework Help
 
Perfect Python Homework Help
Perfect Python Homework HelpPerfect Python Homework Help
Perfect Python Homework Help
 
Python Homework Help
Python Homework HelpPython Homework Help
Python Homework Help
 

Recently uploaded

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Recently uploaded (20)

Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

Introduction to Python Programming.pptx

  • 1. For any help regarding Python Homework Help visit : - https://www.pythonhomeworkhelp.com/, Email :- support@pythonhomeworkhelp.com or call us at :- +1 (315) 557-6473
  • 2. Problem 1. Kalns Ben Bitdiddle has designed a new cryptosystem called Kalns, but we suspect it might not be as strong as we would like to be. Therefore we ask your help to break it. In this problem we will be working with a finite field GF16. The elements of our field are all 4-bit strings. The field addition is computed as xor: GF16(x) + GF16(y) = GF16(x ⊕ y). We provide the two tables describing addition and multiplication laws on the course web page. If you are curious, these tables are obtained by interpreting 4-bit field elements as degree≤ 4 polynomials over GF2 and performing addition and multiplication modulo the irreducible polynomial x4 + x + 1. However, for the purposes of this problem you do not need to understand how our GF16 is constructed; the solutions we know assume black-box access to GF16. We have provided a GF16 implementation for you. Kalns is a 64-bit block cipher. The secret key consists of three parts: •an invertible 16-by-16 matrix A over GF16; •a 16-element vector b over GF16; and •a permutation (bijection) S that maps GF16 one-to-one and onto GF16. https://www.pythonhomeworkhelp.com/
  • 3. To encrypt a 64-bit block B we first break it up in sixteen 4-bit chunks and interpret each of them as a GF16 element. So block B corresponds to length 16 vector x = (x0, . . . , x15) over GF16. The encryptions consists of the following: y = S(Ax + b), where the permutation S is individually applied to each of 16 elements of v = Ax + b. The 16-element vector y is later re-interpreted as 64-bit integer to obtain the encrypted block B'. (a) Ben suspects that his cryptosystem is very secure. After all it has around 16162 1616 16! 21132.25 possible keys. However, we suspect that there are many equivalent keys. These keys have different values for (A, b, S), but produce the same ciphertext for any given plaintext. Is our suspicion well- founded? (b) Describe a chosen-ciphertext attack on Kalns that recovers the unknown key (A, b, S) or an equivalent key. https://www.pythonhomeworkhelp.com/
  • 4. Solution: class GF16(object): """Implementation of GF(2^4) as degree 4 polynomials over GF(2) modulo x^4 + x + 1. """ def __init__(self, val): if not (isinstance(val, int) or isinstance(val, long)) or val < 0 or val > 15: raise ValueError("GF16 elements must be constructed from integers from 0 to 15.") self.val = val def __repr__(self): return "GF16(%d)" % self.val def __add__(self, other): if not isinstance(other, GF16): raise ValueError("Addition is only defined between a GF16 element and GF16 element") return GF16(self.val ^ other.val) def __sub__(self, other): if not isinstance(other, GF16): raise ValueError("Subtraction is only defined between a GF16 element and GF16 element") # +1 equals -1 (mod 2), so both operations are XOR return GF16(self.val ^ other.val) https://www.pythonhomeworkhelp.com/
  • 5. def __neg__(self): # +1 equals -1 (mod 2), so negation is the same element return GF16(self.val) def __mul__(self, other): if not isinstance(other, GF16): raise ValueError("Multiplication is only defined between a GF16 element and GF16 element") a, b = self.val, other.val r = 0 for c in xrange(4): if b & 1: r = r^a a <<= 1 if a & 16 == 16: a ^= (16 + 2 + 1) # subtract x^4 + x + 1 b >>= 1 return GF16(r) def __div__(self, other): if not isinstance(other, GF16): raise ValueError("Division is only defined between a GF16 element and GF16 element") return self * other.inverse() __truediv__ = __div__ https://www.pythonhomeworkhelp.com/
  • 6. def __eq__(self, other): if isinstance(other, GF16): return self.val == other.val else: return False def __ne__(self, other): if isinstance(other, GF16): return self.val != other.val else: return True def inverse(self): if self.val == 0: raise ValueError("Zero is not invertible.") # We know that x^15 = x^14 * x = 1, so x^14 = x^{-1} s2 = self * self s4 = s2 * s2 s7 = self * s2 * s4 s14 = s7 * s7 return s14 def test_GF16(): # associativity https://www.pythonhomeworkhelp.com/
  • 7. for a in xrange(16): for b in xrange(16): for c in xrange(16): assert GF16(a) * (GF16(b) * GF16(c)) == (GF16(a) * GF16(b)) * GF16(c) assert GF16(a) + (GF16(b) + GF16(c)) == (GF16(a) + GF16(b)) + GF16(c) # commutativity for a in xrange(16): for b in xrange(16): assert GF16(a) * GF16(b) == GF16(b) * GF16(a) assert GF16(a) + GF16(b) == GF16(b) + GF16(a) # distributivity for a in xrange(16): for b in xrange(16): for c in xrange(16): assert GF16(a) * (GF16(b) + GF16(c)) == GF16(a) * GF16(b) + GF16(a) * GF16(c) # inverses for a in xrange(16): assert GF16(a) + (-GF16(a)) == GF16(0) for a in xrange(1, 16): assert GF16(a) * (GF16(a).inverse()) == GF16(1) https://www.pythonhomeworkhelp.com/
  • 8. def matrix_by_vector(A, x): zero = type(x[0])(0) result = [] for row in A: assert len(row) == len(x) c = sum((rowi * xi for rowi, xi in zip(row, x)), zero) result.append(c) return result def matrix_by_matrix(A, B): zero = type(A[0][0])(0) C = [] for i in xrange(len(A)): Ci = [] for j in xrange(len(A[i])): Cij = sum((A[i][k] * B[k][j] for k in xrange(len(A[i]))), zero) Ci.append(Cij) C.append(Ci) return C def matrix_inverse(A): # O(n^3) inversion using Gauss-Jordan algorithm https://www.pythonhomeworkhelp.com/
  • 9. zero = type(A[0][0])(0) one = type(A[0][0])(1) n = len(A) M = [A[i] + [zero] * i + [one] + [zero] * (n-1-i) for i in xrange(n)] # augment with identity matrix # first make the matrix in upper-triangular form for i in xrange(n): pivot = None for j in xrange(i, n): if M[j][i] != zero: pivot = j break if pivot is None: raise ValueError, "Matrix is not invertible?" # swap rows i and pivot if pivot != i: M[pivot], M[i] = M[i], M[pivot] # divide out the row itself by the diagonal element multiple = M[i][i] for k in xrange(2*n): M[i][k] /= multiple https://www.pythonhomeworkhelp.com/
  • 10. # subtract this row appropriate number of times for every row below it for j in xrange(i+1, n): multiple = M[j][i] for k in xrange(2*n): M[j][k] -= M[i][k] * multiple # then work our way back up for i in xrange(n-1, -1, -1): for j in xrange(i-1, -1, -1): multiple = M[j][i] for k in xrange(2*n): M[j][k] -= M[i][k] * multiple # return the augmented part return [M[i][n:] for i in xrange(n)] def test_matrix_inverse(n): r = range(n) random.shuffle(r) P = [[GF16(0)] * n for i in xrange(n)] L = [[GF16(0)] * n for i in xrange(n)] U = [[GF16(0)] * n for i in xrange(n)] for i in xrange(n): https://www.pythonhomeworkhelp.com/
  • 11. P[i][r[i]] = GF16(1) L[i][i] = GF16(random.randint(1, 15)) U[i][i] = GF16(random.randint(1, 15)) for j in xrange(i): L[i][j] = GF16(random.randint(0, 15)) for j in xrange(i+1, n): U[i][j] = GF16(random.randint(0, 15)) M = matrix_by_matrix(matrix_by_matrix(L, U), P) Minv = matrix_inverse(M) MMinv = matrix_by_matrix(M, Minv) for i in xrange(n): for j in xrange(n): assert MMinv[i][j] == (GF16(1) if i == j else GF16(0)) def vector_add(u, v): assert len(u) == len(v) return [ui + vi for ui, vi in zip(u, v)] def vector_sub(u, v): assert len(u) == len(v) return [ui - vi for ui, vi in zip(u, v)] def random_GF16_vector(n): return [GF16(random.randint(0, 15)) for i in xrange(n)] https://www.pythonhomeworkhelp.com/
  • 12. def random_GF16_matrix(n): return [random_GF16_vector(n) for i in xrange(n)] def random_invertible_GF16_matrix(n): while True: M = random_GF16_matrix(n) try: Minv = matrix_inverse(M) except ValueError, e: continue return M def int64_to_GF16_vec(x): v = [None] * 16 for i in xrange(16): v[15-i] = GF16(x & 15) x >>= 4 assert x == 0 return v def GF16_vec_to_int64(v): assert len(v) == 16 x = 0 for el in v: x = (x << 4) | el.val return x https://www.pythonhomeworkhelp.com/