Builtin random

This commit is contained in:
Pieter Wuille
2013-04-20 23:34:41 +02:00
parent b2966ce852
commit d06e61cbb5
8 changed files with 101 additions and 56 deletions

View File

@@ -16,12 +16,6 @@ void static secp256k1_num_sanity(const secp256k1_num_t *a) {
#define secp256k1_num_sanity(a) do { } while(0)
#endif
void static secp256k1_num_start(void) {
}
void static secp256k1_num_stop(void) {
}
void static secp256k1_num_init(secp256k1_num_t *r) {
r->neg = 0;
r->limbs = 1;
@@ -385,11 +379,4 @@ void static secp256k1_num_negate(secp256k1_num_t *r) {
r->neg ^= 1;
}
void static secp256k1_num_set_rand(secp256k1_num_t *r, const secp256k1_num_t *a) {
mpn_random(r->data, a->limbs);
r->limbs = a->limbs;
r->neg = 0;
secp256k1_num_mod(r, a);
}
#endif

View File

@@ -9,13 +9,6 @@
#include "../num.h"
void static secp256k1_num_start() {
}
void static secp256k1_num_stop() {
}
void static secp256k1_num_init(secp256k1_num_t *r) {
BN_init(&r->bn);
}
@@ -145,8 +138,4 @@ void static secp256k1_num_negate(secp256k1_num_t *r) {
BN_set_negative(&r->bn, !BN_is_negative(&r->bn));
}
void static secp256k1_num_set_rand(secp256k1_num_t *r, const secp256k1_num_t *a) {
BN_pseudo_rand_range(&r->bn, &a->bn);
}
#endif

48
src/impl/util.h Normal file
View File

@@ -0,0 +1,48 @@
#ifndef _SECP256K1_UTIL_IMPL_H_
#define _SECP256K1_UTIL_IMPL_H_
#include <stdint.h>
#include <string.h>
#include "../util.h"
static inline uint32_t secp256k1_rand32(void) {
static uint32_t Rz = 11, Rw = 11;
Rz = 36969 * (Rz & 0xFFFF) + (Rz >> 16);
Rw = 18000 * (Rw & 0xFFFF) + (Rw >> 16);
return (Rw << 16) + Rz;
}
static void secp256k1_rand256(unsigned char *b32) {
for (int i=0; i<8; i++) {
uint32_t r = secp256k1_rand32();
b32[i*4 + 0] = (r >> 0) & 0xFF;
b32[i*4 + 1] = (r >> 8) & 0xFF;
b32[i*4 + 2] = (r >> 16) & 0xFF;
b32[i*4 + 3] = (r >> 24) & 0xFF;
}
}
static void secp256k1_rand256_test(unsigned char *b32) {
int bits=0;
uint32_t ent;
int entbits = 0;
memset(b32, 0, 32);
while (bits < 256) {
if (entbits < 15) {
ent = secp256k1_rand32();
entbits = 32;
}
int now = 1 + ((ent % 64)*((ent >> 6) % 32)+16)/31;
uint32_t val = 1 & (ent >> 11);
ent -= 12;
entbits >>= 12;
while (now > 0 && bits < 256) {
b32[bits / 8] |= val << (bits % 8);
now--;
bits++;
}
}
}
#endif