Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(math): add range check to string conversion methods #389

Merged
merged 2 commits into from
Apr 16, 2024
Merged
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
13 changes: 11 additions & 2 deletions tachyon/math/base/big_int.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@ namespace {
template <size_t Base>
bool DoStringToLimbs(std::string_view str, uint64_t* limbs, size_t limb_nums) {
mpz_class out;
if (!gmp::ParseIntoMpz(str, Base, &out)) return false;
if (gmp::GetLimbSize(out) > limb_nums) return false;
if (!gmp::ParseIntoMpz(str, Base, &out)) {
LOG(ERROR) << "failed to parse string(" << str << ") with base(" << Base
<< ")";
return false;
}
size_t actual_limb_nums = gmp::GetLimbSize(out);
if (actual_limb_nums > limb_nums) {
LOG(ERROR) << "actual limb nums(" << actual_limb_nums
<< ") is greater than expected limb nums(" << limb_nums << ")";
return false;
}
gmp::CopyLimbs(out, limbs);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,19 @@ class PrimeField<_Config, std::enable_if_t<_Config::%{flag}>> final
constexpr static std::optional<PrimeField> FromDecString(std::string_view str) {
std::optional<BigInt<N>> value = BigInt<N>::FromDecString(str);
if (!value.has_value()) return std::nullopt;
if (value >= Config::kModulus) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeField(std::move(value).value());
}
constexpr static std::optional<PrimeField> FromHexString(std::string_view str) {
std::optional<BigInt<N>> value = BigInt<N>::FromHexString(str);
if (!value.has_value()) return std::nullopt;
if (value >= Config::kModulus) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeField(std::move(value).value());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ template <typename Config>
std::optional<CLASS> CLASS::FromDecString(std::string_view str) {
uint64_t value = 0;
if (!base::StringToUint64(str, &value)) return std::nullopt;
if (value >= Config::kModulus[0]) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeField(::Goldilocks::fromU64(value).fe);
}

Expand All @@ -42,6 +46,10 @@ template <typename Config>
std::optional<CLASS> CLASS::FromHexString(std::string_view str) {
uint64_t value = 0;
if (!base::HexStringToUint64(str, &value)) return std::nullopt;
if (value >= Config::kModulus[0]) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeField(::Goldilocks::fromU64(value).fe);
}

Expand Down
8 changes: 8 additions & 0 deletions tachyon/math/finite_fields/prime_field_generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,20 @@ class PrimeField<_Config, std::enable_if_t<!_Config::kIsSpecialPrime>> final
std::string_view str) {
std::optional<BigInt<N>> value = BigInt<N>::FromDecString(str);
if (!value.has_value()) return std::nullopt;
if (value >= Config::kModulus) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeField(std::move(value).value());
}
constexpr static std::optional<PrimeField> FromHexString(
std::string_view str) {
std::optional<BigInt<N>> value = BigInt<N>::FromHexString(str);
if (!value.has_value()) return std::nullopt;
if (value >= Config::kModulus) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeField(std::move(value).value());
}

Expand Down
8 changes: 8 additions & 0 deletions tachyon/math/finite_fields/prime_field_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ class PrimeFieldGpu final : public PrimeFieldBase<PrimeFieldGpu<_Config>> {
constexpr static PrimeFieldGpu FromDecString(std::string_view str) {
std::optional<BigInt<N>> value = BigInt<N>::FromDecString(str);
if (!value.has_value()) return std::nullopt;
if (value >= Config::kModulus) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeFieldGpu(std::move(value).value());
}
constexpr static PrimeFieldGpu FromHexString(std::string_view str) {
std::optional<BigInt<N>> value = BigInt<N>::FromHexString(str);
if (!value.has_value()) return std::nullopt;
if (value >= Config::kModulus) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeFieldGpu(std::move(value).value());
}

Expand Down
8 changes: 8 additions & 0 deletions tachyon/math/finite_fields/prime_field_gpu_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,19 @@ class PrimeFieldGpuDebug final
constexpr static PrimeFieldGpuDebug FromDecString(std::string_view str) {
std::optional<BigInt<N>> value = BigInt<N>::FromDecString(str);
if (!value.has_value()) return std::nullopt;
if (value >= Config::kModulus) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeFieldGpuDebug(std::move(value).value());
}
constexpr static PrimeFieldGpuDebug FromHexString(std::string_view str) {
std::optional<BigInt<N>> value = BigInt<N>::FromHexString(str);
if (!value.has_value()) return std::nullopt;
if (value >= Config::kModulus) {
LOG(ERROR) << "value(" << str << ") is greater than or equal to modulus";
return std::nullopt;
}
return PrimeFieldGpuDebug(std::move(value).value());
}

Expand Down
Loading