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

Support compressed and uncompressed point serialization #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
80 changes: 43 additions & 37 deletions libff/algebra/curves/alt_bn128/alt_bn128_g1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,31 +405,51 @@ alt_bn128_G1 alt_bn128_G1::random_element()
return (scalar_field::random_element().as_bigint()) * G1_one;
}

std::ostream& operator<<(std::ostream &out, const alt_bn128_G1 &g)
void alt_bn128_G1::write_uncompressed(std::ostream &out) const
{
alt_bn128_G1 copy(g);
alt_bn128_G1 copy(*this);
copy.to_affine_coordinates();

out << (copy.is_zero() ? 1 : 0) << OUTPUT_SEPARATOR;
#ifdef NO_PT_COMPRESSION
out << copy.X << OUTPUT_SEPARATOR << copy.Y;
#else
}

void alt_bn128_G1::write_compressed(std::ostream &out) const
{
alt_bn128_G1 copy(*this);
copy.to_affine_coordinates();

out << (copy.is_zero() ? 1 : 0) << OUTPUT_SEPARATOR;
/* storing LSB of Y */
out << copy.X << OUTPUT_SEPARATOR << (copy.Y.as_bigint().data[0] & 1);
#endif

return out;
}

std::istream& operator>>(std::istream &in, alt_bn128_G1 &g)
void alt_bn128_G1::read_uncompressed(std::istream &in, alt_bn128_G1 &g)
{
char is_zero;
alt_bn128_Fq tX, tY;

#ifdef NO_PT_COMPRESSION
in >> is_zero >> tX >> tY;
is_zero -= '0';
#else

// using Jacobian coordinates
if (!is_zero)
{
g.X = tX;
g.Y = tY;
g.Z = alt_bn128_Fq::one();
Copy link
Collaborator

@AntoineRondelet AntoineRondelet Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we check that the point is on the curve first?
Seems to me that the deserialization logic of a point in the affine space should be:

  1. Recover x, y
  2. Check that the tuple (x, y) is on the curve, i.e. satisfies the curve equation
  3. Either do the "safe subgroup membership test" or mult by cofactor to make sure we have a point in G1 and not a point (which may be of low order) in the E/Fq group

Do I miss something? (Note: this code initially originates from the library, and has just been moved around in this PR - so this question applies to the current state of the library, as in master, for the deserialization of uncompressed points)

Copy link
Collaborator

@AntoineRondelet AntoineRondelet Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, it seems to me that, the same assumption is made in the deserialization of compressed points, since I can read here that the sqrt function does not terminate if the element is a quadratic non-residue in the field: https://github.com/scipr-lab/libff/blob/master/libff/algebra/fields/fp.hpp#L108

We can "skip" the bullet 3. above here since cofactor = 1, but why skipping bullet 2.? I'd be in favor to test for quadratic residuity using Euler's criterion (and the euler exponent defined here: https://github.com/scipr-lab/libff/blob/master/libff/algebra/fields/fp.hpp#L54) first, and only proceed if the test is valid.

These functions act as I/O with the backend of the library, so I would be in favor to make no assumption about their input, and the parsed points.
As such, I would be in favor to add these checks. @dtebbs @madars @ValarDragon thoughts?
This is related to #50

}
else
{
g = alt_bn128_G1::zero();
}
}

void alt_bn128_G1::read_compressed(std::istream &in, alt_bn128_G1 &g)
{
char is_zero;
alt_bn128_Fq tX, tY;

in.read((char*)&is_zero, 1); // this reads is_zero;
is_zero -= '0';
consume_OUTPUT_SEPARATOR(in);
Expand All @@ -452,7 +472,7 @@ std::istream& operator>>(std::istream &in, alt_bn128_G1 &g)
tY = -tY;
}
}
#endif

// using Jacobian coordinates
if (!is_zero)
{
Expand All @@ -464,39 +484,25 @@ std::istream& operator>>(std::istream &in, alt_bn128_G1 &g)
{
g = alt_bn128_G1::zero();
}

return in;
}

std::ostream& operator<<(std::ostream& out, const std::vector<alt_bn128_G1> &v)
std::ostream& operator<<(std::ostream &out, const alt_bn128_G1 &g)
{
out << v.size() << "\n";
for (const alt_bn128_G1& t : v)
{
out << t << OUTPUT_NEWLINE;
}

#ifdef NO_PT_COMPRESSION
g.write_uncompressed(out);
#else
g.write_compressed(out);
#endif
return out;
}

std::istream& operator>>(std::istream& in, std::vector<alt_bn128_G1> &v)
std::istream& operator>>(std::istream &in, alt_bn128_G1 &g)
{
v.clear();

size_t s;
in >> s;
consume_newline(in);

v.reserve(s);

for (size_t i = 0; i < s; ++i)
{
alt_bn128_G1 g;
in >> g;
consume_OUTPUT_NEWLINE(in);
v.emplace_back(g);
}

#ifdef NO_PT_COMPRESSION
alt_bn128_G1::read_uncompressed(in, g);
#else
alt_bn128_G1::read_compressed(in, g);
#endif
return in;
}

