Skip to content

Commit

Permalink
fix Fp on neg input
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiqiNs committed Oct 31, 2024
1 parent 85324ea commit 23f9f0b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ void Field::mod(Fp& x) const{

Fp Field::from_int(const int& x) const{
Fp r;
bn_set_dig(r.value, x);
mod(r);

// Check if the input integer is negative.
if (x >= 0){
bn_set_dig(r.value, x);
mod(r);
} else{
bn_set_dig(r.value, -x);
mod(r);
r = neg(r);
}

return r;
}

Expand Down
15 changes: 15 additions & 0 deletions tests/test_field.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
#include <gtest/gtest.h>
#include "bp.hpp"

TEST(FieldElementTests, FromInt){
// Initialize the scheme.
BP::init();

// Create a field.
const Field Zp(11);

// Perform testing.
const Fp x = Zp.from_int(-17);

// Test and then clear the core.
EXPECT_TRUE(Field::cmp(x, 5));
BP::close();
}

TEST(FieldElementTests, Add){
// Initialize the scheme.
BP::init();
Expand Down

0 comments on commit 23f9f0b

Please sign in to comment.