33
COS 126 LFSR Assignment Overview ??

COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

COS 126 LFSR Assignment Overview

??

Page 2: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

General advice for assignments

2

Get an early start

Follow checklists

Use incremental approach

DO NOT BINGE

Study assignment

Page 3: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Things you need to know

3

Purpose of class meetings: point out Things You Need To Know

TYNTK for LFSR

• How an LFSR works.

• How to create a Java class.

• Importance of proper representation.

• How to use Picture and Color.

• XOR.

Note. You will not be tested on this content. Except to the extent that ignoring it slows you down.

Page 4: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Linear Feedback Shift Registers

Page 5: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

LFSR basics

5

For this assignment you will need to know what an LFSR is and how it works

Review slides 27–30 in Lecture 0

• We’ll do it again next.

• Easier to understand now.

Page 6: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Linear feedback shift register

Terminology

• Bit: 0 or 1.

• Cell: storage element that holds one bit.

• Register: sequence of cells.

• Seed: initial sequence of bits.

• Feedback: Compute XOR of two bits and put result at right.

• Shift register: when clock ticks, bits propagate one position to left.

6

^

01000010110 1

More terminology

• Tap: Bit positions used for XOR (one must be leftmost).

• [N, k] LFSR: N-bit register with taps at N and k.

Numbered from right, starting at 1.

Not all values of k give desired effect (stay tuned).

11 10 9 8 7 6 5 4 3 2 1

An [11, 9] LFSR

m k m ^ k

0 0 0

0 1 1

1 0 1

1 1 0

XOR operation (^ in Java)

Page 7: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Linear feedback shift register simulation

7

^

01000010110 1 0 1 1 0 1 0 0 0 0 1 0 0

Time

^

10100001011 1 1 1 0 1 0 0 0 0 1 0 1 1

^

11010000101 0 1 0 1 0 0 0 0 1 0 1 1 2

^

01101000010 0 0 1 0 0 0 0 1 0 1 1 0 3

^

00110100001 1 1 0 0 0 0 1 0 1 1 0 0 4

^

10011010000 0 0 0 0 0 1 0 1 1 0 0 1 5

History of register contents

a pseudo-random bit sequence !

Page 8: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Pop quiz on LFSRs

8

0 0 0 0 1

0 0 0 1 0

0 0 1 0 0

0 1 0 0 0

1 0 0 0 1

0 0 0 1 1

0 0 1 1 0

0 1 1 0 0

1 1 0 0 1

1 0 0 1 0

0 0 1 0 1

^

0

0

0

1

1

0

0

1

0

1

0

Q. Give first 10 steps of [5, 4] LFSR with initial fill 00001.

Page 9: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Creating a Java class

Page 10: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

First step: LFSR.java

10

For this assignment you will need to create a Java class

Review (for example) slides 23-29 in Lecture 9

See Section 3.2 in text for several more examples. R O B E R T S E D G E W I C K

K E V I N W A Y N E

[Sedgewick and Flajolet] are not only worldwide leaders of the field, they also are masters of exposition. I am sure that every serious computer scientist

will find this book rewarding in many ways. —From the Foreword by Donald E. Knuth

Despite growing interest, basic information on methods and models for mathematically analyzing algorithms has rarely been directly accessible to practitioners, researchers, or students. An Introduction to the Analysis of Algorithms, Second Edition, organizes and presents that knowledge, fully introducing primary techniques and results in the field.

Robert Sedgewick and the late Philippe Flajolet have drawn from both classical mathematics and computer science, integrating discrete mathematics, elementary real analysis, combinatorics, algorithms, and data structures. They emphasize the mathematics needed to support scientific studies that can serve as the basis for predicting algorithm performance and for comparing different algorithms on the basis of performance.

Techniques covered in the first half of the book include recurrences, generating functions, asymptotics, and analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples are included throughout to illustrate applications to the analysis of algorithms that are playing a critical role in the evolution of our modern computational infrastructure.

