SKALE-1512-add-DKG-to-SGX change std randomizer to sgx randomizer in trusted libff

parent fadcd44d
...@@ -76,6 +76,8 @@ BLSUtils.o: BLSUtils.cpp \ ...@@ -76,6 +76,8 @@ BLSUtils.o: BLSUtils.cpp \
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \ /home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \
../trusted_libff/libff/algebra/fields/bigint.tcc \ ../trusted_libff/libff/algebra/fields/bigint.tcc \
../sgx-sdk-build/sgxsdk/include/libcxx/random \ ../sgx-sdk-build/sgxsdk/include/libcxx/random \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h \
../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc \ ../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc \
../trusted_libff/libff/algebra/fields/fp.tcc \ ../trusted_libff/libff/algebra/fields/fp.tcc \
../sgx-sdk-build/sgxsdk/include/libcxx/cmath \ ../sgx-sdk-build/sgxsdk/include/libcxx/cmath \
...@@ -245,6 +247,10 @@ BLSUtils.h: ...@@ -245,6 +247,10 @@ BLSUtils.h:
../sgx-sdk-build/sgxsdk/include/libcxx/random: ../sgx-sdk-build/sgxsdk/include/libcxx/random:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h:
../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc: ../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc:
../trusted_libff/libff/algebra/fields/fp.tcc: ../trusted_libff/libff/algebra/fields/fp.tcc:
......
...@@ -73,6 +73,8 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \ ...@@ -73,6 +73,8 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \ /home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \
../trusted_libff/libff/algebra/fields/bigint.tcc \ ../trusted_libff/libff/algebra/fields/bigint.tcc \
../sgx-sdk-build/sgxsdk/include/libcxx/random \ ../sgx-sdk-build/sgxsdk/include/libcxx/random \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h \
../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc \ ../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc \
../trusted_libff/libff/algebra/fields/fp.tcc \ ../trusted_libff/libff/algebra/fields/fp.tcc \
../sgx-sdk-build/sgxsdk/include/libcxx/cmath \ ../sgx-sdk-build/sgxsdk/include/libcxx/cmath \
...@@ -88,7 +90,8 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \ ...@@ -88,7 +90,8 @@ DKGUtils.o: DKGUtils.cpp DKGUtils.h \
../trusted_libff/libff/algebra/fields/fp.hpp ../sgxwallet_common.h \ ../trusted_libff/libff/algebra/fields/fp.hpp ../sgxwallet_common.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/unistd.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/unistd.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/types.h \ /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/types.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/mbusafecrt.h
DKGUtils.h: DKGUtils.h:
...@@ -240,6 +243,10 @@ DKGUtils.h: ...@@ -240,6 +243,10 @@ DKGUtils.h:
../sgx-sdk-build/sgxsdk/include/libcxx/random: ../sgx-sdk-build/sgxsdk/include/libcxx/random:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h:
../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc: ../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc:
../trusted_libff/libff/algebra/fields/fp.tcc: ../trusted_libff/libff/algebra/fields/fp.tcc:
...@@ -273,3 +280,5 @@ DKGUtils.h: ...@@ -273,3 +280,5 @@ DKGUtils.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/types.h: /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/sys/types.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h: /home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/stdbool.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/tlibc/mbusafecrt.h:
...@@ -73,6 +73,8 @@ alt_bn128_g1.o: \ ...@@ -73,6 +73,8 @@ alt_bn128_g1.o: \
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \ /home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \
../trusted_libff/libff/algebra/fields/bigint.tcc \ ../trusted_libff/libff/algebra/fields/bigint.tcc \
../sgx-sdk-build/sgxsdk/include/libcxx/random \ ../sgx-sdk-build/sgxsdk/include/libcxx/random \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h \
../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc \ ../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc \
../trusted_libff/libff/algebra/fields/fp.tcc \ ../trusted_libff/libff/algebra/fields/fp.tcc \
../sgx-sdk-build/sgxsdk/include/libcxx/cmath \ ../sgx-sdk-build/sgxsdk/include/libcxx/cmath \
...@@ -232,6 +234,10 @@ alt_bn128_g1.o: \ ...@@ -232,6 +234,10 @@ alt_bn128_g1.o: \
../sgx-sdk-build/sgxsdk/include/libcxx/random: ../sgx-sdk-build/sgxsdk/include/libcxx/random:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h:
../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc: ../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc:
../trusted_libff/libff/algebra/fields/fp.tcc: ../trusted_libff/libff/algebra/fields/fp.tcc:
......
...@@ -73,6 +73,8 @@ alt_bn128_init.o: \ ...@@ -73,6 +73,8 @@ alt_bn128_init.o: \
/home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \ /home/kladko/sgxwallet/tgmp-build/include/sgx_tgmp.h \
../trusted_libff/libff/algebra/fields/bigint.tcc \ ../trusted_libff/libff/algebra/fields/bigint.tcc \
../sgx-sdk-build/sgxsdk/include/libcxx/random \ ../sgx-sdk-build/sgxsdk/include/libcxx/random \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h \
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h \
../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc \ ../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc \
../trusted_libff/libff/algebra/fields/fp.tcc \ ../trusted_libff/libff/algebra/fields/fp.tcc \
../sgx-sdk-build/sgxsdk/include/libcxx/cmath \ ../sgx-sdk-build/sgxsdk/include/libcxx/cmath \
...@@ -232,6 +234,10 @@ alt_bn128_init.o: \ ...@@ -232,6 +234,10 @@ alt_bn128_init.o: \
../sgx-sdk-build/sgxsdk/include/libcxx/random: ../sgx-sdk-build/sgxsdk/include/libcxx/random:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_trts.h:
/home/kladko/sgxwallet/sgx-sdk-build/sgxsdk/include/sgx_error.h:
../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc: ../trusted_libff/libff/algebra/exponentiation/exponentiation.tcc:
../trusted_libff/libff/algebra/fields/fp.tcc: ../trusted_libff/libff/algebra/fields/fp.tcc:
......
This diff is collapsed.
...@@ -2,13 +2,18 @@ ...@@ -2,13 +2,18 @@
// Created by kladko on 9/5/19. // Created by kladko on 9/5/19.
// //
#include "DKGUtils.h" #include "DKGUtils.h"
#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <libff/algebra/exponentiation/exponentiation.hpp>
#include <../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <../trusted_libff/libff/algebra/fields/fp.hpp> #include <../trusted_libff/libff/algebra/fields/fp.hpp>
#include "../sgxwallet_common.h" #include "../sgxwallet_common.h"
#include <cstdio>
#include <stdio.h>
#include <mbusafecrt.h>
std::string stringFromFr(libff::alt_bn128_Fr& _el) { std::string stringFromFr(libff::alt_bn128_Fr& _el) {
...@@ -25,18 +30,19 @@ std::string stringFromFr(libff::alt_bn128_Fr& _el) { ...@@ -25,18 +30,19 @@ std::string stringFromFr(libff::alt_bn128_Fr& _el) {
return std::string(tmp); return std::string(tmp);
} }
void gen_dkg_poly( char secret[BUF_LEN], unsigned len, unsigned _t ){ void gen_dkg_poly( char* secret/*[BUF_LEN]*/, unsigned len, unsigned _t ){
libff::init_alt_bn128_params(); libff::init_alt_bn128_params();
std::string result; std::string result;
for (size_t i = 0; i < _t; ++i) { for (size_t i = 0; i < _t; ++i) {
libff::alt_bn128_Fr cur_coef = 1;//libff::alt_bn128_Fr::random_element(); libff::alt_bn128_Fr cur_coef = libff::alt_bn128_Fr::random_element();
/* while (i == _t - 1 && cur_coef == libff::alt_bn128_Fr::zero()) { while (i == _t - 1 && cur_coef == libff::alt_bn128_Fr::zero()) {
cur_coef = libff::alt_bn128_Fr::random_element(); cur_coef = libff::alt_bn128_Fr::random_element();
}*/ }
result += stringFromFr(cur_coef); result = stringFromFr(cur_coef);
if ( i < _t - 1) result += ":"; result += ":";
} }
strncpy(secret, result.c_str(), BUF_LEN);
len = result.length(); strncpy(secret, result.c_str(), result.length());
len = _t*33;//result.length();
} }
\ No newline at end of file
...@@ -85,7 +85,8 @@ CLEANFILES+= secure_enclave_t.c secure_enclave_t.h ...@@ -85,7 +85,8 @@ CLEANFILES+= secure_enclave_t.c secure_enclave_t.h
secure_enclave_SOURCES = secure_enclave_t.c secure_enclave_t.h \ secure_enclave_SOURCES = secure_enclave_t.c secure_enclave_t.h \
secure_enclave.c \ secure_enclave.c \
DKGUtils.cpp BLSUtils.cpp ../trusted_libff/libff/algebra/fields/fp.hpp ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.cpp \ DKGUtils.cpp BLSUtils.cpp ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.cpp \
## ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_pp.cpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.cpp $(ENCLAVE_KEY) $(ENCLAVE_CONFIG) ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.cpp $(ENCLAVE_KEY) $(ENCLAVE_CONFIG)
......
...@@ -337,7 +337,7 @@ ENCLAVE_CONFIG = $(ENCLAVE).config.xml ...@@ -337,7 +337,7 @@ ENCLAVE_CONFIG = $(ENCLAVE).config.xml
ENCLAVE_KEY = $(ENCLAVE)_private.pem ENCLAVE_KEY = $(ENCLAVE)_private.pem
secure_enclave_SOURCES = secure_enclave_t.c secure_enclave_t.h \ secure_enclave_SOURCES = secure_enclave_t.c secure_enclave_t.h \
secure_enclave.c \ secure_enclave.c \
DKGUtils.cpp BLSUtils.cpp ../trusted_libff/libff/algebra/fields/fp.hpp ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.cpp \ DKGUtils.cpp BLSUtils.cpp ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_init.cpp \
../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.cpp $(ENCLAVE_KEY) $(ENCLAVE_CONFIG) ../trusted_libff/libff/algebra/curves/alt_bn128/alt_bn128_g1.cpp $(ENCLAVE_KEY) $(ENCLAVE_CONFIG)
secure_enclave_LDADD = @SGX_ENCLAVE_LDADD@ secure_enclave_LDADD = @SGX_ENCLAVE_LDADD@
......
...@@ -50,6 +50,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -50,6 +50,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "../sgxwallet_common.h" #include "../sgxwallet_common.h"
void *(*gmp_realloc_func)(void *, size_t, size_t); void *(*gmp_realloc_func)(void *, size_t, size_t);
void *(*oc_realloc_func)(void *, size_t, size_t); void *(*oc_realloc_func)(void *, size_t, size_t);
void (*gmp_free_func)(void *, size_t); void (*gmp_free_func)(void *, size_t);
...@@ -349,27 +350,36 @@ void ecdsa_sign_message(int *err_status, char *err_string, uint8_t *encrypted_k ...@@ -349,27 +350,36 @@ void ecdsa_sign_message(int *err_status, char *err_string, uint8_t *encrypted_k
void gen_dkg_secret (int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, size_t _t){ void gen_dkg_secret (int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, size_t _t){
size_t len = 0; size_t len = 0;
char dkg_secret[BUF_LEN]; //char dkg_secret[BUF_LEN];
//char dkg_secret[10];
//memset(dkg_secret, 5, 10);
char* dkg_secret = (char*)malloc(1024);
memset(dkg_secret, 0, 1024);
gen_dkg_poly( dkg_secret, len, _t); gen_dkg_poly( dkg_secret, len, _t);
char poly[BUF_LEN];
memset(poly, 0, BUF_LEN);
strncpy(poly, dkg_secret, len); uint32_t sealedLen = sgx_calc_sealed_data_size(0, sizeof((uint8_t*)dkg_secret));//sizeof(sgx_sealed_data_t) + sizeof(dkg_secret); //
memset(encrypted_dkg_secret, 0, BUF_LEN); sgx_status_t status = sgx_seal_data(0, NULL, sizeof(dkg_secret), (uint8_t*)dkg_secret, sealedLen,(sgx_sealed_data_t*)encrypted_dkg_secret);
if( status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"SGX seal data failed");
}
}
uint32_t sealedLen = sgx_calc_sealed_data_size(0, sizeof(poly)); void decrypt_dkg_secret (int *err_status, char* err_string, uint8_t* encrypted_dkg_secret, uint8_t* decrypted_dkg_secret){
sgx_status_t status = sgx_seal_data(0, NULL, BUF_LEN, (uint8_t*)dkg_secret, sealedLen,(sgx_sealed_data_t*)encrypted_dkg_secret); uint32_t dec_size = 1024;//sgx_get_encrypt_txt_len( (const sgx_sealed_data_t *)encrypted_dkg_secret);
// sgx_sealed_data_t *tmp = (sgx_sealed_data_t*)malloc(dec_size);
//memcpy(tmp, encrypted_dkg_secret, dec_size);
if ( poly[0] != '1'){ sgx_status_t status = sgx_unseal_data(
snprintf(err_string, BUF_LEN,"wrong poly"); (const sgx_sealed_data_t *)encrypted_dkg_secret, NULL, 0, decrypted_dkg_secret, &dec_size);
} //tmp, NULL, 0, decrypted_dkg_secret, &dec_size);
if( status != SGX_SUCCESS) { if (status != SGX_SUCCESS) {
snprintf(err_string, BUF_LEN,"SGX seal data failed"); snprintf(err_string, BUF_LEN,"sgx_unseal_data failed with status %d", status);
return;
} }
} }
\ No newline at end of file
...@@ -62,6 +62,12 @@ enclave { ...@@ -62,6 +62,12 @@ enclave {
[out, count = 1024] char* err_string, [out, count = 1024] char* err_string,
[out, count = 1024] uint8_t* encrypted_dkg_secret, [out, count = 1024] uint8_t* encrypted_dkg_secret,
size_t _t); size_t _t);
public void decrypt_dkg_secret (
[user_check] int *err_status,
[out, count = 1024] char* err_string,
[in, count = 1024] uint8_t* encrypted_dkg_secret,
[out, count = 1024] uint8_t* decrypted_dkg_secret);
}; };
......
...@@ -31,6 +31,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -31,6 +31,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "sgx_tcrypto.h"
#include "sgx_tseal.h"
#include <sgx_tgmp.h>
#include <sgx_trts.h>
#include "sgxwallet_common.h" #include "sgxwallet_common.h"
#include "create_enclave.h" #include "create_enclave.h"
#include "secure_enclave_u.h" #include "secure_enclave_u.h"
...@@ -41,6 +46,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -41,6 +46,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "BLSCrypto.h" #include "BLSCrypto.h"
#include <libff/algebra/curves/alt_bn128/alt_bn128_pp.hpp>
#include <libff/algebra/exponentiation/exponentiation.hpp>
#include <libff/algebra/fields/fp.hpp>
#define ENCLAVE_NAME "secure_enclave.signed.so" #define ENCLAVE_NAME "secure_enclave.signed.so"
...@@ -58,6 +73,21 @@ sgx_status_t status; ...@@ -58,6 +73,21 @@ sgx_status_t status;
int updated; int updated;
std::string stringFromFr(libff::alt_bn128_Fr& el) {
mpz_t t;
mpz_init(t);
el.as_bigint().to_mpz(t);
char arr[mpz_sizeinbase(t, 10) + 2];
char *tmp = mpz_get_str(arr, 10, t);
mpz_clear(t);
return std::string(tmp);
}
TEST_CASE( "BLS sign test", "[bls-sign]" ) { TEST_CASE( "BLS sign test", "[bls-sign]" ) {
...@@ -103,35 +133,46 @@ TEST_CASE( "BLS sign test", "[bls-sign]" ) { ...@@ -103,35 +133,46 @@ TEST_CASE( "BLS sign test", "[bls-sign]" ) {
REQUIRE(dec_len == enc_len); REQUIRE(dec_len == enc_len);
gmp_printf("Result: %s", result); printf("Result: %s", result);
gmp_printf("\n Length: %d \n", enc_len); printf("\n Length: %d \n", enc_len);
} }
TEST_CASE( "DKG gen test", "[dkg-gen]" ) { TEST_CASE( "DKG gen test", "[dkg-gen]" ) {
init_all(); init_all();
uint8_t* encrypted_dkg_secret = (uint8_t*) calloc(1024, 1);
//char* Array = (char*) calloc(128, 1); uint8_t* encrypted_dkg_secret = (uint8_t*) malloc(1024);//(uint8_t*) calloc(1024, 1);
memset(encrypted_dkg_secret, 0, 1024);
char* errMsg = (char*) calloc(1024,1); char* errMsg = (char*) calloc(1024,1);
int err_status = 0; int err_status = 0;
// unsigned int enc_len = 0;
//(int *err_status, char *err_string, uint8_t *encrypted_dkg_secret, size_t _t)
status = gen_dkg_secret (eid, &err_status, errMsg, encrypted_dkg_secret, 1); status = gen_dkg_secret (eid, &err_status, errMsg, encrypted_dkg_secret, 1);
REQUIRE(status == SGX_SUCCESS);
printf("gen_dkg_secret completed with status: %d %s \n", err_status, errMsg);
printf("encrypted secret length %ld \n", sizeof(encrypted_dkg_secret));
uint8_t* secret = (uint8_t*)calloc(1024, sizeof(uint8_t));
char* errMsg1 = (char*) calloc(1024,1);
status = decrypt_dkg_secret(eid, &err_status, errMsg1, (uint8_t*)encrypted_dkg_secret, secret);
REQUIRE(status == SGX_SUCCESS); REQUIRE(status == SGX_SUCCESS);
printf("gen_dkg_secret completed with status: %d %s \n", err_status, errMsg); printf("decrypt_dkg_secret completed with status: %d %s \n", err_status, errMsg1);
printf(" Encrypted key len %d\n", sizeof(encrypted_dkg_secret)); printf("decrypted secret length %ld \n", sizeof(secret));
printf("decrypted secret %s \n", secret);
/*libff::alt_bn128_Fr cur_coef = libff::alt_bn128_Fr::random_element();
std::string rand_el_str = stringFromFr(cur_coef);
printf("rand element is: %s", rand_el_str.c_str());
printf("rand element length: %d", (int)rand_el_str.length());*/
} }
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#include <cstring> #include <cstring>
#include <random> #include <random>
#include <sgx_trts.h>
namespace libff { namespace libff {
template<mp_size_t n> template<mp_size_t n>
...@@ -163,6 +165,30 @@ bool bigint<n>::test_bit(const std::size_t bitno) const ...@@ -163,6 +165,30 @@ bool bigint<n>::test_bit(const std::size_t bitno) const
} }
} }
template<mp_size_t n>
bigint<n>& bigint<n>::randomize()
{
const size_t size = 1;
static_assert(GMP_NUMB_BITS == sizeof(mp_limb_t) * 8, "Wrong GMP_NUMB_BITS value");
//std::random_device rd;
constexpr size_t num_random_words = sizeof(mp_limb_t) * n / size;//sizeof(std::random_device::result_type);
//auto random_words = reinterpret_cast<std::random_device::result_type*>(this->data);
auto random_words = reinterpret_cast<unsigned char*>(this->data);
sgx_read_rand(random_words, num_random_words);
/* auto random_words = reinterpret_cast<unsigned int*>(this->data);
for (size_t i = 0; i < num_random_words; ++i)
{
unsigned char rand[size];
sgx_read_rand(rand, size);
memcpy(&random_words[i], rand, size);
//random_words[i] = rd();
}*/
return (*this);
}
} // libff } // libff
#endif // BIGINT_TCC_ #endif // BIGINT_TCC_
...@@ -684,6 +684,35 @@ Fp_model<n,modulus>& Fp_model<n,modulus>::invert() ...@@ -684,6 +684,35 @@ Fp_model<n,modulus>& Fp_model<n,modulus>::invert()
return *this; return *this;
} }
template<mp_size_t n, const bigint<n>& modulus>
Fp_model<n, modulus> Fp_model<n,modulus>::random_element() /// returns random element of Fp_model
{
/* note that as Montgomery representation is a bijection then
selecting a random element of {xR} is the same as selecting a
random element of {x} */
Fp_model<n, modulus> r;
do
{
r.mont_repr.randomize();
/* clear all bits higher than MSB of modulus */
size_t bitno = GMP_NUMB_BITS * n - 1;
while (modulus.test_bit(bitno) == false)
{
const std::size_t part = bitno/GMP_NUMB_BITS;
const std::size_t bit = bitno - (GMP_NUMB_BITS*part);
r.mont_repr.data[part] &= ~(1ul<<bit);
bitno--;
}
}
/* if r.data is still >= modulus -- repeat (rejection sampling) */
while (mpn_cmp(r.mont_repr.data, modulus.data, n) >= 0);
return r;
}
template<mp_size_t n, const bigint<n>& modulus> template<mp_size_t n, const bigint<n>& modulus>
Fp_model<n,modulus> Fp_model<n,modulus>::inverse() const Fp_model<n,modulus> Fp_model<n,modulus>::inverse() const
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment