Skip to content

Commit

Permalink
Update to fail if passed invalid claim
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Lodder <[email protected]>
  • Loading branch information
mikelodder7 committed Apr 27, 2020
1 parent cc48bf0 commit 3227345
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 9 deletions.
96 changes: 95 additions & 1 deletion __tests__/bbsSignature/verifyProof.bbsSignature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@
* limitations under the License.
*/

import { BbsVerifyProofRequest, verifyProof, blsVerifyProof } from "../../src";
import {
BbsVerifyProofRequest,
verifyProof,
blsVerifyProof,
BbsCreateProofRequest,
bls12381toBbs,
Bls12381ToBbsRequest
} from "../../src";
import { Coder } from "@stablelib/base64";
import {createProof} from "../../lib";
import {BlsToBbsRequest} from "../../lib/types/BlsToBbsRequest";
import {BlsKeyPair} from "../../lib/types/BlsKeyPair";

const base64Decode = (string: string): Uint8Array => {
const coder = new Coder();
Expand Down Expand Up @@ -106,6 +116,90 @@ describe("bbsSignature", () => {

expect(verifyProof(request).verified).toBeFalsy();
});

it("should not verify with a message that wasn't signed", () => {
// Expects messages to be ["Message1", "Message2", "Message3", "Message4"];
const messages = ["BadMessage1", "Message2", "Message3", "Message4"];
const bbsPublicKey = base64Decode(
"S+bRoSJJOet/8hKDpXFV+8TXzg0gPcD64lMFtIUzhYtMJAnNqfJRJnFIS0Vs2VC8AK6MBa6TYgILMqVv4RTSEl3H66mOF6jrEOHelKGlkJCNY8u3bI2aXrmqTkhnjxck"
);
const proof = base64Decode(
"hB0FDeTQc5KEm00wG5HNRvCJ6uoA9flPeTv08PGQct5URoP+mxn6K4hmgRFUMPDZGGspwrc4fCs5SDF+O0nbSyHNLRemj8IMsoruTqhLWrqDWxDhdDDoPYZ4uYoOGIuTBoJqxkv9uFjDRiRnINGvcIJ+QV2iwzwesAHcFmQnxOu/UBEm4XMCDiU93HABZn1uAAAAdJbRgZKb314nZ/PcSUH79GQacq9OAtiOfrCxyaVL5Nt8wXmoY0ri9cBF3XzrySjY7QAAAAIfsXyZmQFTmcislP+mYAk+9nl3V7hTQnc6VIz1Vzayyyses57/fYftogle+iFyMP9kfMujXAf0AOx268LFfZnuA4+PYfY+mH1l/ieGMkvaTFRrfi+sfxFX14wCTH8Qy7Z4+DjTUIPIBGwqCMiBWZ3ZAAAABUtLeqN3HfQNyXQNf7A5MKx00tYvssxatlylAv+lmU2cD2ke0dY7hltzXgGFJ8LjtPIe6uMlZdmdu6+l/0IHK4RGYhEYGwZ4NHu6mlydV/7XiFpJKd9vtmfGBYXU6nXHkxGZ3I0D3FfFFA30B/UFBYqGZ7EKFIObMmvmcQWbTFkkHYAlabXh2RAVSlTmNL+NrIu1LbLW0CBExF4f+H8kZmM="
);

const request: BbsVerifyProofRequest = {
proof,
publicKey: bbsPublicKey,
messageCount: 4,
messages,
nonce: "0123456789",
revealed: [0]
};
expect(verifyProof(request).verified).toBeFalsy();
})
});

it("should not verify with revealed message that was supposed to be hidden", () => {
let messages = ["Message1", "Message2", "Message3", "Message4"];
const signature = base64Decode(
"jps9JChJlTj8upAO+S+0PFH1FFjEC/6wsACGO8sDnsDtH53KbWhiN7Xo/UpAe3q2CydfRcjUi3oOTfxj+IOC9dooSjsfy4WXwBIwAKuD74tc1B+b9ORf/SM2+EM3BVLdPmgj8i4gA1NTdQdbyznHQg=="
);
const bbsPublicKey = base64Decode(
"S+bRoSJJOet/8hKDpXFV+8TXzg0gPcD64lMFtIUzhYtMJAnNqfJRJnFIS0Vs2VC8AK6MBa6TYgILMqVv4RTSEl3H66mOF6jrEOHelKGlkJCNY8u3bI2aXrmqTkhnjxckD1f1djGEQgco//uD1BMpDNmv/OMlQqECeBeev7wJnkXFDfiO6Dw1TvAqTo1HyHcAAAAABI0jHoOG0vFL+EGcD4P5yGs4rlO17j/6dYqrltPk8PwMfe9pDK6zPFcdRbXpFgUHvQTwjgDAEee7S318rCU0h665rUq8ZXJ2R2rS0UpvoHuy+29oJsBWQeIxquKH8pt0YRTZbFJQ+o+6rFrzHyRFcYz9y3f8BsG7wuRsmkENYLfWVUN9MFhfrmEu8re5/ZWmZwxbPPEi7Lo45QS9BQdFPmvRC+GcKP5hfdKz2HulxyJcBnxFmguFoZgldmZGrvmGew=="
);
const nonce = "0123456789";

const proof_request: BbsCreateProofRequest = {
signature,
publicKey: bbsPublicKey,
messages,
revealed: [0],
nonce
};
let proof = createProof(proof_request);

let proof_messages = ["Message2", "Message3"];
let request: BbsVerifyProofRequest = {
proof,
publicKey: bbsPublicKey,
messageCount: 4,
messages: proof_messages,
nonce,
revealed: [1, 2]
};

proof_messages = ["Message2"];
request = {
proof,
publicKey: bbsPublicKey,
messageCount: 4,
messages: proof_messages,
nonce,
revealed: [1]
};

proof_messages = ["BadMessage9"];
request = {
proof,
publicKey: bbsPublicKey,
messageCount: 4,
messages: proof_messages,
nonce,
revealed: [0]
};

expect(verifyProof(request).verified).toBeFalsy();

proof_messages = ["Message1"];
request = {
proof,
publicKey: bbsPublicKey,
messageCount: 4,
messages: proof_messages,
nonce,
revealed: [0]
};
expect(verifyProof(request).verified).toBeTruthy();
});

