iPXE
weierstrass.h File Reference

Weierstrass elliptic curves. More...

#include <ipxe/bigint.h>
#include <ipxe/crypto.h>
#include <ipxe/elliptic.h>

Go to the source code of this file.

Data Structures

struct  weierstrass_curve
 A Weierstrass elliptic curve. More...

Macros

#define WEIERSTRASS_AXES   2
 Number of axes in Weierstrass curve point representation.
#define WEIERSTRASS_MAX_MULT_LOG2   5 /* maximum reached is mod 20N */
 Maximum multiple of field prime encountered during calculations.
#define weierstrass_size(len)
 Determine number of elements in scalar values for a Weierstrass curve.
#define weierstrass_t(size)
 Define a Weierstrass projective co-ordinate type.
#define WEIERSTRASS_NUM_MONT   3
 Number of cached in Montgomery form for each Weierstrass curve.
#define WEIERSTRASS_NUM_CACHED
 Number of cached big integers for each Weierstrass curve.
#define WEIERSTRASS_CURVE(_name, _curve, _len, _prime, _a, _b, _base, _order)
 Define a Weierstrass curve.

Enumerations

enum  weierstrass_multiple { WEIERSTRASS_N = 0 , WEIERSTRASS_2N , WEIERSTRASS_4N , WEIERSTRASS_NUM_MULTIPLES }
 Indexes for stored multiples of the field prime. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
int weierstrass_is_infinity (struct elliptic_curve *curve, const void *point)
 Check if this is the point at infinity.
int weierstrass_multiply (struct elliptic_curve *curve, const void *base, const void *scalar, void *result)
 Multiply curve point by scalar.
int weierstrass_add_once (struct elliptic_curve *curve, const void *addend, const void *augend, void *result)
 Add curve points (as a one-off operation).
void weierstrass_share (struct exchange_algorithm *exchange, const void *private, void *public)
int weierstrass_agree (struct exchange_algorithm *exchange, const void *private, const void *partner, void *shared)

Detailed Description

Weierstrass elliptic curves.

Definition in file weierstrass.h.

Macro Definition Documentation

◆ WEIERSTRASS_AXES

#define WEIERSTRASS_AXES   2

Number of axes in Weierstrass curve point representation.

Definition at line 18 of file weierstrass.h.

Referenced by weierstrass_done_raw(), weierstrass_init_raw(), and weierstrass_is_infinity().

◆ WEIERSTRASS_MAX_MULT_LOG2

#define WEIERSTRASS_MAX_MULT_LOG2   5 /* maximum reached is mod 20N */

Maximum multiple of field prime encountered during calculations.

Calculations are performed using values modulo a small multiple of the field prime, rather than modulo the field prime itself. This allows explicit reductions after additions, subtractions, and relaxed Montgomery multiplications to be omitted entirely, provided that we keep careful track of the field prime multiple for each intermediate value.

Relaxed Montgomery multiplication will produce a result in the range t < (1+(m^2)/k)N, where m is this maximum multiple of the field prime, and k is the constant in R > kN representing the leading zero padding in the big integer representation of the field prime. We choose to set k=m^2 so that multiplications will always produce a result in the range t < 2N.

A lower value of k would be possible, at the cost of having to keep track of the field prime multiples used at each multiplication input (rather than keeping track only of a single global maximum). This would gain nothing in practice: any value of k below around 2^32 will end up requiring just a single extra big integer element.

This is expressed as the base-two logarithm of the multiple (rounded up), to simplify compile-time calculations.

Definition at line 46 of file weierstrass.h.

◆ weierstrass_size

#define weierstrass_size ( len)
Value:
( ( 2 * WEIERSTRASS_MAX_MULT_LOG2 + 7 ) \
/ 8 ) )
ring len
Length.
Definition dwmac.h:226
#define bigint_required_size(len)
Determine number of elements required for a big-integer type.
Definition bigint.h:32
#define WEIERSTRASS_MAX_MULT_LOG2
Maximum multiple of field prime encountered during calculations.
Definition weierstrass.h:46

Determine number of elements in scalar values for a Weierstrass curve.

Parameters
lenLength of field prime, in bytes
Return values
sizeNumber of elements

Definition at line 54 of file weierstrass.h.

54#define weierstrass_size( len ) \
55 bigint_required_size ( (len) + \
56 ( ( 2 * WEIERSTRASS_MAX_MULT_LOG2 + 7 ) \
57 / 8 ) )

◆ weierstrass_t

#define weierstrass_t ( size)
Value:
union { \
bigint_t ( size ) axis[3]; \
struct { \
bigint_t ( size ) x; \
bigint_t ( size ) y; \
bigint_t ( size ) z; \
}; \
bigint_t ( size * 2 ) xy; \
bigint_t ( size * 3 ) all; \
}
uint16_t size
Buffer size.
Definition dwmac.h:3
static unsigned int unsigned int y
Definition pixbuf.h:63
static unsigned int x
Definition pixbuf.h:63

Define a Weierstrass projective co-ordinate type.

Parameters
sizeNumber of elements in scalar values
Return values
typeProjective co-ordinate type

