Commit b0a21107 authored by Michael Hamburg's avatar Michael Hamburg

remove probably-unnecessary optimizations (still needs benching to make sure)

Conflicts:
	src/include/field.h
parent 825dd212
......@@ -21,7 +21,7 @@ field_eq (
field_copy(&rb, b);
field_weak_reduce(&ra);
field_weak_reduce(&rb);
field_sub(&ra, &ra, &rb);
field_sub_RAW(&ra, &ra, &rb);
field_bias(&ra, 2);
return field_is_zero(&ra);
}
......@@ -47,8 +47,7 @@ field_is_square (
field_isr ( &L0, x );
field_sqr ( &L1, &L0 );
field_mul ( &L0, x, &L1 );
field_subw ( &L0, 1 );
field_bias ( &L0, 1 );
field_subw( &L0, 1 );
L3 = field_is_zero( &L0 );
L2 = field_is_zero( x );
return L3 | L2;
......
This diff is collapsed.
......@@ -14,6 +14,13 @@
#include "f_field.h"
#include <string.h>
#define is32 (GOLDI_BITS == 32 || FIELD_BITS != 448)
#if (is32)
#define IF32(s) (s)
#else
#define IF32(s)
#endif
/** @brief Bytes in a field element */
#define FIELD_BYTES (1+(FIELD_BITS-1)/8)
......@@ -53,21 +60,6 @@ field_copy (
memcpy(a,b,sizeof(*a));
}
/**
* Negate a in place if doNegate.
*/
static inline void
__attribute__((unused,always_inline))
field_cond_neg(
field_t *a,
mask_t doNegate
) {
struct field_t negated;
field_neg(&negated, a);
field_bias(&negated, 2);
constant_time_select(a, &negated, a, sizeof(negated), doNegate);
}
/**
* Returns 1/sqrt(+- x).
*
......@@ -142,7 +134,7 @@ field_sqrn (
static __inline__ mask_t
__attribute__((unused,always_inline))
field_low_bit (const field_t *f) {
field_low_bit (const struct field_t *f) {
struct field_t red;
field_copy(&red,f);
field_strong_reduce(&red);
......@@ -151,10 +143,111 @@ field_low_bit (const field_t *f) {
static __inline__ mask_t
__attribute__((unused,always_inline))
field_make_nonzero (field_t *f) {
field_make_nonzero (struct field_t *f) {
mask_t z = field_is_zero(f);
field_addw( f, -z );
return z;
}
/* Multiply by signed curve constant */
static __inline__ void
field_mulw_scc (
struct field_t* __restrict__ out,
const struct field_t *a,
int64_t scc
) {
if (scc >= 0) {
field_mulw(out, a, scc);
} else {
field_mulw(out, a, -scc);
field_neg_RAW(out,out);
field_bias(out,2);
}
}
/* Multiply by signed curve constant and weak reduce if biased */
static __inline__ void
field_mulw_scc_wr (
struct field_t* __restrict__ out,
const struct field_t *a,
int64_t scc
) {
field_mulw_scc(out, a, scc);
if (scc < 0)
field_weak_reduce(out);
}
static __inline__ void
field_subx_RAW (
struct field_t *d,
const struct field_t *a,
const struct field_t *b
) {
field_sub_RAW ( d, a, b );
field_bias( d, 2 );
IF32( field_weak_reduce ( d ) );
}
static __inline__ void
field_sub (
struct field_t *d,
const struct field_t *a,
const struct field_t *b
) {
field_sub_RAW ( d, a, b );
field_bias( d, 2 );
field_weak_reduce ( d );
}
static __inline__ void
field_add (
struct field_t *d,
const struct field_t *a,
const struct field_t *b
) {
field_add_RAW ( d, a, b );
field_weak_reduce ( d );
}
static __inline__ void
field_subw (
struct field_t *d,
word_t c
) {
field_subw_RAW ( d, c );
field_bias( d, 1 );
field_weak_reduce ( d );
}
static __inline__ void
field_negx (
struct field_t *d,
const struct field_t *a
) {
field_neg_RAW ( d, a );
field_bias( d, 2 );
field_weak_reduce ( d );
}
/**
* Negate a in place if doNegate.
*/
static inline void
__attribute__((unused,always_inline))
field_cond_neg (
field_t *a,
mask_t doNegate
) {
struct field_t negated;
field_negx(&negated, a);
constant_time_select(a, &negated, a, sizeof(negated), doNegate);
}
/** Require the warning annotation on raw routines */
#define ANALYZE_THIS_ROUTINE_CAREFULLY const int ANNOTATE___ANALYZE_THIS_ROUTINE_CAREFULLY = 0;
#define MUST_BE_CAREFUL (void) ANNOTATE___ANALYZE_THIS_ROUTINE_CAREFULLY
#define field_add_nr(a,b,c) { MUST_BE_CAREFUL; field_add_RAW(a,b,c); }
#define field_sub_nr(a,b,c) { MUST_BE_CAREFUL; field_sub_RAW(a,b,c); }
#define field_subx_nr(a,b,c) { MUST_BE_CAREFUL; field_subx_RAW(a,b,c); }
#endif // __FIELD_H__
......@@ -24,21 +24,21 @@ p448_set_ui (
) __attribute__((unused,always_inline));
static __inline__ void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) __attribute__((unused,always_inline));
......@@ -130,7 +130,7 @@ p448_set_ui (
}
void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -148,7 +148,7 @@ p448_add (
}
void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -166,7 +166,7 @@ p448_sub (
}
void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) {
......
......@@ -24,21 +24,21 @@ p448_set_ui (
) __attribute__((unused,always_inline));
static __inline__ void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) __attribute__((unused,always_inline));
......@@ -130,7 +130,7 @@ p448_set_ui (
}
void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -148,7 +148,7 @@ p448_add (
}
void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -166,7 +166,7 @@ p448_sub (
}
void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) {
......
......@@ -24,21 +24,21 @@ p448_set_ui (
) __attribute__((unused,always_inline));
static __inline__ void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) __attribute__((unused,always_inline));
......@@ -130,7 +130,7 @@ p448_set_ui (
}
void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -148,7 +148,7 @@ p448_add (
}
void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -166,7 +166,7 @@ p448_sub (
}
void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) {
......
......@@ -27,21 +27,21 @@ p448_set_ui (
) __attribute__((unused,always_inline));
static __inline__ void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) __attribute__((unused,always_inline));
......@@ -133,7 +133,7 @@ p448_set_ui (
}
void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -145,7 +145,7 @@ p448_add (
}
void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -163,7 +163,7 @@ p448_sub (
}
void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) {
......
......@@ -25,21 +25,21 @@ p448_set_ui (
) __attribute__((unused));
static __inline__ void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused));
static __inline__ void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused));
static __inline__ void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) __attribute__((unused));
......@@ -136,7 +136,7 @@ p448_set_ui (
}
void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -149,7 +149,7 @@ p448_add (
}
void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -163,7 +163,7 @@ p448_sub (
}
void
p448_neg (
p448_neg_RAW (
struct p448_t *out,
const p448_t *a
) {
......
......@@ -24,21 +24,21 @@ p448_set_ui (
) __attribute__((unused,always_inline));
static __inline__ void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p448_neg (
p448_neg_RAW (
p448_t *out,
const p448_t *a
) __attribute__((unused,always_inline));
......@@ -129,7 +129,7 @@ p448_set_ui (
}
void
p448_add (
p448_add_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -147,7 +147,7 @@ p448_add (
}
void
p448_sub (
p448_sub_RAW (
p448_t *out,
const p448_t *a,
const p448_t *b
......@@ -165,7 +165,7 @@ p448_sub (
}
void
p448_neg (
p448_neg_RAW (
struct p448_t *out,
const p448_t *a
) {
......
......@@ -9,23 +9,22 @@
#ifndef __F_FIELD_H__
#define __F_FIELD_H__ 1
#include <string.h>
#include "constant_time.h"
#include <string.h>
#include "p448.h"
#define FIELD_BITS 448
#define field_t p448_t
#define field_mul p448_mul
#define field_sqr p448_sqr
#define field_add p448_add
#define field_sub p448_sub
#define field_add_RAW p448_add_RAW
#define field_sub_RAW p448_sub_RAW
#define field_mulw p448_mulw
#define field_addw p448_addw
#define field_subw p448_subw
#define field_neg p448_neg
#define field_subw_RAW p448_subw
#define field_neg_RAW p448_neg_RAW
#define field_set_ui p448_set_ui
#define field_bias p448_bias
#define field_cond_neg p448_cond_neg
#define field_inverse p448_inverse
#define field_eq p448_eq
#define field_isr p448_isr
......
......@@ -24,21 +24,21 @@ p480_set_ui (
) __attribute__((unused,always_inline));
static __inline__ void
p480_add (
p480_add_RAW (
p480_t *out,
const p480_t *a,
const p480_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p480_sub (
p480_sub_RAW (
p480_t *out,
const p480_t *a,
const p480_t *b
) __attribute__((unused,always_inline));
static __inline__ void
p480_neg (
p480_neg_RAW (
p480_t *out,
const p480_t *a
) __attribute__((unused,always_inline));
......@@ -129,7 +129,7 @@ p480_set_ui (
}
void
p480_add (
p480_add_RAW (
p480_t *out,
const p480_t *a,
const p480_t *b
......@@ -147,7 +147,7 @@ p480_add (
}
void
p480_sub (
p480_sub_RAW (
p480_t *out,
const p480_t *a,
const p480_t *b
......@@ -165,7 +165,7 @@ p480_sub (
}
void
p480_neg (
p480_neg_RAW (
struct p480_t *out,
const p480_t *a
) {
......
......@@ -9,23 +9,22 @@
#ifndef __F_FIELD_H__
#define __F_FIELD_H__ 1
#include <string.h>
#include "constant_time.h"
#include <string.h>
#include "p480.h"
#define FIELD_BITS 480
#define field_t p480_t
#define field_mul p480_mul
#define field_sqr p480_sqr
#define field_add p480_add
#define field_sub p480_sub
#define field_add_RAW p480_add_RAW
#define field_sub_RAW p480_sub_RAW
#define field_mulw p480_mulw
#define field_addw p480_addw
#define field_subw p480_subw
#define field_neg p480_neg
#define field_subw_RAW p480_subw
#define field_neg_RAW p480_neg_RAW
#define field_set_ui p480_set_ui
#define field_bias p480_bias
#define field_cond_neg p480_cond_neg
#define field_inverse p480_inverse
#define field_eq p480_eq
#define field_isr p480_isr
......
......@@ -25,21 +25,21 @@ p521_set_ui (
) __attribute__((unused));
static __inline__ void
p521_add (
p521_add_RAW (
p521_t *out,
const p521_t *a,
const p521_t *b
) __attribute__((unused));
static __inline__ void
p521_sub (
p521_sub_RAW (
p521_t *out,
const p521_t *a,
const p521_t *b
) __attribute__((unused));
static __inline__ void
p521_neg (
p521_neg_RAW (
p521_t *out,
const p521_t *a
) __attribute__((unused));
......@@ -136,7 +136,7 @@ p521_set_ui (
}
void
p521_add (
p521_add_RAW (
p521_t *out,
const p521_t *a,
const p521_t *b
......@@ -149,7 +149,7 @@ p521_add (
}
void
p521_sub (
p521_sub_RAW (
p521_t *out,
const p521_t *a,
const p521_t *b
......@@ -163,7 +163,7 @@ p521_sub (
}
void
p521_neg (
p521_neg_RAW (
struct p521_t *out,
const p521_t *a
) {
......
......@@ -29,21 +29,21 @@ p521_set_ui (
) __attribute__((unused));
static __inline__ void
p521_add (
p521_add_RAW (
p521_t *out,
const p521_t *a,
const p521_t *b
) __attribute__((unused));
static __inline__ void
p521_sub (
p521_sub_RAW (
p521_t *out,
const p521_t *a,
const p521_t *b
) __attribute__((unused));
static __inline__ void
p521_neg (
p521_neg_RAW (
p521_t *out,
const p521_t *a
) __attribute__((unused));
......@@ -147,7 +147,7 @@ p521_set_ui (
}
void
p521_add (
p521_add_RAW (
p521_t *out,
const p521_t *a,
const p521_t *b
......@@ -159,7 +159,7 @@ p521_add (
}
void
p521_sub (
p521_sub_RAW (
p521_t *out,
const p521_t *a,
const p521_t *b
......@@ -171,7 +171,7 @@ p521_sub (
}
void
p521_neg (
p521_neg_RAW (
struct p521_t *out,
const p521_t *a
) {
......
......@@ -17,15 +17,14 @@
#define field_t p521_t
#define field_mul p521_mul
#define field_sqr p521_sqr
#define field_add p521_add
#define field_sub p521_sub
#define field_add_RAW p521_add_RAW
#define field_sub_RAW p521_sub_RAW
#define field_mulw p521_mulw
#define field_addw p521_addw
#define field_subw p521_subw
#define field_neg p521_neg
#define field_subw_RAW p521_subw
#define field_neg_RAW p521_neg_RAW
#define field_set_ui p521_set_ui
#define field_bias p521_bias
#define field_cond_neg p521_cond_neg
#define field_inverse p521_inverse
#define field_eq p521_eq
#define field_isr p521_isr
......
......@@ -178,7 +178,6 @@ int main(int argc, char **argv) {
field_mul(&c,&b,&a);
field_sqr(&b,&c);
field_subw(&b,1);
field_bias(&b,1);
if (!field_is_zero(&b)) {
printf("ISR validation failure!\n");
field_print("a", &a);
......@@ -233,7 +232,6 @@ int main(int argc, char **argv) {
convert_affine_to_extensible(&exta,&affine);
serialize_extensible(&b, &exta);
field_sub(&c,&b,&a);
field_bias(&c,2);
if (!field_is_zero(&c)) {
printf("Reserialize validation failure!\n");
field_print("a", &a);
......@@ -644,7 +642,6 @@ int main(int argc, char **argv) {
ignore_result(montgomery_ladder(&b,&a,&z,WORD_BITS,0));
field_sub(&d,&b,&c);
field_bias(&d,2);
if (!field_is_zero(&d)) {
printf("Odd ladder validation failure %d!\n", ++failures);
field_print("a", &a);
......@@ -670,7 +667,6 @@ int main(int argc, char **argv) {
untwist_and_double_and_serialize(&c, &ext);
field_sub(&d,&b,&c);
field_bias(&d,2);
if (good && !field_is_zero(&d)){
printf("Iso+serial validation failure %d!\n", ++failures);
......@@ -726,7 +722,6 @@ int main(int argc, char **argv) {
serialize_extensible(&c, &exta);
field_sub(&d,&b,&c);
field_bias(&d,2);
if (!field_is_zero(&d)){
printf("PreWNAF combo validation failure %d!\n", ++failures);
......
......@@ -83,7 +83,7 @@ static mask_t field_assert_eq_gmp(
return MASK_SUCCESS;
}
static mask_t test_add_sub (
static mask_t test_add_sub_RAW (
const mpz_t x,
const mpz_t y,
word_t word
......@@ -95,11 +95,11 @@ static mask_t test_add_sub (