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

Fix pairing with operator& #386

Merged
merged 1 commit into from
Jun 9, 2023
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
4 changes: 2 additions & 2 deletions python-bindings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_schemes():
pk2 = sk2.get_g1()

g1 = G1Element.from_message(b"abcd", b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_AUG_")
# Fix this! assert bytes(g1) == bytes.fromhex("a5f756594a96c55f302360378568378dc19ea5eae3d5a88d77b8a30bb25c25ce24a85c6d7c851bcb1e34064fc0c79383")
assert bytes(g1) == bytes.fromhex("a5f756594a96c55f302360378568378dc19ea5eae3d5a88d77b8a30bb25c25ce24a85c6d7c851bcb1e34064fc0c79383")

g2 = G2Element.from_message(b"abcd", b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_AUG_")
assert g2 == AugSchemeMPL.g2_from_message(b"abcd")
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_schemes():
pair2 = pk2.pair(Scheme.g2_from_message(aug_msg2))
pair = pair1 * pair2
agg_sig_pair = G1Element.generator().pair(agg_sig)
# fix this assert pair == agg_sig_pair
assert pair == agg_sig_pair

# HD keys
child = Scheme.derive_child_sk(sk1, 123)
Expand Down
47 changes: 33 additions & 14 deletions src/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ G1Element G1Element::FromMessage(
const byte* aug = nullptr;
size_t aug_len = 0;

blst_encode_to_g1(
blst_hash_to_g1(
&(ans.p),
message.begin(),
(int)message.size(),
Expand Down Expand Up @@ -278,7 +278,7 @@ G2Element G2Element::FromMessage(
const byte* aug = nullptr;
size_t aug_len = 0;

blst_encode_to_g2(
blst_hash_to_g2(
&(ans.q),
message.begin(),
(int)message.size(),
Expand Down Expand Up @@ -388,11 +388,18 @@ G2Element operator*(const blst_scalar& k, const G2Element& a) { return a * k; }

const size_t GTElement::SIZE;

/*
* Currently deserliazation is not available - these are currently
* broken and just return the zero element
*/
GTElement GTElement::FromBytes(Bytes const bytes)
{
GTElement ele = GTElement::FromBytesUnchecked(bytes);
if (!blst_fp12_in_group(&(ele.r)))
throw std::invalid_argument("GTElement is invalid");
//
// this doesn't seem to be the proper check as it doesn't work as expeced
//
// if (!blst_fp12_in_group(&(ele.r)))
// throw std::invalid_argument("GTElement is invalid");
return ele;
}

Expand All @@ -402,7 +409,7 @@ GTElement GTElement::FromBytesUnchecked(Bytes const bytes)
throw std::invalid_argument("GTElement::FromBytes: Invalid size");
}
GTElement ele = GTElement();
// wjb gt_read_bin(ele.r, bytes.begin(), GTElement::SIZE);
// TO DO blst_fp12_from_bendian(&(ele.r), bytes.begin());
return ele;
}

Expand All @@ -414,7 +421,21 @@ GTElement GTElement::FromByteVector(const std::vector<uint8_t>& bytevec)
GTElement GTElement::FromNative(const blst_fp12* element)
{
GTElement ele = GTElement();
memcpy(&(ele.r), element, sizeof(blst_fp12));
ele.r = *element;
return ele;
}

GTElement GTElement::FromAffine(const blst_p1_affine& affine)
{
GTElement ele = GTElement();
blst_aggregated_in_g1(&ele.r, &affine);
return ele;
}

GTElement GTElement::FromAffine(const blst_p2_affine& affine)
{
GTElement ele = GTElement();
blst_aggregated_in_g2(&ele.r, &affine);
return ele;
}

Expand All @@ -441,18 +462,16 @@ GTElement operator&(const G1Element& a, const G2Element& b)
{
blst_fp12 ans;

blst_p1 p1;
blst_p2 p2;
a.ToNative(&p1);
b.ToNative(&p2);

blst_p1_affine aff1;
blst_p1_to_affine(&aff1, &p1);
blst_p2_affine aff2;
blst_p2_to_affine(&aff2, &p2);
a.ToAffine(&aff1);
b.ToAffine(&aff2);

blst_miller_loop(&ans, &aff2, &aff1);
blst_final_exp(&ans, &ans);

GTElement ret = GTElement::FromNative(&ans);

return ret;
}

Expand All @@ -465,7 +484,7 @@ GTElement operator*(GTElement& a, GTElement& b)

void GTElement::Serialize(uint8_t* buffer) const
{
// wjb gt_write_bin(buffer, GTElement::SIZE, *(blst_fp12 *)&r, 1);
blst_bendian_from_fp12(buffer, &r);
}

std::vector<uint8_t> GTElement::Serialize() const
Expand Down
6 changes: 4 additions & 2 deletions src/elements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ class G2Element {

class GTElement {
public:
static const size_t SIZE = 384;
static const size_t SIZE = 576;

static GTElement FromBytes(Bytes bytes);
static GTElement FromBytesUnchecked(Bytes bytes);
static GTElement FromByteVector(const std::vector<uint8_t> &bytevec);
static GTElement FromNative(const blst_fp12 *element);
static GTElement FromAffine(const blst_p1_affine &element);
static GTElement FromAffine(const blst_p2_affine &element);
static GTElement Unity(); // unity

void Serialize(uint8_t *buffer) const;
Expand All @@ -131,7 +134,6 @@ class GTElement {
friend bool operator!=(GTElement const &a, GTElement const &b);
friend std::ostream &operator<<(std::ostream &os, const GTElement &s);
friend GTElement operator*(GTElement &a, GTElement &b);
GTElement &operator=(const GTElement &rhs);

private:
blst_fp12 r;
Expand Down