Definition at line 65 of file weierstrass.h.

65#define weierstrass_t( size ) \
66 union { \
67 bigint_t ( size ) axis[3]; \
68 struct { \
69 bigint_t ( size ) x; \
70 bigint_t ( size ) y; \
71 bigint_t ( size ) z; \
72 }; \
73 bigint_t ( size * 2 ) xy; \
74 bigint_t ( size * 3 ) all; \
75 }

Referenced by weierstrass_add_ladder(), weierstrass_add_once(), weierstrass_add_raw(), weierstrass_done_raw(), weierstrass_init_raw(), weierstrass_multiply(), and weierstrass_verify_raw().

◆ WEIERSTRASS_NUM_MONT

#define WEIERSTRASS_NUM_MONT   3

Number of cached in Montgomery form for each Weierstrass curve.

Definition at line 86 of file weierstrass.h.

Referenced by weierstrass_init_curve().

◆ WEIERSTRASS_NUM_CACHED

#define WEIERSTRASS_NUM_CACHED
Value:
1 /* fermat */ + 1 /* mont */ + \
#define WEIERSTRASS_NUM_MONT
Number of cached in Montgomery form for each Weierstrass curve.
Definition weierstrass.h:86
@ WEIERSTRASS_NUM_MULTIPLES
Definition weierstrass.h:82

Number of cached big integers for each Weierstrass curve.

Definition at line 89 of file weierstrass.h.

89#define WEIERSTRASS_NUM_CACHED \
90 ( WEIERSTRASS_NUM_MULTIPLES + \
91 1 /* fermat */ + 1 /* mont */ + \
92 WEIERSTRASS_NUM_MONT )

◆ WEIERSTRASS_CURVE

#define WEIERSTRASS_CURVE ( _name,
_curve,
_len,
_prime,
_a,
_b,
_base,
_order )

Define a Weierstrass curve.

Definition at line 148 of file weierstrass.h.

Enumeration Type Documentation

◆ weierstrass_multiple

Indexes for stored multiples of the field prime.

Enumerator
WEIERSTRASS_N 
WEIERSTRASS_2N 
WEIERSTRASS_4N 
WEIERSTRASS_NUM_MULTIPLES 

Definition at line 78 of file weierstrass.h.

78 {
79 WEIERSTRASS_N = 0,
83};
@ WEIERSTRASS_N
Definition weierstrass.h:79
@ WEIERSTRASS_2N
Definition weierstrass.h:80
@ WEIERSTRASS_4N
Definition weierstrass.h:81

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ weierstrass_is_infinity()

int weierstrass_is_infinity ( struct elliptic_curve * curve,
const void * point )
extern

Check if this is the point at infinity.

Parameters
curveElliptic curve
pointCurve point
Return values
is_infinityThis is the point at infinity

Definition at line 929 of file weierstrass.c.

930 {
931 struct weierstrass_curve *weierstrass = curve->priv;
932 unsigned int size = weierstrass->size;
933 size_t len = weierstrass->len;
934 struct {
935 bigint_t ( size ) axis;
936 } temp;
937 size_t offset;
938 int is_finite = 0;
939 unsigned int i;
940
941 /* We use all zeroes to represent the point at infinity */
942 DBGC ( curve, "WEIERSTRASS %s point (", curve->name );
943 for ( i = 0, offset = 0 ; i < WEIERSTRASS_AXES ; i++, offset += len ) {
944 bigint_init ( &temp.axis, ( point + offset ), len );
945 DBGC ( curve, "%s%s", ( i ? "," : "" ),
946 bigint_ntoa ( &temp.axis ) );
947 is_finite |= ( ! bigint_is_zero ( &temp.axis ) );
948 }
949 DBGC ( curve, ") is%s infinity\n", ( is_finite ? " not" : "" ) );
950
951 return ( ! is_finite );
952}
uint16_t offset
Offset to command line.
Definition bzimage.h:3
#define DBGC(...)
Definition compiler.h:530
#define bigint_is_zero(value)
Test if big integer is equal to zero.
Definition bigint.h:135
#define bigint_t(size)
Define a big-integer type.
Definition bigint.h:21
#define bigint_ntoa(value)
Transcribe big integer (for debugging).
Definition bigint.h:51
#define bigint_init(value, data, len)
Initialise big integer.
Definition bigint.h:63
const char * name
Curve name.
Definition crypto.h:248
void * priv
Algorithm private data.
Definition crypto.h:290
A Weierstrass elliptic curve.
Definition weierstrass.h:99
size_t len
Length of raw scalar values.
const unsigned int size
Number of elements in scalar values.
#define WEIERSTRASS_AXES
Number of axes in Weierstrass curve point representation.
Definition weierstrass.h:18

References bigint_init, bigint_is_zero, bigint_ntoa, bigint_t, DBGC, len, weierstrass_curve::len, elliptic_curve::name, offset, elliptic_curve::priv, size, weierstrass_curve::size, and WEIERSTRASS_AXES.

◆ weierstrass_multiply()

