Permalink
Browse files

supercop-20120329

  • Loading branch information...
1 parent a0b6b25 commit 4b72f5376212258070afe0b51010f2b06c37c755 Daniel J. Bernstein committed with Mar 29, 2012
Showing with 3,520 additions and 1,918 deletions.
  1. +1 −0 cpucycles/do
  2. +1 −1 cpucycles/mips.c
  3. +27 −0 cpucycles/perfevent.c
  4. +27 −0 cpucycles/perfevent.h
  5. +0 −5 crypto_dh/curve2251/relic/amd64-avx/Makefile
  6. +9 −57 crypto_dh/curve2251/relic/amd64-avx/curve2251.c
  7. +1 −1 crypto_dh/curve2251/relic/amd64-avx/implementors
  8. +0 −4 crypto_dh/curve2251/relic/amd64-avx/relic_bn.c
  9. +0 −210 crypto_dh/curve2251/relic/amd64-avx/src/bn/relic_bn_div.c
  10. +0 −47 crypto_dh/curve2251/relic/amd64-avx/src/bn/relic_bn_mul.c
  11. +1 −155 crypto_dh/curve2251/relic/amd64-avx/src/bn/relic_bn_util.c
  12. +53 −8 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_add_low.c
  13. +19 −6 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_cmp_low.c
  14. +0 −46 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_div_low.c
  15. +0 −58 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_mul_low.c
  16. +64 −11 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_shift_low.c
  17. +27 −13 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_fb_cmp_low.c
  18. +74 −15 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_fb_shift_low.c
  19. +1 −6 crypto_dh/curve2251/relic/amd64-clmul/Makefile
  20. +9 −57 crypto_dh/curve2251/relic/amd64-clmul/curve2251.c
  21. +1 −1 crypto_dh/curve2251/relic/amd64-clmul/implementors
  22. +0 −4 crypto_dh/curve2251/relic/amd64-clmul/relic_bn.c
  23. +0 −210 crypto_dh/curve2251/relic/amd64-clmul/src/bn/relic_bn_div.c
  24. +0 −47 crypto_dh/curve2251/relic/amd64-clmul/src/bn/relic_bn_mul.c
  25. +1 −155 crypto_dh/curve2251/relic/amd64-clmul/src/bn/relic_bn_util.c
  26. +53 −8 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_add_low.c
  27. +19 −6 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_cmp_low.c
  28. +0 −46 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_div_low.c
  29. +0 −58 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_mul_low.c
  30. +64 −11 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_shift_low.c
  31. +27 −13 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_fb_cmp_low.c
  32. +74 −15 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_fb_shift_low.c
  33. +0 −4 crypto_dh/curve2251/relic/amd64-ssse3/1
  34. +1 −6 crypto_dh/curve2251/relic/amd64-ssse3/Makefile
  35. +9 −57 crypto_dh/curve2251/relic/amd64-ssse3/curve2251.c
  36. +1 −1 crypto_dh/curve2251/relic/amd64-ssse3/implementors
  37. +0 −4 crypto_dh/curve2251/relic/amd64-ssse3/relic_bn.c
  38. +0 −210 crypto_dh/curve2251/relic/amd64-ssse3/src/bn/relic_bn_div.c
  39. +0 −47 crypto_dh/curve2251/relic/amd64-ssse3/src/bn/relic_bn_mul.c
  40. +0 −154 crypto_dh/curve2251/relic/amd64-ssse3/src/bn/relic_bn_util.c
  41. +53 −8 crypto_dh/curve2251/relic/amd64-ssse3/src/low/relic_bn_add_low.c
  42. +19 −6 crypto_dh/curve2251/relic/amd64-ssse3/src/low/relic_bn_cmp_low.c
  43. +0 −46 crypto_dh/curve2251/relic/amd64-ssse3/src/low/relic_bn_div_low.c
  44. +0 −58 crypto_dh/curve2251/relic/amd64-ssse3/src/low/relic_bn_mul_low.c
  45. +64 −11 crypto_dh/curve2251/relic/amd64-ssse3/src/low/relic_bn_shift_low.c
  46. +27 −13 crypto_dh/curve2251/relic/amd64-ssse3/src/low/relic_fb_cmp_low.c
  47. +74 −15 crypto_dh/curve2251/relic/amd64-ssse3/src/low/relic_fb_shift_low.c
  48. +1 −0 crypto_hash/bblake512/description
  49. +1 −0 crypto_hash/bblake512/designers
  50. +3 −0 crypto_hash/bblake512/xop/api.h
  51. +2 −0 crypto_hash/bblake512/xop/architectures
  52. +8 −0 crypto_hash/bblake512/xop/config.h
  53. +504 −0 crypto_hash/bblake512/xop/hash.c
  54. +2 −0 crypto_hash/bblake512/xop/implementors
  55. +2,153 −0 crypto_hash/bblake512/xop/rounds.h
  56. +2 −0 crypto_hash/mgrostl256/designers
  57. +12 −1 data-do
  58. +3 −1 measure-anything.c
  59. +12 −0 okcompilers/c
  60. +12 −0 okcompilers/cpp
  61. +3 −1 try-anything.c
  62. +1 −1 version
View
1 cpucycles/do
@@ -68,6 +68,7 @@ okabi | (
echo sgi
echo cortex
echo dev4ns
+ echo perfevent
echo monotoniccpuinfo
echo monotonic
echo gettimeofday
View
2 cpucycles/mips.c
@@ -18,7 +18,7 @@ static void readticks(unsigned int *result)
{
struct timeval t;
unsigned int cc;
- asm volatile(".byte 59; .byte 16; .byte 2; .byte 124; move %0,$2" : "=r"(cc) : : "$2");
+ asm volatile(".long 2080510011; move %0,$2" : "=r"(cc) : : "$2");
gettimeofday(&t,(struct timezone *) 0);
result[0] = cc;
result[1] = t.tv_usec;
View
27 cpucycles/perfevent.c
@@ -0,0 +1,27 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/syscall.h>
+#include <linux/perf_event.h>
+#include "osfreq.c"
+
+static int fddev = -1;
+
+long long cpucycles_perfevent(void)
+{
+ long long result;
+
+ if (fddev == -1) {
+ static struct perf_event_attr attr;
+ attr.type = PERF_TYPE_HARDWARE;
+ attr.config = PERF_COUNT_HW_CPU_CYCLES;
+ fddev = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
+ }
+
+ if (read(fddev,&result,sizeof result) < sizeof result) return 0;
+ return result;
+}
+
+long long cpucycles_perfevent_persecond(void)
+{
+ return osfreq();
+}
View
27 cpucycles/perfevent.h
@@ -0,0 +1,27 @@
+/*
+cpucycles perfevent.h version 20120327
+D. J. Bernstein
+Public domain.
+*/
+
+#ifndef CPUCYCLES_perfevent_h
+#define CPUCYCLES_perfevent_h
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern long long cpucycles_perfevent(void);
+extern long long cpucycles_perfevent_persecond(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifndef cpucycles_implementation
+#define cpucycles_implementation "perfevent"
+#define cpucycles cpucycles_perfevent
+#define cpucycles_persecond cpucycles_perfevent_persecond
+#endif
+
+#endif
View
5 crypto_dh/curve2251/relic/amd64-avx/Makefile
@@ -2,13 +2,8 @@ SRC:=$(wildcard src/eb/*.c) $(wildcard src/fb/*.c) $(wildcard src/bn/*.c) $(wild
ASM:=$(wildcard src/low/*.s)
OBJ:=$(SRC:.c=.o) $(ASM:.s=.o) curve2251.o
-ifndef GMP
-GMP:=../../lib/amd64
-endif
-
CC:=gcc
CFLAGS:=-O3 -std=c99 -I include/low -I target/include -I . -I$(GMP) -funroll-loops -fomit-frame-pointer -msse4 -mpclmul -DMAIN -march=native
-LIB:=-L$(GMP) -L target/lib -lgmp
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
View
66 crypto_dh/curve2251/relic/amd64-avx/curve2251.c
@@ -23,7 +23,6 @@ int crypto_dh_keypair(unsigned char *pk, unsigned char *sk) {
bn_new(k);
bn_new(n);
-
n->used = 4;
n->dp[0] = 0x00BEF3B9ABB767E1;
n->dp[1] = 0xF3E3AA131A2E1A82;
@@ -34,22 +33,18 @@ int crypto_dh_keypair(unsigned char *pk, unsigned char *sk) {
fb_zero(a);
fb_set_dig(b, 0x2387);
- //bench_reset();
- //bench_before();
eb_curve_set_ordin(a, b, &(gen_tab[1]), n, k);
- //eb_param_set(EBACS_B251);
#ifdef MAIN
rand_bytes(sk, SK_BYTES);
#else
randombytes(sk, SK_BYTES);
#endif
bn_read_bin(k, sk, SK_BYTES);
- bn_div_rem(NULL, k, k, n);
+ if (bn_cmp(k, n) != CMP_LT) {
+ bn_sub(k, k, n);
+ }
- //bench_after();
- //bench_compute(1);
- //bench_print();
eb_mul_fix(p, (eb_t *)gen_tab, k);
bn_read_raw(k, p->x, FB_DIGS);
@@ -109,65 +104,22 @@ int main(int argc, char *argv[]) {
}
printf("\n");
-#if 0
- eb_t *tab = eb_curve_get_tab();
- printf("eb_st tab[%d] = {\n", EB_TABLE);
- for (int i = 0; i < EB_TABLE; i++) {
- printf("\t{\n");
-
- printf("\t\t{");
- for (int k = 0; k < FB_DIGS; k++) {
- util_print("0x%.*lX, ", (int)(2 * sizeof(dig_t)), (unsigned long int)tab[i]->x[k]);
- }
- printf("},\n");
-
- printf("\t\t{");
- for (int k = 0; k < FB_DIGS; k++) {
- util_print("0x%.*lX, ", (int)(2 * sizeof(dig_t)), (unsigned long int)tab[i]->y[k]);
- }
- printf("},\n");
-
- printf("\t\t{");
- for (int k = 0; k < FB_DIGS; k++) {
- util_print("0x%.*lX, ", (int)(2 * sizeof(dig_t)), (unsigned long int)tab[i]->z[k]);
- }
- printf("},\n");
-
- printf("\t\t1\n\t},\n");
- }
- printf("};\n");
-
- int chain;
- fb_poly_get_chain(&chain);
- printf("fb_st inv_tab[%d][FB_TABLE] = {\n", chain);
- for (int i = 0; i < chain; i++) {
- printf("\t{\n");
- fb_st *tab = fb_poly_tab_sqr(i);
- for (int j = 0; j < FB_TABLE; j++) {
- printf("\t\t{");
- for (int k = 0; k < FB_DIGS; k++) {
- util_print("0x%.*lX, ", (int)(2 * sizeof(dig_t)), (unsigned long int)tab[j][k]);
- }
- printf("},\n");
- }
- printf("\t},\n");
- }
- printf("};\n");
-
-#endif
-
eb_curve_get_ord(n);
eb_curve_get_gen(p);
BENCH_BEGIN("eb_mul_fix") {
bn_rand(k, BN_POS, bn_bits(n));
- bn_div_rem(NULL, k, k, n);
+ if (bn_cmp(k, n) != CMP_LT) {
+ bn_sub(k, k, n);
+ }
BENCH_ADD(eb_mul_fix(p, (eb_t *)gen_tab, k));
} BENCH_END;
BENCH_BEGIN("eb_mul") {
bn_rand(k, BN_POS, bn_bits(n));
- bn_div_rem(NULL, k, k, n);
+ if (bn_cmp(k, n) != CMP_LT) {
+ bn_sub(k, k, n);
+ }
BENCH_ADD(eb_mul(q, p, k));
} BENCH_END;
View
2 crypto_dh/curve2251/relic/amd64-avx/implementors
@@ -1,4 +1,4 @@
-Diego de Freitas Aranha, Institute of Computing, University of Campinas, Brazil
+Diego de Freitas Aranha, Department of Computer Science, University of Brasília, Brazil
Jonathan Taverne, Université de Lyon, Université Lyon1, ISFA, France
Armando Faz-Hernández, Computer Science Department, CINVESTAV-IPN, Mexico
Francisco Rodríguez-Henríquez, Computer Science Department, CINVESTAV-IPN, Mexico
View
4 crypto_dh/curve2251/relic/amd64-avx/relic_bn.c
@@ -1,12 +1,8 @@
#include "src/bn/relic_bn_mem.c"
#include "src/bn/relic_bn_add.c"
#include "src/bn/relic_bn_cmp.c"
-#include "src/bn/relic_bn_div.c"
-#include "src/bn/relic_bn_mul.c"
#include "src/bn/relic_bn_shift.c"
#include "src/bn/relic_bn_util.c"
#include "src/low/relic_bn_add_low.c"
-#include "src/low/relic_bn_div_low.c"
#include "src/low/relic_bn_cmp_low.c"
-#include "src/low/relic_bn_mul_low.c"
#include "src/low/relic_bn_shift_low.c"
View
210 crypto_dh/curve2251/relic/amd64-avx/src/bn/relic_bn_div.c
@@ -1,210 +0,0 @@
-/*
- * RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
- *
- * This file is part of RELIC. RELIC is legal property of its developers,
- * whose names are not listed here. Please refer to the COPYRIGHT file
- * for contact information.
- *
- * RELIC is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * RELIC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * @file
- *
- * Implementation of the multiple precision division functions.
- *
- * @version $Id: relic_bn_div.c,v 1.1 2011/08/13 04:38:37 diego Exp $
- * @ingroup bn
- */
-
-#include "relic.h"
-
-/*============================================================================*/
-/* Private definitions */
-/*============================================================================*/
-
-/**
- * Divides two multiple precision integers, computing the quotient and the
- * remainder.
- *
- * @param[out] c - the quotient.
- * @param[out] d - the remainder.
- * @param[in] a - the dividend.
- * @param[in] b - the the divisor.
- */
-static void bn_div_imp(bn_t c, bn_t d, bn_t a, bn_t b) {
- bn_t q, x, y, r;
- int sign;
-
- bn_null(q);
- bn_null(x);
- bn_null(y);
- bn_null(r);
-
- /* If a < b, we're done. */
- if (bn_cmp_abs(a, b) == CMP_LT) {
- if (d != NULL) {
- bn_copy(d, a);
- }
- if (c != NULL) {
- bn_zero(c);
- }
- return;
- }
-
- TRY {
- bn_new(x);
- bn_new(y);
- bn_new_size(q, a->used + 1);
- bn_new(r);
- bn_zero(q);
- bn_zero(r);
- bn_abs(x, a);
- bn_abs(y, b);
-
- /* Find the sign. */
- sign = (a->sign == b->sign ? BN_POS : BN_NEG);
-
- bn_divn_low(q->dp, r->dp, x->dp, a->used, y->dp, b->used);
-
- /* We have the quotient in q and the remainder in r. */
- if (c != NULL) {
- q->used = a->used - b->used + 1;
- q->sign = sign;
- bn_trim(q);
- bn_copy(c, q);
- }
-
- if (d != NULL) {
- r->used = b->used;
- r->sign = a->sign;
- bn_trim(r);
- bn_copy(d, r);
- }
- }
- CATCH_ANY {
- THROW(ERR_CAUGHT);
- }
- FINALLY {
- bn_free(r);
- bn_free(q);
- bn_free(x);
- bn_free(y);
- }
-}
-
-/*============================================================================*/
-/* Public definitions */
-/*============================================================================*/
-
-void bn_div(bn_t c, bn_t a, bn_t b) {
- if (bn_is_zero(b)) {
- THROW(ERR_INVALID);
- }
- bn_div_imp(c, NULL, a, b);
-}
-
-void bn_div_rem(bn_t c, bn_t d, bn_t a, bn_t b) {
- if (bn_is_zero(b)) {
- THROW(ERR_INVALID);
- }
- bn_div_imp(c, d, a, b);
-}
-
-void bn_div_dig(bn_t c, bn_t a, dig_t b) {
- bn_t q;
- dig_t r;
-
- bn_null(q);
-
- if (b == 0) {
- THROW(ERR_INVALID);
- }
-
- if (b == 1 || bn_is_zero(a) == 1) {
- if (c != NULL) {
- bn_copy(c, a);
- }
- return;
- }
-
- TRY {
- bn_new(q);
- int size = a->used;
- dig_t *ap = a->dp;
-
- bn_div1_low(q->dp, &r, ap, size, b);
-
- if (c != NULL) {
- q->used = a->used;
- q->sign = a->sign;
- bn_trim(q);
- bn_copy(c, q);
- }
- }
- CATCH_ANY {
- THROW(ERR_CAUGHT);
- }
- FINALLY {
- bn_free(q);
- }
-}
-
-void bn_div_rem_dig(bn_t c, dig_t *d, bn_t a, dig_t b) {
- bn_t q;
- dig_t r;
-
- bn_null(q);
-
- if (b == 0) {
- THROW(ERR_INVALID);
- }
-
- if (b == 1 || bn_is_zero(a) == 1) {
- if (d != NULL) {
- *d = 0;
- }
- if (c != NULL) {
- bn_copy(c, a);
- }
- return;
- }
-
- TRY {
- bn_new(q);
- int size = a->used;
- dig_t *ap = a->dp;
-
- bn_div1_low(q->dp, &r, ap, size, b);
-
- if (c != NULL) {
- q->used = a->used;
- q->sign = a->sign;
- bn_trim(q);
- bn_copy(c, q);
- }
-
- if (d != NULL) {
- *d = r;
- }
- }
- CATCH_ANY {
- THROW(ERR_CAUGHT);
- }
- FINALLY {
- bn_free(q);
- }
-}
-
View
47 crypto_dh/curve2251/relic/amd64-avx/src/bn/relic_bn_mul.c
@@ -1,47 +0,0 @@
-/*
- * RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
- *
- * This file is part of RELIC. RELIC is legal property of its developers,
- * whose names are not listed here. Please refer to the COPYRIGHT file
- * for contact information.
- *
- * RELIC is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * RELIC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * @file
- *
- * Implementation of the multiple precision multiplication functions.
- *
- * @version $Id: relic_bn_mul.c,v 1.1 2011/08/13 04:38:37 diego Exp $
- * @ingroup bn
- */
-
-#include "relic.h"
-
-/*============================================================================*/
-/* Public definitions */
-/*============================================================================*/
-
-void bn_mul_dig(bn_t c, bn_t a, dig_t b) {
- dig_t carry;
-
- bn_grow(c, a->used + 1);
- carry = bn_mul1_low(c->dp, a->dp, b, a->used);
- c->dp[a->used] = carry;
- c->used = a->used + 1;
- c->sign = a->sign;
- bn_trim(c);
-}
View
156 crypto_dh/curve2251/relic/amd64-avx/src/bn/relic_bn_util.c
@@ -25,7 +25,7 @@
*
* Implementation of the multiple precision utilities.
*
- * @version $Id: relic_bn_util.c,v 1.1 2011/08/13 04:38:37 diego Exp $
+ * @version $Id: relic_bn_util.c,v 1.1 2011/08/12 19:27:16 diego Exp $
* @ingroup bn
*/
@@ -224,160 +224,6 @@ void bn_print(bn_t a) {
}
}
-void bn_size_str(int *size, bn_t a, int radix) {
- int digits;
- bn_t t;
-
- bn_null(t);
-
- *size = 0;
-
- /* Binary case. */
- if (radix == 2) {
- *size = bn_bits(a) + (a->sign == BN_NEG ? 1 : 0) + 1;
- return;
- }
-
- /* Check the radix. */
- if (radix < 2 || radix > 64) {
- THROW(ERR_INVALID);
- }
-
- if (bn_is_zero(a)) {
- *size = 2;
- return;
- }
-
- digits = 0;
-
- if (a->sign == BN_NEG) {
- digits++;
- }
-
- TRY {
- bn_new(t);
- bn_copy(t, a);
-
- t->sign = BN_POS;
-
- while (!bn_is_zero(t)) {
- bn_div_dig(t, t, (dig_t)radix);
- digits++;
- }
- *size = digits + 1;
-
- } CATCH_ANY {
- THROW(ERR_CAUGHT);
- } FINALLY {
- bn_free(t);
- }
-}
-
-void bn_read_str(bn_t a, const char *str, int len, int radix) {
- int sign, i, j;
- char c;
-
- bn_zero(a);
-
- if (radix < 2 || radix > 64) {
- THROW(ERR_INVALID)
- }
-
- j = 0;
- if (str[0] == '-') {
- j++;
- sign = BN_NEG;
- } else {
- sign = BN_POS;
- }
-
- while (str[j] && j < len) {
- c = (char)((radix < 36) ? TOUPPER(str[j]) : str[j]);
- for (i = 0; i < 64; i++) {
- if (c == util_conv_char(i)) {
- break;
- }
- }
-
- if (i < radix) {
- bn_mul_dig(a, a, (dig_t)radix);
- bn_add_dig(a, a, (dig_t)i);
- } else {
- break;
- }
- j++;
- }
-
- a->sign = sign;
-}
-
-void bn_write_str(char *str, int len, bn_t a, int radix) {
- bn_t t;
- dig_t d;
- int digits, l, i, j;
- char c;
-
- bn_null(t);
-
- bn_size_str(&l, a, radix);
- if (len < l) {
- THROW(ERR_NO_BUFFER);
- }
-
- if (radix < 2 || radix > 64) {
- THROW(ERR_INVALID)
- }
-
- if (bn_is_zero(a) == 1) {
- *str++ = '0';
- *str = '\0';
- return;
- }
-
- TRY {
- bn_new(t);
- bn_copy(t, a);
-
- j = 0;
- if (t->sign == BN_NEG) {
- str[j] = '-';
- j++;
- t->sign = BN_POS;
- }
-
- digits = 0;
- while (!bn_is_zero(t)) {
- bn_div_rem_dig(t, &d, t, (dig_t)radix);
- str[j] = util_conv_char(d);
- digits++;
- j++;
- }
-
- /* Reverse the digits of the string. */
- i = 0;
- if (str[0] == '-') {
- i = 1;
- }
- j = l - 2;
- while (i < j) {
- c = str[i];
- str[i] = str[j];
- str[j] = c;
- ++i;
- --j;
- }
-
- str[l - 1] = '\0';
-
- }
- CATCH_ANY {
- THROW(ERR_CAUGHT);
- }
- FINALLY {
- bn_free(t);
- }
-}
-
void bn_size_bin(int *size, bn_t a) {
dig_t d;
int digits;
View
61 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_add_low.c
@@ -1,6 +1,6 @@
/*
* RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
+ * Copyright (C) 2007-2012 RELIC Authors
*
* This file is part of RELIC. RELIC is legal property of its developers,
* whose names are not listed here. Please refer to the COPYRIGHT file
@@ -26,30 +26,75 @@
* Implementation of the low-level multiple precision addition and subtraction
* functions.
*
- * @version $Id: relic_bn_add_low.c,v 1.1 2011/08/13 04:38:37 diego Exp $
+ * @version $Id: relic_bn_add_low.c 677 2011-03-05 22:19:43Z dfaranha $
* @ingroup bn
*/
-#include <gmp.h>
-
#include "relic.h"
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
dig_t bn_add1_low(dig_t *c, dig_t *a, dig_t digit, int size) {
- return mpn_add_1(c, a, size, digit);
+ int i;
+ register dig_t carry, r0;
+
+ carry = digit;
+ for (i = 0; i < size && carry; i++, a++, c++) {
+ r0 = (*a) + carry;
+ carry = (r0 < carry);
+ (*c) = r0;
+ }
+ for (; i < size; i++, a++, c++) {
+ (*c) = (*a);
+ }
+ return carry;
}
dig_t bn_addn_low(dig_t *c, dig_t *a, dig_t *b, int size) {
- return mpn_add_n(c, a, b, size);
+ int i;
+ register dig_t carry, c0, c1, r0, r1;
+
+ carry = 0;
+ for (i = 0; i < size; i++, a++, b++, c++) {
+ r0 = (*a) + (*b);
+ c0 = (r0 < (*a));
+ r1 = r0 + carry;
+ c1 = (r1 < r0);
+ carry = c0 | c1;
+ (*c) = r1;
+ }
+ return carry;
}
dig_t bn_sub1_low(dig_t *c, dig_t *a, dig_t digit, int size) {
- return mpn_sub_1(c, a, size, digit);
+ int i;
+ dig_t carry, r0;
+
+ carry = digit;
+ for (i = 0; i < size && carry; i++, c++, a++) {
+ r0 = (*a) - carry;
+ carry = (r0 > (*a));
+ (*c) = r0;
+ }
+ for (; i < size; i++, a++, c++) {
+ (*c) = (*a);
+ }
+ return carry;
}
dig_t bn_subn_low(dig_t *c, dig_t *a, dig_t *b, int size) {
- return mpn_sub_n(c, a, b, size);
+ int i;
+ dig_t carry, r0, diff;
+
+ /* Zero the carry. */
+ carry = 0;
+ for (i = 0; i < size; i++, a++, b++, c++) {
+ diff = (*a) - (*b);
+ r0 = diff - carry;
+ carry = ((*a) < (*b)) || (carry && !diff);
+ (*c) = r0;
+ }
+ return carry;
}
View
25 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_cmp_low.c
@@ -1,6 +1,6 @@
/*
* RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
+ * Copyright (C) 2007-2012 RELIC Authors
*
* This file is part of RELIC. RELIC is legal property of its developers,
* whose names are not listed here. Please refer to the COPYRIGHT file
@@ -25,22 +25,35 @@
*
* Implementation of the low-level multiple precision comparison functions.
*
- * @version $Id: relic_bn_cmp_low.c,v 1.1 2011/08/13 04:38:37 diego Exp $
+ * @version $Id: relic_bn_cmp_low.c 681 2011-03-10 07:18:24Z dfaranha $
* @ingroup bn
*/
-#include <gmp.h>
-
#include "relic.h"
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
int bn_cmp1_low(dig_t a, dig_t b) {
- return mpn_cmp(&a, &b, 1);
+ if (a > b)
+ return CMP_GT;
+ if (a < b)
+ return CMP_LT;
+ return CMP_EQ;
}
int bn_cmpn_low(dig_t *a, dig_t *b, int size) {
- return mpn_cmp(a, b, size);
+ int i, r;
+
+ a += (size - 1);
+ b += (size - 1);
+
+ r = CMP_EQ;
+ for (i = 0; i < size; i++, --a, --b) {
+ if (*a != *b && r == CMP_EQ) {
+ r = (*a > *b ? CMP_GT : CMP_LT);
+ }
+ }
+ return r;
}
View
46 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_div_low.c
@@ -1,46 +0,0 @@
-/*
- * RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
- *
- * This file is part of RELIC. RELIC is legal property of its developers,
- * whose names are not listed here. Please refer to the COPYRIGHT file
- * for contact information.
- *
- * RELIC is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * RELIC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * @file
- *
- * Implementation of the low-level multiple precision division functions.
- *
- * @version $Id: relic_bn_div_low.c,v 1.1 2011/08/13 04:38:38 diego Exp $
- * @ingroup bn
- */
-
-#include <gmp.h>
-
-#include "relic.h"
-
-/*============================================================================*/
-/* Public definitions */
-/*============================================================================*/
-
-void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, int sa, dig_t *b, int sb) {
- mpn_tdiv_qr(c, d, 0, a, sa, b, sb);
-}
-
-void bn_div1_low(dig_t *c, dig_t *d, dig_t *a, int size, dig_t b) {
- *d = mpn_divrem_1(c, 0, a, size, b);
-}
View
58 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_mul_low.c
@@ -1,58 +0,0 @@
-/*
- * RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
- *
- * This file is part of RELIC. RELIC is legal property of its developers,
- * whose names are not listed here. Please refer to the COPYRIGHT file
- * for contact information.
- *
- * RELIC is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * RELIC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * @file
- *
- * Implementation of the multiple precision integer arithmetic multiplication
- * functions.
- *
- * @version $Id: relic_bn_mul_low.c,v 1.1 2011/08/13 04:38:38 diego Exp $
- * @ingroup bn
- */
-
-#include <gmp.h>
-
-#include "relic.h"
-
-/*============================================================================*/
-/* Public definitions */
-/*============================================================================*/
-
-dig_t bn_muladd_low(dig_t *c, dig_t *a, dig_t digit, int size) {
- return mpn_addmul_1(c, a, size, digit);
-}
-
-dig_t bn_mul1_low(dig_t *c, dig_t *a, dig_t digit, int size) {
- return mpn_mul_1(c, a, size, digit);
-}
-
-void bn_muln_low(dig_t *c, dig_t *a, dig_t *b, int size) {
- mpn_mul_n(c, a, b, size);
-}
-
-void bn_muld_low(dig_t *c, dig_t *a, int sizea, dig_t *b, int sizeb,
- int low, int high) {
- (void) low;
- (void) high;
- mpn_mul(c, a, sizea, b, sizeb);
-}
View
75 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_bn_shift_low.c
@@ -1,6 +1,6 @@
/*
* RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
+ * Copyright (C) 2007-2012 RELIC Authors
*
* This file is part of RELIC. RELIC is legal property of its developers,
* whose names are not listed here. Please refer to the COPYRIGHT file
@@ -25,27 +25,48 @@
*
* Implementation of the low-level multiple precision bit shifting functions.
*
- * @version $Id: relic_bn_shift_low.c,v 1.1 2011/08/13 04:38:38 diego Exp $
+ * @version $Id: relic_bn_shift_low.c 677 2011-03-05 22:19:43Z dfaranha $
* @ingroup bn
*/
-#include <gmp.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
#include "relic.h"
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
dig_t bn_lsh1_low(dig_t *c, dig_t *a, int size) {
- return mpn_lshift(c, a, size, 1);
+ int i;
+ dig_t r, carry;
+
+ carry = 0;
+ for (i = 0; i < size; i++, a++, c++) {
+ /* Get the most significant bit. */
+ r = *a >> (BN_DIGIT - 1);
+ /* Shift the operand and insert the carry, */
+ *c = (*a << 1) | carry;
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
dig_t bn_lshb_low(dig_t *c, dig_t *a, int size, int bits) {
- return mpn_lshift(c, a, size, bits);
+ int i;
+ dig_t r, carry, shift, mask;
+
+ shift = BN_DIGIT - bits;
+ carry = 0;
+ mask = MASK(bits);
+ for (i = 0; i < size; i++, a++, c++) {
+ /* Get the needed least significant bits. */
+ r = ((*a) >> shift) & mask;
+ /* Shift left the operand. */
+ *c = ((*a) << bits) | carry;
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
void bn_lshd_low(dig_t *c, dig_t *a, int size, int digits) {
@@ -64,11 +85,43 @@ void bn_lshd_low(dig_t *c, dig_t *a, int size, int digits) {
}
dig_t bn_rsh1_low(dig_t *c, dig_t *a, int size) {
- return mpn_rshift(c, a, size, 1);
+ int i;
+ dig_t r, carry;
+
+ c += size - 1;
+ a += size - 1;
+ carry = 0;
+ for (i = size - 1; i >= 0; i--, a--, c--) {
+ /* Get the least significant bit. */
+ r = *a & 0x01;
+ /* Shift the operand and insert the carry. */
+ carry <<= BN_DIGIT - 1;
+ *c = (*a >> 1) | carry;
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
dig_t bn_rshb_low(dig_t *c, dig_t *a, int size, int bits) {
- return mpn_rshift(c, a, size, bits);
+ int i;
+ dig_t r, carry, shift, mask;
+
+ c += size - 1;
+ a += size - 1;
+ /* Prepare the bit mask. */
+ shift = BN_DIGIT - bits;
+ carry = 0;
+ mask = MASK(bits);
+ for (i = size - 1; i >= 0; i--, a--, c--) {
+ /* Get the needed least significant bits. */
+ r = (*a) & mask;
+ /* Shift left the operand. */
+ *c = ((*a) >> bits) | (carry << shift);
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
void bn_rshd_low(dig_t *c, dig_t *a, int size, int digits) {
View
40 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_fb_cmp_low.c
@@ -1,45 +1,59 @@
/*
- * Copyright (C) 2011 BACKEND Authors
+ * RELIC is an Efficient LIbrary for Cryptography
+ * Copyright (C) 2007-2012 RELIC Authors
*
- * This code is legal property of its developers, whose names are not
- * listed here. Please refer to the COPYRIGHT file for contact
- * information.
+ * This file is part of RELIC. RELIC is legal property of its developers,
+ * whose names are not listed here. Please refer to the COPYRIGHT file
+ * for contact information.
*
- * This code is free software; you can redistribute it and/or
+ * RELIC is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * This code is distributed in the hope that it will be useful,
+ * RELIC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
- * along with this code. If not, see <http://www.gnu.org/licenses/>.
+ * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
*
* Implementation of the low-level binary field comparison functions.
*
- * @version $Id: relic_fb_cmp_low.c,v 1.1 2011/08/13 04:38:38 diego Exp $
- * @ingroup bn
+ * @version $Id: relic_fb_cmp_low.c 681 2011-03-10 07:18:24Z dfaranha $
+ * @ingroup fb
*/
-#include <gmp.h>
-
#include "relic.h"
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
int fb_cmp1_low(dig_t a, dig_t b) {
- return mpn_cmp(&a, &b, 1);
+ if (a > b)
+ return CMP_GT;
+ if (a < b)
+ return CMP_LT;
+ return CMP_EQ;
}
int fb_cmpn_low(dig_t *a, dig_t *b) {
- return mpn_cmp(a, b, FB_DIGS);
+ int i, r;
+
+ a += (FB_DIGS - 1);
+ b += (FB_DIGS - 1);
+
+ r = CMP_EQ;
+ for (i = 0; i < FB_DIGS; i++, --a, --b) {
+ if (*a != *b && r == CMP_EQ) {
+ r = (*a > *b ? CMP_GT : CMP_LT);
+ }
+ }
+ return r;
}
View
89 crypto_dh/curve2251/relic/amd64-avx/src/low/relic_fb_shift_low.c
@@ -1,47 +1,74 @@
/*
- * Copyright (C) 2011 BACKEND Authors
+ * RELIC is an Efficient LIbrary for Cryptography
+ * Copyright (C) 2007-2012 RELIC Authors
*
- * This code is legal property of its developers, whose names are not
- * listed here. Please refer to the COPYRIGHT file for contact
- * information.
+ * This file is part of RELIC. RELIC is legal property of its developers,
+ * whose names are not listed here. Please refer to the COPYRIGHT file
+ * for contact information.
*
- * This code is free software; you can redistribute it and/or
+ * RELIC is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * This code is distributed in the hope that it will be useful,
+ * RELIC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
- * along with this code. If not, see <http://www.gnu.org/licenses/>.
+ * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
*
* Implementation of the low-level binary field bit shifting functions.
*
- * @version $Id: relic_fb_shift_low.c,v 1.1 2011/08/13 04:38:38 diego Exp $
- * @ingroup bn
+ * @version $Id: relic_fb_shift_low.c 677 2011-03-05 22:19:43Z dfaranha $
+ * @ingroup fb
*/
-#include <gmp.h>
-
#include "relic.h"
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
dig_t fb_lsh1_low(dig_t *c, dig_t *a) {
- return mpn_lshift(c, a, FB_DIGS, 1);
+ int i;
+ dig_t r, carry;
+
+ /* Prepare the bit mask. */
+ carry = 0;
+ for (i = 0; i < FB_DIGS; i++, a++, c++) {
+ /* Get the most significant bit. */
+ r = *a >> (FB_DIGIT - 1);
+ /* Shift the operand and insert the carry, */
+ *c = (*a << 1) | carry;
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
dig_t fb_lshb_low(dig_t *c, dig_t *a, int bits) {
- return mpn_lshift(c, a, FB_DIGS, bits);
+ int i;
+ dig_t r, carry, mask, shift;
+
+ /* Prepare the bit mask. */
+ shift = FB_DIGIT - bits;
+ carry = 0;
+ mask = MASK(bits);
+ for (i = 0; i < FB_DIGS; i++, a++, c++) {
+ /* Get the needed least significant bits. */
+ r = ((*a) >> shift) & mask;
+ /* Shift left the operand. */
+ *c = ((*a) << bits) | carry;
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
void fb_lshd_low(dig_t *c, dig_t *a, int digits) {
@@ -60,11 +87,43 @@ void fb_lshd_low(dig_t *c, dig_t *a, int digits) {
}
dig_t fb_rsh1_low(dig_t *c, dig_t *a) {
- return mpn_rshift(c, a, FB_DIGS, 1);
+ int i;
+ dig_t r, carry;
+
+ c += FB_DIGS - 1;
+ a += FB_DIGS - 1;
+ carry = 0;
+ for (i = FB_DIGS - 1; i >= 0; i--, a--, c--) {
+ /* Get the least significant bit. */
+ r = *a & 0x01;
+ /* Shift the operand and insert the carry. */
+ carry <<= FB_DIGIT - 1;
+ *c = (*a >> 1) | carry;
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
dig_t fb_rshb_low(dig_t *c, dig_t *a, int bits) {
- return mpn_rshift(c, a, FB_DIGS, bits);
+ int i;
+ dig_t r, carry, mask, shift;
+
+ c += FB_DIGS - 1;
+ a += FB_DIGS - 1;
+ /* Prepare the bit mask. */
+ shift = FB_DIGIT - bits;
+ carry = 0;
+ mask = MASK(bits);
+ for (i = FB_DIGS - 1; i >= 0; i--, a--, c--) {
+ /* Get the needed least significant bits. */
+ r = (*a) & mask;
+ /* Shift left the operand. */
+ *c = ((*a) >> bits) | (carry << shift);
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
void fb_rshd_low(dig_t *c, dig_t *a, int digits) {
View
7 crypto_dh/curve2251/relic/amd64-clmul/Makefile
@@ -2,13 +2,8 @@ SRC:=$(wildcard src/eb/*.c) $(wildcard src/fb/*.c) $(wildcard src/bn/*.c) $(wild
ASM:=$(wildcard src/low/*.s)
OBJ:=$(SRC:.c=.o) $(ASM:.s=.o) curve2251.o
-ifndef GMP
-GMP:=../../lib/amd64
-endif
-
CC:=gcc
CFLAGS:=-O3 -std=c99 -I include/low -I target/include -I . -I$(GMP) -funroll-loops -fomit-frame-pointer -msse4 -mpclmul -DMAIN -march=native
-LIB:=-L$(GMP) -L target/lib -lgmp
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
@@ -17,7 +12,7 @@ LIB:=-L$(GMP) -L target/lib -lgmp
$(CC) $(CFLAGS) -c $< -o $@
curve2251: $(OBJ)
- $(CC) -static $(OBJ) $(LIB) -o $@
+ $(CC) -static $(OBJ) -o $@
clean:
-rm -f $(OBJ) curve2251
View
66 crypto_dh/curve2251/relic/amd64-clmul/curve2251.c
@@ -23,7 +23,6 @@ int crypto_dh_keypair(unsigned char *pk, unsigned char *sk) {
bn_new(k);
bn_new(n);
-
n->used = 4;
n->dp[0] = 0x00BEF3B9ABB767E1;
n->dp[1] = 0xF3E3AA131A2E1A82;
@@ -34,22 +33,18 @@ int crypto_dh_keypair(unsigned char *pk, unsigned char *sk) {
fb_zero(a);
fb_set_dig(b, 0x2387);
- //bench_reset();
- //bench_before();
eb_curve_set_ordin(a, b, &(gen_tab[1]), n, k);
- //eb_param_set(EBACS_B251);
#ifdef MAIN
rand_bytes(sk, SK_BYTES);
#else
randombytes(sk, SK_BYTES);
#endif
bn_read_bin(k, sk, SK_BYTES);
- bn_div_rem(NULL, k, k, n);
+ if (bn_cmp(k, n) != CMP_LT) {
+ bn_sub(k, k, n);
+ }
- //bench_after();
- //bench_compute(1);
- //bench_print();
eb_mul_fix(p, (eb_t *)gen_tab, k);
bn_read_raw(k, p->x, FB_DIGS);
@@ -109,65 +104,22 @@ int main(int argc, char *argv[]) {
}
printf("\n");
-#if 0
- eb_t *tab = eb_curve_get_tab();
- printf("eb_st tab[%d] = {\n", EB_TABLE);
- for (int i = 0; i < EB_TABLE; i++) {
- printf("\t{\n");
-
- printf("\t\t{");
- for (int k = 0; k < FB_DIGS; k++) {
- util_print("0x%.*lX, ", (int)(2 * sizeof(dig_t)), (unsigned long int)tab[i]->x[k]);
- }
- printf("},\n");
-
- printf("\t\t{");
- for (int k = 0; k < FB_DIGS; k++) {
- util_print("0x%.*lX, ", (int)(2 * sizeof(dig_t)), (unsigned long int)tab[i]->y[k]);
- }
- printf("},\n");
-
- printf("\t\t{");
- for (int k = 0; k < FB_DIGS; k++) {
- util_print("0x%.*lX, ", (int)(2 * sizeof(dig_t)), (unsigned long int)tab[i]->z[k]);
- }
- printf("},\n");
-
- printf("\t\t1\n\t},\n");
- }
- printf("};\n");
-
- int chain;
- fb_poly_get_chain(&chain);
- printf("fb_st inv_tab[%d][FB_TABLE] = {\n", chain);
- for (int i = 0; i < chain; i++) {
- printf("\t{\n");
- fb_st *tab = fb_poly_tab_sqr(i);
- for (int j = 0; j < FB_TABLE; j++) {
- printf("\t\t{");
- for (int k = 0; k < FB_DIGS; k++) {
- util_print("0x%.*lX, ", (int)(2 * sizeof(dig_t)), (unsigned long int)tab[j][k]);
- }
- printf("},\n");
- }
- printf("\t},\n");
- }
- printf("};\n");
-
-#endif
-
eb_curve_get_ord(n);
eb_curve_get_gen(p);
BENCH_BEGIN("eb_mul_fix") {
bn_rand(k, BN_POS, bn_bits(n));
- bn_div_rem(NULL, k, k, n);
+ if (bn_cmp(k, n) != CMP_LT) {
+ bn_sub(k, k, n);
+ }
BENCH_ADD(eb_mul_fix(p, (eb_t *)gen_tab, k));
} BENCH_END;
BENCH_BEGIN("eb_mul") {
bn_rand(k, BN_POS, bn_bits(n));
- bn_div_rem(NULL, k, k, n);
+ if (bn_cmp(k, n) != CMP_LT) {
+ bn_sub(k, k, n);
+ }
BENCH_ADD(eb_mul(q, p, k));
} BENCH_END;
View
2 crypto_dh/curve2251/relic/amd64-clmul/implementors
@@ -1,4 +1,4 @@
-Diego de Freitas Aranha, Institute of Computing, University of Campinas, Brazil
+Diego de Freitas Aranha, Department of Computer Science, University of Brasília, Brazil
Jonathan Taverne, Université de Lyon, Université Lyon1, ISFA, France
Armando Faz-Hernández, Computer Science Department, CINVESTAV-IPN, Mexico
Francisco Rodríguez-Henríquez, Computer Science Department, CINVESTAV-IPN, Mexico
View
4 crypto_dh/curve2251/relic/amd64-clmul/relic_bn.c
@@ -1,12 +1,8 @@
#include "src/bn/relic_bn_mem.c"
#include "src/bn/relic_bn_add.c"
#include "src/bn/relic_bn_cmp.c"
-#include "src/bn/relic_bn_div.c"
-#include "src/bn/relic_bn_mul.c"
#include "src/bn/relic_bn_shift.c"
#include "src/bn/relic_bn_util.c"
#include "src/low/relic_bn_add_low.c"
-#include "src/low/relic_bn_div_low.c"
#include "src/low/relic_bn_cmp_low.c"
-#include "src/low/relic_bn_mul_low.c"
#include "src/low/relic_bn_shift_low.c"
View
210 crypto_dh/curve2251/relic/amd64-clmul/src/bn/relic_bn_div.c
@@ -1,210 +0,0 @@
-/*
- * RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
- *
- * This file is part of RELIC. RELIC is legal property of its developers,
- * whose names are not listed here. Please refer to the COPYRIGHT file
- * for contact information.
- *
- * RELIC is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * RELIC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * @file
- *
- * Implementation of the multiple precision division functions.
- *
- * @version $Id: relic_bn_div.c,v 1.1 2011/08/12 19:27:15 diego Exp $
- * @ingroup bn
- */
-
-#include "relic.h"
-
-/*============================================================================*/
-/* Private definitions */
-/*============================================================================*/
-
-/**
- * Divides two multiple precision integers, computing the quotient and the
- * remainder.
- *
- * @param[out] c - the quotient.
- * @param[out] d - the remainder.
- * @param[in] a - the dividend.
- * @param[in] b - the the divisor.
- */
-static void bn_div_imp(bn_t c, bn_t d, bn_t a, bn_t b) {
- bn_t q, x, y, r;
- int sign;
-
- bn_null(q);
- bn_null(x);
- bn_null(y);
- bn_null(r);
-
- /* If a < b, we're done. */
- if (bn_cmp_abs(a, b) == CMP_LT) {
- if (d != NULL) {
- bn_copy(d, a);
- }
- if (c != NULL) {
- bn_zero(c);
- }
- return;
- }
-
- TRY {
- bn_new(x);
- bn_new(y);
- bn_new_size(q, a->used + 1);
- bn_new(r);
- bn_zero(q);
- bn_zero(r);
- bn_abs(x, a);
- bn_abs(y, b);
-
- /* Find the sign. */
- sign = (a->sign == b->sign ? BN_POS : BN_NEG);
-
- bn_divn_low(q->dp, r->dp, x->dp, a->used, y->dp, b->used);
-
- /* We have the quotient in q and the remainder in r. */
- if (c != NULL) {
- q->used = a->used - b->used + 1;
- q->sign = sign;
- bn_trim(q);
- bn_copy(c, q);
- }
-
- if (d != NULL) {
- r->used = b->used;
- r->sign = a->sign;
- bn_trim(r);
- bn_copy(d, r);
- }
- }
- CATCH_ANY {
- THROW(ERR_CAUGHT);
- }
- FINALLY {
- bn_free(r);
- bn_free(q);
- bn_free(x);
- bn_free(y);
- }
-}
-
-/*============================================================================*/
-/* Public definitions */
-/*============================================================================*/
-
-void bn_div(bn_t c, bn_t a, bn_t b) {
- if (bn_is_zero(b)) {
- THROW(ERR_INVALID);
- }
- bn_div_imp(c, NULL, a, b);
-}
-
-void bn_div_rem(bn_t c, bn_t d, bn_t a, bn_t b) {
- if (bn_is_zero(b)) {
- THROW(ERR_INVALID);
- }
- bn_div_imp(c, d, a, b);
-}
-
-void bn_div_dig(bn_t c, bn_t a, dig_t b) {
- bn_t q;
- dig_t r;
-
- bn_null(q);
-
- if (b == 0) {
- THROW(ERR_INVALID);
- }
-
- if (b == 1 || bn_is_zero(a) == 1) {
- if (c != NULL) {
- bn_copy(c, a);
- }
- return;
- }
-
- TRY {
- bn_new(q);
- int size = a->used;
- dig_t *ap = a->dp;
-
- bn_div1_low(q->dp, &r, ap, size, b);
-
- if (c != NULL) {
- q->used = a->used;
- q->sign = a->sign;
- bn_trim(q);
- bn_copy(c, q);
- }
- }
- CATCH_ANY {
- THROW(ERR_CAUGHT);
- }
- FINALLY {
- bn_free(q);
- }
-}
-
-void bn_div_rem_dig(bn_t c, dig_t *d, bn_t a, dig_t b) {
- bn_t q;
- dig_t r;
-
- bn_null(q);
-
- if (b == 0) {
- THROW(ERR_INVALID);
- }
-
- if (b == 1 || bn_is_zero(a) == 1) {
- if (d != NULL) {
- *d = 0;
- }
- if (c != NULL) {
- bn_copy(c, a);
- }
- return;
- }
-
- TRY {
- bn_new(q);
- int size = a->used;
- dig_t *ap = a->dp;
-
- bn_div1_low(q->dp, &r, ap, size, b);
-
- if (c != NULL) {
- q->used = a->used;
- q->sign = a->sign;
- bn_trim(q);
- bn_copy(c, q);
- }
-
- if (d != NULL) {
- *d = r;
- }
- }
- CATCH_ANY {
- THROW(ERR_CAUGHT);
- }
- FINALLY {
- bn_free(q);
- }
-}
-
View
47 crypto_dh/curve2251/relic/amd64-clmul/src/bn/relic_bn_mul.c
@@ -1,47 +0,0 @@
-/*
- * RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
- *
- * This file is part of RELIC. RELIC is legal property of its developers,
- * whose names are not listed here. Please refer to the COPYRIGHT file
- * for contact information.
- *
- * RELIC is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * RELIC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * @file
- *
- * Implementation of the multiple precision multiplication functions.
- *
- * @version $Id: relic_bn_mul.c,v 1.1 2011/08/12 19:27:15 diego Exp $
- * @ingroup bn
- */
-
-#include "relic.h"
-
-/*============================================================================*/
-/* Public definitions */
-/*============================================================================*/
-
-void bn_mul_dig(bn_t c, bn_t a, dig_t b) {
- dig_t carry;
-
- bn_grow(c, a->used + 1);
- carry = bn_mul1_low(c->dp, a->dp, b, a->used);
- c->dp[a->used] = carry;
- c->used = a->used + 1;
- c->sign = a->sign;
- bn_trim(c);
-}
View
156 crypto_dh/curve2251/relic/amd64-clmul/src/bn/relic_bn_util.c
@@ -25,7 +25,7 @@
*
* Implementation of the multiple precision utilities.
*
- * @version $Id: relic_bn_util.c,v 1.1 2011/08/12 19:27:15 diego Exp $
+ * @version $Id: relic_bn_util.c,v 1.1 2011/08/12 19:27:16 diego Exp $
* @ingroup bn
*/
@@ -224,160 +224,6 @@ void bn_print(bn_t a) {
}
}
-void bn_size_str(int *size, bn_t a, int radix) {
- int digits;
- bn_t t;
-
- bn_null(t);
-
- *size = 0;
-
- /* Binary case. */
- if (radix == 2) {
- *size = bn_bits(a) + (a->sign == BN_NEG ? 1 : 0) + 1;
- return;
- }
-
- /* Check the radix. */
- if (radix < 2 || radix > 64) {
- THROW(ERR_INVALID);
- }
-
- if (bn_is_zero(a)) {
- *size = 2;
- return;
- }
-
- digits = 0;
-
- if (a->sign == BN_NEG) {
- digits++;
- }
-
- TRY {
- bn_new(t);
- bn_copy(t, a);
-
- t->sign = BN_POS;
-
- while (!bn_is_zero(t)) {
- bn_div_dig(t, t, (dig_t)radix);
- digits++;
- }
- *size = digits + 1;
-
- } CATCH_ANY {
- THROW(ERR_CAUGHT);
- } FINALLY {
- bn_free(t);
- }
-}
-
-void bn_read_str(bn_t a, const char *str, int len, int radix) {
- int sign, i, j;
- char c;
-
- bn_zero(a);
-
- if (radix < 2 || radix > 64) {
- THROW(ERR_INVALID)
- }
-
- j = 0;
- if (str[0] == '-') {
- j++;
- sign = BN_NEG;
- } else {
- sign = BN_POS;
- }
-
- while (str[j] && j < len) {
- c = (char)((radix < 36) ? TOUPPER(str[j]) : str[j]);
- for (i = 0; i < 64; i++) {
- if (c == util_conv_char(i)) {
- break;
- }
- }
-
- if (i < radix) {
- bn_mul_dig(a, a, (dig_t)radix);
- bn_add_dig(a, a, (dig_t)i);
- } else {
- break;
- }
- j++;
- }
-
- a->sign = sign;
-}
-
-void bn_write_str(char *str, int len, bn_t a, int radix) {
- bn_t t;
- dig_t d;
- int digits, l, i, j;
- char c;
-
- bn_null(t);
-
- bn_size_str(&l, a, radix);
- if (len < l) {
- THROW(ERR_NO_BUFFER);
- }
-
- if (radix < 2 || radix > 64) {
- THROW(ERR_INVALID)
- }
-
- if (bn_is_zero(a) == 1) {
- *str++ = '0';
- *str = '\0';
- return;
- }
-
- TRY {
- bn_new(t);
- bn_copy(t, a);
-
- j = 0;
- if (t->sign == BN_NEG) {
- str[j] = '-';
- j++;
- t->sign = BN_POS;
- }
-
- digits = 0;
- while (!bn_is_zero(t)) {
- bn_div_rem_dig(t, &d, t, (dig_t)radix);
- str[j] = util_conv_char(d);
- digits++;
- j++;
- }
-
- /* Reverse the digits of the string. */
- i = 0;
- if (str[0] == '-') {
- i = 1;
- }
- j = l - 2;
- while (i < j) {
- c = str[i];
- str[i] = str[j];
- str[j] = c;
- ++i;
- --j;
- }
-
- str[l - 1] = '\0';
-
- }
- CATCH_ANY {
- THROW(ERR_CAUGHT);
- }
- FINALLY {
- bn_free(t);
- }
-}
-
void bn_size_bin(int *size, bn_t a) {
dig_t d;
int digits;
View
61 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_add_low.c
@@ -1,6 +1,6 @@
/*
* RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
+ * Copyright (C) 2007-2012 RELIC Authors
*
* This file is part of RELIC. RELIC is legal property of its developers,
* whose names are not listed here. Please refer to the COPYRIGHT file
@@ -26,30 +26,75 @@
* Implementation of the low-level multiple precision addition and subtraction
* functions.
*
- * @version $Id: relic_bn_add_low.c,v 1.1 2011/08/12 19:27:15 diego Exp $
+ * @version $Id: relic_bn_add_low.c 677 2011-03-05 22:19:43Z dfaranha $
* @ingroup bn
*/
-#include <gmp.h>
-
#include "relic.h"
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
dig_t bn_add1_low(dig_t *c, dig_t *a, dig_t digit, int size) {
- return mpn_add_1(c, a, size, digit);
+ int i;
+ register dig_t carry, r0;
+
+ carry = digit;
+ for (i = 0; i < size && carry; i++, a++, c++) {
+ r0 = (*a) + carry;
+ carry = (r0 < carry);
+ (*c) = r0;
+ }
+ for (; i < size; i++, a++, c++) {
+ (*c) = (*a);
+ }
+ return carry;
}
dig_t bn_addn_low(dig_t *c, dig_t *a, dig_t *b, int size) {
- return mpn_add_n(c, a, b, size);
+ int i;
+ register dig_t carry, c0, c1, r0, r1;
+
+ carry = 0;
+ for (i = 0; i < size; i++, a++, b++, c++) {
+ r0 = (*a) + (*b);
+ c0 = (r0 < (*a));
+ r1 = r0 + carry;
+ c1 = (r1 < r0);
+ carry = c0 | c1;
+ (*c) = r1;
+ }
+ return carry;
}
dig_t bn_sub1_low(dig_t *c, dig_t *a, dig_t digit, int size) {
- return mpn_sub_1(c, a, size, digit);
+ int i;
+ dig_t carry, r0;
+
+ carry = digit;
+ for (i = 0; i < size && carry; i++, c++, a++) {
+ r0 = (*a) - carry;
+ carry = (r0 > (*a));
+ (*c) = r0;
+ }
+ for (; i < size; i++, a++, c++) {
+ (*c) = (*a);
+ }
+ return carry;
}
dig_t bn_subn_low(dig_t *c, dig_t *a, dig_t *b, int size) {
- return mpn_sub_n(c, a, b, size);
+ int i;
+ dig_t carry, r0, diff;
+
+ /* Zero the carry. */
+ carry = 0;
+ for (i = 0; i < size; i++, a++, b++, c++) {
+ diff = (*a) - (*b);
+ r0 = diff - carry;
+ carry = ((*a) < (*b)) || (carry && !diff);
+ (*c) = r0;
+ }
+ return carry;
}
View
25 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_cmp_low.c
@@ -1,6 +1,6 @@
/*
* RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
+ * Copyright (C) 2007-2012 RELIC Authors
*
* This file is part of RELIC. RELIC is legal property of its developers,
* whose names are not listed here. Please refer to the COPYRIGHT file
@@ -25,22 +25,35 @@
*
* Implementation of the low-level multiple precision comparison functions.
*
- * @version $Id: relic_bn_cmp_low.c,v 1.1 2011/08/12 19:27:15 diego Exp $
+ * @version $Id: relic_bn_cmp_low.c 681 2011-03-10 07:18:24Z dfaranha $
* @ingroup bn
*/
-#include <gmp.h>
-
#include "relic.h"
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
int bn_cmp1_low(dig_t a, dig_t b) {
- return mpn_cmp(&a, &b, 1);
+ if (a > b)
+ return CMP_GT;
+ if (a < b)
+ return CMP_LT;
+ return CMP_EQ;
}
int bn_cmpn_low(dig_t *a, dig_t *b, int size) {
- return mpn_cmp(a, b, size);
+ int i, r;
+
+ a += (size - 1);
+ b += (size - 1);
+
+ r = CMP_EQ;
+ for (i = 0; i < size; i++, --a, --b) {
+ if (*a != *b && r == CMP_EQ) {
+ r = (*a > *b ? CMP_GT : CMP_LT);
+ }
+ }
+ return r;
}
View
46 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_div_low.c
@@ -1,46 +0,0 @@
-/*
- * RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
- *
- * This file is part of RELIC. RELIC is legal property of its developers,
- * whose names are not listed here. Please refer to the COPYRIGHT file
- * for contact information.
- *
- * RELIC is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * RELIC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * @file
- *
- * Implementation of the low-level multiple precision division functions.
- *
- * @version $Id: relic_bn_div_low.c,v 1.1 2011/08/12 19:27:15 diego Exp $
- * @ingroup bn
- */
-
-#include <gmp.h>
-
-#include "relic.h"
-
-/*============================================================================*/
-/* Public definitions */
-/*============================================================================*/
-
-void bn_divn_low(dig_t *c, dig_t *d, dig_t *a, int sa, dig_t *b, int sb) {
- mpn_tdiv_qr(c, d, 0, a, sa, b, sb);
-}
-
-void bn_div1_low(dig_t *c, dig_t *d, dig_t *a, int size, dig_t b) {
- *d = mpn_divrem_1(c, 0, a, size, b);
-}
View
58 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_mul_low.c
@@ -1,58 +0,0 @@
-/*
- * RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
- *
- * This file is part of RELIC. RELIC is legal property of its developers,
- * whose names are not listed here. Please refer to the COPYRIGHT file
- * for contact information.
- *
- * RELIC is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * RELIC is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/**
- * @file
- *
- * Implementation of the multiple precision integer arithmetic multiplication
- * functions.
- *
- * @version $Id: relic_bn_mul_low.c,v 1.1 2011/08/12 19:27:15 diego Exp $
- * @ingroup bn
- */
-
-#include <gmp.h>
-
-#include "relic.h"
-
-/*============================================================================*/
-/* Public definitions */
-/*============================================================================*/
-
-dig_t bn_muladd_low(dig_t *c, dig_t *a, dig_t digit, int size) {
- return mpn_addmul_1(c, a, size, digit);
-}
-
-dig_t bn_mul1_low(dig_t *c, dig_t *a, dig_t digit, int size) {
- return mpn_mul_1(c, a, size, digit);
-}
-
-void bn_muln_low(dig_t *c, dig_t *a, dig_t *b, int size) {
- mpn_mul_n(c, a, b, size);
-}
-
-void bn_muld_low(dig_t *c, dig_t *a, int sizea, dig_t *b, int sizeb,
- int low, int high) {
- (void) low;
- (void) high;
- mpn_mul(c, a, sizea, b, sizeb);
-}
View
75 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_bn_shift_low.c
@@ -1,6 +1,6 @@
/*
* RELIC is an Efficient LIbrary for Cryptography
- * Copyright (C) 2007-2011 RELIC Authors
+ * Copyright (C) 2007-2012 RELIC Authors
*
* This file is part of RELIC. RELIC is legal property of its developers,
* whose names are not listed here. Please refer to the COPYRIGHT file
@@ -25,27 +25,48 @@
*
* Implementation of the low-level multiple precision bit shifting functions.
*
- * @version $Id: relic_bn_shift_low.c,v 1.1 2011/08/12 19:27:15 diego Exp $
+ * @version $Id: relic_bn_shift_low.c 677 2011-03-05 22:19:43Z dfaranha $
* @ingroup bn
*/
-#include <gmp.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-
#include "relic.h"
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
dig_t bn_lsh1_low(dig_t *c, dig_t *a, int size) {
- return mpn_lshift(c, a, size, 1);
+ int i;
+ dig_t r, carry;
+
+ carry = 0;
+ for (i = 0; i < size; i++, a++, c++) {
+ /* Get the most significant bit. */
+ r = *a >> (BN_DIGIT - 1);
+ /* Shift the operand and insert the carry, */
+ *c = (*a << 1) | carry;
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
dig_t bn_lshb_low(dig_t *c, dig_t *a, int size, int bits) {
- return mpn_lshift(c, a, size, bits);
+ int i;
+ dig_t r, carry, shift, mask;
+
+ shift = BN_DIGIT - bits;
+ carry = 0;
+ mask = MASK(bits);
+ for (i = 0; i < size; i++, a++, c++) {
+ /* Get the needed least significant bits. */
+ r = ((*a) >> shift) & mask;
+ /* Shift left the operand. */
+ *c = ((*a) << bits) | carry;
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
void bn_lshd_low(dig_t *c, dig_t *a, int size, int digits) {
@@ -64,11 +85,43 @@ void bn_lshd_low(dig_t *c, dig_t *a, int size, int digits) {
}
dig_t bn_rsh1_low(dig_t *c, dig_t *a, int size) {
- return mpn_rshift(c, a, size, 1);
+ int i;
+ dig_t r, carry;
+
+ c += size - 1;
+ a += size - 1;
+ carry = 0;
+ for (i = size - 1; i >= 0; i--, a--, c--) {
+ /* Get the least significant bit. */
+ r = *a & 0x01;
+ /* Shift the operand and insert the carry. */
+ carry <<= BN_DIGIT - 1;
+ *c = (*a >> 1) | carry;
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
dig_t bn_rshb_low(dig_t *c, dig_t *a, int size, int bits) {
- return mpn_rshift(c, a, size, bits);
+ int i;
+ dig_t r, carry, shift, mask;
+
+ c += size - 1;
+ a += size - 1;
+ /* Prepare the bit mask. */
+ shift = BN_DIGIT - bits;
+ carry = 0;
+ mask = MASK(bits);
+ for (i = size - 1; i >= 0; i--, a--, c--) {
+ /* Get the needed least significant bits. */
+ r = (*a) & mask;
+ /* Shift left the operand. */
+ *c = ((*a) >> bits) | (carry << shift);
+ /* Update the carry. */
+ carry = r;
+ }
+ return carry;
}
void bn_rshd_low(dig_t *c, dig_t *a, int size, int digits) {
View
40 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_fb_cmp_low.c
@@ -1,45 +1,59 @@
/*
- * Copyright (C) 2011 BACKEND Authors
+ * RELIC is an Efficient LIbrary for Cryptography
+ * Copyright (C) 2007-2012 RELIC Authors
*
- * This code is legal property of its developers, whose names are not
- * listed here. Please refer to the COPYRIGHT file for contact
- * information.
+ * This file is part of RELIC. RELIC is legal property of its developers,
+ * whose names are not listed here. Please refer to the COPYRIGHT file
+ * for contact information.
*
- * This code is free software; you can redistribute it and/or
+ * RELIC is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * This code is distributed in the hope that it will be useful,
+ * RELIC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
- * along with this code. If not, see <http://www.gnu.org/licenses/>.
+ * along with RELIC. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
*
* Implementation of the low-level binary field comparison functions.
*
- * @version $Id: relic_fb_cmp_low.c,v 1.1 2011/08/12 19:27:15 diego Exp $
- * @ingroup bn
+ * @version $Id: relic_fb_cmp_low.c 681 2011-03-10 07:18:24Z dfaranha $
+ * @ingroup fb
*/
-#include <gmp.h>
-
#include "relic.h"
/*============================================================================*/
/* Public definitions */
/*============================================================================*/
int fb_cmp1_low(dig_t a, dig_t b) {
- return mpn_cmp(&a, &b, 1);
+ if (a > b)
+ return CMP_GT;
+ if (a < b)
+ return CMP_LT;
+ return CMP_EQ;
}
int fb_cmpn_low(dig_t *a, dig_t *b) {
- return mpn_cmp(a, b, FB_DIGS);
+ int i, r;
+
+ a += (FB_DIGS - 1);
+ b += (FB_DIGS - 1);
+
+ r = CMP_EQ;
+ for (i = 0; i < FB_DIGS; i++, --a, --b) {
+ if (*a != *b && r == CMP_EQ) {
+ r = (*a > *b ? CMP_GT : CMP_LT);
+ }
+ }
+ return r;
}
View
89 crypto_dh/curve2251/relic/amd64-clmul/src/low/relic_fb_shift_low.c
@@ -1,47 +1,74 @@
/*
- * Copyright (C) 2011 BACKEND Authors
+ * RELIC is an Efficient LIbrary for Cryptography
+ * Copyright (C) 2007-2012 RELIC Authors
*
- * This code is legal property of its developers, whose names are not
- * listed here. Please refer to the COPYRIGHT file for contact
- * information.
+ * This file is part of RELIC. RELIC is legal property of its developers,
+ * whose names are not listed here. Please refer to the COPYRIGHT file
+ * for contact information.
*
- * This code is free software; you can redistribute it and/or
+ * RELIC is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public