Improvements and additions in this new edition include

n Upgraded figures and code n An all-new chapter introducing analytic combinatorics n Simplified derivations via analytic combinatorics throughout

The book’s thorough, self-contained coverage will help readers appreciate the field’s challenges, prepare them for advanced results—covered in their monograph Analytic Combinatorics and in Donald Knuth’s Art of Computer Programming books—and provide the background they need to keep abreast of new research.

ROBERT SEDGEWICK is the William O. Baker Professor of Computer Science at Princeton University, where was founding chair of the computer science department and has been a member of the faculty since 1985. He is a Director of Adobe Systems and has served on the research staffs at Xerox PARC, IDA, and INRIA. He is the coauthor of the landmark introductory book, Algorithms, Fourth Edition. Professor Sedgewick earned his Ph.D from Stanford University under Donald E. Knuth.

The late PHILIPPE FLAJOLET was a Senior Research Director at INRIA, Rocquencourt, where he created and led the ALGO research group. He is celebrated for having opened new lines of research in the analysis of algorithms; having systematized and developed powerful new methods in the field of analytic combinatorics; having solved numerous difficult, open problems; and having lectured on the analysis of algorithms all over the world. Dr. Flajolet was a member of the French Academy of Sciences.

informit.com/aw | aofa.cs.princeton.edu

Cover design by Chuti Prasertsith Cover illustration by

Text printed on recycled paper

$79.99 U.S. | $83.99 CANADA

Com

puter ScienceA

N IN

TE

RD

ISC

IPL

INA

RY

AP

PR

OA

CH

SEDGEWICK

WAYNE

Programming | Algorithms

ComputerScience

An Interdisciplinary Approach

Page 11: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

ADT for LFSRs

11

public class LFSR

LFSR(String seed, int tap)

public int length() length

public int bitAt(int i) bit at given position

public String toString() string representation

public int step() step, return bit produced

public int generate(int k) k steps, return bits produced as int value

API (operations)

Values

An ADT allows us to write Java programs that use LFSRs.

A LFSR is an idealized model of a device that generates random bits.

size 11 5 20

tap 9 4 17

fill 1101000010 00001 01101000010100010000

Page 12: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

12

LFSR implementation

public class LFSR { private int n; . . . public LFSR(String seed, int tap) { . . . }

public int length() { . . . } public int bitAt(int i) { . . . } public String toString() { . . . } public int step() { . . . } public int generate(int k) { . . . } public static void main(String[] args) { . . . } }

LFSR.java

instance variables

constructor

methods

test client

Page 13: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

LFSR implementation: Test client

public static void main(String[] args)

{

String seed = "01101000010";

int tap = 9;

LFSR test = new LFSR(seed, tap);

StdOut.println(test);

int bit = test.step();

StdOut.println(test + " " + bit);

}

13

Best practice. Begin by implementing a simple test client.

• Start with test of constructor and toString().

• Can’t run until implementing LFSR, but knowing client code is helpful.

• Test each method as you implement it

instance variables

constructors

methods

test client

tests constructor

Note. Add more extensive tests when you are done.

tests toString() (called automatically)

tests step()

% java-introcs LFSR 01101000010 11010000101 1

what we expect, after implementing LFSR

Page 14: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

LFSR implementation: Instance variables and constructor

14

Instance variables define data-type values.instance variablesconstructor

methods

test client

public class LFSR { private int n; private int tap; private . . .

public LFSR(String seed, int tap) { . . . } . . . }

Constructors create and initialize new objects.

code to initialize instance variables

KEY DECISION: How to represent register fill?

could call it “size” or whatever you want

Page 15: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Important decision for LFSR

15

Need to decide how to represent the register fill value

• Affects all other code in the implementation.

• Many possible choices, with pros and cons.

idea pros cons

String easy to initialize from argument too difficult to shift

int long

trivial to shift code in Lecture 0

