Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bindings/blst_aux.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ size_t blst_p2_sizeof(void);
size_t blst_p2_affine_sizeof(void);
size_t blst_fp12_sizeof(void);

void blst_fp_from_le_bytes(blst_fp *ret, const byte *in, size_t len);
void blst_fp_from_be_bytes(blst_fp *ret, const byte *in, size_t len);

/*
* Single-shot SHA-256 hash function.
*/
Expand Down
6 changes: 6 additions & 0 deletions bindings/rust/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,12 @@ extern "C" {
extern "C" {
pub fn blst_fp12_sizeof() -> usize;
}
extern "C" {
pub fn blst_fp_from_le_bytes(ret: *mut blst_fp, in_: *const byte, len: usize);
}
extern "C" {
pub fn blst_fp_from_be_bytes(ret: *mut blst_fp, in_: *const byte, len: usize);
}
extern "C" {
pub fn blst_sha256(out: *mut byte, msg: *const byte, msg_len: usize);
}
Expand Down
41 changes: 39 additions & 2 deletions src/exports.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ void blst_uint64_from_fr(unsigned long long ret[4], const vec256 a)

int blst_scalar_from_le_bytes(pow256 out, const unsigned char *bytes, size_t n)
{
size_t rem = (n - 1) % 32 + 1;
size_t rem = n ? ((n - 1) % 32 + 1) : 0;
struct { vec256 out, digit; } t;
limb_t ret;

Expand Down Expand Up @@ -525,7 +525,7 @@ int blst_scalar_from_le_bytes(pow256 out, const unsigned char *bytes, size_t n)

int blst_scalar_from_be_bytes(pow256 out, const unsigned char *bytes, size_t n)
{
size_t rem = (n - 1) % 32 + 1;
size_t rem = n ? ((n - 1) % 32 + 1) : 0;
struct { vec256 out, digit; } t;
limb_t ret;

Expand All @@ -550,6 +550,43 @@ int blst_scalar_from_be_bytes(pow256 out, const unsigned char *bytes, size_t n)
return (int)(ret^1);
}

void blst_fp_from_le_bytes(vec384 out, const unsigned char *bytes, size_t n)
{
size_t rem = n ? ((n - 1) % 48 + 1) : 0;
vec384 digit;

vec_zero(out, sizeof(vec384));

n -= rem;
limbs_from_le_bytes(out, bytes += n, rem);
mul_mont_384(out, BLS12_381_RR, out, BLS12_381_P, p0);

while (n) {
limbs_from_le_bytes(digit, bytes -= 48, 48);
add_mod_384(out, out, digit, BLS12_381_P);
mul_mont_384(out, BLS12_381_RR, out, BLS12_381_P, p0);
n -= 48;
}
}

void blst_fp_from_be_bytes(vec384 out, const unsigned char *bytes, size_t n)
{
size_t rem = n ? ((n - 1) % 48 + 1) : 0;
vec384 digit;

vec_zero(out, sizeof(vec384));

limbs_from_be_bytes(out, bytes, rem);
mul_mont_384(out, BLS12_381_RR, out, BLS12_381_P, p0);

while (n -= rem) {
limbs_from_be_bytes(digit, bytes += rem, 48);
add_mod_384(out, out, digit, BLS12_381_P);
mul_mont_384(out, BLS12_381_RR, out, BLS12_381_P, p0);
rem = 48;
}
}

/*
* Single-short SHA-256 hash function.
*/
Expand Down