Expand Down
9 changes: 4 additions & 5 deletions libff/algebra/curves/alt_bn128/alt_bn128_g1.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ class alt_bn128_G1 {
static bigint<base_field::num_limbs> base_field_char() { return base_field::field_char(); }
static bigint<scalar_field::num_limbs> order() { return scalar_field::field_char(); }

friend std::ostream& operator<<(std::ostream &out, const alt_bn128_G1 &g);
friend std::istream& operator>>(std::istream &in, alt_bn128_G1 &g);
void write_uncompressed(std::ostream &) const;
void write_compressed(std::ostream &) const;
static void read_uncompressed(std::istream &, alt_bn128_G1 &);
static void read_compressed(std::istream &, alt_bn128_G1 &);

static void batch_to_special_all_non_zeros(std::vector<alt_bn128_G1> &vec);
};
Expand All @@ -87,8 +89,5 @@ alt_bn128_G1 operator*(const Fp_model<m,modulus_p> &lhs, const alt_bn128_G1 &rhs
return scalar_mul<alt_bn128_G1, m>(rhs, lhs.as_bigint());
}

std::ostream& operator<<(std::ostream& out, const std::vector<alt_bn128_G1> &v);
std::istream& operator>>(std::istream& in, std::vector<alt_bn128_G1> &v);

} // libff
#endif // ALT_BN128_G1_HPP_
57 changes: 46 additions & 11 deletions libff/algebra/curves/alt_bn128/alt_bn128_g2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,30 +419,48 @@ alt_bn128_G2 alt_bn128_G2::random_element()
return (alt_bn128_Fr::random_element().as_bigint()) * G2_one;
}

std::ostream& operator<<(std::ostream &out, const alt_bn128_G2 &g)
void alt_bn128_G2::write_uncompressed(std::ostream &out) const
{
alt_bn128_G2 copy(g);
alt_bn128_G2 copy(*this);
copy.to_affine_coordinates();
out << (copy.is_zero() ? 1 : 0) << OUTPUT_SEPARATOR;
#ifdef NO_PT_COMPRESSION
out << copy.X << OUTPUT_SEPARATOR << copy.Y;
#else
}

void alt_bn128_G2::write_compressed(std::ostream &out) const
{
alt_bn128_G2 copy(*this);
copy.to_affine_coordinates();
out << (copy.is_zero() ? 1 : 0) << OUTPUT_SEPARATOR;
/* storing LSB of Y */
out << copy.X << OUTPUT_SEPARATOR << (copy.Y.c0.as_bigint().data[0] & 1);
#endif

return out;
}

std::istream& operator>>(std::istream &in, alt_bn128_G2 &g)
void alt_bn128_G2::read_uncompressed(std::istream &in, alt_bn128_G2 &g)
{
char is_zero;
alt_bn128_Fq2 tX, tY;

#ifdef NO_PT_COMPRESSION
in >> is_zero >> tX >> tY;
is_zero -= '0';
#else
// using projective coordinates
if (!is_zero)
{
g.X = tX;
g.Y = tY;
g.Z = alt_bn128_Fq2::one();
}
else
{
g = alt_bn128_G2::zero();
}
}

void alt_bn128_G2::read_compressed(std::istream &in, alt_bn128_G2 &g)
{
char is_zero;
alt_bn128_Fq2 tX, tY;

in.read((char*)&is_zero, 1); // this reads is_zero;
is_zero -= '0';
consume_OUTPUT_SEPARATOR(in);
Expand All @@ -465,7 +483,6 @@ std::istream& operator>>(std::istream &in, alt_bn128_G2 &g)
tY = -tY;
}
}
#endif
// using projective coordinates
if (!is_zero)
{
Expand All @@ -477,7 +494,25 @@ std::istream& operator>>(std::istream &in, alt_bn128_G2 &g)
{
g = alt_bn128_G2::zero();
}
}

std::ostream& operator<<(std::ostream &out, const alt_bn128_G2 &g)
{
#ifdef NO_PT_COMPRESSION
g.write_uncompressed(out);
#else
g.write_compressed(out);
#endif
return out;
}

std::istream& operator>>(std::istream &in, alt_bn128_G2 &g)
{
#ifdef NO_PT_COMPRESSION
alt_bn128_G2::read_uncompressed(in, g);
#else
alt_bn128_G2::read_compressed(in, g);
#endif
return in;
}

Expand Down
6 changes: 4 additions & 2 deletions libff/algebra/curves/alt_bn128/alt_bn128_g2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ class alt_bn128_G2 {
static bigint<base_field::num_limbs> base_field_char() { return base_field::field_char(); }
static bigint<scalar_field::num_limbs> order() { return scalar_field::field_char(); }

friend std::ostream& operator<<(std::ostream &out, const alt_bn128_G2 &g);
friend std::istream& operator>>(std::istream &in, alt_bn128_G2 &g);
void write_uncompressed(std::ostream &) const;
void write_compressed(std::ostream &) const;
static void read_uncompressed(std::istream &, alt_bn128_G2 &);
static void read_compressed(std::istream &, alt_bn128_G2 &);

static void batch_to_special_all_non_zeros(std::vector<alt_bn128_G2> &vec);
};
Expand Down
Loading