conversion from String in constructor 32- or 64-bit limit

char[] boolean[] not hard to shift

conversion to int for generate() conversion from String in constructor conversion to String for toString()

int[] not hard to shift no conversion for generate()

conversion from String in constructor conversion to String for toString()

Hint (after carefully reading checklist): use this one!

Page 16: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Array of length n or n+1?

16

^

01000010110An [11, 9] LFSR

0 1 1 0 1 0 0 0 0 1 0Array of length n.

• Compute new bit

• Shift

• Store new bit in array 1 1 0 1 0 0 0 0 1 0

0 1 1 0 1 0 0 0 0 1 0Array of length n+1.

• Compute new bit

• Store new bit in array

• Shift 1 1 0 1 0 0 0 0 1 0 1

^ 1

1

^

1

Page 17: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Preserve order or preserve numbering?

17

^

01000010110

11 10 9 8 7 6 5 4 3 2 1

An [11, 9] LFSR

0 1 0 0 0 0 1 0 1 1 0

0 1 2 3 4 5 6 7 8 9 10 11

^Array of length 12, preserving numbering.

• Order appears reversed

• bit() is trivial and tap is preserved

• Go backwards for toString()

• Move each entry right for step()1 0 1 0 0 0 0 1 0 1 1 0

1 0 1 0 0 0 1 1 0 1 1

XOR

shift

0 1 1 0 1 0 0 0 0 1 0

0 1 2 3 4 5 6 7 8 9 10 11

^Array of length 12, preserving order.

• New bit goes at the end

• bit() and tap position need to be computed

• Move each entry left for step() 0 1 1 0 1 0 0 0 0 1 0 1

1 1 0 1 0 0 0 0 1 0 1

XOR

shift

Page 18: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

LFSR implementation: Instance variables and constructor

18

instance variablesconstructor

methods

test client

public class LFSR { private int n; private int tap; private int[] YouNameIt

public LFSR(String seed, int tap) { . . . } . . . }

code to initialize instance variables

Bottom line. YOU decide, and stick with your decision.

Any later change might impact ALL your code

Array length n or n+1?

Preserve order or numbering??

Page 19: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

LFSR implementation: Methods

19

Methods define data-type operations (implement APIs).instance variables

constructors

methods

test client

public class LFSR { ... public int length() { . . . } public int bitAt(int i) { . . . } public String toString() { . . . } public int step() { . . . } public int generate(int k) { . . . } ... }

One-liners

Starting with empty string, loop through array, appending chars

XOR and shift, return new bit

k calls to step, accumulating bits in an int value

Page 20: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

LFSR implementation: generate() method

20

instance variables

constructors

methods

test client

public int generate(int k) { . . . }

k calls to step() accumulating bits

in an int value

From assignment spec. The method generate(k) returns an int value obtained from k steps of the LFSR.

• Initialize a variable to zero.

• Use step() to generate a bit.

• Double the variable and add the bit.

• Repeat the previous two steps k times.

bit returned by step() accumulated value

0

1 2*0 + 1 = 1

1 2*1 + 1 = 3

0 2*3 + 0 = 6

0 2*6 + 0 = 12

1 2*12 + 1 = 25

Note: 2510= 110012

Page 21: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Final step in LFSR implementation: Revisit test client

public static void main(String[] args)

{

}

21

% java-introcs LFSR 01101000010 11010000101 1 . . . . . .

Best practice. Finish by implementing a thorough test client.

• Make sure every method is tested.

• Make sure every line of code is tested.

instance variables

constructors

methods

test client

Note. We will do extensive tests of your code, too!

Page 22: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Developing an LFSR client

Page 23: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Crypto basics

23

Having a general idea of how a cryptosystem works is relevant for this assignment

Review slide 33 in Lecture 0

• Use of LFSR to encrypt/decrypt text.

• You will do it for images.

Page 24: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

PhotoMagic: where you are headed

24

An "encryption-decryption" program that

