Download Introduction-Programming Foundation-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!
2
Overview
What is COS 126? Broad, but technical, intro to CS.
Goals.
! Demystify computer systems.
! Empower you to exploit available technology.
! Build awareness of substantial intellectual underpinnings.
Topics.
! Programming in Java.
! Machine architecture.
! Theory of computation.
! Applications to science, engineering, and commercial computing.
“ Computers are incredibly fast, accurate, and stupid;
Humans are incredibly slow, inaccurate, and brilliant;
together they are powerful beyond imagination. ”
3
The Basics
Lectures. [Kevin Wayne]
! Tuesdays and Thursdays, Frist 302.
! Same lecture at 10am and 11am.
Precepts. [Donna Gabai (lead) · Aditya Bhaskara · Will Clarkson ·
Rob Dockins · Michael Golightly · Thomas Mason · Chris Park · JP Singh]
! Tue+Thu or Wed+Fri.
! Tips on assignments, worked examples, clarify lecture material.
Friend 016/017 lab. [Undergrad lab assistants]
! Weekdays 7-11pm, some weekend hours.
! Full schedule on Web.
For full details: See www.princeton.edu/~cos
4
Grades
Course grades. No preset curve or quota.
9 programming assignments. 40%.
2 exams. 50%.
Final programming project. 10%.
Extra credit and staff discretion. Adjust borderline cases.
you are here participation helps, frequent absences hurts
5
Course Materials
Course website. [ www.princeton.edu/~cos126 ]
! Submit assignments, check grades.
! Programming assignments.
! Lecture slides.
Required readings. Sedgewick and Wayne. Intro to Programming in
Java: An Interdisciplinary Approach. [Labyrinth Books]
Recommended readings. Harel. What computers can't do. [Labyrinth]
print before lecture; annotate during lecture (^) skim before lecture; read thoroughly afterwards Princeton royalties donated to ACM-W 6
Programming Assignments
Desiderata.
! Address an important scientific or commercial problem.
! Illustrate the importance of a fundamental CS concept.
Examples.
! N-body simulation.
! Pluck a guitar string.
! DNA sequence alignment.
! Estimate Avogadro's number.
Due. Mondays 11pm via Web submission.
Computing equipment.
! Your laptop. [OS X, Windows, Linux, iPhone, … ]
! OIT desktop. [Friend 016 and 017 labs]
you solve scientific problems from scratch! 7
What's Ahead?
Lecture 2. Intro to Java.
Precept 1. Meets today/tomorrow.
Precept 2. Meets Thu/Fri.
Not registered? Go to any precept now; officially register ASAP.
Change precepts? Use SCORE.
Assignment 0. Due Monday, 11pm.
! Read Sections 1.1 and 1.2 in textbook.
! Install Java programming environment + a few exercises.
! Lots of help available, don't be bashful.
END OF ADMINISTRATIVE STUFF
see Donna O'Leary in CS 410 if the only precept you can attend is closed 8
0. Prologue: A Simple Machine
· · ·
17 char dec binary Base64 Encoding A 0 000000 B 1 000001 … … … Y 24 011000 … … …
One-Time Pad Encryption
Encryption.
! Convert text message to N bits.
S E N D M O N E Y^ message
010010 000100 001101 000011 001100 001110 001101 000100 011000 base
0 or 1 18
One-Time Pad Encryption
Encryption.
! Convert text message to N bits.
! Generate N random bits (one-time pad).
random bits
message
010010 000100 001101 000011 001100 001110 001101 000100 011000 base
S E N D M O N E Y
19
message
base
S E N D M O N E Y
One-Time Pad Encryption
Encryption.
! Convert text message to N bits.
! Generate N random bits (one-time pad).
! Take bitwise XOR of two bitstrings.
100000 010111 111011 111010 010110 110111 101111 111011 001010 XOR
110010 010011 110110 111001 011010 111001 100010 111111 010010 random bits
x y x ^ y XOR Truth Table 0 0 0 0 1 1 1 0 1 1 1 0 sum corresponding pair of bits: 1 if sum is odd, 0 if even 0 ^ 1 = 1 20
One-Time Pad Encryption
Encryption.
! Convert text message to N bits.
! Generate N random bits (one-time pad).
! Take bitwise XOR of two bitstrings.
! Convert binary back into text.
g X 7 6 W 3 v 7 K encrypted
message
base
S E N D M O N E Y
XOR
110010 010011 110110 111001 011010 111001 100010 111111 010010 random bits
char dec binary Base64 Encoding A 0 000000 B 1 000001 … … … X 23 010111 … … …
21
Secure Chat
Alice wants to send a secret message to Bob?
! Can you read the secret message gX76W3v7K?
! But Bob can. How?
22
One-Time Pad Decryption
Decryption.
! Convert encrypted message to binary.
g X 7 6 W 3 v 7 K encrypted
23
100000 010111 111011 111010 010110 110111 101111 111011 001010 base
One-Time Pad Decryption
Decryption.
! Convert encrypted message to binary.
g X 7 6 W 3 v 7 K encrypted
char dec binary Base64 Encoding A 0 000000 B 1 000001 … … … X 23 010111 … … … 24
One-Time Pad Decryption
Decryption.
! Convert encrypted message to binary.
! Use same N random bits (one-time pad).
encrypted
110010 010011 110110 111001 011010 111001 100010 111111 010010 random bits
100000 010111 111011 111010 010110 110111 101111 111011 001010 base
g X 7 6 W 3 v 7 K
34
Goods and Bads of One-Time Pads
Good.
! Very simple encryption/decryption processes.
! Provably unbreakable if pad is truly random. [Shannon, 1940s]
Bad.
! Easily breakable if pad is re-used.
! Pad must be as long as the message.
! Truly random bits are very hard to come by.
! Pad must be distributed securely.
"one time" means one time only impractical for Web commerce eavesdropper Eve sees only random bits 36
Random Numbers
Jon von Neumann (left), ENIAC (right)
“ Anyone who considers arithmetical methods of producing
random digits is, of course, in a state of sin. ”
37
Pseudo-Random Bit Generator
Practical middle-ground.
! Let’s make a pseudo-random bit generator gadget.
! Alice and Bob each get identical small gadgets.
How to make small gadget that produces "random" numbers.
! Linear feedback shift register.
! Linear congruential generator.
! Blum-Blum-Shub generator.
instead of identical large one-time pads 38
Shift Register
Shift register terminology.
! Bit: 0 or 1.
! Cell: storage element that holds one bit.
! Register: sequence of cells.
! Seed: initial sequence of bits.
! Shift register: when clock ticks, bits propagate one position to left.
register
time t
time t + 1
39
Linear Feedback Shift Register
{8, 10} linear feedback shift register.
! 11 bit shift register.
! New output bit 0 is XOR of previous bits 8 and 10.
! Output bit = bit 0.
LFSR demo
b 10 b 9 b 8 b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 time t
b 9 b 8 b 7 b 6 b 5 b 4 b 3 b 2 b 1 b 0 b 8 ^b 10 time t + 1
^
feedback 40
Random Numbers
Q. Are these 2000 numbers random? If not, what is the pattern?
110010010011110110111001011010111001100010111111010010000100110100101111001100100111111 101110000010101100010000111010100110100001111001001100111011111110101000001000010001010 010101000110000010111100010010011010110111100011010011011100111101011110010001001110101 011101000001010010001000110101010111000000010110000010011100010111011010010101100110000 111111100110000011111100011000011011110011101001111010011100100111011101110101010101000 000000010000000010100000010001000010101010010000000110100000111001000110111010111010100 010100001010001001000101011010100001100001001111001011100111001011110111001001010111011 000010101110010000101110100100101001101100011110111011001010101111000000100110000101111 100100100011101101011010110001100011101111011010100101100001100111001111110111100001010 011001000111111010110000100011100101011011100001101011001110001111101101100010110111010 011010100111100001110011001101111111110100000001001000001011010001001100101011111100001 000011001010011111000111000110110110111011011010101101100000110111000111010110110100011 011001011101111001010100111000001110110001101011101110001010101101000000110010000111110 100110001001111101011100010001011010101001100000011111000011000110011110111111001010000 111000100110110101111011000100101110101100101000111100010110011010011111100111000011110 110011001011111111001000000111010000110100100111001101110111110101010001000000101010000 100000100101000101100010100111010001110100101101001100110011111111111000000000110000000 111100000110011000111111110110000001011100001001011001011001111001111100111100011110011 011001111101111100010100011010001011100101001011100011001011011111001101000111110010110 001110011101101111010110100100011001101011111110001000001101010001110000101101100100110 111101111010010100100110001101111101110100010101001010000011000100011110101011001000001 111010001100100101111101100100010111101010010010000110110100111011001110101111101000100 01001010101011000000001110000001101100001110111001101010111110000010001100010101111010
A. No. This is output of an 11 bit LFSR!
41
LFSR Challenge 1
Goal. Decrypt/encrypt 1,000 characters. Can we use an 11-bit LFSR?
A. Yes, no problem.
B. No, the bits it produces are not truly random.
! True, but that's beside the point.
C. No, need a longer LFSR.
! Only 2^11 bit patterns for register.
! "Random" bits cycle after 2^11 - 1 = 2047 steps.
Lesson. LFSR are scalable: 20 cells for 1 million bits; 30 cells for 1 billion.
(but need theory of finite groups to know where to put taps)
42
LFSR Challenge 2
Goal. Decrypt/encrypt 1 gigabyte movie. How big an LFSR?
A. 30 bits should be enough.
! Probably not; Eve can try all 2^30 possibilities and see which one
results in a movie.
B. 100 bits is safe.
! Maybe; Eve would need 10^12 centuries to try all 2^100 possibilities.
C. 1000 bits makes it sufficiently secure.
! Experts have cracked LFSR.
! More complicated machines needed.