github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/crypto/secp256k1/libsecp256k1/src/testrand_impl.h (about)

     1  /**********************************************************************
     2   * Copyright (c) 2013, 2014 Pieter Wuille                             *
     3   * Distributed under the MIT software license, see the accompanying   *
     4   * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
     5   **********************************************************************/
     6  
     7  #ifndef _SECP256K1_TESTRAND_IMPL_H_
     8  #define _SECP256K1_TESTRAND_IMPL_H_
     9  
    10  #include <stdint.h>
    11  #include <string.h>
    12  
    13  #include "testrand.h"
    14  #include "hash.h"
    15  
    16  static secp256k1_rfc6979_hmac_sha256_t secp256k1_test_rng;
    17  static uint32_t secp256k1_test_rng_precomputed[8];
    18  static int secp256k1_test_rng_precomputed_used = 8;
    19  
    20  SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed16) {
    21      secp256k1_rfc6979_hmac_sha256_initialize(&secp256k1_test_rng, seed16, 16);
    22  }
    23  
    24  SECP256K1_INLINE static uint32_t secp256k1_rand32(void) {
    25      if (secp256k1_test_rng_precomputed_used == 8) {
    26          secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, (unsigned char*)(&secp256k1_test_rng_precomputed[0]), sizeof(secp256k1_test_rng_precomputed));
    27          secp256k1_test_rng_precomputed_used = 0;
    28      }
    29      return secp256k1_test_rng_precomputed[secp256k1_test_rng_precomputed_used++];
    30  }
    31  
    32  static void secp256k1_rand256(unsigned char *b32) {
    33      secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, b32, 32);
    34  }
    35  
    36  static void secp256k1_rand256_test(unsigned char *b32) {
    37      int bits=0;
    38      uint64_t ent = 0;
    39      int entleft = 0;
    40      memset(b32, 0, 32);
    41      while (bits < 256) {
    42          int now;
    43          uint32_t val;
    44          if (entleft < 12) {
    45              ent |= ((uint64_t)secp256k1_rand32()) << entleft;
    46              entleft += 32;
    47          }
    48          now = 1 + ((ent % 64)*((ent >> 6) % 32)+16)/31;
    49          val = 1 & (ent >> 11);
    50          ent >>= 12;
    51          entleft -= 12;
    52          while (now > 0 && bits < 256) {
    53              b32[bits / 8] |= val << (bits % 8);
    54              now--;
    55              bits++;
    56          }
    57      }
    58  }
    59  
    60  #endif