Ejemplo n.º 1
0
/*
* Multiply-Add Operation
*/
BigInt mul_add(const BigInt& a, const BigInt& b, const BigInt& c)
{
    if(c.is_negative() || c.is_zero())
        throw Invalid_Argument("mul_add: Third argument must be > 0");

    BigInt::Sign sign = BigInt::Positive;
    if(a.sign() != b.sign())
        sign = BigInt::Negative;

    const size_t a_sw = a.sig_words();
    const size_t b_sw = b.sig_words();
    const size_t c_sw = c.sig_words();

    BigInt r(sign, std::max(a.size() + b.size(), c_sw) + 1);
    secure_vector<word> workspace(r.size());

    bigint_mul(r.mutable_data(), r.size(),
               workspace.data(),
               a.data(), a.size(), a_sw,
               b.data(), b.size(), b_sw);

    const size_t r_size = std::max(r.sig_words(), c_sw);
    bigint_add2(r.mutable_data(), r_size, c.data(), c_sw);
    return r;
}
Ejemplo n.º 2
0
void Montgomery_Params::mul_by(BigInt& x,
                               const BigInt& y,
                               secure_vector<word>& ws) const
   {
   const size_t output_size = 2*m_p_words + 2;

   if(ws.size() < 2*output_size)
      ws.resize(2*output_size);

   word* z_data = &ws[0];
   word* ws_data = &ws[output_size];

   BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);

   bigint_mul(z_data, output_size,
              x.data(), x.size(), std::min(m_p_words, x.size()),
              y.data(), y.size(), std::min(m_p_words, y.size()),
              ws_data, output_size);

   bigint_monty_redc(z_data,
                     m_p.data(), m_p_words, m_p_dash,
                     ws_data, output_size);

   if(x.size() < output_size)
      x.grow_to(output_size);
   copy_mem(x.mutable_data(), z_data, output_size);
   }
Ejemplo n.º 3
0
void bigint_monty_mul(word z[], size_t z_size,
                      const word x[], size_t x_size, size_t x_sw,
                      const word y[], size_t y_size, size_t y_sw,
                      const word p[], size_t p_size, word p_dash,
                      word ws[])
   {
   bigint_mul(&z[0], z_size, &ws[0],
              &x[0], x_size, x_sw,
              &y[0], y_size, y_sw);

   bigint_monty_redc(&z[0],
                     &p[0], p_size, p_dash,
                     &ws[0]);
   }
Ejemplo n.º 4
0
BigInt Montgomery_Exponentation_State::exponentiation(const BigInt& k) const
   {
   const size_t exp_nibbles = (k.bits() + m_window_bits - 1) / m_window_bits;

   BigInt x = m_R_mod;

   const size_t z_size = 2*(m_p_words + 1);

   BigInt z(BigInt::Positive, z_size);
   secure_vector<word> workspace(z.size());
   secure_vector<word> e(m_p_words);

   for(size_t i = exp_nibbles; i > 0; --i)
      {
      for(size_t j = 0; j != m_window_bits; ++j)
         {
         bigint_monty_sqr(z, x, m_p.data(), m_p_words, m_mod_prime,
                          workspace.data());

         x = z;
         }

      const uint32_t nibble = k.get_substring(m_window_bits*(i-1), m_window_bits);

      BigInt::const_time_lookup(e, m_g, nibble);

      bigint_mul(z.mutable_data(), z.size(),
                 x.data(), x.size(), x.sig_words(),
                 e.data(), m_p_words, m_p_words,
                 workspace.data());

      bigint_monty_redc(z.mutable_data(),
                        m_p.data(), m_p_words, m_mod_prime,
                        workspace.data());

      x = z;
      }

   x.grow_to(2*m_p_words + 1);

   bigint_monty_redc(x.mutable_data(),
                     m_p.data(), m_p_words, m_mod_prime,
                     workspace.data());

   return x;
   }
Ejemplo n.º 5
0
int main() 
{
    int i,j;
    printf("Factorial of:");
    scanf("%d",&i);
    bigint k;
    k=int_to_bigint(1);
    for (j=i;j>0;j--)
    	{
    	k=bigint_mul(k,int_to_bigint(j));
  		}
print_bigint(k);
// print_bigint(bigint_mul(make_bigint("128938499999999"),make_bigint("11123455544444")));
// printf("%d ",compare_bigint(int_to_bigint(444),int_to_bigint(4)));
//print_bigint(bigint_mul(make_bigint("-77777777777666666666666666"),make_bigint("-76777777777776564534467687")));
//print_bigint(make_bigint("23898293829389238239"));
 return(0); 
 }
