Skip to content

Commit e107b5c

Browse files
Use bls types in args instead of data (#2)
1 parent d2dd70f commit e107b5c

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

Sources/ObjCBLS/Include/ObjCBLS.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#import <Foundation/Foundation.h>
22

3+
NS_ASSUME_NONNULL_BEGIN
4+
35
@interface G1Element: NSObject
46

57
+ (size_t)SIZE;
@@ -18,6 +20,8 @@
1820

1921
@interface G2Element: NSObject
2022

23+
+ (size_t)SIZE;
24+
2125
- (G2Element *)init;
2226

2327
+ (G2Element *)from_bytes:(NSData *)b;
@@ -44,8 +48,10 @@
4448
+ (G2Element *)aggregate:(NSArray<G2Element *> *)signatures;
4549
+ (G2Element *)sign:(PrivateKey *)pk msg:(NSData *)msg;
4650

47-
+ (BOOL)verify:(NSData *)pk msg:(NSData *)msg sig:(NSData *)sig;
51+
+ (BOOL)verify:(G1Element *)pk msg:(NSData *)msg sig:(G2Element *)sig;
4852
+ (BOOL)aggregate_verify:(NSArray<G1Element *> *)pks msgs:(NSArray<NSData *> *)msgs sig:(G2Element *)sig;
4953

5054
@end
5155

56+
NS_ASSUME_NONNULL_END
57+

Sources/ObjCBLS/ObjCBLS.mm

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,7 @@ - (G1Element *)get_g1
3939

4040
- (NSData *)get_bytes
4141
{
42-
uint8_t *output =
43-
bls::Util::SecAlloc<uint8_t>(bls::PrivateKey::PRIVATE_KEY_SIZE);
44-
45-
bls::PrivateKey k = bls::PrivateKey::FromBytes(bls::Bytes((uint8_t *)_bytes.bytes, bls::PrivateKey::PRIVATE_KEY_SIZE));
46-
k.Serialize(output);
47-
NSData *ret = [NSData dataWithBytes:output length:bls::PrivateKey::PRIVATE_KEY_SIZE];
48-
bls::Util::SecFree(output);
49-
return ret;
42+
return _bytes;
5043
}
5144

5245
@end
@@ -106,13 +99,7 @@ - (uint32_t)get_fingerprint
10699

107100
- (NSData *)get_bytes
108101
{
109-
const bls::Bytes bytes = bls::Bytes((uint8_t *)_bytes.bytes, bls::G1Element::SIZE);
110-
bls::G1Element ele = bls::G1Element().FromBytes(bytes);
111-
112-
vector<uint8_t> out;
113-
out = ele.Serialize();
114-
115-
return [NSData dataWithBytes:out.data() length:out.size()];
102+
return _bytes;
116103
}
117104

118105
- (BOOL)isEqual:(nullable id)object
@@ -176,17 +163,19 @@ - (instancetype)initWithBytes:(NSData *)b
176163

177164
}
178165

166+
+ (size_t)SIZE
167+
{
168+
return bls::G2Element::SIZE;
169+
}
170+
179171
+ (G2Element *)from_bytes:(NSData *)b
180172
{
181173
return [[G2Element alloc] initWithBytes:b];
182174
}
183175

184176
- (NSData *)get_bytes
185177
{
186-
const bls::Bytes bytes = bls::Bytes((uint8_t *)_bytes.bytes, bls::G2Element::SIZE);
187-
bls::G2Element ele = bls::G2Element().FromBytes(bytes);
188-
vector<uint8_t> out = ele.Serialize();
189-
return [NSData dataWithBytes:out.data() length:out.size()];
178+
return _bytes;
190179
}
191180

192181
@end
@@ -203,12 +192,16 @@ + (PrivateKey *)key_gen:(NSData *)b
203192
return [[PrivateKey alloc] initWithBytes:pkData];
204193
}
205194

206-
+ (BOOL)verify:(NSData *)pk msg:(NSData *)msg sig:(NSData *)sig;
195+
+ (BOOL)verify:(G1Element *)pk msg:(NSData *)msg sig:(G2Element *)sig;
207196
{
208-
const bls::Bytes pkBytes = bls::Bytes((uint8_t *)pk.bytes, pk.length);
197+
NSData *pk_data = pk.get_bytes;
198+
const uint8_t *pk_bytes = (const uint8_t *)pk_data.bytes;
199+
const bls::Bytes pkBytes = bls::Bytes(pk_bytes, pk_data.length);
209200
const bls::G1Element pk2 = bls::G1Element::FromBytes(pkBytes);
210201

211-
const bls::Bytes sigBytes = bls::Bytes((uint8_t *)sig.bytes, sig.length);
202+
NSData *sig_data = sig.get_bytes;
203+
const uint8_t *sig_bytes = (const uint8_t *)sig_data.bytes;
204+
const bls::Bytes sigBytes = bls::Bytes(sig_bytes, sig_data.length);
212205
const bls::G2Element sig2 = bls::G2Element::FromBytes(sigBytes);
213206

214207
// std::string s((char *)msg.bytes);

0 commit comments

Comments
 (0)