Beispiel #1
0
int generate_key(NUMBER p1, NUMBER p2, NUMBER_SET *g_public_key, NUMBER_SET *g_secret_key)
{
	NUMBER n,d,e,phi,*max_p;
	int len;
	
	if ( !a_cmp( &p1, &p2 ) ) {
		//fprintf(stderr,"the prime numbers must not be identical!\n");
		//exit(1);
		return -1 ;
	}
	
	if (a_cmp( &p1, &p2 ) > 0)
		max_p = &p1;
	else
		max_p = &p2;

	a_mult( &p1, &p2, &n );

	a_sub( &p1, &a_one, &phi );
	a_sub( &p2, &a_one, &e );
	a_mult( &phi, &e, &phi );
	
	len = n_bitlen( &phi );
	len = ( len + 3 ) / 4;
	
	a_assign( &p1, &phi );
	a_sub( &p1, &a_one, &p1 );
	init_rnd();
	do {
		do {
			gen_number( len, &d );
		} while (a_cmp( &d, max_p ) <= 0 || a_cmp( &d, &p1 ) >= 0);
				
		a_ggt( &d, &phi, &e );
	} while ( a_cmp( &e, &a_one ) );
	
	inv( &d, &phi, &e );
	
	// public key
	a_assign(&(g_public_key->n_key1), &n) ;
	a_assign(&(g_public_key->n_key2), &e) ;

	// secret key
	a_assign(&(g_secret_key->n_key1), &n) ;
	a_assign(&(g_secret_key->n_key2), &d) ;

	return 0 ;
}
Beispiel #2
0
static Int a_eq(Term t1, Term t2) {
  CACHE_REGS
  /* A =:= B		 */
  Int out;
  t1 = Deref(t1);
  t2 = Deref(t2);

  if (IsVarTerm(t1)) {
    Yap_Error(INSTANTIATION_ERROR, t1, "=:=/2");
    return (FALSE);
  }
  if (IsVarTerm(t2)) {
    Yap_Error(INSTANTIATION_ERROR, t2, "=:=/2");
    return (FALSE);
  }
  if (IsFloatTerm(t1)) {
    if (IsFloatTerm(t2))
      return (FloatOfTerm(t1) == FloatOfTerm(t2));
    else if (IsIntegerTerm(t2)) {
      return (FloatOfTerm(t1) == IntegerOfTerm(t2));
    }
  }
  if (IsIntegerTerm(t1)) {
    if (IsIntegerTerm(t2)) {
      return (IntegerOfTerm(t1) == IntegerOfTerm(t2));
    } else if (IsFloatTerm(t2)) {
      return (FloatOfTerm(t2) == IntegerOfTerm(t1));
    }
  }
  out = a_cmp(t1, t2 PASS_REGS);
  return out == 0;
}
Beispiel #3
0
static Int 
a_le(Term t1, Term t2)
{				/* A <= B */
  Int out = a_cmp(Deref(t1),Deref(t2));
  if (ArithError) { Yap_Error(Yap_Error_TYPE, Yap_Error_Term, Yap_ErrorMessage); return FALSE; }
  return out <= 0;
}
Beispiel #4
0
static Int 
a_eq(Term t1, Term t2)
{				/* A =:= B		 */
  int out;

  if (IsVarTerm(t1)) {
    Yap_Error(INSTANTIATION_ERROR, t1, "=:=/2");
    return(FALSE);
  }
  if (IsVarTerm(t2)) {
    Yap_Error(INSTANTIATION_ERROR, t2, "=:=/2");
    return(FALSE);
  }
  if (IsFloatTerm(t1)) {
    if (IsFloatTerm(t2))
      return (FloatOfTerm(t1) == FloatOfTerm(t2));
    else if (IsIntegerTerm(t2)) {
      return (FloatOfTerm(t1) == IntegerOfTerm(t2));
    }
  }
  if (IsIntegerTerm(t1)) {
    if (IsIntegerTerm(t2)) {
      return (IntegerOfTerm(t1) == IntegerOfTerm(t2));
    } else if (IsFloatTerm(t2)) {
      return (FloatOfTerm(t2) == IntegerOfTerm(t1));
    }
  }
  out = a_cmp(t1,t2);
  if (ArithError) { Yap_Error(Yap_Error_TYPE, Yap_Error_Term, Yap_ErrorMessage); return FALSE; }
  return out == 0;
}
void testcases()
{
	int i,check,len;
	char op,b;
	for(i=0;i<10;i++)
	{
		for(len=0;testDB[i].input[len]!='\0';len++);

		if(testDB[i].k>=len || testDB[i].k<0)
			b='\0';
		else
			b=kthindex(testDB[i].input,testDB[i].k);

			op=testDB[i].output;

			check=a_cmp(b,op);
		

		if(check==0)
			printf("passed\n");
		else 
			printf("failed\n");

	}

}
Beispiel #6
0
static Int p_acomp(USES_REGS1) { /* $a_compare(?R,+X,+Y) */
  Term t1 = Deref(ARG1);
  Term t2 = Deref(ARG2);
  Int out;

  out = a_cmp(t1, t2 PASS_REGS);
  return out;
}
Beispiel #7
0
static Int 
p_acomp(void)
{				/* $a_compare(?R,+X,+Y) */
  Term t1 = Deref(ARG1);
  Term t2 = Deref(ARG2);
  Int out;

  out = a_cmp(t1, t2);
  if (ArithError) { Yap_Error(Yap_Error_TYPE, Yap_Error_Term, Yap_ErrorMessage); return FALSE; }
  return out;
}
void testcases()
{
	int i,check,len1;
	char *a;
	
	for(i=0;i<11;i++)
	{

		a=lastword(testDB[i].input);
	
		check=a_cmp(a,testDB[i].output);

		if(check==0)
			printf("passed\n");
		else 
			printf("failed\n");
		free(a);
	}

}
void testcases()
{
	int i,check,op,b;
	char *a;

	for(i=0;i<11;i++)
	{
		struct sll *ihead,*ohead,*j;

		a=malloc_char(testDB[i].input);

		op=testDB[i].even_odd;

		b=even_odd_check(convert_to_list(a));

		check=a_cmp(b,op);

		if(check==0)
			printf("passed\n");
		else 
			printf("failed\n");
	}
} 
Beispiel #10
0
/**
 *
 @pred +X =< +Y
  Lesser than or equal to arithmetic expressions


 The value of the expression  _X_ is less than or equal to the value
 of expression  _Y_.
*/
static Int a_le(Term t1, Term t2) { /* A <= B */
  CACHE_REGS
  Int out = a_cmp(Deref(t1), Deref(t2) PASS_REGS);
  return out <= 0;
}
Beispiel #11
0
/**
   @pred +_X_ \> +_Y_ is iso
   Greater than arithmetic expressions

  The value of the expression  _X_ is less than or equal to the value
  of expression  _Y_.
*/
static Int a_gt(Term t1, Term t2) { /* A > B		 */
  CACHE_REGS
  Int out = a_cmp(Deref(t1), Deref(t2) PASS_REGS);
  return out > 0;
}
Beispiel #12
0
/*
   @pred +_X_ =\\= _Y_ is iso
    Difference of arithmetic expressions

   The value of the expression  _X_ is different from the value of expression
   _Y_.
 */
static Int a_dif(Term t1, Term t2) {
  CACHE_REGS
  Int out = a_cmp(Deref(t1), Deref(t2) PASS_REGS);
  return out != 0;
}
Beispiel #13
0
Int Yap_acmp(Term t1, Term t2 USES_REGS) {
  Int out = a_cmp(t1, t2 PASS_REGS);
  return out;
}