71 lines
2.2 KiB
C
71 lines
2.2 KiB
C
#pragma once
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include <gmp.h>
|
|
|
|
#define assert_zero(__c__) { if ((__c__)!=0) { printf("assert_zero: " __FILE__ "\n"); return -1; }; }
|
|
//#define assert_zero(__c__) __c__
|
|
|
|
#define DUMP_MPZ(__prefix__, __mpz__) { char t[128]; mpz_get_str(t, 16, __mpz__); printf(__prefix__ ": %s\n", t); };
|
|
|
|
struct ec_point
|
|
{
|
|
mpz_t x, y;
|
|
};
|
|
typedef struct ec_point ec_point_t[1];
|
|
|
|
/***
|
|
* Define a curve of type y*y = x*x*x + a*x + b
|
|
***/
|
|
struct curve
|
|
{
|
|
mpz_t p;
|
|
mpz_t a, b;
|
|
mpz_t n;
|
|
|
|
int32_t h;
|
|
ec_point_t G;
|
|
|
|
int bitwidth;
|
|
};
|
|
typedef struct curve curve_t[1];
|
|
int named_curves_count;
|
|
|
|
int32_t curve_create_from_name(char *curve_name, curve_t curve);
|
|
int32_t curve_create(curve_t curve, mpz_t p, mpz_t a, mpz_t b, mpz_t n, int32_t h, mpz_t xg, mpz_t yg);
|
|
int32_t curve_destroy(curve_t curve);
|
|
|
|
int32_t curve_point_init(ec_point_t point);
|
|
int32_t curve_point_init_set(ec_point_t point, ec_point_t src);
|
|
int32_t curve_point_init_set_xy(ec_point_t point, mpz_t x, mpz_t y);
|
|
|
|
int32_t curve_point_clear(ec_point_t point);
|
|
|
|
int32_t curve_point_is_valid(ec_point_t point, curve_t curve);
|
|
int32_t curve_point_is_infinity(ec_point_t point);
|
|
|
|
int32_t curve_point_add(ec_point_t dst, ec_point_t p1, ec_point_t p2, curve_t curve);
|
|
int32_t curve_point_mul(ec_point_t dst, ec_point_t p1, mpz_t i, curve_t curve);
|
|
|
|
int32_t curve_map_to_curve( ec_point_t dst, mpz_t u, curve_t curve);
|
|
int32_t curve_hash_to_point( ec_point_t dst, char *message, int len, curve_t curve);
|
|
|
|
int32_t curve_get_generator( ec_point_t dst, curve_t curve );
|
|
|
|
static inline void curve_point_set(ec_point_t dst, ec_point_t src) {
|
|
mpz_set( dst->x, src->x );
|
|
mpz_set( dst->y, src->y );
|
|
};
|
|
|
|
static inline int32_t curve_point_equals(ec_point_t p1, ec_point_t p2) { return (mpz_cmp(p1->x, p2->x) == 0) && (mpz_cmp(p1->y, p2->y) == 0); }
|
|
|
|
void sha256(char *message, int len, char *hash);
|
|
void sha256_curve_points(ec_point_t* points, int points_length, curve_t curve, char *hash);
|
|
|
|
static inline void __tools_ec_point_cleanup(ec_point_t *p) { curve_point_clear( *p ); }
|
|
|
|
#define ec_point_t_cleanup(name) ec_point_t name __attribute__ (( __cleanup__(__tools_ec_point_cleanup))); curve_point_init( name ); |