int main(int argc, char* argv[]) {
  int n,i;
  scanf("%d",&n);
  bigint num,temp;
  char str[2]="1";
  num=make_bigint(str);
  //printf("%s %d %d\n",num.arr,strlen(num.arr),num.sign);
  //print_bigint(num);
  //printf("\n");
  for(i=1;i<=n;i++){
    temp=int_to_bigint(i);
    num=bigint_mul(num,temp);
    //num.arr[strlen(num.arr)]='\0';
    //print_bigint(num);
  }
  //num.arr[strlen(num.arr)]='\0';
  //printf("%d\n",strlen(num.arr));
  //printf("%s\n",num.arr);
  print_bigint(num);
  return 0;
}
Ejemplo n.º 7
0
int main(void) {
    char * bigint1 = (char *)malloc(int_max * sizeof(char));
    char * bigint2 = (char *)malloc(int_max * sizeof(char));
    char * bigint3 = (char *)malloc(int_max * sizeof(char));
    char * bigintsum = (char *)malloc(int_max * sizeof(char));
    char * bigintsub = (char *)malloc(int_max * sizeof(char));
    char * bigintmul = (char *)malloc(int_max * sizeof(char));

    memset(bigint1, 0, int_max);
    memset(bigint2, 0, int_max);
    memset(bigint3, 0, int_max);
    memset(bigintsum, 0, int_max);
    memset(bigintsub, 0, int_max);
    memset(bigintmul, 0, int_max);

    double_2_bigint(bigint1, 7462911385);
    double_2_bigint(bigint2, 9800724324238);
    string_2_bigint(bigint3, "-4399000324898602398927826786492023650000000000000000000043242340000");
    bigint_sum(bigint1, bigint2, bigintsum);
    bigint_substract(bigint1, bigint2, bigintsub);
    bigint_mul(bigint1, bigint3, bigintmul);

    print_bigint(bigint1);
    print_bigint(bigint2);
    printf("large? %d\n", bigint_ge(bigint1, bigint2));
    printf("small? %d\n", bigint_le(bigint1, bigint2));
    printf("equal? %d\n", bigint_eq(bigint1, bigint2));
    print_bigint(bigint3);
    print_bigint(bigintsum);
    print_bigint(bigintsub);
    print_bigint(bigintmul);

    free(bigint1);
    free(bigint2);
    free(bigint3);
    free(bigintsum);
    free(bigintsub);
    free(bigintmul);
    return 0;
}
Ejemplo n.º 8
0
BigInt Montgomery_Params::mul(const BigInt& x,
                              const secure_vector<word>& y,
                              secure_vector<word>& ws) const
   {
   const size_t output_size = 2*m_p_words + 2;
   if(ws.size() < output_size)
      ws.resize(output_size);
   BigInt z(BigInt::Positive, output_size);

   BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);

   bigint_mul(z.mutable_data(), z.size(),
              x.data(), x.size(), std::min(m_p_words, x.size()),
              y.data(), y.size(), std::min(m_p_words, y.size()),
              ws.data(), ws.size());

   bigint_monty_redc(z.mutable_data(),
                     m_p.data(), m_p_words, m_p_dash,
                     ws.data(), ws.size());

   return z;
   }
Ejemplo n.º 9
0
/*
* Multiplication Operator
*/
BigInt operator*(const BigInt& x, const BigInt& y)
   {
   const size_t x_sw = x.sig_words(), y_sw = y.sig_words();

   BigInt z(BigInt::Positive, x.size() + y.size());

   if(x_sw == 1 && y_sw)
      bigint_linmul3(z.mutable_data(), y.data(), y_sw, x.word_at(0));
   else if(y_sw == 1 && x_sw)
      bigint_linmul3(z.mutable_data(), x.data(), x_sw, y.word_at(0));
   else if(x_sw && y_sw)
      {
      secure_vector<word> workspace(z.size());

      bigint_mul(z.mutable_data(), z.size(),
                 x.data(), x.size(), x_sw,
                 y.data(), y.size(), y_sw,
                 workspace.data(), workspace.size());
      }

   if(x_sw && y_sw && x.sign() != y.sign())
      z.flip_sign();
   return z;
   }
