Skip to content

Commit

Permalink
Move F25519 ops into separate header; add header for Curve25519
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Harvey committed May 29, 2014
1 parent 2dd6d73 commit d027d15
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 56 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@
*.exe
*.out
*.app
*.pyc

12 changes: 1 addition & 11 deletions src/curve25519_mini.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,7 @@
*/

#define MPIMINI_INTERNAL_API
#include "mpi_mini.h"

#include <stdio.h>
typedef struct
{
uint8_t bytes[F25519MINI_MSGSIZE];
}
Curve25519Msg_Mini;

extern MCResult Curve25519_mini(Curve25519Msg_Mini *res, const Curve25519Msg_Mini *a,
const Curve25519Msg_Mini *P );
#include "curve25519_mini.h"

/* ------------------ */

Expand Down
39 changes: 39 additions & 0 deletions src/curve25519_mini.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Minicrypt library - Curve25519 public-key crypto primitive
*
* See: http://cr.yp.to/ecdh.html
*
* https://github.com/IanHarvey/minicrypt
*
* This file is placed in the public domain by its author.
* Note there is NO WARRANTY.
*
*/
#ifndef CURVE25519_MINI_H
#define CURVE25519_MINI_H

#include "minicrypt.h"
#include "f25519_mini.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct
{
uint8_t bytes[F25519MINI_MSGSIZE];
}
Curve25519Msg_Mini;

extern MCResult Curve25519_mini(Curve25519Msg_Mini *res, const Curve25519Msg_Mini *a,
const Curve25519Msg_Mini *P );
/* Multiplies base point P by scalar a, giving result in res.
* Returns error only if base point is invalid.
*/

#ifdef __cplusplus
}
#endif

#endif

60 changes: 60 additions & 0 deletions src/f25519_mini.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Minicrypt library - F25519 (2^255-19) field operations
*
* https://github.com/IanHarvey/minicrypt
*
* This file is placed in the public domain by its author.
* Note there is NO WARRANTY.
*
*/
#ifndef F25519_MINI_H
#define F25519_MINI_H

#include "minicrypt.h"
#include "mpi_mini.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef UInt_Mini F25519_Mini;

#define F25519MINI_MSGSIZE 32
extern MCResult F25519_set_mini(F25519_Mini *res, const uint8_t *bytes, size_t len);
/* Sets a field element from a byte-block message, in
* little-endian. 'len' must be exactly F25519MINI_MSGSIZE. */

extern void F25519_setK_mini(F25519_Mini *res, uint32_t n);
/* Sets a field element to a 1-word value */

extern MCResult F25519_get_mini(uint8_t *bytes, size_t len, const F25519_Mini *s);
/* Writes a field element as a byte-block message. len should be F25519MINI_MSGSIZE */

extern void F25519_copy_mini(F25519_Mini *res, const F25519_Mini *s);
/* Does *res = *s. No-op if args are the same */

extern int F25519_equal_mini(const UInt_Mini *a, const UInt_Mini *b);
/* Returns nonzero if *a==*b, or 0 if they're different */

extern void F25519_add3_mini(F25519_Mini *res, const F25519_Mini *s1, const F25519_Mini *s2);
/* Does *res = *s1 + *s2 in field arithmetic. Operands are allowed to be the same */

extern void F25519_sub3_mini(F25519_Mini *res, const F25519_Mini *s1, const F25519_Mini *s2);
/* Does *res = *s1 - *s2 in field arithmetic. Operands are allowed to be the same */

extern void F25519_mul3_mini(F25519_Mini *res, const F25519_Mini *s1, const F25519_Mini *s2);
/* Does *res = *s1 x *s2 in field arithmetic. Operands are allowed to be the same */

extern void F25519_sqr_mini(F25519_Mini *res, const F25519_Mini *s);
/* Does *res = *s x *s in field arithmetic. Operands are allowed to be the same */

extern void F25519_mulK_mini(F25519_Mini *res, const F25519_Mini *s1, uint32_t s2);
/* Does *res = *s1 x s2 in field arithmetic, where s2 is a small integer */


