Valhalla Legends Forums Archive | C/C++ Programming | Algorithms Class Homework

AuthorMessageTime
K
I thought some of you might be interested in this assignment from my 3104 class. This is by far the simplest assignment we've had so far, but probably one of the more interesting. Note that LEDA/integer.h (integer) represents an arbitrary length integer.

Here's the assignment (abbreviated from a nasy postscript file):

[quote]Your task is to write the two routines declared in rsa.h, exponentiate and rsa_decode. exponentiate implements the modular exponentiation routine [we discussed in class]. You should optimize as you see fit, since there will be timing requirements. rsa_decode reads in the four provided ciphertext files and and returns the decrypted message. here are the details:

Each file contains an RSA encoded message. The first two numbers in the file comprise the private key, n [the modulus] and d [the decoding exponent]. After these two numbers, the rest of the numbers decrypt to a sequence of one or more ASCII characters. In addition, each character is 9 bits with the last bit being random. Concatenate all these ASCII characters in the correct order to recover the original message. Each successive file contains more ASCII characters per number. The first file contains only one character for each number.[/quote]

rsa.h
[code]
#ifndef RSA_H
#define RSA_H

#include <iostream>
#include <fstream>
#include <cassert>
#include <LEDA/integer.h>

// use this typedef if you are developing the program without LEDA:
// only the first three tests in main.cc will work.
//typedef long integer;

typedef char file_name [10];
const int message_length=10000;
typedef char plaintext [message_length];
// use this to terminate your decoded message
const char null_terminator = '\0';

// returns the value of a^n (mod m)
integer exponentiate( integer a, integer n, integer m );

/**********************************************************

input parameter f is the name of a file containing
the ciphertext, in the format described in the assignment.

rsa_decode opens & closes file f.

rsa_decode sets the array message to the decrypted text.
message[] should be in the form of a null-terminated string. i.e.,
the original plaintext message consists of characters

message[0],message[1],...,message[k],

and message[k+1] contains the character '\0', available
as the constant declared above.

************************************************************/
void rsa_decode (file_name f, plaintext message);

#endif
[/code]



And here is my solution:

rsa.cc
[code]
#include "rsa.h"

using namespace std;

integer exponentiate( integer a, integer n, integer m )
{
   integer x = 1;
   while( n > 0 )
   {
      if ( (n & 1) != 0)
         x = (x * a) % m;
      a = (a * a) % m;
      n >>= 1;
   }
   return x;
}

inline void helper_swap(char& a, char& b)
{
   // a ^= (b ^= (a ^= b))
   char c = b; b = a; a = c;
}