describe("blsVerifyProof", () => {
Expand Down
2 changes: 1 addition & 1 deletion native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 59 additions & 5 deletions native/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const DOMAIN_SEPARATION_TAG: &str = "BBSSignature2020";
/// Computed by calling
///
/// SecretKey::from_msg_hash(b"aaaaaaaa");
// const SECRET_KEY: &str = "GztATHHZwdAp9wwEiHIshRDi4wMZJjKq0pT5etGII3g=";
const SECRET_KEY: &str = "GztATHHZwdAp9wwEiHIshRDi4wMZJjKq0pT5etGII3g=";

/// Computed by calling
///
Expand Down Expand Up @@ -254,6 +254,60 @@ fn proof_with_8_messages() {
// assert_eq!(proved_messages, vec![SignatureMessage::from_msg_hash(b"Message9")])
}

#[ignore]
#[test]
fn print() {
let sk = get_secret_key(SECRET_KEY);
let dpk = get_public_key(PUBLIC_KEY);
let dst = get_dst(DOMAIN_SEPARATION_TAG);

let messages = vec![
SignatureMessage::from_msg_hash(b"Message1"),
SignatureMessage::from_msg_hash(b"Message2"),
SignatureMessage::from_msg_hash(b"Message3"),
SignatureMessage::from_msg_hash(b"Message4")
];
let pk = dpk.to_public_key(4, dst).unwrap();
let sig = Signature::new(messages.as_slice(), &sk, &pk).unwrap();
println!("pk = {}", base64::encode(&pk.to_compressed_bytes()[..]));
println!("sig = {}", base64::encode(&sig.to_compressed_bytes()[..]));

let nonce = SignatureNonce::from_msg_hash(b"0123456789");
let proof_request = Verifier::new_proof_request(&[0], &pk).unwrap();

// Sends `proof_request` and `nonce` to the prover
let proof_messages = vec![
pm_revealed!(b"Message1"),
pm_hidden!(b"Message2"),
pm_hidden!(b"Message3"),
pm_hidden!(b"Message4"),
];

let pok = Prover::commit_signature_pok(&proof_request, proof_messages.as_slice(), &sig)
.unwrap();

// complete other zkps as desired and compute `challenge_hash`
// add bytes from other proofs

let mut challenge_bytes = Vec::new();
challenge_bytes.extend_from_slice(pok.to_bytes().as_slice());
challenge_bytes.extend_from_slice(&nonce.to_bytes()[..]);

let challenge = SignatureNonce::from_msg_hash(&challenge_bytes);

let proof = Prover::generate_signature_pok(pok, &challenge).unwrap();
println!("proof = {}", base64::encode(&proof.proof.to_compressed_bytes()[..]));

let res = Verifier::verify_signature_pok(&proof_request, &proof, &nonce);

assert!(res.is_ok());
let proved_messages = res.unwrap();

proof_request.revealed_messages = BTreeSet::new();
proof_request.revealed_messages.insert(1);
proof.revealed_messages = vec![SignatureMessage::from_msg_hash(b"Message2")];
}

fn get_dst(dst: &str) -> DomainSeparationTag {
DomainSeparationTag::new(dst.as_bytes(), None, None, None).unwrap()
}
Expand All @@ -263,10 +317,10 @@ fn get_public_key(key: &str) -> DeterministicPublicKey {
DeterministicPublicKey::from(*array_ref![dpk_bytes, 0, COMPRESSED_DETERMINISTIC_PUBLIC_KEY_SIZE])
}

// fn get_secret_key(key: &str) -> SecretKey {
// let sk_bytes = base64::decode(key).unwrap();
// SecretKey::from(array_ref![sk_bytes, 0, COMPRESSED_SECRET_KEY_SIZE])
// }
fn get_secret_key(key: &str) -> SecretKey {
let sk_bytes = base64::decode(key).unwrap();
SecretKey::from(array_ref![sk_bytes, 0, COMPRESSED_SECRET_KEY_SIZE])
}

fn get_signature(sig: &str) -> Signature {
let sig_bytes = base64::decode(sig).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/bbsSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export const createProof = (request: BbsCreateProofRequest): Uint8Array => {
})
);
} catch (ex) {
throw new Error("Failed to create proof");
throw new Error("Failed to create proof: " + ex.toString());
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/types/BbsVerifyProofRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface BbsVerifyProofRequest {
*/
readonly messages: readonly string[];
/**
* Zero based indicies of the revealed messages in original signature
* Zero based indices of the revealed messages in original signature
*/
readonly revealed: readonly number[];
/**
Expand Down

0 comments on commit 3227345

Please sign in to comment.