Ejemplo n.º 10
0
int main( int argc, char* argv[] )
{
    char cmd[ BUF_SIZE ];
    // NOTE: Uncomment this once you've implemented this function
       bigint result = init_bigint();
      //bigint result;
    // At the end of every arithmetic operation update the result.

    // In an infinite loop, get input from user
    while( 1 )
    {
        char* op = NULL; // Store the operation
        // Get a line of input from the user
        fgets( cmd, BUF_SIZE, stdin );

        // Parse the line using strtok
        // Note: Given a string like "a b c d", strtok returns "a" the first
        // time it's called, and "b", "c", "d" and NULL subsequently, if the
        // first argument is NULL. Read the manpage for more details
        op = strtok( cmd, " " );
        // If the last line is a new line, make it a null character
        int len = strlen( op );
        if( op[ len-1 ] == '\n' )
          op[ len-1 ] = 0;
        
        // Match the operation, and do appropriate action
        if( strcmp( op, "quit" ) == 0 || op[0] == EOF )
        {
          // Quit when you see this
          break;
        }
        else if( strcmp( op, "set" ) == 0 )
        {
          // set <data>
          // Sets a big int value to data
         
          int data;
          if( read_args1( &data ) )
          {
            // NOTE: This printf line has been added to help debug. Should be
            // removed before submitting
           // printf( "Setting value as %d\n", data );
            // NOTE: Uncomment this once you've implemented this function
             result = int_to_bigint(data);
          }
        }
        
        else if( strcmp( op, "add" ) == 0 )
        {
          // add <num1> <num2> 
          // Adds two bigints and then prints the result
          
          char num1[512];
          char num2[512];
          if( read_args2( num1, num2 ) )
          {
            // NOTE: This printf line has been added to help debug. Should be
            // removed before submitting
            //printf( "Add %s and %s\n", num1, num2 );
            // NOTE: Uncomment this once you've implemented this function
             bigint a = make_bigint(num1);
             bigint b = make_bigint(num2);
             result = bigint_add( a, b);
             print_bigint(result);
          }
        }
        
        else if( strcmp( op, "sub" ) == 0 )
        {
          // sub <num1> <num2> 
          // Subtracts two bigints and then prints the result
          
          char num1[512];
          char num2[512];
          if( read_args2( num1, num2 ) )
          {
            // NOTE: This printf line has been added to help debug. Should be
            // removed before submitting
            //printf( "Subtract %s and %s\n", num1, num2 );
            // NOTE: Uncomment this once you've implemented this function
             bigint a = make_bigint(num1);
             bigint b = make_bigint(num2);
             result = bigint_sub( a, b);
             print_bigint(result);
	     printf("\n");
          }
        }
        
        else if( strcmp( op, "mul" ) == 0 )
        {
          // mul <num1> <num2> 
          // Multiplies two bigints and then prints the result
          
          char num1[512];
          char num2[512];
          if( read_args2( num1, num2 ) )
          {
            // NOTE: This printf line has been added to help debug. Should be
            // removed before submitting
            //printf( "Multiply %s and %s\n", num1, num2 );
            // NOTE: Uncomment this once you've implemented this function
             bigint a = make_bigint(num1);
             bigint b = make_bigint(num2);
             result = bigint_mul( a, b);
             print_bigint(result);
          }
        }
        else if( strcmp( op, "div" ) == 0 )
        {
          // add <num1> <num2> 
          // Adds two bigints and then prints the result
          
          char num1[512];
          char num2[512];
          if( read_args2( num1, num2 ) )
          {
            // NOTE: This printf line has been added to help debug. Should be
            // removed before submitting
            //printf( "Divide %s and %s\n", num1, num2 );
            // NOTE: Uncomment this once you've implemented this function
             bigint a = make_bigint(num1);
             bigint b = make_bigint(num2);
             result = bigint_div( a, b);
             print_bigint(result);
          }
        }
        else
        {
          printf( "Invalid command\n" );
        }

    }

    return 0;
}