#ifdef __cplusplus
}
#endif

#endif

2 changes: 1 addition & 1 deletion src/f25519add_mini.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

#define MPIMINI_INTERNAL_API
#include "mpi_mini.h"
#include "f25519_mini.h"

void F25519_add3_mini(F25519_Mini *res, const F25519_Mini *s1, const F25519_Mini *s2)
{
Expand Down
2 changes: 1 addition & 1 deletion src/f25519mul_mini.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

#define MPIMINI_INTERNAL_API
#include "mpi_mini.h"
#include "f25519_mini.h"


static void mul_reduce_approx(uint32_t *dst, uint32_t *src)
Expand Down
2 changes: 1 addition & 1 deletion src/f25519sub_mini.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

#define MPIMINI_INTERNAL_API
#include "mpi_mini.h"
#include "f25519_mini.h"

void F25519_sub3_mini(F25519_Mini *res, const F25519_Mini *s1, const F25519_Mini *s2)
{
Expand Down
2 changes: 1 addition & 1 deletion src/f25519util_mini.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

#define MPIMINI_INTERNAL_API
#include "mpi_mini.h"
#include "f25519_mini.h"

const UInt_Mini F25519_P_mini_ =
{
Expand Down
4 changes: 2 additions & 2 deletions src/minicrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* Note there is NO WARRANTY.
*
*/
#ifndef _MINICRYPT_H
#define _MINICRYPT_H
#ifndef MINICRYPT_H
#define MINICRYPT_H

#include <stdint.h>
#include <stddef.h>
Expand Down
39 changes: 0 additions & 39 deletions src/mpi_mini.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,45 +55,6 @@ extern int mpicmp_mini( const UInt_Mini *a, const UInt_Mini *b );
extern void mpisetval_mini( UInt_Mini *res, uint32_t val);
/* Sets 'res' to a 32-bit value */


/* F25519 (2^255-19 prime field) operations
*
*/

typedef UInt_Mini F25519_Mini;

#define F25519MINI_MSGSIZE 32
extern MCResult F25519_set_mini(F25519_Mini *res, const uint8_t *bytes, size_t len);
/* Sets a field element from a byte-block message, in
* little-endian. 'len' must be exactly F25519MINI_MSGSIZE. */

extern void F25519_setK_mini(F25519_Mini *res, uint32_t n);
/* Sets a field element to a 1-word value */

extern MCResult F25519_get_mini(uint8_t *bytes, size_t len, const F25519_Mini *s);
/* Writes a field element as a byte-block message. len should be F25519MINI_MSGSIZE */

extern void F25519_copy_mini(F25519_Mini *res, const F25519_Mini *s);
/* Does *res = *s. No-op if args are the same */

extern int F25519_equal_mini(const UInt_Mini *a, const UInt_Mini *b);
/* Returns nonzero if *a==*b, or 0 if they're different */

extern void F25519_add3_mini(F25519_Mini *res, const F25519_Mini *s1, const F25519_Mini *s2);
/* Does *res = *s1 + *s2 in field arithmetic. Operands are allowed to be the same */

extern void F25519_sub3_mini(F25519_Mini *res, const F25519_Mini *s1, const F25519_Mini *s2);
/* Does *res = *s1 - *s2 in field arithmetic. Operands are allowed to be the same */

extern void F25519_mul3_mini(F25519_Mini *res, const F25519_Mini *s1, const F25519_Mini *s2);
/* Does *res = *s1 x *s2 in field arithmetic. Operands are allowed to be the same */

extern void F25519_sqr_mini(F25519_Mini *res, const F25519_Mini *s);
/* Does *res = *s x *s in field arithmetic. Operands are allowed to be the same */

extern void F25519_mulK_mini(F25519_Mini *res, const F25519_Mini *s1, uint32_t s2);
/* Does *res = *s1 x s2 in field arithmetic, where s2 is a small integer */


/* Internal API
*
Expand Down

0 comments on commit d027d15

Please sign in to comment.