GNUnet 0.21.1
gnsrecord_pow.c File Reference
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_gnsrecord_lib.h"
#include "gnunet_signatures.h"
#include "gnunet_protocols.h"
#include <inttypes.h>
Include dependency graph for gnsrecord_pow.c:

Go to the source code of this file.

Data Structures

struct  BestPow
 API for proof of work. More...
 
struct  GNUNET_GNSRECORD_PowCalculationHandle
 The handle to a PoW calculation. More...
 

Functions

static unsigned int calculate_score (const struct GNUNET_GNSRECORD_PowCalculationHandle *ph)
 Calculate the average zeros in the pows. More...
 
struct GNUNET_GNSRECORD_SignaturePurposePSGNR_create_signature_message (const struct GNUNET_GNSRECORD_PowP *pow)
 Create the revocation metadata to sign for a revocation message. More...
 
enum GNUNET_GenericReturnValue check_signature_identity (const struct GNUNET_GNSRECORD_PowP *pow, const struct GNUNET_CRYPTO_PublicKey *key)
 
enum GNUNET_GenericReturnValue check_signature (const struct GNUNET_GNSRECORD_PowP *pow)
 
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_check_pow (const struct GNUNET_GNSRECORD_PowP *pow, unsigned int difficulty, struct GNUNET_TIME_Relative epoch_duration)
 Check if the given proof-of-work is valid. More...
 
enum GNUNET_GenericReturnValue sign_pow_identity (const struct GNUNET_CRYPTO_PrivateKey *key, struct GNUNET_GNSRECORD_PowP *pow)
 
enum GNUNET_GenericReturnValue sign_pow (const struct GNUNET_CRYPTO_PrivateKey *key, struct GNUNET_GNSRECORD_PowP *pow)
 
void GNUNET_GNSRECORD_pow_init (const struct GNUNET_CRYPTO_PrivateKey *key, struct GNUNET_GNSRECORD_PowP *pow)
 Initializes a fresh PoW computation. More...
 
struct GNUNET_GNSRECORD_PowCalculationHandleGNUNET_GNSRECORD_pow_start (struct GNUNET_GNSRECORD_PowP *pow, int epochs, unsigned int difficulty)
 Starts a proof-of-work calculation given the pow object as well as target epochs and difficulty. More...
 
static int cmp_pow_value (const void *a, const void *b)
 Comparison function for quicksort. More...
 
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_pow_round (struct GNUNET_GNSRECORD_PowCalculationHandle *pc)
 Calculate a key revocation valid for broadcasting for a number of epochs. More...
 
size_t GNUNET_GNSRECORD_proof_get_size (const struct GNUNET_GNSRECORD_PowP *pow)
 
void GNUNET_GNSRECORD_pow_stop (struct GNUNET_GNSRECORD_PowCalculationHandle *pc)
 Stop a PoW calculation. More...
 

Variables

static struct GNUNET_CRYPTO_PowSalt salt = { "GnsRevocationPow" }
 

Function Documentation

◆ calculate_score()

static unsigned int calculate_score ( const struct GNUNET_GNSRECORD_PowCalculationHandle ph)
static

Calculate the average zeros in the pows.

Parameters
phthe PowHandle
Returns
the average number of zeros.

Definition at line 94 of file gnsrecord_pow.c.

95{
96 double sum = 0.0;
97 for (unsigned int j = 0; j<POW_COUNT; j++)
98 sum += ph->best[j].bits;
99 double avg = sum / POW_COUNT;
100 return avg;
101}
#define POW_COUNT
The proof-of-work narrowing factor.
unsigned int bits
Corresponding zero bits in hash.
Definition: gnsrecord_pow.c:47
struct BestPow best[POW_COUNT]
Current set of found PoWs.
Definition: gnsrecord_pow.c:60

References GNUNET_GNSRECORD_PowCalculationHandle::best, BestPow::bits, POW_COUNT, and consensus-simulation::sum.

Referenced by GNUNET_GNSRECORD_pow_round().

Here is the caller graph for this function:

◆ GNR_create_signature_message()

struct GNUNET_GNSRECORD_SignaturePurposePS * GNR_create_signature_message ( const struct GNUNET_GNSRECORD_PowP pow)

Create the revocation metadata to sign for a revocation message.

