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: equal() for PublicKey and Signature #9

Merged
merged 2 commits into from
Dec 20, 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
74 changes: 69 additions & 5 deletions src/sig_variant.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub fn createSigVariant(
hash_or_encode: bool,
hash_or_encode_to_fn: anytype,
sign_fn: anytype,
// pk_eq_fn: anytype,
// sig_eq_fn: anytype,
pk_eq_fn: anytype,
sig_eq_fn: anytype,
verify_fn: anytype,
pk_in_group_fn: anytype,
pk_to_aff_fn: anytype,
Expand Down Expand Up @@ -185,7 +185,12 @@ pub fn createSigVariant(
return self.compress();
}

// TODO: Eq, PartialEq, Serialize, Deserialize?
pub fn isEqual(self: *const @This(), other: *const @This()) bool {
return pk_eq_fn(&self.point, &other.point);
}

// TODO: PartialEq, Serialize, Deserialize?

};

const AggregatePublicKey = struct {
Expand Down Expand Up @@ -413,7 +418,7 @@ pub fn createSigVariant(
}

pub fn deserialize(sig_in: []const u8) BLST_ERROR!@This() {
if ((sig_in.len == sig_ser_size and (sig_in[0] & 0x80) == 0) or (sig_in.len == sig_comp_size and sig_in[0] & 0x80) != 0) {
if ((sig_in.len == sig_ser_size and (sig_in[0] & 0x80) == 0) or (sig_in.len == sig_comp_size and sig_in[0] & 0x80 != 0)) {
var sig = @This().default();
const res = sig_deser_fn(&sig.point, &sig_in[0]);
const err = toBlstError(res);
Expand All @@ -438,7 +443,9 @@ pub fn createSigVariant(
return sig_in_group_fn(&self.point);
}

// TODO: Eq PartialEq, Serialize, Deserialize?
pub fn isEqual(self: *const @This(), other: *const @This()) bool {
return sig_eq_fn(&self.point, &other.point);
}
};

const AggregateSignature = struct {
Expand Down Expand Up @@ -938,6 +945,63 @@ pub fn createSigVariant(
}
}

pub fn testSerialization() !void {
var rng = std.rand.DefaultPrng.init(12345);
const sk = getRandomKey(&rng);
const sk2 = getRandomKey(&rng);

const pk = sk.skToPk();
const pk_comp = pk.compress();
const pk_ser = pk.serialize();

const pk_uncomp = try PublicKey.uncompress(pk_comp[0..]);
try std.testing.expect(pk_uncomp.isEqual(&pk));

const pk_deser = try PublicKey.deserialize(pk_ser[0..]);
try std.testing.expect(pk_deser.isEqual(&pk));

const pk2 = sk2.skToPk();
const pk_comp2 = pk2.compress();
const pk_ser2 = pk2.serialize();

const pk_uncomp2 = try PublicKey.uncompress(pk_comp2[0..]);
try std.testing.expect(pk_uncomp2.isEqual(&pk2));

const pk_deser2 = try PublicKey.deserialize(pk_ser2[0..]);
try std.testing.expect(pk_deser2.isEqual(&pk2));

try std.testing.expect(!pk.isEqual(&pk2));
try std.testing.expect(!pk_uncomp.isEqual(&pk2));
try std.testing.expect(!pk_deser.isEqual(&pk2));
try std.testing.expect(!pk_uncomp2.isEqual(&pk));
try std.testing.expect(!pk_deser2.isEqual(&pk));
}

pub fn testSerde() !void {
var rng = std.rand.DefaultPrng.init(12345);
const sk = getRandomKey(&rng);
const pk = sk.skToPk();
const sig = sk.sign("asdf", "qwer", "zxcv");
try sig.verify(true, "asdf", "qwer", "zxcv", &pk, true);

// roundtrip through serde. TODO: do this in Zig
const pk_ser = pk.serialize();
const sig_ser = sig.serialize();
const pk_des = try PublicKey.deserialize(pk_ser[0..]);
const sig_des = try Signature.deserialize(sig_ser[0..]);

try std.testing.expect(pk.isEqual(&pk_des));
try std.testing.expect(sig.isEqual(&sig_des));

try sig.verify(true, "asdf", "qwer", "zxcv", &pk_des, true);
try sig_des.verify(true, "asdf", "qwer", "zxcv", &pk, true);

const sk_ser = sk.serialize();
const sk_des = try SecretKey.deserialize(sk_ser[0..]);
const sig2 = sk_des.sign("asdf", "qwer", "zxcv");
try std.testing.expect(sig.isEqual(&sig2));
}

fn getRandomKey(rng: *Xoshiro256) SecretKey {
var value: [32]u8 = [_]u8{0} ** 32;
rng.random().bytes(value[0..]);
Expand Down
14 changes: 11 additions & 3 deletions src/sig_variant_min_pk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const SigVariant = createSigVariant(
true,
c.blst_hash_to_g2,
c.blst_sign_pk2_in_g1,
// c.blst_p1_affine_is_equal,
// c.blst_p2_affine_is_equal,
c.blst_p1_affine_is_equal,
c.blst_p2_affine_is_equal,
c.blst_core_verify_pk_in_g1,
c.blst_p1_affine_in_g1,
c.blst_p1_to_affine,
Expand Down Expand Up @@ -74,4 +74,12 @@ test "test_multiple_agg_sigs" {
try SigVariant.testMultipleAggSigs();
}

// TODO test_serialization, test_serde, test_multi_point
test "test_serialization" {
try SigVariant.testSerialization();
}

test "test_serde" {
try SigVariant.testSerde();
}

// TODO test_multi_point
14 changes: 11 additions & 3 deletions src/sig_variant_min_sig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const SigVariant = createSigVariant(
true,
c.blst_hash_to_g1,
c.blst_sign_pk2_in_g2,
// c.blst_p2_affine_is_equal,
// c.blst_p1_affine_is_equal,
c.blst_p2_affine_is_equal,
c.blst_p1_affine_is_equal,
c.blst_core_verify_pk_in_g2,
c.blst_p2_affine_in_g2,
c.blst_p2_to_affine,
Expand Down Expand Up @@ -74,4 +74,12 @@ test "test_multiple_agg_sigs" {
try SigVariant.testMultipleAggSigs();
}

// TODO test_serialization, test_serde, test_multi_point
test "test_serialization" {
try SigVariant.testSerialization();
}

test "test_serde" {
try SigVariant.testSerde();
}

// TODO test_multi_point
Loading