• Takes a picture and a key (seed and tap) from standard input.

• Uses the LFSR to transform the picture.

• Use case 1: Encrypt an image

pipe.png % java-introcs Photomagic pipe.png 01101000010100010000 17

> xxx.png

Page 25: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

PhotoMagic: where you are headed

25

An "encryption-decryption" program that

• Takes a picture and a key (seed and tap) from standard input.

• Uses the LFSR to transform the picture.

• Use case 2: Decrypt an encrypted image

xxx.png % java-introcs Photomagic xxx.png 01101000010100010000 17

Page 26: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Picture and Color

26

Your program will be an LFSR, Picture, and Color client.

Review slides 11–15 and 23-31 in Lecture 8.

Color ADT

Picture ADT

and/or pp. 341-348 in text

Page 27: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Program incrementally

27

Decompose the problem into a step-by-step process

• Read name of picture from command line.

• Read LFSR description from command line.

• Create LFSR.

• Use LFSR to transform picture (implement a static method for this).

• Show result.

Do the steps in order of difficulty, debugging along the way

• Start with comments (no code).

• Parse arguments, read picture, read LFSR description, show result.

• Create LFSR.

• Call static method for transformation.

• Implement static method.

Page 28: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Start with comments and no code

28

Next. Do steps in order of difficulty, referring as needed to the assignment.

import java.awt.Color;

public class PhotoMagic{ public static Picture transform(Picture source, LFSR lfsr) { // transform the source picture using the LFSR } public static void main(String[] args) { // read name of picture from command line // read description of LFSR from command line, // create LFSR // call transform to use LFSR to transform picture // show transformed result } }

PhotoMagic.java

Page 29: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

XOR operation

29

Java supports bitwise XOR operation for int values

x 0 0 1 1 3 310 = 00112

y 0 1 0 1 5 510 = 01012

x ^ y 0 1 1 0 6 610 = 01102

??

% jshell | Welcome to JShell -- Version 11.0.2 | For an introduction type: /help intro

jshell> 1 ^ 0 $1 ==> 1

jshell> 3 ^ 5 $2 ==> 6

jshell> 255 ^ 28 $3 ==> 227

jshell> 255 ^ 128 $4 ==> 127

jshell> 64 ^ 202 $4 ==> 138

. . .

x 25510 = 111111112 6410 = 010000002

y 2810 = 000111002 20210 = 110010102

x ^ y 22710 = 111000112 13810 = 100010102

Page 30: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Image transform function

30

Plan for each pixel.

• Extract red, blue, green components of its color.

• XOR each with an 8-bit LFSR-produced value.

• Create a new Color object with the transformed values.

• Set the pixel color to that Color.

Similar to several examples in Lecture 8 and in text

• Review slide 25 (Grayscale filter) and other examples that process each pixel in an image.

• Watch out for aliasing traps (review pop quizzes on slides 26-31).

original color

LFSR value result original

colorLFSR value result original

colorLFSR value result

R (8 bits) 255 28 227 0 97 97 97 97 0

G (8 bits) 0 146 146 64 202 138 138 202 64

B (8 bits) 0 79 79 128 136 8 8 136 128

color

Page 31: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

What could go wrong?

31

% java-introcs Photomagic pipe.png 01101000010100010000 17pipe.png

Page 32: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

What could go wrong?

32

xxx.png % java-introcs Photomagic xxx.png 01101000010100010000 17

Page 33: COS 126 LFSR Assignment Overview · analytic combinatorics. Structures studied in the second half of the book include permutations, trees, strings, tries, and mappings. Numerous examples

Too much information

33

Sources of information for this assignment

• Lecture.

• Textbook.

• Assignment.

• Checklist.

• This meeting.

• Sample programs from precept.

• Advice from lab, preceptors, peers.

• Web search.

• Personal experiments/experience.

• Help tab on course website

General goal. Develop an ability to critically evaluate which information will be helpful to you.