Is It Ever Useful to Be Confident That a Problem Is Hard?
Total Page:16
File Type:pdf, Size:1020Kb
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Menu • Complexity Question Lecture 9: • Low-Level Programming Low-Level Exam 1 Out Wednesday, Due Monday, 11:01AM Programming Covers everything through Lecture 8 Expect questions on: order notation, algorithm analysis, lists, trees, recursive programming, dynamic programming, greedy algorithms Not on complexity classes (Lecture 8) http://www.cs.virginia.edu/cs216 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 2 Problem Classes if P ≠ NP: Sequence NP Alignment: O(n2) How many problems are in the Θ(n) class? P infinite Is it ever useful to be NP-Complete How many problems are in P but not O( n) in the Θ(n) class? confident that a problem is Note the infinite NP- How many problems Complete hard? are in NP but not class is a in P? ring – others infinite are circles Interval Scheduling: Subset Sum Θ(n log n) 3SAT UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 3 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 4 Knapsack Cipher Subset Sum is Hard [Merkle & Hellman, 1978] • Public Key: A = {a1, a2,…,an} • Given s and A it is NP-Complete to – Set of integers find a subset of A that sums to s • Plain Text: x1,…xn xi = 0 or 1 • Cipher Text: n = • Need to make decrypting each (for s ∑ xiai recipient with the “private key”) i=1 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 5 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 6 1 Superincreasing Set Knapsack Ciphers • Private Key = {p1, p2,…,pn} • Pick {a1, a2,…,an} is a superincreasing – A superincreasing sequence sequence – Values M and W: i−1 > n ai ∑ aj M > ∑bi j=1 i=1 GCD (M ,W ) =1 How hard is subset sum if A • Public Key = {a , a ,…, a } is superincreasing? 1 2 n ai ≡ (biW ) mod M UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 7 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 8 Flawed Security Argument Levels of Abstraction: Program Real World Problem • Subset Sum is NP-Complete High-Level Program • Breaking knapsack cipher involves Physical World solving a subset sum problem • Therefore, knapsack cipher is secure World Flaw: NP-Complete means there is no Virtual fast general solution . Some instances Machine Instructions may be solved quickly. (Note: Adi Shamir found a way of breaking Physical knapsack cipher [1982]) Processor From Lecture 3 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 9 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 10 Crossing-Levels Python Program C Program C compiler Programming Languages x86 Instructions Python Interpreter x86 Instructions UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 11 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 12 2 Fortran (1954), IBM (Backus) Algol (1958) LISP (1957) BASIC (1963) CPL (1963), U Cambridge Scheme (1975) Combined Programming Language Why so many Simula (1967) BCPL (1967), MIT Basic Combined Programming Language Programming Languages? ABC (~1980) B (1969), Bell Labs Smalltalk (1971), PARC C (1970), Bell Labs C++ (1983), Bell Labs Objective C Python (1990), Guido van Rossum Programming Languages Java (1995) Phylogeny (Simplified) UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 13 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 14 “Jamais Jamais Jamais” from Harmonice Musices “Jamais Jamais Jamais” from J S Bach, “Coffee Cantata”, Odhecaton A. Printed by Ottaviano Dei Petrucci in Harmonice Musices Odhecaton A. BWV 211 (1732) 1501 (first music with movable type) (1501) www.npj.com/homepage/teritowe/jsbhand.html UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 15 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 16 Modern Music Notation Roman Haubenstock- John Cage, Fontana Mix Ramati, Concerto a Tre http://www.medienkunstnetz.de/works/fontana-mix/audio/1/ UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 17 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 18 3 Thought and Action Abstractions • Languages change the way we think • Higher level abstractions – Scheme: think about procedures – Python, Java, BASIC, … – BASIC: think about GOTO – Easier to describe abstract algorithms – Algol, Pascal: think about assignments, control – But, cannot manipulate low-level machine blocks state – Java: think about types, squiggles, exceptions • How are things stored in memory? – Python? • Opportunities for optimization lost • Languages provide abstractions of machine • Lower level abstractions resources – C, C++, JVML, MSIL, Assembly, … – Hide dangerous/confusing details: memory – Harder to describe abstraction algorithms locations, instruction opcodes, number – Provides programmer with control over low- representations, calling conventions, etc. level machine state UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 19 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 20 Biggest Single Difference: Fortran (1954), IBM (Backus) Algol (1958) Memory Management LISP (1957) BASIC (1963) • High-level languages (Python, Java) CPL (1963), U Cambridge provide automatic memory Scheme (1975) Combined Programming Language Simula (1967) management BCPL (1967), MIT Basic Combined Programming Language – Programmer has no control over how memory is allocated and reclaimed ABC (~1980) B (1969), Bell Labs Smalltalk – Garbage collector reclaims storage (1971), PARC C (1970), Bell Labs • Low-level languages (C, Assembly) leave it up to the programmer to C++ (1983), Bell Labs Objective C manage memory Python (1990), Guido van Rossum Programming Languages Java (1995) Phylogeny (Simplified) UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 21 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 22 C Programming Language C Language • No support for: • Developed to build Unix OS – Array bounds checking • Main design considerations: – Null dereferences checking – Compiler size: needed to run on PDP-11 – Data abstraction, subtyping, inheritance with 24KB of memory (Algol60 was too big to fit) – Exceptions – Code size: needed to implement the whole – Automatic memory management OS and applications with little memory • Program crashes (or worse) when – Performance, Portability something bad happens • Little (if any consideration): • Lots of syntactically legal programs – Security, robustness, maintainability have undefined behavior UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 23 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 24 4 Fortran (1954) LET Example C Program Algol (1958) := void test (int x) { In Java: while (x = 1) { void test (int x) { printf (“I’m an imbecile!”); CPL (1963), U Cambridge := while (x = 1) { Combined Programming Language x = x + 1; printf (“I’m an imbecile!”); } x = x + 1; BCPL (1967), MIT } } := Basic Combined Programming Language } Weak type checking: > javac Test.java B (1969), Bell Labs = In C, there is no boolean type. Test.java:21: incompatible types Any value can be the test expression. found : int C (1970), Bell Labs = required: boolean x = 1 assigns 1 to x, and has the value 1. while (x = 1) { C++ (1983), Bell Labs = ^ I’m an imbecile! 1 error I’m an imbecile! Java (1995), Sun = I’m an imbecile! I’m an imbecile! UVa CS216 Spring 2006 - LectureI’m 9:an Low-Level imbecile! Programming 25 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 26 I’m an imbecile! = vs. := C Bounds Non-Checking int main (void) { > gcc -o bounds bounds.c int x = 9; Why does Python use = for assignment? > bounds char s[4]; abcdefghijkl • Algol (designed for elegance for presenting s is: abcdefghijkl (User input) gets(s); algorithms) used := x is: 9 printf ("s is: %s\n“, s); > bounds • CPL and BCPL based on Algol, used := printf ("x is: %d\n“, x); abcdefghijklm } • Thompson and Ritchie had a small computer to s is: abcdefghijklmn implement B, saved space by using = instead Note: your results x is: 1828716553 = 0x6d000009 may vary > bounds • C was successor to B (also on small computer) abcdefghijkln (depending on • C++’s main design goal was backwards s is: abcdefghijkln machine, compiler, x is: 1845493769 = 0x6e 000009 compatibility with C what else is > bounds • Python was designed to be easy for C and C++ aaa... [a few thousand characters] running, time of crashes shell programmers to learn day, etc.). This is what makes C fun! UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 27 UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 28 Charge • Wednesday: Exam 1 is out, due Monday • No regularly scheduled Small Hall and office hours while Exam 1 is out UVa CS216 Spring 2006 - Lecture 9: Low-Level Programming 29 5.