Parameters
powthe PoW to sign
Returns
the signature purpose

Definition at line 105 of file gnsrecord_pow.c.

106{
108 const struct GNUNET_CRYPTO_PublicKey *pk;
109 size_t ksize;
110
111 pk = (const struct GNUNET_CRYPTO_PublicKey *) &pow[1];
113 spurp = GNUNET_malloc (sizeof (*spurp) + ksize);
114 spurp->timestamp = pow->timestamp;
116 spurp->purpose.size = htonl (sizeof(*spurp) + ksize);
118 (char*) &spurp[1],
119 ksize);
120 return spurp;
121}
struct GNUNET_CRYPTO_PrivateKey pk
Private key from command line option, or NULL.
ssize_t GNUNET_CRYPTO_public_key_get_length(const struct GNUNET_CRYPTO_PublicKey *key)
Get the compacted length of a GNUNET_CRYPTO_PublicKey.
Definition: crypto_pkey.c:68
ssize_t GNUNET_CRYPTO_write_public_key_to_buffer(const struct GNUNET_CRYPTO_PublicKey *key, void *buffer, size_t len)
Writes a GNUNET_CRYPTO_PublicKey to a compact buffer.
Definition: crypto_pkey.c:128
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_SIGNATURE_PURPOSE_GNS_REVOCATION
GNS zone key revocation (GNS)
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!...
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
An identity key as per LSD0001.
struct GNUNET_TIME_AbsoluteNBO timestamp
The timestamp of the revocation.
The signature object we use for the PoW.
struct GNUNET_TIME_AbsoluteNBO timestamp
The timestamp of the revocation.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
The signature purpose.

References GNUNET_CRYPTO_public_key_get_length(), GNUNET_CRYPTO_write_public_key_to_buffer(), GNUNET_malloc, GNUNET_SIGNATURE_PURPOSE_GNS_REVOCATION, pk, GNUNET_CRYPTO_EccSignaturePurpose::purpose, GNUNET_GNSRECORD_SignaturePurposePS::purpose, GNUNET_CRYPTO_EccSignaturePurpose::size, GNUNET_GNSRECORD_PowP::timestamp, and GNUNET_GNSRECORD_SignaturePurposePS::timestamp.

Referenced by check_signature_identity(), run_with_key(), and sign_pow_identity().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_signature_identity()

enum GNUNET_GenericReturnValue check_signature_identity ( const struct GNUNET_GNSRECORD_PowP pow,
const struct GNUNET_CRYPTO_PublicKey key 
)

Definition at line 125 of file gnsrecord_pow.c.

127{
129 unsigned char *sig;
130 size_t ksize;
131 int ret;
132
134 spurp = GNR_create_signature_message (pow);
135 sig = ((unsigned char*) &pow[1] + ksize);
136 ret =
139 &spurp->purpose,
140 sig,
141 key);
142 GNUNET_free (spurp);
143 return ret == GNUNET_OK ? GNUNET_OK : GNUNET_SYSERR;
144}
struct GNUNET_GNSRECORD_SignaturePurposePS * GNR_create_signature_message(const struct GNUNET_GNSRECORD_PowP *pow)
Create the revocation metadata to sign for a revocation message.
static int ret
Final status code.
Definition: gnunet-arm.c:94
struct GNUNET_HashCode key
The key used in the DHT.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_signature_verify_raw_(uint32_t purpose, const struct GNUNET_CRYPTO_EccSignaturePurpose *validate, const unsigned char *sig, const struct GNUNET_CRYPTO_PublicKey *pub)
Verify a given signature.
Definition: crypto_pkey.c:346
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_free(ptr)
Wrapper around free.

References GNR_create_signature_message(), GNUNET_CRYPTO_public_key_get_length(), GNUNET_CRYPTO_signature_verify_raw_(), GNUNET_free, GNUNET_OK, GNUNET_SIGNATURE_PURPOSE_GNS_REVOCATION, GNUNET_SYSERR, key, GNUNET_GNSRECORD_SignaturePurposePS::purpose, and ret.

Referenced by check_signature().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_signature()

enum GNUNET_GenericReturnValue check_signature ( const struct GNUNET_GNSRECORD_PowP pow)

Definition at line 148 of file gnsrecord_pow.c.