void rsa_decode (file_name f, plaintext message)
{
   ifstream in;

   integer n, d;
   integer read_val, decoded_val;
   int i = 0, start_i, end_i, end_val;

   in.open(f);
   in >> n >> d;

   while(!in.eof())
   {
      in >> read_val;
      decoded_val = exponentiate(read_val, d, n);

      start_i = i;
      while( (decoded_val & 0xFF) != 0)
      {
         message[i] = static_cast<char>((decoded_val >>= 1) & 0xFF);
         ++i;
         decoded_val >>= 8;
      }
      end_i = i - 1;

      end_val = (end_i - start_i + 1) >> 1;
      
      // reorder the characters since we read them in the reverse order
      for(int q = 0; q < end_val; ++q)
         helper_swap(message[start_i + q], message[end_i - q]);
   }

   message[i] = null_terminator;
   in.close();
}
[/code]
March 29, 2004, 8:03 PM
Fr0z3N
at what Grade do you get to learn to program?
March 30, 2004, 5:30 PM
Yoni
4th grade.
Requires high ambition and a good book. Knowledge of English is not a requirement.
March 30, 2004, 6:04 PM
Fr0z3N
My 4th grade teacher didn't teach me.
March 30, 2004, 6:10 PM
j0k3r
That's because you're not jewish.
March 30, 2004, 10:55 PM
Fr0z3N
WTF, Jews learn C++ before me!?
March 30, 2004, 11:31 PM
j0k3r
Only if they're named Yoni.
March 31, 2004, 12:05 AM
Fr0z3N
Hahaha!
March 31, 2004, 12:31 AM
Noodlez
I learned VB in 4th grade! I was making teh leet AOL proggiez~
(In VB 3.0)
April 2, 2004, 8:33 PM
Newby
[quote author=Noodlez link=board=30;threadid=6062;start=0#msg53242 date=1080938034]
I learned VB in 4th grade! I was making teh leet AOL proggiez~
(In VB 3.0)
[/quote]
I attempted to learn C in 4th grade.

I stopped after 3 days because the phrase "data types" scared me >_<

(I was only 10 @ the time and didn't have much of an attention span :P)
April 3, 2004, 5:44 AM
j0k3r
[quote author=Newby link=board=30;threadid=6062;start=0#msg53310 date=1080971055]
(I was only 10 @ the time and didn't have much of an attention span :P)
[/quote]

I'm 16 and still don't.
April 3, 2004, 1:05 PM
St0rm.iD
i was 9
April 4, 2004, 8:23 PM
kamakazie
What book do you use in your Algorithms course?
April 4, 2004, 8:49 PM
K
[quote author=dxoigmn link=board=30;threadid=6062;start=0#msg53451 date=1081111773]
What book do you use in your Algorithms course?
[/quote]

We use a collection of notes that my professor has written, but the suggested book that some our assignments reference is Introduction to Algorithms, Second Edition by Cormen, Leiserson, Rivest, and Stein. (CLRS)
April 4, 2004, 8:58 PM
Yoni
[quote author=K link=board=30;threadid=6062;start=0#msg53452 date=1081112292]
[quote author=dxoigmn link=board=30;threadid=6062;start=0#msg53451 date=1081111773]
What book do you use in your Algorithms course?
[/quote]

We use a collection of notes that my professor has written, but the suggested book that some our assignments reference is Introduction to Algorithms, Second Edition by Cormen, Leiserson, Rivest, and Stein. (CLRS)
[/quote]That is one of the best books ever written. I have it in Hebrew.
April 5, 2004, 2:55 AM
Maddox
[quote author=Yoni link=board=30;threadid=6062;start=0#msg53503 date=1081133733]
[quote author=K link=board=30;threadid=6062;start=0#msg53452 date=1081112292]
[quote author=dxoigmn link=board=30;threadid=6062;start=0#msg53451 date=1081111773]
What book do you use in your Algorithms course?
[/quote]

We use a collection of notes that my professor has written, but the suggested book that some our assignments reference is Introduction to Algorithms, Second Edition by Cormen, Leiserson, Rivest, and Stein. (CLRS)
[/quote]That is one of the best books ever written. I have it in Hebrew.
[/quote]

Well I have it in Latin.
April 5, 2004, 6:28 AM
kamakazie
[quote author=K link=board=30;threadid=6062;start=0#msg53452 date=1081112292]
[quote author=dxoigmn link=board=30;threadid=6062;start=0#msg53451 date=1081111773]
What book do you use in your Algorithms course?
[/quote]

We use a collection of notes that my professor has written, but the suggested book that some our assignments reference is Introduction to Algorithms, Second Edition by Cormen, Leiserson, Rivest, and Stein. (CLRS)
[/quote]

Hehe ahh yes. Tom Cormen is the CS Undergraduate Advisor here, and teaches the Algorithms course sometimes. Is a good book indeed, I hope he teaches the course when I take it :)
April 5, 2004, 7:09 AM
Maddox
You know that Dartmouth College has the same engineering rating as San Jose State.
April 5, 2004, 11:06 PM
K
Haha I wonder what our engineering rating is...I know what our party school rating is. ;)
April 5, 2004, 11:15 PM
Myndfyr
Ours at Arizona State is (from US News and World Report) 33, but it's definitely not because of our CS department. :-/

I've been programming in BASIC since I was 8 or 9 (when I first could get my hands on GW-BASIC and some Apple II's). I started Javascript when I was 13.
April 5, 2004, 11:57 PM
Maddox
[quote author=K link=board=30;threadid=6062;start=15#msg53613 date=1081206933]
Haha I wonder what our engineering rating is...I know what our party school rating is. ;)
[/quote]

Some of the top 100 best undergraduate engineering programs

5 is the highest.
MIT : 4.8
UCLA : 3.7
University of Colorado - Boulder : 3.5
Az. Sate: 3.3
Yale : 3.3
Dartmouth : 3.2
Washington State : 2.9
San Jose State : 3.2
April 6, 2004, 12:37 AM
K
[quote author=Maddox link=board=30;threadid=6062;start=15#msg53624 date=1081211853]
[quote author=K link=board=30;threadid=6062;start=15#msg53613 date=1081206933]
Haha I wonder what our engineering rating is...I know what our party school rating is. ;)
[/quote]

Some of the top 100 best undergraduate engineering programs

5 is the highest.
MIT : 4.8
UCLA : 3.7
University of Colorado - Boulder : 3.5
Az. Sate: 3.3
Yale : 3.3
Darmouth : 3.2
Washington Sate ; 2.9
San Jose State : 3.2
[/quote]

Sweet, I have a 3.5 GPA too.
April 6, 2004, 12:38 AM

Search