Skip to content

Commit dddfd4e

Browse files
authored
repo-sync-2025-05-14T14:40:52+0800 (#526)
1 parent 8965322 commit dddfd4e

File tree

7 files changed

+808
-5
lines changed

7 files changed

+808
-5
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ _build
2626

2727
# bazel
2828
bazel-*
29-
# bazel lock file
30-
MODULE.bazel.lock
3129

3230
# cmake related
3331
abseil-cpp

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
module(
2323
name = "yacl",
24-
version = "0.4.5b11-nightly-20250428",
24+
version = "0.4.5b11-nightly-20250514",
2525
compatibility_level = 1,
2626
)
2727

MODULE.bazel.lock

Lines changed: 757 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

yacl/crypto/experimental/zkp/bulletproofs/bp_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ namespace examples::zkp {
2020
inline constexpr const char* kBpEcName = "secp256k1";
2121
inline constexpr const char* kBpEcLib = "openssl";
2222

23-
} // namespace examples::zkp
23+
} // namespace examples::zkp

yacl/math/bigint/bigint_test.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,34 @@ TEST_P(BigIntArithTest, ToBytes) {
10401040
BigInt a("1234567890", 10, lib_);
10411041
BigInt b("-1234567890", 10, lib_);
10421042

1043+
{
1044+
uint8_t buf;
1045+
a.ToBytes(reinterpret_cast<unsigned char*>(&buf), 1, Endian::native);
1046+
EXPECT_EQ(buf, 210);
1047+
b.ToBytes(reinterpret_cast<unsigned char*>(&buf), 1, Endian::native);
1048+
EXPECT_EQ(buf, 46);
1049+
}
1050+
{
1051+
uint8_t buf;
1052+
a.ToBytes(reinterpret_cast<unsigned char*>(&buf), 1, Endian::big);
1053+
EXPECT_EQ(buf, 210);
1054+
b.ToBytes(reinterpret_cast<unsigned char*>(&buf), 1, Endian::big);
1055+
EXPECT_EQ(buf, 46);
1056+
}
1057+
{
1058+
uint16_t buf;
1059+
a.ToBytes(reinterpret_cast<unsigned char*>(&buf), 2, Endian::native);
1060+
EXPECT_EQ(buf, 722);
1061+
b.ToBytes(reinterpret_cast<unsigned char*>(&buf), 2, Endian::native);
1062+
EXPECT_EQ(buf, 64814);
1063+
}
1064+
{
1065+
uint16_t buf;
1066+
a.ToBytes(reinterpret_cast<unsigned char*>(&buf), 2, Endian::big);
1067+
EXPECT_EQ(buf, 53762);
1068+
b.ToBytes(reinterpret_cast<unsigned char*>(&buf), 2, Endian::big);
1069+
EXPECT_EQ(buf, 12029);
1070+
}
10431071
{
10441072
uint32_t buf;
10451073
a.ToBytes(reinterpret_cast<unsigned char*>(&buf), 4, Endian::native);

yacl/math/bigint/gmp/gmp_int.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,16 @@ yacl::Buffer GMPInt::ToBytes(size_t byte_len, Endian endian) const {
594594

595595
void GMPInt::ToBytes(unsigned char* buf, size_t buf_len, Endian endian) const {
596596
size_t byte_count = (gmp_.mpz_sizeinbase_(z_, 2) + 7) / 8;
597-
YACL_ENFORCE_GE(buf_len, byte_count, "Buffer is too small");
597+
if (buf_len < byte_count) {
598+
std::vector<unsigned char> tmp(byte_count);
599+
ToBytes(tmp.data(), byte_count, endian);
600+
if (endian == Endian::little) {
601+
memcpy(buf, tmp.data(), buf_len);
602+
} else {
603+
memcpy(buf, tmp.data() + byte_count - buf_len, buf_len);
604+
}
605+
return;
606+
}
598607
memset(buf, 0, buf_len);
599608
int endianness = endian == Endian::big ? 1 : -1;
600609
if (endian == Endian::little) {

yacl/math/bigint/openssl/bignum.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,17 @@ yacl::Buffer BigNum::ToBytes(size_t byte_len, Endian endian) const {
497497
}
498498

499499
void BigNum::ToBytes(unsigned char* buf, size_t buf_len, Endian endian) const {
500+
size_t byte_count = BN_num_bytes(bn_.get());
501+
if (buf_len < byte_count) {
502+
std::vector<unsigned char> tmp(byte_count);
503+
ToBytes(tmp.data(), byte_count, endian);
504+
if (endian == Endian::little) {
505+
memcpy(buf, tmp.data(), buf_len);
506+
} else {
507+
memcpy(buf, tmp.data() + byte_count - buf_len, buf_len);
508+
}
509+
return;
510+
}
500511
memset(buf, 0, buf_len);
501512
if (endian == Endian::big) {
502513
OSSL_RET_NOT_MINUS_1(BN_bn2binpad(bn_.get(), buf, buf_len));

0 commit comments

Comments
 (0)