@@ -56,6 +56,86 @@ pub const SecretKey = struct {
5656 }
5757};
5858
59+ pub const PT = c .blst_fp12 ;
60+
61+ pub const Pairing = struct {
62+ ctx : []u64 = &[_ ]u64 {},
63+ allocator : std.mem.Allocator ,
64+
65+ pub fn init (hash_or_encode : bool , DST : []const u8 ,
66+ allocator : std.mem.Allocator ) ! Pairing {
67+ const nlimbs = (c .blst_pairing_sizeof () + @sizeOf (u64 ) - 1 ) / @sizeOf (u64 );
68+ const buffer = try allocator .alloc (u64 , nlimbs );
69+
70+ c .blst_pairing_init (@ptrCast (buffer ), hash_or_encode , & DST [0 ], DST .len );
71+
72+ return Pairing {
73+ .ctx = buffer ,
74+ .allocator = allocator ,
75+ };
76+ }
77+
78+ pub fn deinit (self : * Pairing ) void {
79+ self .allocator .free (self .ctx );
80+ self .ctx = &[_ ]u64 {};
81+ }
82+
83+ pub fn aggregate (self : * Pairing , pk : anytype , sig : anytype ,
84+ msg : []const u8 , aug : ? []const u8 ) ERROR {
85+ const opt = aug orelse &[_ ]u8 {};
86+ var err : c.BLST_ERROR = undefined ;
87+
88+ switch (@TypeOf (pk )) {
89+ * const P1_Affine , * P1_Affine = > {
90+ const sigp : [* c ]const c.blst_p2_affine = switch (@TypeOf (sig )) {
91+ @TypeOf (null ) = > null ,
92+ else = > & sig .point ,
93+ };
94+ err = c .blst_pairing_aggregate_pk_in_g1 (@ptrCast (self .ctx ),
95+ & pk .point , sigp ,
96+ @ptrCast (msg ), msg .len ,
97+ @ptrCast (opt ), opt .len );
98+ },
99+ * const P2_Affine , * P2_Affine = > {
100+ const sigp : [* c ]const c.blst_p1_affine = switch (@TypeOf (sig )) {
101+ @TypeOf (null ) = > null ,
102+ else = > & sig .point ,
103+ };
104+ err = c .blst_pairing_aggregate_pk_in_g2 (@ptrCast (self .ctx ),
105+ & pk .point , sigp ,
106+ @ptrCast (msg ), msg .len ,
107+ @ptrCast (opt ), opt .len );
108+ },
109+ else = > | T | @compileError ("expected type '*const blst.P1_Affine' "
110+ ++ "or '*const blst.P2_Affine', found '"
111+ ++ @typeName (T ) ++ "'" ),
112+ }
113+
114+ return @as (ERROR , @enumFromInt (err ));
115+ }
116+
117+ pub fn commit (self : * Pairing ) void {
118+ c .blst_pairing_commit (@ptrCast (self .ctx ));
119+ }
120+
121+ pub fn merge (self : * Pairing , second : * const Pairing ) ERROR {
122+ return c .blst_pairing_merge (@ptrCast (self .ctx ), @ptrCast (second .ctx ));
123+ }
124+
125+ pub fn finalverify (self : * Pairing , optional : ? * const PT ) bool {
126+ return c .blst_pairing_finalverify (@ptrCast (self .ctx ), optional );
127+ }
128+
129+ pub fn raw_aggregate (self : * Pairing , q : * const P2_Affine ,
130+ p : * const P1_Affine ) void {
131+ c .blst_pairing_raw_aggregate (@ptrCast (self .ctx ), q , p );
132+ }
133+
134+ pub fn as_fp12 (self : * Pairing ) * const PT {
135+ return c .blst_pairing_as_fp12 (@ptrCast (self .ctx ));
136+ }
137+ };
138+
59139const FP_BYTES = 384 / 8 ;
60140pub const P1_COMPRESS_BYTES = FP_BYTES ;
61141pub const P1_SERIALIZE_BYTES = FP_BYTES * 2 ;
@@ -79,20 +159,20 @@ pub const P1_Affine = struct {
79159
80160 var ret : P1_Affine = undefined ;
81161 const err = ret .deserialize (in );
82- return if (err == ERROR .SUCCESS ) ret else err .as_error ();
162+ return if (err == .SUCCESS ) ret else err .as_error ();
83163 },
84164 }
85165 unreachable ;
86166 }
87167
88168 pub fn deserialize (self : * P1_Affine , in : []const u8 ) ERROR {
89169 if (in .len == 0 ) {
90- return ERROR .BAD_ENCODING ;
170+ return .BAD_ENCODING ;
91171 }
92172 const expected = @as (usize , if (in [0 ]&0x80 != 0 ) P1_COMPRESS_BYTES
93173 else P1_SERIALIZE_BYTES );
94174 if (in .len != expected ) {
95- return ERROR .BAD_ENCODING ;
175+ return .BAD_ENCODING ;
96176 }
97177 const err = c .blst_p1_deserialize (& self .point , & in [0 ]);
98178 return @as (ERROR , @enumFromInt (err ));
@@ -176,20 +256,20 @@ pub const P1 = struct {
176256
177257 var ret : P1 = undefined ;
178258 const err = ret .deserialize (in );
179- return if (err == ERROR .SUCCESS ) ret else err .as_error ();
259+ return if (err == .SUCCESS ) ret else err .as_error ();
180260 },
181261 }
182262 unreachable ;
183263 }
184264
185265 pub fn deserialize (self : * P1 , in : []const u8 ) ERROR {
186266 if (in .len == 0 ) {
187- return ERROR .BAD_ENCODING ;
267+ return .BAD_ENCODING ;
188268 }
189269 const expected = @as (usize , if (in [0 ]&0x80 != 0 ) P1_COMPRESS_BYTES
190270 else P1_SERIALIZE_BYTES );
191271 if (in .len != expected ) {
192- return ERROR .BAD_ENCODING ;
272+ return .BAD_ENCODING ;
193273 }
194274 const err = c .blst_p1_deserialize (@ptrCast (& self .point ), & in [0 ]);
195275 if (err == c .BLST_SUCCESS ) {
@@ -217,9 +297,7 @@ pub const P1 = struct {
217297 }
218298
219299 pub fn dup (self : * const P1 ) P1 {
220- return P1 {
221- .point = self .point ,
222- };
300+ return self .* ;
223301 }
224302
225303 pub fn on_curve (self : * const P1 ) bool {
@@ -300,20 +378,20 @@ pub const P2_Affine = struct {
300378
301379 var ret : P2_Affine = undefined ;
302380 const err = ret .deserialize (in );
303- return if (err == ERROR .SUCCESS ) ret else err .as_error ();
381+ return if (err == .SUCCESS ) ret else err .as_error ();
304382 },
305383 }
306384 unreachable ;
307385 }
308386
309387 pub fn deserialize (self : * P2_Affine , in : []const u8 ) ERROR {
310388 if (in .len == 0 ) {
311- return ERROR .BAD_ENCODING ;
389+ return .BAD_ENCODING ;
312390 }
313391 const expected = @as (usize , if (in [0 ]&0x80 != 0 ) P2_COMPRESS_BYTES
314392 else P2_SERIALIZE_BYTES );
315393 if (in .len != expected ) {
316- return ERROR .BAD_ENCODING ;
394+ return .BAD_ENCODING ;
317395 }
318396 const err = c .blst_p2_deserialize (& self .point , & in [0 ]);
319397 return @as (ERROR , @enumFromInt (err ));
@@ -397,20 +475,20 @@ pub const P2 = struct {
397475
398476 var ret : P2 = undefined ;
399477 const err = ret .deserialize (in );
400- return if (err == ERROR .SUCCESS ) ret else err .as_error ();
478+ return if (err == .SUCCESS ) ret else err .as_error ();
401479 },
402480 }
403481 unreachable ;
404482 }
405483
406484 pub fn deserialize (self : * P2 , in : []const u8 ) ERROR {
407485 if (in .len == 0 ) {
408- return ERROR .BAD_ENCODING ;
486+ return .BAD_ENCODING ;
409487 }
410488 const expected = @as (usize , if (in [0 ]&0x80 != 0 ) P2_COMPRESS_BYTES
411489 else P2_SERIALIZE_BYTES );
412490 if (in .len != expected ) {
413- return ERROR .BAD_ENCODING ;
491+ return .BAD_ENCODING ;
414492 }
415493 const err = c .blst_p2_deserialize (@ptrCast (& self .point ), & in [0 ]);
416494 if (err == c .BLST_SUCCESS ) {
@@ -438,9 +516,7 @@ pub const P2 = struct {
438516 }
439517
440518 pub fn dup (self : * const P2 ) P2 {
441- return P2 {
442- .point = self .point ,
443- };
519+ return self .* ;
444520 }
445521
446522 pub fn on_curve (self : * const P2 ) bool {
0 commit comments