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

Commit bf6453f

Browse files
authored
fix: create proof with invalid message set (#81)
1 parent 67aa9bd commit bf6453f

8 files changed

+198
-24
lines changed

__tests__/bbsSignature/createProof.bbsSignature.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe("bbsSignature", () => {
7070
expect(proof.length).toEqual(380); //TODO add a reason for this and some constants?
7171
});
7272

73-
it("should create proof revealing single messages from multi-message signature", () => {
73+
it("should create proof revealing single message from multi-message signature", () => {
7474
const messages = ["J42AxhciOVkE9w==", "PNMnARWIHP+s2g==", "ti9WYhhEej85jw=="];
7575

7676
const bbsPublicKey = base64Decode(
@@ -159,14 +159,14 @@ describe("bbsSignature", () => {
159159
expect(proof.length).toEqual(380); //TODO add a reason for this and some constants?
160160
});
161161

162-
it("should create proof revealing single messages from multi-message signature", () => {
163-
const messages = ["J42AxhciOVkE9w==", "PNMnARWIHP+s2g==", "ti9WYhhEej85jw=="];
162+
it("should create proof revealing single message from multi-message signature", () => {
163+
const messages = ["uiSKIfNoO2rMrA==", "lMoHHrFx0LxwAw==", "wdwqLVm9chMMnA=="];
164164

165165
const blsPublicKey = base64Decode(
166-
"hmkyvMRoyPLss8TOaencpCmin1Lm0uW8+/N3ZI/ekj/LhCcr4EGCT3rpalTyyNa7B97znLHxW4mijINkyfRhNwSsWxCXHnzFShj3sT4jsCzaGQMqccdZU5qEkpmg9Isl"
166+
"V3FW9jlCSwPYOr7SVFXID0nytBj/e6wHoc8dK0Kn3pkckX2+UVKVgpCsFdSweJyvAc9wk6suCxmFJtfojw8BQmG3DtHbyHFWqgxIk9JyH2ZlR6fxtvZKkSlc0LDHHSN2"
167167
);
168168
const signature = base64Decode(
169-
"D3+1eJbJ3FgSNAq5IoZDUAEC2haOw4F1h2+wsoyfKRIfIwatCfJ5x5apyXnqfLnuAr/tunzDy+xE00CdLvwp9cMKqgphbiqHMXFfAAIPYiMjQZmnrvty6cHCD3nbLTajWuJKB7uM5UTiixByY//GHA=="
169+
"AgRmGhPpX/BhxmMHSgRjkjm8v2v7ZT+D4lLbOhQZkU1o8pic21gOPuP6tRBbNKByDWrM4usV+wQgncqA7KdbWbHl3u8PDZvoCsFs2JVs+nAJdK7/BMmAPu1bncufslXMdp9RjXCDYlHfxbYVKhQotQ=="
170170
);
171171

172172
const request: BbsCreateProofRequest = {
@@ -182,13 +182,13 @@ describe("bbsSignature", () => {
182182
});
183183

184184
it("should create proof revealing multiple messages from multi-message signature", () => {
185-
const messages = ["J42AxhciOVkE9w==", "PNMnARWIHP+s2g==", "ti9WYhhEej85jw=="];
185+
const messages = ["uiSKIfNoO2rMrA==", "lMoHHrFx0LxwAw==", "wdwqLVm9chMMnA=="];
186186

187187
const blsPublicKey = base64Decode(
188-
"hmkyvMRoyPLss8TOaencpCmin1Lm0uW8+/N3ZI/ekj/LhCcr4EGCT3rpalTyyNa7B97znLHxW4mijINkyfRhNwSsWxCXHnzFShj3sT4jsCzaGQMqccdZU5qEkpmg9Isl"
188+
"V3FW9jlCSwPYOr7SVFXID0nytBj/e6wHoc8dK0Kn3pkckX2+UVKVgpCsFdSweJyvAc9wk6suCxmFJtfojw8BQmG3DtHbyHFWqgxIk9JyH2ZlR6fxtvZKkSlc0LDHHSN2"
189189
);
190190
const signature = base64Decode(
191-
"D3+1eJbJ3FgSNAq5IoZDUAEC2haOw4F1h2+wsoyfKRIfIwatCfJ5x5apyXnqfLnuAr/tunzDy+xE00CdLvwp9cMKqgphbiqHMXFfAAIPYiMjQZmnrvty6cHCD3nbLTajWuJKB7uM5UTiixByY//GHA=="
191+
"AgRmGhPpX/BhxmMHSgRjkjm8v2v7ZT+D4lLbOhQZkU1o8pic21gOPuP6tRBbNKByDWrM4usV+wQgncqA7KdbWbHl3u8PDZvoCsFs2JVs+nAJdK7/BMmAPu1bncufslXMdp9RjXCDYlHfxbYVKhQotQ=="
192192
);
193193

194194
const request: BbsCreateProofRequest = {

__tests__/bbsSignature/verifyProof.bbsSignature.spec.ts

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

14-
import { BbsVerifyProofRequest, verifyProof, blsVerifyProof } from "../../src";
14+
import { BbsVerifyProofRequest, verifyProof, blsVerifyProof, BbsCreateProofRequest } from "../../src";
1515
import { Coder } from "@stablelib/base64";
16+
import { createProof } from "../../lib";
1617

1718
const base64Decode = (string: string): Uint8Array => {
1819
const coder = new Coder();
@@ -106,6 +107,59 @@ describe("bbsSignature", () => {
106107

107108
expect(verifyProof(request).verified).toBeFalsy();
108109
});
110+
111+
it("should not verify with a message that wasn't signed", () => {
112+
// Expects messages to be ["Message1", "Message2", "Message3", "Message4"];
113+
const messages = ["BadMessage1", "Message2", "Message3", "Message4"];
114+
const bbsPublicKey = base64Decode(
115+
"S+bRoSJJOet/8hKDpXFV+8TXzg0gPcD64lMFtIUzhYtMJAnNqfJRJnFIS0Vs2VC8AK6MBa6TYgILMqVv4RTSEl3H66mOF6jrEOHelKGlkJCNY8u3bI2aXrmqTkhnjxck"
116+
);
117+
const proof = base64Decode(
118+
"hB0FDeTQc5KEm00wG5HNRvCJ6uoA9flPeTv08PGQct5URoP+mxn6K4hmgRFUMPDZGGspwrc4fCs5SDF+O0nbSyHNLRemj8IMsoruTqhLWrqDWxDhdDDoPYZ4uYoOGIuTBoJqxkv9uFjDRiRnINGvcIJ+QV2iwzwesAHcFmQnxOu/UBEm4XMCDiU93HABZn1uAAAAdJbRgZKb314nZ/PcSUH79GQacq9OAtiOfrCxyaVL5Nt8wXmoY0ri9cBF3XzrySjY7QAAAAIfsXyZmQFTmcislP+mYAk+9nl3V7hTQnc6VIz1Vzayyyses57/fYftogle+iFyMP9kfMujXAf0AOx268LFfZnuA4+PYfY+mH1l/ieGMkvaTFRrfi+sfxFX14wCTH8Qy7Z4+DjTUIPIBGwqCMiBWZ3ZAAAABUtLeqN3HfQNyXQNf7A5MKx00tYvssxatlylAv+lmU2cD2ke0dY7hltzXgGFJ8LjtPIe6uMlZdmdu6+l/0IHK4RGYhEYGwZ4NHu6mlydV/7XiFpJKd9vtmfGBYXU6nXHkxGZ3I0D3FfFFA30B/UFBYqGZ7EKFIObMmvmcQWbTFkkHYAlabXh2RAVSlTmNL+NrIu1LbLW0CBExF4f+H8kZmM="
119+
);
120+
121+
const request: BbsVerifyProofRequest = {
122+
proof,
123+
publicKey: bbsPublicKey,
124+
messageCount: 4,
125+
messages,
126+
nonce: "0123456789",
127+
revealed: [0],
128+
};
129+
expect(verifyProof(request).verified).toBeFalsy();
130+
});
131+
});
132+
133+
it("should not verify with revealed message that was supposed to be hidden", () => {
134+
const messages = ["Message1", "Message2", "Message3", "Message4"];
135+
const signature = base64Decode(
136+
"jps9JChJlTj8upAO+S+0PFH1FFjEC/6wsACGO8sDnsDtH53KbWhiN7Xo/UpAe3q2CydfRcjUi3oOTfxj+IOC9dooSjsfy4WXwBIwAKuD74tc1B+b9ORf/SM2+EM3BVLdPmgj8i4gA1NTdQdbyznHQg=="
137+
);
138+
const bbsPublicKey = base64Decode(
139+
"S+bRoSJJOet/8hKDpXFV+8TXzg0gPcD64lMFtIUzhYtMJAnNqfJRJnFIS0Vs2VC8AK6MBa6TYgILMqVv4RTSEl3H66mOF6jrEOHelKGlkJCNY8u3bI2aXrmqTkhnjxckD1f1djGEQgco//uD1BMpDNmv/OMlQqECeBeev7wJnkXFDfiO6Dw1TvAqTo1HyHcAAAAABI0jHoOG0vFL+EGcD4P5yGs4rlO17j/6dYqrltPk8PwMfe9pDK6zPFcdRbXpFgUHvQTwjgDAEee7S318rCU0h665rUq8ZXJ2R2rS0UpvoHuy+29oJsBWQeIxquKH8pt0YRTZbFJQ+o+6rFrzHyRFcYz9y3f8BsG7wuRsmkENYLfWVUN9MFhfrmEu8re5/ZWmZwxbPPEi7Lo45QS9BQdFPmvRC+GcKP5hfdKz2HulxyJcBnxFmguFoZgldmZGrvmGew=="
140+
);
141+
const nonce = "0123456789";
142+
143+
const proofRequest: BbsCreateProofRequest = {
144+
signature,
145+
publicKey: bbsPublicKey,
146+
messages,
147+
revealed: [0],
148+
nonce,
149+
};
150+
const proof = createProof(proofRequest);
151+
152+
const proofMessages = ["BadMessage9"];
153+
const request = {
154+
proof,
155+
publicKey: bbsPublicKey,
156+
messageCount: 4,
157+
messages: proofMessages,
158+
nonce,
159+
revealed: [0],
160+
};
161+
162+
expect(verifyProof(request).verified).toBeFalsy();
109163
});
110164

111165
describe("blsVerifyProof", () => {

__tests__/bls12381.spec.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,23 @@ describe("bls12381", () => {
2929
});
3030

3131
it("should be able to generate a key pair with a seed", () => {
32-
const seed = randomBytes(50);
33-
const result = generateBls12381KeyPair(seed);
32+
const result = generateBls12381KeyPair(
33+
new Uint8Array(new Buffer("H297BpoOgkfpXcxr1fJyQRiNx1+ZekeQ+OU/AYV/lVxaPXXhFBIbxeIU8kIAAX68cwQ=", "base64"))
34+
);
3435
expect(result.publicKey).toBeDefined();
3536
expect(result.secretKey).toBeDefined();
3637
expect(result.secretKey?.length as number).toEqual(DEFAULT_BLS12381_PRIVATE_KEY_LENGTH);
3738
expect(result.publicKey.length).toEqual(DEFAULT_BLS12381_PUBLIC_KEY_LENGTH);
39+
expect(result.publicKey).toEqual(
40+
new Uint8Array(
41+
new Buffer(
42+
"ha+sckj0C+dXR6IPUfxGJMCc3XHkGgoDz2PHPMVrMMhJXSGO5y7VrAHrZt64MThKGXE+SAOTHFS5jGoP5uHWvhabYHuIlHLpZHiLyg2m4sc6yfMG3tloUxLY+TiaeQCG",
43+
"base64"
44+
)
45+
)
46+
);
47+
expect(result.secretKey as Uint8Array).toEqual(
48+
new Uint8Array(new Buffer("Ovy0NyLx6ET9/AkuSmxw7X3IGMRq4IqrmFRfzWf/QvQ=", "base64"))
49+
);
3850
});
3951
});

jest.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module.exports = {
66
testPathIgnorePatterns: ["/node_modules/", "/output/"],
77
testRegex: [".spec.ts$"],
88
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
9-
coveragePathIgnorePatterns: ["<rootDir>/__tests__"],
9+
coveragePathIgnorePatterns: ["<rootDir>/__tests__", "<rootDir>/lib"],
1010
verbose: true,
1111
name: pack.name,
1212
displayName: pack.name,
13-
};
13+
};

native/Cargo.lock

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

native/artifacts.json

+17-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
{"active":"release","targets":{"release":{"rustc":"","env":{"npm_config_target":null,"npm_config_arch":null,"npm_config_target_arch":null,"npm_config_disturl":null,"npm_config_runtime":null,"npm_config_build_from_source":null,"npm_config_devdir":null}}}}
1+
{
2+
"active": "release",
3+
"targets": {
4+
"release": {
5+
"rustc": "",
6+
"env": {
7+
"npm_config_target": null,
8+
"npm_config_arch": null,
9+
"npm_config_target_arch": null,
10+
"npm_config_disturl": null,
11+
"npm_config_runtime": null,
12+
"npm_config_build_from_source": null,
13+
"npm_config_devdir": null
14+
}
15+
}
16+
}
17+
}

native/tests/vectors.rs

+59-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const DOMAIN_SEPARATION_TAG: &str = "BBSSignature2020";
2222
/// Computed by calling
2323
///
2424
/// SecretKey::from_msg_hash(b"aaaaaaaa");
25-
// const SECRET_KEY: &str = "GztATHHZwdAp9wwEiHIshRDi4wMZJjKq0pT5etGII3g=";
25+
const SECRET_KEY: &str = "GztATHHZwdAp9wwEiHIshRDi4wMZJjKq0pT5etGII3g=";
2626

2727
/// Computed by calling
2828
///
@@ -254,6 +254,60 @@ fn proof_with_8_messages() {
254254
// assert_eq!(proved_messages, vec![SignatureMessage::from_msg_hash(b"Message9")])
255255
}
256256

257+
#[ignore]
258+
#[test]
259+
fn print() {
260+
let sk = get_secret_key(SECRET_KEY);
261+
let dpk = get_public_key(PUBLIC_KEY);
262+
let dst = get_dst(DOMAIN_SEPARATION_TAG);
263+
264+
let messages = vec![
265+
SignatureMessage::from_msg_hash(b"Message1"),
266+
SignatureMessage::from_msg_hash(b"Message2"),
267+
SignatureMessage::from_msg_hash(b"Message3"),
268+
SignatureMessage::from_msg_hash(b"Message4")
269+
];
270+
let pk = dpk.to_public_key(4, dst).unwrap();
271+
let sig = Signature::new(messages.as_slice(), &sk, &pk).unwrap();
272+
println!("pk = {}", base64::encode(&pk.to_compressed_bytes()[..]));
273+
println!("sig = {}", base64::encode(&sig.to_compressed_bytes()[..]));
274+
275+
let nonce = SignatureNonce::from_msg_hash(b"0123456789");
276+
let proof_request = Verifier::new_proof_request(&[0], &pk).unwrap();
277+
278+
// Sends `proof_request` and `nonce` to the prover
279+
let proof_messages = vec![
280+
pm_revealed!(b"Message1"),
281+
pm_hidden!(b"Message2"),
282+
pm_hidden!(b"Message3"),
283+
pm_hidden!(b"Message4"),
284+
];
285+
286+
let pok = Prover::commit_signature_pok(&proof_request, proof_messages.as_slice(), &sig)
287+
.unwrap();
288+
289+
// complete other zkps as desired and compute `challenge_hash`
290+
// add bytes from other proofs
291+
292+
let mut challenge_bytes = Vec::new();
293+
challenge_bytes.extend_from_slice(pok.to_bytes().as_slice());
294+
challenge_bytes.extend_from_slice(&nonce.to_bytes()[..]);
295+
296+
let challenge = SignatureNonce::from_msg_hash(&challenge_bytes);
297+
298+
let proof = Prover::generate_signature_pok(pok, &challenge).unwrap();
299+
println!("proof = {}", base64::encode(&proof.proof.to_compressed_bytes()[..]));
300+
301+
let res = Verifier::verify_signature_pok(&proof_request, &proof, &nonce);
302+
303+
assert!(res.is_ok());
304+
let proved_messages = res.unwrap();
305+
306+
proof_request.revealed_messages = BTreeSet::new();
307+
proof_request.revealed_messages.insert(1);
308+
proof.revealed_messages = vec![SignatureMessage::from_msg_hash(b"Message2")];
309+
}
310+
257311
fn get_dst(dst: &str) -> DomainSeparationTag {
258312
DomainSeparationTag::new(dst.as_bytes(), None, None, None).unwrap()
259313
}
@@ -263,10 +317,10 @@ fn get_public_key(key: &str) -> DeterministicPublicKey {
263317
DeterministicPublicKey::from(*array_ref![dpk_bytes, 0, COMPRESSED_DETERMINISTIC_PUBLIC_KEY_SIZE])
264318
}
265319

266-
// fn get_secret_key(key: &str) -> SecretKey {
267-
// let sk_bytes = base64::decode(key).unwrap();
268-
// SecretKey::from(array_ref![sk_bytes, 0, COMPRESSED_SECRET_KEY_SIZE])
269-
// }
320+
fn get_secret_key(key: &str) -> SecretKey {
321+
let sk_bytes = base64::decode(key).unwrap();
322+
SecretKey::from(array_ref![sk_bytes, 0, COMPRESSED_SECRET_KEY_SIZE])
323+
}
270324

271325
fn get_signature(sig: &str) -> Signature {
272326
let sig_bytes = base64::decode(sig).unwrap();

src/types/BbsVerifyProofRequest.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface BbsVerifyProofRequest {
2828
*/
2929
readonly messages: readonly string[];
3030
/**
31-
* Zero based indicies of the revealed messages in original signature
31+
* Zero based indices of the revealed messages in original signature
3232
*/
3333
readonly revealed: readonly number[];
3434
/**

0 commit comments

Comments
 (0)