Skip to content

Commit b95e350

Browse files
authored
feat: consume pairing (#6)
1 parent 73b96e3 commit b95e350

File tree

5 files changed

+300
-93
lines changed

5 files changed

+300
-93
lines changed

src/bls/pairing.zig

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ const PairingPk = union(PTag) {
2020
p2: c.blst_p2_affine,
2121
};
2222

23-
const Pairing = struct {
23+
pub const Pairing = struct {
2424
v: []u8,
2525

26-
/// Rust always use a heap allocation here, but adding an allocator is too complex
27-
/// instead of that zig provide a buffer that's big enough for the struct to operate on so that:
26+
/// Rust always use a heap allocation here, but adding an allocator as param for Zig is too complex
27+
/// instead of that we provide a buffer that's big enough for the struct to operate on so that:
2828
/// - it does not have allocator in its api
2929
/// - can use stack allocation at consumer side
3030
/// - can reuse memory if it makes sense at consumer side
31-
pub fn new(buffer: []u8, hash_or_encode: bool, dst: []u8) PairingError!Pairing {
31+
pub fn new(buffer: []u8, hash_or_encode: bool, dst: []const u8) PairingError!Pairing {
3232
if (buffer.len < c.blst_pairing_sizeof()) {
3333
return PairingError.BufferTooSmall;
3434
}
@@ -37,10 +37,10 @@ const Pairing = struct {
3737
return PairingError.DstTooSmall;
3838
}
3939

40-
const obj = Pairing{
40+
var obj = Pairing{
4141
.v = buffer[0..c.blst_pairing_sizeof()],
4242
};
43-
obj.init(hash_or_encode, &dst[0]);
43+
obj.init(hash_or_encode, dst);
4444

4545
return obj;
4646
}
@@ -50,7 +50,7 @@ const Pairing = struct {
5050
return c.blst_pairing_sizeof();
5151
}
5252

53-
pub fn init(self: *Pairing, hash_or_encode: bool, dst: []u8) void {
53+
pub fn init(self: *Pairing, hash_or_encode: bool, dst: []const u8) void {
5454
c.blst_pairing_init(self.ctx(), hash_or_encode, &dst[0], dst.len);
5555
}
5656

@@ -64,51 +64,55 @@ const Pairing = struct {
6464
return ptr;
6565
}
6666

67-
pub fn aggregateG1(self: *Pairing, pk: *const c.blst_p1_affine, pk_validate: bool, sig: *const c.blst_p2_affine, sig_groupcheck: bool, msg: []u8, aug: ?[]u8) BLST_ERROR!void {
68-
const aug_ptr = if (aug != null and aug.len > 0) &aug[0] else null;
69-
const aug_len = if (aug != null) aug.len else null;
67+
pub fn aggregateG1(self: *Pairing, pk: *const c.blst_p1_affine, pk_validate: bool, sig: ?*const c.blst_p2_affine, sig_groupcheck: bool, msg: []const u8, aug: ?[]u8) BLST_ERROR!void {
68+
const aug_ptr = if (aug != null and aug.?.len > 0) &aug.?[0] else null;
69+
const aug_len = if (aug != null) aug.?.len else 0;
70+
const sig_ptr = if (sig != null) sig.? else null;
7071

71-
const res = c.blst_pairing_chk_n_aggr_pk_in_g1(self.ctx, pk, pk_validate, sig, sig_groupcheck, &msg[0], msg.len, aug_ptr, aug_len);
72+
const res = c.blst_pairing_chk_n_aggr_pk_in_g1(self.ctx(), pk, pk_validate, sig_ptr, sig_groupcheck, &msg[0], msg.len, aug_ptr, aug_len);
7273

7374
const err = toBlstError(res);
7475
if (err != null) {
75-
return err;
76+
return err.?;
7677
}
7778
}
7879

79-
pub fn aggregateG2(self: *Pairing, pk: *const c.blst_p2_affine, pk_validate: bool, sig: *const c.blst_p1_affine, sig_groupcheck: bool, msg: []u8, aug: ?[]u8) BLST_ERROR!void {
80-
const aug_ptr = if (aug != null and aug.len > 0) &aug[0] else null;
81-
const aug_len = if (aug != null) aug.len else null;
80+
pub fn aggregateG2(self: *Pairing, pk: *const c.blst_p2_affine, pk_validate: bool, sig: ?*const c.blst_p1_affine, sig_groupcheck: bool, msg: []u8, aug: ?[]u8) BLST_ERROR!void {
81+
const aug_ptr = if (aug != null and aug.?.len > 0) &aug.?[0] else null;
82+
const aug_len = if (aug != null) aug.?.len else null;
83+
const sig_ptr = if (sig != null) sig.? else null;
8284

83-
const res = c.blst_pairing_chk_n_aggr_pk_in_g2(self.ctx, pk, pk_validate, sig, sig_groupcheck, &msg[0], msg.len, aug_ptr, aug_len);
85+
const res = c.blst_pairing_chk_n_aggr_pk_in_g2(self.ctx(), pk, pk_validate, sig_ptr, sig_groupcheck, &msg[0], msg.len, aug_ptr, aug_len);
8486

8587
const err = toBlstError(res);
8688
if (err != null) {
87-
return err;
89+
return err.?;
8890
}
8991
}
9092

91-
pub fn mulAndAggregateG1(self: *Pairing, pk: *const c.blst_p1_affine, pk_validate: bool, sig: *const c.blst_p2_affine, sig_groupcheck: bool, scalar: []u8, nbits: usize, msg: []u8, aug: ?[]u8) BLST_ERROR!void {
92-
const aug_ptr = if (aug != null and aug.len > 0) &aug[0] else null;
93-
const aug_len = if (aug != null) aug.len else null;
93+
// TODO: msgs and scalar should have len > 0
94+
// check for other apis as well
95+
pub fn mulAndAggregateG1(self: *Pairing, pk: *const c.blst_p1_affine, pk_validate: bool, sig: *const c.blst_p2_affine, sig_groupcheck: bool, scalar: []const u8, nbits: usize, msg: []const u8, aug: ?[]u8) BLST_ERROR!void {
96+
const aug_ptr = if (aug != null and aug.?.len > 0) &aug.?[0] else null;
97+
const aug_len = if (aug != null) aug.?.len else 0;
9498

95-
const res = c.blst_pairing_chk_n_mul_n_aggr_pk_in_g1(self.ctx, pk, pk_validate, sig, sig_groupcheck, &scalar[0], nbits, &msg[0], msg.len, aug_ptr, aug_len);
99+
const res = c.blst_pairing_chk_n_mul_n_aggr_pk_in_g1(self.ctx(), pk, pk_validate, sig, sig_groupcheck, &scalar[0], nbits, &msg[0], msg.len, aug_ptr, aug_len);
96100

97101
const err = toBlstError(res);
98102
if (err != null) {
99-
return err;
103+
return err.?;
100104
}
101105
}
102106

103107
pub fn mulAndAggregateG2(self: *Pairing, pk: *const c.blst_p2_affine, pk_validate: bool, sig: *const c.blst_p1_affine, sig_groupcheck: bool, scalar: []u8, nbits: usize, msg: []u8, aug: ?[]u8) BLST_ERROR!void {
104-
const aug_ptr = if (aug != null and aug.len > 0) &aug[0] else null;
105-
const aug_len = if (aug != null) aug.len else null;
108+
const aug_ptr = if (aug != null and aug.?.len > 0) &aug.?[0] else null;
109+
const aug_len = if (aug != null) aug.?.len else 0;
106110

107111
const res = c.blst_pairing_chk_n_mul_n_aggr_pk_in_g2(self.ctx, pk, pk_validate, sig, sig_groupcheck, &scalar[0], nbits, &msg[0], msg.len, aug_ptr, aug_len);
108112

109113
const err = toBlstError(res);
110114
if (err != null) {
111-
return err;
115+
return err.?;
112116
}
113117
}
114118

@@ -149,14 +153,15 @@ const Pairing = struct {
149153

150154
test "init Pairing" {
151155
const allocator = std.testing.allocator;
152-
const buffer = allocator.alloc(u8, Pairing.sizeOf());
156+
const buffer = try allocator.alloc(u8, Pairing.sizeOf());
153157
defer allocator.free(buffer);
154158

155-
_ = try Pairing.new(buffer, true, "destination");
159+
const dst = "destination";
160+
_ = try Pairing.new(buffer, true, dst);
156161
}
157162

158163
test "sizeOf Pairing" {
159164
// this works on MacOS, adding this test to understand more about the size of Pairing
160165
std.debug.print("Size of Pairing: {}", .{Pairing.sizeOf()});
161-
std.testing.expectEqual(3192, Pairing.sizeOf());
166+
try std.testing.expectEqual(3192, Pairing.sizeOf());
162167
}

src/bls/public_key.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,15 @@ pub const AggregatePublicKey = struct {
115115
pub fn toPublicKey(self: *const AggregatePublicKey) PublicKey {
116116
var pk = PublicKey.default();
117117
c.blst_p1_to_affine(&pk.point, &self.point);
118+
return pk;
118119
}
119120

120121
// Aggregate
121-
pub fn aggregate(pks: []*const PublicKey, pks_validate: bool) BLST_ERROR!AggregatePublicKey {
122+
pub fn aggregate(pks: []const *PublicKey, pks_validate: bool) BLST_ERROR!AggregatePublicKey {
122123
if (pks.len == 0) {
123124
return BLST_ERROR.AGGR_TYPE_MISMATCH;
124125
}
125-
if (pks.validate) {
126+
if (pks_validate) {
126127
try pks[0].validate();
127128
}
128129

0 commit comments

Comments
 (0)