149{
150 const struct GNUNET_CRYPTO_PublicKey *pk;
151
152 pk = (const struct GNUNET_CRYPTO_PublicKey *) &pow[1];
153 return check_signature_identity (pow, pk);
154}
enum GNUNET_GenericReturnValue check_signature_identity(const struct GNUNET_GNSRECORD_PowP *pow, const struct GNUNET_CRYPTO_PublicKey *key)

References check_signature_identity(), and pk.

Referenced by GNUNET_GNSRECORD_check_pow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sign_pow_identity()

enum GNUNET_GenericReturnValue sign_pow_identity ( const struct GNUNET_CRYPTO_PrivateKey key,
struct GNUNET_GNSRECORD_PowP pow 
)

Predate the validity period to prevent rejections due to unsynchronized clocks

Definition at line 268 of file gnsrecord_pow.c.

270{
273 const struct GNUNET_CRYPTO_PublicKey *pk;
274 size_t ksize;
275 char *sig;
276
283 pk = (const struct GNUNET_CRYPTO_PublicKey *) &pow[1];
287 sig = ((char*) &pow[1]) + ksize;
289 &rp->purpose,
290 (void*) sig);
291 GNUNET_free (rp);
292 if (result == GNUNET_SYSERR)
293 return GNUNET_NO;
294 else
295 return result;
296}
static char * rp
Relying party.
static int result
Global testing status.
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_sign_raw_(const struct GNUNET_CRYPTO_PrivateKey *priv, const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose, unsigned char *sig)
Sign a given block.
Definition: crypto_pkey.c:264
@ GNUNET_NO
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:111
#define GNUNET_TIME_UNIT_WEEKS
One week.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_subtract(struct GNUNET_TIME_Absolute start, struct GNUNET_TIME_Relative duration)
Subtract a given relative duration from the given start time.
Definition: time.c:469
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:638
Time for absolute times used by GNUnet, in microseconds.

References GNR_create_signature_message(), GNUNET_CRYPTO_public_key_get_length(), GNUNET_CRYPTO_sign_raw_(), GNUNET_free, GNUNET_NO, GNUNET_SYSERR, GNUNET_TIME_absolute_get(), GNUNET_TIME_absolute_hton(), GNUNET_TIME_absolute_subtract(), GNUNET_TIME_UNIT_WEEKS, key, pk, result, rp, and GNUNET_GNSRECORD_PowP::timestamp.

Referenced by sign_pow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sign_pow()

enum GNUNET_GenericReturnValue sign_pow ( const struct GNUNET_CRYPTO_PrivateKey key,
struct GNUNET_GNSRECORD_PowP pow 
)

Definition at line 300 of file gnsrecord_pow.c.

302{
304
305 pk = (struct GNUNET_CRYPTO_PublicKey *) &pow[1];
307 return sign_pow_identity (key, pow);
308}
enum GNUNET_GenericReturnValue sign_pow_identity(const struct GNUNET_CRYPTO_PrivateKey *key, struct GNUNET_GNSRECORD_PowP *pow)
enum GNUNET_GenericReturnValue GNUNET_CRYPTO_key_get_public(const struct GNUNET_CRYPTO_PrivateKey *privkey, struct GNUNET_CRYPTO_PublicKey *key)
Retrieves the public key representation of a private key.
Definition: crypto_pkey.c:602

References GNUNET_CRYPTO_key_get_public(), key, pk, and sign_pow_identity().

Referenced by GNUNET_GNSRECORD_pow_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cmp_pow_value()

static int cmp_pow_value ( const void *  a,
const void *  b 
)
static

Comparison function for quicksort.

Parameters
aleft element
bright element
Returns
a-b

Definition at line 355 of file gnsrecord_pow.c.

356{
357 return (GNUNET_ntohll (*(uint64_t*) a) - GNUNET_ntohll (*(uint64_t*) b));
358}
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
Definition: common_endian.c:54

References GNUNET_ntohll().

Referenced by GNUNET_GNSRECORD_pow_round().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ salt

struct GNUNET_CRYPTO_PowSalt salt = { "GnsRevocationPow" }
static

Definition at line 85 of file gnsrecord_pow.c.

Referenced by GNUNET_GNSRECORD_check_pow(), and GNUNET_GNSRECORD_pow_round().