From fc641a21976525aaf6ff70f1b3e7f3268d2ff947 Mon Sep 17 00:00:00 2001 From: Anton Nashatyrev Date: Fri, 2 Jul 2021 14:48:36 +0300 Subject: [PATCH 1/2] Add fr_to_uint64s() function --- src/bls12_381.c | 10 ++++++++++ src/bls12_381.h | 1 + 2 files changed, 11 insertions(+) diff --git a/src/bls12_381.c b/src/bls12_381.c index 6592284..250679f 100644 --- a/src/bls12_381.c +++ b/src/bls12_381.c @@ -121,6 +121,16 @@ void fr_from_uint64(fr_t *out, uint64_t n) { fr_from_uint64s(out, vals); } +/** + * Convert the field element to an array of four 64-bit unsigned integers. + * + * @param out array for returned values, little-endian ordering of the 64-bit words + * @param vals The field element equivalent of @p n + */ +void fr_to_uint64s(uint64_t out[4], const fr_t* fr) { + blst_uint64_from_fr(out, fr); +} + /** * Test whether two field elements are equal. * diff --git a/src/bls12_381.h b/src/bls12_381.h index add1c94..9649c97 100644 --- a/src/bls12_381.h +++ b/src/bls12_381.h @@ -104,6 +104,7 @@ bool fr_is_null(const fr_t *p); void fr_from_scalar(fr_t *out, const scalar_t *a); void fr_from_uint64s(fr_t *out, const uint64_t *vals); void fr_from_uint64(fr_t *out, uint64_t n); +void fr_to_uint64s(uint64_t out[4], const fr_t* fr); bool fr_equal(const fr_t *aa, const fr_t *bb); void fr_negate(fr_t *out, const fr_t *in); void fr_add(fr_t *out, const fr_t *a, const fr_t *b); From 958dc1aee78e469da868f730b5c941a01690e0a0 Mon Sep 17 00:00:00 2001 From: Ben Edgington Date: Sat, 3 Jul 2021 09:37:20 +0100 Subject: [PATCH 2/2] Minor tweaks and add a unit test --- src/bls12_381.c | 8 ++++---- src/bls12_381_test.c | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/bls12_381.c b/src/bls12_381.c index 250679f..5ca4842 100644 --- a/src/bls12_381.c +++ b/src/bls12_381.c @@ -104,7 +104,7 @@ void fr_from_scalar(fr_t *out, const scalar_t *a) { * @param out The field element equivalent of @p n * @param vals The array of 64-bit integers to be converted, little-endian ordering of the 64-bit words */ -void fr_from_uint64s(fr_t *out, const uint64_t *vals) { +void fr_from_uint64s(fr_t *out, const uint64_t vals[4]) { blst_fr_from_uint64(out, vals); } @@ -122,12 +122,12 @@ void fr_from_uint64(fr_t *out, uint64_t n) { } /** - * Convert the field element to an array of four 64-bit unsigned integers. + * Convert a field element to an array of four 64-bit unsigned integers. * - * @param out array for returned values, little-endian ordering of the 64-bit words + * @param out Array for returned values, little-endian ordering of the 64-bit words * @param vals The field element equivalent of @p n */ -void fr_to_uint64s(uint64_t out[4], const fr_t* fr) { +void fr_to_uint64s(uint64_t out[4], const fr_t *fr) { blst_uint64_from_fr(out, fr); } diff --git a/src/bls12_381_test.c b/src/bls12_381_test.c index 126c9f7..35216cf 100644 --- a/src/bls12_381_test.c +++ b/src/bls12_381_test.c @@ -110,6 +110,20 @@ void fr_div_by_zero(void) { TEST_CHECK(fr_is_zero(&tmp)); } +void fr_uint64s_roundtrip(void) { + fr_t fr; + uint64_t expected[4] = {1, 2, 3, 4}; + uint64_t actual[4]; + + fr_from_uint64s(&fr, expected); + fr_to_uint64s(actual, &fr); + + TEST_CHECK(expected[0] == actual[0]); + TEST_CHECK(expected[1] == actual[1]); + TEST_CHECK(expected[2] == actual[2]); + TEST_CHECK(expected[3] == actual[3]); +} + void p1_mul_works(void) { fr_t minus1; g1_t res; @@ -235,6 +249,7 @@ TEST_LIST = { {"fr_pow_works", fr_pow_works}, {"fr_div_works", fr_div_works}, {"fr_div_by_zero", fr_div_by_zero}, + {"fr_uint64s_roundtrip", fr_uint64s_roundtrip}, {"p1_mul_works", p1_mul_works}, {"p1_sub_works", p1_sub_works}, {"p2_mul_works", p2_mul_works},