Skip to content

Commit 1d52a8b

Browse files
committed
Implementations for scalar without data-dependent branches.
1 parent 0ce80ef commit 1d52a8b

10 files changed

+1194
-57
lines changed

.travis.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ install:
66
- if [ "$FIELD" = "64bit_asm" ]; then sudo apt-get install -qq yasm; fi
77
env:
88
global:
9-
- FIELD=auto BIGNUM=auto ENDOMORPHISM=no BUILD=check
9+
- FIELD=auto BIGNUM=auto SCALAR=auto ENDOMORPHISM=no BUILD=check
1010
matrix:
11+
- SCALAR=32bit
12+
- SCALAR=64bit
1113
- FIELD=gmp
1214
- FIELD=gmp ENDOMORPHISM=yes
1315
- FIELD=64bit_asm
@@ -18,5 +20,5 @@ env:
1820
- FIELD=32bit ENDOMORPHISM=yes
1921
- BUILD=distcheck
2022
before_script: ./autogen.sh
21-
script: ./configure --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM && make -j2 $BUILD
23+
script: ./configure --enable-endomorphism=$ENDOMORPHISM --with-field=$FIELD --with-bignum=$BIGNUM --with-scalar=$SCALAR && make -j2 $BUILD
2224
os: linux

Makefile.am

+4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ noinst_LTLIBRARIES = $(COMMON_LIB)
1010
include_HEADERS = include/secp256k1.h
1111
noinst_HEADERS =
1212
noinst_HEADERS += src/scalar.h
13+
noinst_HEADERS += src/scalar_4x64.h
14+
noinst_HEADERS += src/scalar_8x32.h
1315
noinst_HEADERS += src/scalar_impl.h
16+
noinst_HEADERS += src/scalar_4x64_impl.h
17+
noinst_HEADERS += src/scalar_8x32_impl.h
1418
noinst_HEADERS += src/group.h
1519
noinst_HEADERS += src/group_impl.h
1620
noinst_HEADERS += src/num_gmp.h

configure.ac

+44
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,19 @@ AC_ARG_WITH([field], [AS_HELP_STRING([--with-field=gmp|64bit|64bit_asm|32bit|aut
6464
AC_ARG_WITH([bignum], [AS_HELP_STRING([--with-bignum=gmp|auto],
6565
[Specify Bignum Implementation. Default is auto])],[req_bignum=$withval], [req_bignum=auto])
6666

67+
AC_ARG_WITH([scalar], [AS_HELP_STRING([--with-scalar=64bit|32bit|auto],
68+
[Specify scalar implementation. Default is auto])],[req_scalar=$withval], [req_scalar=auto])
69+
6770
AC_CHECK_TYPES([__int128])
6871

6972
AC_DEFUN([SECP_INT128_CHECK],[
7073
has_int128=$ac_cv_type___int128
7174
if test x"$has_int128" != x"yes" && test x"$set_field" = x"64bit"; then
7275
AC_MSG_ERROR([$set_field field support explicitly requested but is not compatible with this host])
7376
fi
77+
if test x"$has_int128" != x"yes" && test x"$set_scalar" = x"64bit"; then
78+
AC_MSG_ERROR([$set_scalar scalar support explicitly requested but is not compatible with this host])
79+
fi
7480
])
7581

7682
AC_DEFUN([SECP_64BIT_ASM_CHECK],[
@@ -194,6 +200,30 @@ else
194200
esac
195201
fi
196202

203+
if test x"$req_scalar" = x"auto"; then
204+
if test x"$set_scalar" = x; then
205+
SECP_INT128_CHECK
206+
if test x"$has_int128" = x"yes"; then
207+
set_scalar=64bit
208+
fi
209+
fi
210+
if test x"$set_scalar" = x; then
211+
set_scalar=32bit
212+
fi
213+
else
214+
set_scalar=$req_scalar
215+
case $set_scalar in
216+
64bit)
217+
SECP_INT128_CHECK
218+
;;
219+
32bit)
220+
;;
221+
*)
222+
AC_MSG_ERROR([invalid scalar implementation selected])
223+
;;
224+
esac
225+
fi
226+
197227
if test x"$req_bignum" = x"auto"; then
198228
SECP_GMP_CHECK
199229
if test x"$has_gmp" = x"yes"; then
@@ -252,6 +282,19 @@ gmp)
252282
;;
253283
esac
254284

285+
#select scalar implementation
286+
case $set_scalar in
287+
64bit)
288+
AC_DEFINE(USE_SCALAR_4X64, 1, [Define this symbol to use the 4x64 scalar implementation])
289+
;;
290+
32bit)
291+
AC_DEFINE(USE_SCALAR_8X32, 1, [Define this symbol to use the 8x32 scalar implementation])
292+
;;
293+
*)
294+
AC_MSG_ERROR([invalid scalar implementation])
295+
;;
296+
esac
297+
255298
if test x"$use_tests" = x"yes"; then
256299
SECP_OPENSSL_CHECK
257300
if test x"$has_openssl_ec" == x"yes"; then
@@ -278,6 +321,7 @@ fi
278321

279322
AC_MSG_NOTICE([Using field implementation: $set_field])
280323
AC_MSG_NOTICE([Using bignum implementation: $set_bignum])
324+
AC_MSG_NOTICE([Using scalar implementation: $set_scalar])
281325

282326
AC_CONFIG_HEADERS([src/libsecp256k1-config.h])
283327
AC_CONFIG_FILES([Makefile libsecp256k1.pc])

src/scalar.h

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@
77

88
#include "num.h"
99

10-
/** A scalar modulo the group order of the secp256k1 curve. */
11-
typedef struct {
12-
secp256k1_num_t n;
13-
} secp256k1_scalar_t;
10+
#if defined HAVE_CONFIG_H
11+
#include "libsecp256k1-config.h"
12+
#endif
13+
14+
#if defined(USE_SCALAR_4X64)
15+
#include "scalar_4x64.h"
16+
#elif defined(USE_SCALAR_8X32)
17+
#include "scalar_8x32.h"
18+
#else
19+
#error "Please select scalar implementation"
20+
#endif
1421

1522
/** Clear a scalar to prevent the leak of sensitive data. */
1623
void static secp256k1_scalar_clear(secp256k1_scalar_t *r);
@@ -30,6 +37,9 @@ void static secp256k1_scalar_add(secp256k1_scalar_t *r, const secp256k1_scalar_t
3037
/** Multiply two scalars (modulo the group order). */
3138
void static secp256k1_scalar_mul(secp256k1_scalar_t *r, const secp256k1_scalar_t *a, const secp256k1_scalar_t *b);
3239

40+
/** Compute the square of a scalar (modulo the group order). */
41+
void static secp256k1_scalar_sqr(secp256k1_scalar_t *r, const secp256k1_scalar_t *a);
42+
3343
/** Compute the inverse of a scalar (modulo the group order). */
3444
void static secp256k1_scalar_inverse(secp256k1_scalar_t *r, const secp256k1_scalar_t *a);
3545

src/scalar_4x64.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2014 Pieter Wuille
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef _SECP256K1_SCALAR_REPR_
6+
#define _SECP256K1_SCALAR_REPR_
7+
8+
#include <stdint.h>
9+
10+
/** A scalar modulo the group order of the secp256k1 curve. */
11+
typedef struct {
12+
uint64_t d[4];
13+
} secp256k1_scalar_t;
14+
15+
#endif

0 commit comments

Comments
 (0)