Feige菲亚特Shamir计划不起作用

时间:2015-04-15 10:58:01

标签: c algorithm math cryptography arduino

我正在尝试在C(Arduino)中实施 Feige菲亚特Shamir识别方案并且它有效,但仅限于e = 0。当e = 1不起作用时。

如何让它发挥作用?

#include <Wire.h>

int getGCD(int a, int b)
{
   int c;
   while (a != 0)
   {
      c = a;
      a = b % a;
      b = c;
   }
   return b;
}

int getCoprime(int n)
{
   int coprime;
   do
   {
      coprime = random(1, n);
   } 
   while (getGCD(n, coprime) != 1);
   return coprime;
}


//Preparation
int n = 7 * 3;
int s = getCoprime(n);
int v = (s * s) % n;

void loop ()
{
  e = random(0, 2);
  r = random(1, n);
  int y = (r * (int)pow(s, e)) % n;
  int x = (r * r) % n;

  int ysqmodn = y * y % n;
  int test = (x * (int)pow(v, e)) % n;

  if(ysqmodn == test)
  {
    Serial.print("The current ICC matches. \n");
  }  
  else
  {  
    Serial.print(String(e));
    Serial.print("\n");
  }
  delay(500);
}

1 个答案:

答案 0 :(得分:1)

确实e==1时有效。当e==0计算是微不足道的时候,因为sv由于0的幂而一直是1,所以这是复制和修改的代码,只是为了使它能够编译。 / p>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>

int random (int low, int high) {
    return low + rand() % (high - low);
}

int getGCD(int a, int b) {
    int c;
    while (a != 0)
    {
        c = a;
        a = b % a;
        b = c;
    }
    return b;
}

int getCoprime(int n) {
    int coprime;
    do
    {
        coprime = random(1, n);
    } 
    while (getGCD(n, coprime) != 1);
    return coprime;
}

int main(void) {
        int e, x, y, r, n, s, v, test, ysqmodn;

        srand((unsigned)time(NULL));
        n = 7 * 3;
        s = getCoprime(n);
        v = (s * s) % n;

        e = random(0, 2);
        r = random(1, n);

        printf("n=%d, s=%d, e=%d, r=%d\n", n,s,e,r);

        y = (r * (int)pow(s, e)) % n;
        x = (r * r) % n;

        ysqmodn = y * y % n;
        test = (x * (int)pow(v, e)) % n;

        if(ysqmodn == test)
            printf("The current ICC matches. \n");
        else
            printf("%d\n", e);

        return 0;
    }

示例结果:

n=21, s=2, e=1, r=2
The current ICC matches.

n=21, s=11, e=0, r=12
The current ICC matches.

n=21, s=8, e=1, r=14
The current ICC matches.

n=21, s=17, e=1, r=13
The current ICC matches.

n=21, s=1, e=0, r=9
The current ICC matches.

n=21, s=4, e=0, r=13
The current ICC matches.