int weierstrass_multiply ( struct elliptic_curve * curve,
const void * base,
const void * scalar,
void * result )
extern

Multiply curve point by scalar.

Parameters
curveElliptic curve
baseBase point
scalarScalar multiple
resultResult point to fill in
Return values
rcReturn status code

Definition at line 963 of file weierstrass.c.

964 {
965 struct weierstrass_curve *weierstrass = curve->priv;
966 unsigned int size = weierstrass->size;
967 size_t len = weierstrass->len;
968 const bigint_t ( size ) __attribute__ (( may_alias )) *one =
969 ( ( const void * ) weierstrass->one );
970 struct {
972 weierstrass_t ( size ) multiple;
973 bigint_t ( bigint_required_size ( len ) ) scalar;
974 } temp;
975 int rc;
976
977 /* Convert input to projective coordinates in Montgomery form */
978 if ( ( rc = weierstrass_init ( curve, &temp.multiple, &temp.result,
979 base ) ) != 0 ) {
980 return rc;
981 }
982
983 /* Construct identity element (the point at infinity) */
984 memset ( &temp.result, 0, sizeof ( temp.result ) );
985 bigint_copy ( one, &temp.result.y );
986
987 /* Initialise scalar */
988 bigint_init ( &temp.scalar, scalar, len );
989 DBGC ( curve, "WEIERSTRASS %s scalar %s\n",
990 curve->name, bigint_ntoa ( &temp.scalar ) );
991
992 /* Perform multiplication via Montgomery ladder */
993 bigint_ladder ( &temp.result.all, &temp.multiple.all, &temp.scalar,
995
996 /* Convert result back to affine co-ordinates */
997 weierstrass_done ( curve, &temp.result, &temp.multiple, result );
998
999 return 0;
1000}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
uint16_t result
Definition hyperv.h:33
#define __attribute__(x)
Definition compiler.h:10
#define bigint_ladder(result, multiple, exponent, op, ctx, tmp)
Perform generalised exponentiation via a Montgomery ladder.
Definition bigint.h:331
#define bigint_copy(source, dest)
Copy big integer.
Definition bigint.h:236
void * memset(void *dest, int character, size_t len) __nonnull
uint32_t base
Base.
Definition librm.h:3
bigint_element_t * one
Cached constant "1", in Montgomery form.
#define weierstrass_done(curve, point, temp, out)
Finalise curve point.
static void weierstrass_add_ladder(const bigint_element_t *operand0, bigint_element_t *result0, unsigned int size, const void *ctx, void *tmp __unused)
Add points on curve as part of a Montgomery ladder.
#define weierstrass_init(curve, point, temp, data)
Initialise curve point.
#define weierstrass_t(size)
Define a Weierstrass projective co-ordinate type.
Definition weierstrass.h:65

References __attribute__, base, bigint_copy, bigint_init, bigint_ladder, bigint_ntoa, bigint_required_size, bigint_t, DBGC, len, weierstrass_curve::len, memset(), elliptic_curve::name, NULL, weierstrass_curve::one, elliptic_curve::priv, rc, result, size, weierstrass_curve::size, weierstrass_add_ladder(), weierstrass_done, weierstrass_init, and weierstrass_t.

◆ weierstrass_add_once()

int weierstrass_add_once ( struct elliptic_curve * curve,
const void * addend,
const void * augend,
void * result )
extern

Add curve points (as a one-off operation).

Parameters
curveElliptic curve
addendCurve point to add
augendCurve point to add
resultCurve point to hold result
Return values
rcReturn status code

Definition at line 1011 of file weierstrass.c.

1013 {
1014 struct weierstrass_curve *weierstrass = curve->priv;
1015 unsigned int size = weierstrass->size;
1016 struct {
1017 weierstrass_t ( size ) addend;
1018 weierstrass_t ( size ) augend;
1020 } temp;
1021 int rc;
1022
1023 /* Convert inputs to projective coordinates in Montgomery form */
1024 if ( ( rc = weierstrass_init ( curve, &temp.addend, &temp.result,
1025 addend ) ) != 0 ) {
1026 return rc;
1027 }
1028 if ( ( rc = weierstrass_init ( curve, &temp.augend, &temp.result,
1029 augend ) ) != 0 ) {
1030 return rc;
1031 }
1032
1033 /* Add curve points */
1034 weierstrass_add ( curve, &temp.augend, &temp.addend, &temp.result );
1035
1036 /* Convert result back to affine co-ordinates */
1037 weierstrass_done ( curve, &temp.result, &temp.addend, result );
1038
1039 return 0;
1040}
#define weierstrass_add(curve, augend, addend, result)
Add points on curve.

References elliptic_curve::priv, rc, result, size, weierstrass_curve::size, weierstrass_add, weierstrass_done, weierstrass_init, and weierstrass_t.

◆ weierstrass_share()

void weierstrass_share ( struct exchange_algorithm * exchange,
const void * private,
void * public )
extern

◆ weierstrass_agree()

int weierstrass_agree ( struct exchange_algorithm * exchange,
const void * private,
const void * partner,
void * shared )
extern

References partner.