64bit 정수형의 처리가 OS에 종속적인가요?

seoleda의 이미지

제가 Knapsack problem을 이용한 비대칭키 암호화를 linux 환경에서 간단히구현해 보았습니다.

그런데 이 소스를 윈도우 용으로 컴파일 할 필요가 있어서, 코드를 보았는데,

거기서 사용한 long long 형이 걸렸습니다.

그래서 long long 형을 __int64 형으로 고치고, 컴파일을 했는데,

linux에선 잘 동작하던 코드가, windows 에선 동작하지 않더군요.

아 참고로 윈도우에선 DEV-CPP를 사용하였습니다.

어쨌건 왜 그런지 궁금해서 디버거와 printf를 사용하여 원인을 분석하는데,

decryption 과정에서 target= (cyper*w_inverse)%n;

이 제데로 동작하지 않더군요.

위에 나온 변수는 모두 32 비트 정수형인데, cyper*w_inverse 결과가 64비트로 나옵니다.

제가 테스트 했을때, cyper=552076, n=1646460, w_inverse=708331 이였습니다.

이 값을 가지고 다른 프로그램으로 테스트를 해 봤는데, 거기선 결과가 정확히

계산 돼었고, 디버거로 확인한 결과도 맞았습니다.

아 그리고, 64bit 정수형의 결과가 제데로 출력이 안돼더군요.

그리고 비주얼 C++ 7.0 에서도 동일한 에러나 갔습니다.

어디에서 문제가 있었는지 정말로 알 수가 없네요.

물론 화면을 캡춰하는 방식으로 일은 끝냈지만, 제가 어떤 부분에서 실수가 있었는지 여러분의 의견을 듣고 싶습니다. ^^

다음은 제가 의심하는 코드의 일부분이고, 전체 화일을 첨부 했습니다.

bool Knapsack::Decryption(char* filename){

	FILE *fp = fopen(filename, "r");
	string outfilename(filename);
	outfilename+=string(".decrypted");

	FILE *dec_fp =fopen(outfilename.c_str(), "w+");

	__int64 cyper;
    int w_inverse= getInverse();
	int target, message;

	#ifdef DEBUG
	printf("n: %d w:%d W_inverse %d\n",n, w, w_inverse);
	#endif

	int super_set[LENGTH];
	
	for (int i=0; i<LENGTH; i++){
		super_set[i]=((__int64)H_set[i]*w_inverse)%n;
		#ifdef DEBUG
		printf("super_set[%d]=%d H_set[%d]=%d \n", i, super_set[i],i, H_set[i]);
		#endif
	}
	while(!feof(fp)){
		fscanf(fp, "%lld ", &cyper);
[b]
		__int64 mul = cyper*w_inverse;
		target = mul%n;
[/b]
		if (target<0){
          printf("strange!!!n=%d\n",n);
		}
		#ifdef DEBUG
		printf("cyper: %lld target:%d cyper*w_inv:%lld \n", cyper, target , cyper*w_inverse);
		#endif
		message=0;
		for (int i=0; i<LENGTH; i++){
		//    printf("target:        %d \n super_set[%2d]=%d i=%d \n", target, 15-i, super_set[15-i], i);
			if (target>=super_set[LENGTH-1-i]){
				target-=super_set[LENGTH-1-i];
				#ifdef DEBUG
				printf("super_set[%d]=%d target=%d\n",
						15-i, super_set[LENGTH-1-i], target);
				#endif
				message = message | (0x01<<i);
			}
		}
		#ifdef DEBUG
		printf("decryptmessage %d %c, %c.  \n", message, message>>8, message);
		#endif
		fprintf(dec_fp,"%c%c", message>>8, message);
	}
	fclose(fp);
	fclose(dec_fp);
	return true;
}
[/b]
File attachments: 
첨부파일 크기
Package icon knapsack.zip304.48 KB
Necromancer의 이미지

OS보다는 컴파일러의 특성과 CPU 아키텍처에 의한 영향을 많이 받습니다.

특히 컴파일러 특성을 많이 탑니다. C언어 자체가 몇몇 부분에 있어서는

유연(?)하게 되어 있어서 컴파일러 구현방법에 따라 달라지죠..

일례로 gcc에서는 32767이상의 정수가 int에 별탈없이 들어가지만 TurboC에서는

잘려서 들어가버리죠. 이 문제일것 같은 느낌이

Written By the Black Knight of Destruction

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.