ucryptolib/test.c
static void precomputeModulus(const BIGNUM* n, clBignumModulus* mod) {
  int size = BN_num_bytes(n);
  int nwords = size / 4;  // bytes to dwords
  int i;

  // assert n is positive, odd, and multiple of 32 bits.
  CHECK(!BN_is_negative(n));
  CHECK((BN_num_bits(n) & 31) == 0);
  CHECK(BN_is_odd(n));

  memset(mod, 0, sizeof *mod);

  {
    BN_CTX* ctx = BN_CTX_new();
    BIGNUM* R = BN_new();
    BIGNUM* RR = BN_new();
    BIGNUM* r = BN_new();
    BIGNUM* rem = BN_new();
    BIGNUM* N = BN_new();
    BIGNUM* N0inv = BN_new();

    BN_set_bit(r, 32);  // 2^32
    BN_copy(N, n);  // modulus
    BN_set_bit(R, nwords * 32);
    BN_mod_sqr(RR, R, N, ctx);

    BN_div(NULL, rem, N, r, ctx);
    BN_mod_inverse(N0inv, rem, r, ctx);

    mod->size = size;
    mod->nwords = nwords;
    mod->n0inv = 0 - BN_get_word(N0inv);
    for (i = 0; i < nwords; ++i) {
      BN_div(RR, rem, RR, r, ctx);
      mod->rr[i] = BN_get_word(rem);
      BN_div(N, rem, N, r, ctx);
      mod->n[i] = BN_get_word(rem);
    }

    BN_free(N0inv);
    BN_free(N);
    BN_free(rem);
    BN_free(r);
    BN_free(RR);
    BN_free(R);
    BN_CTX_free(ctx);
  }
}
// Compute bignum structure for simple C bignum code.
Compute bignum structure for simple C bignum code.
Compute bignum structure for simple C bignum code .
static void precomputeModulus(const BIGNUM* n, clBignumModulus* mod) {
  int size = BN_num_bytes(n);
  int nwords = size / 4;  
  int i;
  CHECK(!BN_is_negative(n));
  CHECK((BN_num_bits(n) & 31) == 0);
  CHECK(BN_is_odd(n));
  memset(mod, 0, sizeof *mod);
  {
    BN_CTX* ctx = BN_CTX_new();
    BIGNUM* R = BN_new();
    BIGNUM* RR = BN_new();
    BIGNUM* r = BN_new();
    BIGNUM* rem = BN_new();
    BIGNUM* N = BN_new();
    BIGNUM* N0inv = BN_new();
    BN_set_bit(r, 32);  
    BN_copy(N, n);  
    BN_set_bit(R, nwords * 32);
    BN_mod_sqr(RR, R, N, ctx);
    BN_div(NULL, rem, N, r, ctx);
    BN_mod_inverse(N0inv, rem, r, ctx);
    mod->size = size;
    mod->nwords = nwords;
    mod->n0inv = 0 - BN_get_word(N0inv);
    for (i = 0; i < nwords; ++i) {
      BN_div(RR, rem, RR, r, ctx);
      mod->rr[i] = BN_get_word(rem);
      BN_div(N, rem, N, r, ctx);
      mod->n[i] = BN_get_word(rem);
    }
    BN_free(N0inv);
    BN_free(N);
    BN_free(rem);
    BN_free(r);
    BN_free(RR);
    BN_free(R);
    BN_CTX_free(ctx);
  }
}
static void precomputeModulus ( const BIGNUM * n , clBignumModulus * mod ) { int size = BN_num_bytes ( n ) ; int nwords = size / 4 ; int i ; CHECK ( ! BN_is_negative ( n ) ) ; CHECK ( ( BN_num_bits ( n ) & 31 ) == 0 ) ; CHECK ( BN_is_odd ( n ) ) ; memset ( mod , 0 , sizeof * mod ) ; { BN_CTX * ctx = BN_CTX_new ( ) ; BIGNUM * R = BN_new ( ) ; BIGNUM * RR = BN_new ( ) ; BIGNUM * r = BN_new ( ) ; BIGNUM * rem = BN_new ( ) ; BIGNUM * N = BN_new ( ) ; BIGNUM * N0inv = BN_new ( ) ; BN_set_bit ( r , 32 ) ; BN_copy ( N , n ) ; BN_set_bit ( R , nwords * 32 ) ; BN_mod_sqr ( RR , R , N , ctx ) ; BN_div ( NULL , rem , N , r , ctx ) ; BN_mod_inverse ( N0inv , rem , r , ctx ) ; mod -> size = size ; mod -> nwords = nwords ; mod -> n0inv = 0 - BN_get_word ( N0inv ) ; for ( i = 0 ; i < nwords ; ++ i ) { BN_div ( RR , rem , RR , r , ctx ) ; mod -> rr [ i ] = BN_get_word ( rem ) ; BN_div ( N , rem , N , r , ctx ) ; mod -> n [ i ] = BN_get_word ( rem ) ; } BN_free ( N0inv ) ; BN_free ( N ) ; BN_free ( rem ) ; BN_free ( r ) ; BN_free ( RR ) ; BN_free ( R ) ; BN_CTX_free ( ctx ) ; } }
Compute bignum structure for simple C bignum code.
Compute bignum structure for simple C bignum code .
// bytes to dwords // assert n is positive, odd, and multiple of 32 bits. // 2^32 // modulus