[关闭]
@Lin-- 2019-09-29T09:43:08.000000Z 字数 2757 阅读 406

ComSec作业三--GF(2^8)

ComSec


题目:编程实现AES算法中定义的GF(2^8)的乘法(给定a和b,求a*b)、求乘法逆元(给定a和m,求a',使得a a' = 1,m按课本给定的值即可. )。

此题本人用python3和C语言两种语言实现两个版本。
计算逆元时:用课本给定的值

计算结果

由于在中运算,数值均为8位无符号整数,故用C语言实现时,未采用GMP库。

python3实现版本

  1. '''
  2. # File : GF_compute.py
  3. # Author : Hongpei Lin
  4. # Date : 20190924
  5. # Purpose : In GF(2^8) :
  6. # 1. giving a and b, compute the reslut about a*b
  7. # 2. giving a and m, compute the inverse about a', where a*a'=1(mod m)
  8. '''
  9. #multilpy in GF(2^8)
  10. def mul(a,b):
  11. r=0
  12. while b:
  13. if b%2:
  14. r=r^a #add operation : XOR
  15. b=b>>1
  16. if a&int('10000000',2)==0: #first bit's value = 0
  17. a=a<<1
  18. else: #first bit's value = 1
  19. a=a<<1
  20. a=a^283
  21. return r
  22. #compute the max index number which < 2^count
  23. #return count, from 0
  24. def highest_bit(n):
  25. count = 0
  26. while n:
  27. count+=1
  28. n=n>>1
  29. return count-1
  30. #division about polymerization
  31. #return quotient and remainder
  32. def div(a,b):
  33. if a==b:
  34. return 1,0
  35. if a<b:
  36. return 0,a
  37. a_bit = highest_bit(a)
  38. b_bit = highest_bit(b)
  39. result = 0
  40. while not a_bit<b_bit:
  41. move=a_bit-b_bit
  42. temp=b<<move
  43. result=result+(1<<move)
  44. a=a^temp
  45. a_bit=highest_bit(a)
  46. return result,a
  47. #compute the inverse about a', where a*a'=1(mod m)
  48. #the algorithrm likes EGCD
  49. def inverse(a,m):
  50. r0,s0,r1,s1=1,0,0,1
  51. while m>0:
  52. t=m
  53. q,m=div(a,m)#q=a//m,m=a%m
  54. a=t#a=m
  55. r0,r1=r1,r0^mul(q,r1)#sub operation:XOR
  56. s0,s1=s1,s0^mul(q,s1)
  57. return r0 #a'
  58. #giving a=x^7+x+1, m=x^8+x^4+x^3+x+1
  59. #we get a'=128(mod m)
  60. print(inverse(int('10000011',2),int('100011011',2)))

C语言实现版本

  1. /*
  2. * File : GF_compute.c
  3. * Author : Hongpei Lin
  4. * Date : 20190924
  5. * Purpose : In GF(2^8) :
  6. * 1. giving a and b, compute the reslut about a*b
  7. * 2. giving a and m, compute the inverse about a', where a*a'=1(mod m)
  8. */
  9. #include<stdio.h>
  10. #include<stdlib.h>
  11. //multilpy in GF(2^8)
  12. int mul(int a, int b);
  13. //compute the max index number which < 2^count
  14. //return count, from 0
  15. int highest_bit(int n);
  16. //division about polymerization
  17. //return quotient and remainder
  18. int * DIV(int a, int b);
  19. //compute the inverse about a', where a*a'=1(mod m)
  20. //the algorithrm likes EGCD
  21. int inverse(int a, int m);
  22. int main()
  23. {
  24. //giving a=x^7+x+1, m=x^8+x^4+x^3+x+1
  25. //we get a'=128(mod m)
  26. printf("%d",inverse(131,283));
  27. return 0;
  28. }
  29. int mul(int a, int b)
  30. {
  31. int r=0;
  32. while(b>0)
  33. {
  34. if(b%2==1){r=r^a;}//add operation:XOR
  35. b=b>>1;
  36. if((a&128)==0){a=a<<1;}//first bit's value = 0
  37. else//first bit's value = 1
  38. {
  39. a=a<<1;
  40. a=a%283;
  41. }
  42. }
  43. return r;
  44. }
  45. int highest_bit(int n)
  46. {
  47. int count=0;
  48. while (n>0)
  49. {
  50. ++count;
  51. n=n>>1;
  52. }
  53. return count-1;
  54. }
  55. int * DIV(int a, int b)
  56. {
  57. int *result;
  58. result=(int*)malloc(sizeof(int)*2);
  59. if(a==b)
  60. {
  61. result[0]=1;result[1]=0;return result;
  62. }
  63. if(a<b)
  64. {
  65. result[0]=0;result[1]=a;return result;
  66. }
  67. int a_bit=highest_bit(a);
  68. int b_bit=highest_bit(b);
  69. int move,temp;
  70. result[0]=0;
  71. while(!(a_bit<b_bit))
  72. {
  73. move=a_bit-b_bit;
  74. temp=b<<move;
  75. result[0]=result[0]+(1<<move);
  76. a=a^temp;
  77. a_bit=highest_bit(a);
  78. }
  79. result[1]=a;
  80. return result;
  81. }
  82. int inverse(int a,int m)
  83. {
  84. int r0=1,s0=0,r1=0,s1=1;
  85. int t,*q,r0_temp,s0_temp;
  86. q=(int*)malloc(sizeof(int)*2);//q[0]=a//m,q[0]=a mod m
  87. q[1]=m;
  88. while(q[1]>0)
  89. {
  90. t=q[1];
  91. q=DIV(a,q[1]);
  92. a=t;
  93. r0_temp=r0;s0_temp=s0;
  94. r0=r1;s0=s1;
  95. r1=r0_temp^mul(q[0],r1);//sub operation:XOR
  96. s1=s0_temp^mul(q[0],s1);
  97. }
  98. free(q);
  99. return r0;
  100. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注