Skip to content
This repository was archived by the owner on Feb 26, 2025. It is now read-only.

Commit 85e89a0

Browse files
authored
feat: add bls12-381 g1 key generation support (#127)
BREAKING CHANGE: generateBls12381KeyPair has been changed to generateBls12381G2KeyPair
1 parent 3fa07dd commit 85e89a0

11 files changed

+244
-49
lines changed

__tests__/bbsSignature/sign.bbsSignature.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313

1414
import {
15-
generateBls12381KeyPair,
15+
generateBls12381G2KeyPair,
1616
BbsSignRequest,
1717
BlsBbsSignRequest,
1818
sign,
@@ -25,7 +25,7 @@ import {
2525
import { stringToBytes } from "../utilities";
2626

2727
describe("bbsSignature", () => {
28-
const blsKeyPair = generateBls12381KeyPair();
28+
const blsKeyPair = generateBls12381G2KeyPair();
2929
describe("sign", () => {
3030
const bbsKeyPair = bls12381toBbs({ keyPair: blsKeyPair, messageCount: 3 });
3131

__tests__/bbsSignature/verify.bbsSignature.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
blsVerify,
1919
BbsSignRequest,
2020
sign,
21-
generateBls12381KeyPair,
21+
generateBls12381G2KeyPair,
2222
BlsBbsSignRequest,
2323
blsSign,
2424
BlsBbsVerifyRequest,
@@ -27,7 +27,7 @@ import { base64Decode, stringToBytes } from "../utilities";
2727

2828
describe("bbsSignature", () => {
2929
describe("verify", () => {
30-
const blsKeyPair = generateBls12381KeyPair();
30+
const blsKeyPair = generateBls12381G2KeyPair();
3131
it("should verify valid signature with a single message", () => {
3232
const BbsPublicKey = bls12381toBbs({ keyPair: blsKeyPair, messageCount: 1 });
3333
const request: BbsSignRequest = {
@@ -109,7 +109,7 @@ describe("bbsSignature", () => {
109109
});
110110
});
111111
describe("blsVerify", () => {
112-
const blsKeyPair = generateBls12381KeyPair();
112+
const blsKeyPair = generateBls12381G2KeyPair();
113113
it("should verify valid signature with a single message", () => {
114114
const request: BlsBbsSignRequest = {
115115
keyPair: blsKeyPair,

__tests__/bls12381.spec.ts

+88-26
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,101 @@
1212
*/
1313

1414
import {
15-
generateBls12381KeyPair,
16-
DEFAULT_BLS12381_PUBLIC_KEY_LENGTH,
15+
generateBls12381G1KeyPair,
16+
generateBls12381G2KeyPair,
17+
DEFAULT_BLS12381_G1_PUBLIC_KEY_LENGTH,
18+
DEFAULT_BLS12381_G2_PUBLIC_KEY_LENGTH,
1719
DEFAULT_BLS12381_PRIVATE_KEY_LENGTH,
1820
} from "../src";
1921

2022
describe("bls12381", () => {
21-
it("should be able to generate a key pair", () => {
22-
const result = generateBls12381KeyPair();
23-
expect(result).toBeDefined();
24-
expect(result.publicKey).toBeDefined();
25-
expect(result.secretKey).toBeDefined();
26-
expect(result.secretKey?.length as number).toEqual(DEFAULT_BLS12381_PRIVATE_KEY_LENGTH);
27-
expect(result.publicKey.length).toEqual(DEFAULT_BLS12381_PUBLIC_KEY_LENGTH);
23+
[
24+
{
25+
field: "G1",
26+
generateKeyFn: generateBls12381G1KeyPair,
27+
secretKeyLength: DEFAULT_BLS12381_PRIVATE_KEY_LENGTH,
28+
publicKeyLength: DEFAULT_BLS12381_G1_PUBLIC_KEY_LENGTH,
29+
},
30+
{
31+
field: "G2",
32+
generateKeyFn: generateBls12381G2KeyPair,
33+
secretKeyLength: DEFAULT_BLS12381_PRIVATE_KEY_LENGTH,
34+
publicKeyLength: DEFAULT_BLS12381_G2_PUBLIC_KEY_LENGTH,
35+
},
36+
].forEach((value) => {
37+
it(`should be able to generate a key pair in ${value.field} field`, () => {
38+
const result = value.generateKeyFn();
39+
expect(result).toBeDefined();
40+
expect(result.publicKey).toBeDefined();
41+
expect(result.secretKey).toBeDefined();
42+
expect(result.secretKey?.length as number).toEqual(value.secretKeyLength);
43+
expect(result.publicKey.length).toEqual(value.publicKeyLength);
44+
});
2845
});
2946

30-
it("should be able to generate a key pair with a seed", () => {
31-
const result = generateBls12381KeyPair(
32-
new Uint8Array(new Buffer("H297BpoOgkfpXcxr1fJyQRiNx1+ZekeQ+OU/AYV/lVxaPXXhFBIbxeIU8kIAAX68cwQ=", "base64"))
33-
);
34-
expect(result.publicKey).toBeDefined();
35-
expect(result.secretKey).toBeDefined();
36-
expect(result.secretKey?.length as number).toEqual(DEFAULT_BLS12381_PRIVATE_KEY_LENGTH);
37-
expect(result.publicKey.length).toEqual(DEFAULT_BLS12381_PUBLIC_KEY_LENGTH);
38-
expect(result.publicKey).toEqual(
39-
new Uint8Array(
40-
new Buffer(
41-
"qJgttTOthlZHltz+c0PE07hx3worb/cy7QY5iwRegQ9BfwvGahdqCO9Q9xuOnF5nD/Tq6t8zm9z26EAFCiaEJnL5b50D1cHDgNxBUPEEae+4bUb3JRsHaxBdZWDOo3pb",
47+
[
48+
{
49+
field: "G1",
50+
generateKeyFn: generateBls12381G1KeyPair,
51+
secretKeyLength: DEFAULT_BLS12381_PRIVATE_KEY_LENGTH,
52+
publicKeyLength: DEFAULT_BLS12381_G1_PUBLIC_KEY_LENGTH,
53+
},
54+
{
55+
field: "G2",
56+
generateKeyFn: generateBls12381G2KeyPair,
57+
secretKeyLength: DEFAULT_BLS12381_PRIVATE_KEY_LENGTH,
58+
publicKeyLength: DEFAULT_BLS12381_G2_PUBLIC_KEY_LENGTH,
59+
},
60+
].forEach((value) => {
61+
it(`should be able to generate a key pairs in ${value.field} field without seed which are random`, () => {
62+
const keyPair1 = value.generateKeyFn();
63+
const keyPair2 = value.generateKeyFn();
64+
expect(keyPair1).toBeDefined();
65+
expect(keyPair2).toBeDefined();
66+
expect((keyPair1.secretKey as Uint8Array) === (keyPair2.secretKey as Uint8Array)).toBeFalsy();
67+
expect(keyPair1.publicKey === keyPair2.publicKey).toBeFalsy();
68+
});
69+
});
70+
71+
[
72+
{
73+
field: "G1",
74+
generateKeyFn: generateBls12381G1KeyPair,
75+
secretKeyLength: DEFAULT_BLS12381_PRIVATE_KEY_LENGTH,
76+
publicKeyLength: DEFAULT_BLS12381_G1_PUBLIC_KEY_LENGTH,
77+
seed: new Uint8Array(
78+
Buffer.from("H297BpoOgkfpXcxr1fJyQRiNx1+ZekeQ+OU/AYV/lVxaPXXhFBIbxeIU8kIAAX68cwQ=", "base64")
79+
),
80+
secretKey: new Uint8Array(Buffer.from("Cm550dHeqo5I/dVC/bXD9s5Cx8vnyhV/gm7KO5UuviE=", "base64")),
81+
publicKey: new Uint8Array(
82+
Buffer.from("ufHHJwY9/xjrtHgGyQD1F9zNRJqiB4zhSXDEBC/MJrNuXJFwI+ILxu2uPASyd5Vf", "base64")
83+
),
84+
},
85+
{
86+
field: "G2",
87+
generateKeyFn: generateBls12381G2KeyPair,
88+
secretKeyLength: DEFAULT_BLS12381_PRIVATE_KEY_LENGTH,
89+
publicKeyLength: DEFAULT_BLS12381_G2_PUBLIC_KEY_LENGTH,
90+
seed: new Uint8Array(
91+
Buffer.from("H297BpoOgkfpXcxr1fJyQRiNx1+ZekeQ+OU/AYV/lVxaPXXhFBIbxeIU8kIAAX68cwQ=", "base64")
92+
),
93+
secretKey: new Uint8Array(Buffer.from("Cm550dHeqo5I/dVC/bXD9s5Cx8vnyhV/gm7KO5UuviE=", "base64")),
94+
publicKey: new Uint8Array(
95+
Buffer.from(
96+
"pQro1uqpvUPM31sr+jHffz7+KJIpA3kFen4SoKATURRgo7pk582aaqIxSinWsgHDB9j9dwxYRbC3q2ZmICR2OVMX3FHW9LZV2QAauTYFn7gEra1BSeKhdKDpzBxPjI36",
4297
"base64"
4398
)
44-
)
45-
);
46-
expect(result.secretKey as Uint8Array).toEqual(
47-
new Uint8Array(new Buffer("YoASulEi3WV7yfJ+yWctJRCbHfr7WjK7JjcMrRqbL6E=", "base64"))
48-
);
99+
),
100+
},
101+
].forEach((value) => {
102+
it(`should be able to generate a key pair with a seed in ${value.field} field`, () => {
103+
const result = value.generateKeyFn(value.seed);
104+
expect(result.publicKey).toBeDefined();
105+
expect(result.secretKey).toBeDefined();
106+
expect(result.secretKey?.length as number).toEqual(value.secretKeyLength);
107+
expect(result.publicKey.length).toEqual(value.publicKeyLength);
108+
expect(result.secretKey as Uint8Array).toEqual(value.secretKey);
109+
expect(result.publicKey).toEqual(value.publicKey);
110+
});
49111
});
50112
});

__tests__/bls12381ToBbs.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
* limitations under the License.
1212
*/
1313

14-
import { generateBls12381KeyPair, bls12381toBbs, BlsKeyPair } from "../src";
14+
import { generateBls12381G2KeyPair, bls12381toBbs, BlsKeyPair } from "../src";
1515

1616
describe("bls12381toBbs", () => {
1717
it("should be able to convert bls12381 key pair to bbs key", () => {
18-
const blsKeyPair = generateBls12381KeyPair();
18+
const blsKeyPair = generateBls12381G2KeyPair();
1919
expect(blsKeyPair).toBeDefined();
2020
const bbsKeyPair = bls12381toBbs({
2121
keyPair: blsKeyPair,
@@ -30,7 +30,7 @@ describe("bls12381toBbs", () => {
3030
});
3131

3232
it("should be able to convert bls12381 public key to bbs key", () => {
33-
const blsKeyPair = generateBls12381KeyPair();
33+
const blsKeyPair = generateBls12381G2KeyPair();
3434
expect(blsKeyPair).toBeDefined();
3535
const blsPublicKey: BlsKeyPair = {
3636
publicKey: blsKeyPair.publicKey,
@@ -48,7 +48,7 @@ describe("bls12381toBbs", () => {
4848
});
4949

5050
it("should throw error when message count 0", () => {
51-
const blsKeyPair = generateBls12381KeyPair();
51+
const blsKeyPair = generateBls12381G2KeyPair();
5252
expect(blsKeyPair).toBeDefined();
5353

5454
expect(() =>

native/Cargo.lock

+57
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ neon-build = "0.4"
1515
[dependencies]
1616
arrayref = "0.3"
1717
bbs = "0.4"
18+
bls_sigs_ref = "0.3"
19+
ff-zeroize = "0.6"
20+
hkdf = "0.8"
1821
neon = "0.4"
22+
pairing-plus = "0.19"
23+
rand = "0.7"
24+
sha2 = "0.8"
1925

2026
[dev-dependencies]
2127
base64 = "0.12"

0 commit comments

Comments
 (0)