Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions tests/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright (c) 2025 Cloudflare, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use core::{any::type_name, num::NonZero};

use arc::{
ClientSecret, Credential, CredentialRequest, CredentialResponse, Presentation, SecretKey,
State, Suite,
};
use rand::rngs::ThreadRng;

#[macro_export]
macro_rules! all_arc_tests {
($suite:ty) => {
#[test]
fn happy_path() {
$crate::protocol::happy_path::<$suite>();
}
#[test]
fn multiple_presentations() {
$crate::protocol::multiple_presentations::<$suite>();
}
};
}

macro_rules! test_serialization {
($T:ty, $val:expr $(, $context:expr)?) => {
let a_bytes = $val.to_bytes();
let b = <$T>::from_bytes(&a_bytes $(, $context)? )
.expect(&format!("deserialization of {} failed", type_name::<$T>()));
let b_bytes = b.to_bytes();
assert_eq!(a_bytes, b_bytes);
};
}

pub(crate) fn happy_path<S: Suite>() {
let req_context = b"RequestContext";
let csrng = &mut ThreadRng::default();
let key = SecretKey::<S>::new(csrng);
test_serialization!(SecretKey::<S>, &key);

let params = key.issuer_params().unwrap();
test_serialization!(SecretKey::<S>, &key);

let (request, secrets) = CredentialRequest::new(csrng, req_context).unwrap();
test_serialization!(CredentialRequest::<S>, &request);
test_serialization!(ClientSecret::<S>, &secrets);

let response = CredentialResponse::new(csrng, &key, request.clone()).unwrap();
test_serialization!(CredentialResponse::<S>, &response);

let credential = Credential::new(&params, request, secrets, response).unwrap();
test_serialization!(Credential::<S>, &credential);

let pres_limit = NonZero::new(3).expect("non-zero limit");
let pres_context = b"PresentationContext";

let mut state = credential
.presentation_state(pres_limit, pres_context)
.unwrap();
test_serialization!(State::<S>, &state);

assert_eq!(state.used_presentations(), 0);
assert_eq!(state.remaining_presentations(), pres_limit.get());

for _ in 0..pres_limit.get() {
let presentation = state.present(csrng).unwrap();
test_serialization!(Presentation::<S>, &presentation, pres_limit);

let result_verify = presentation.verify(&key, req_context, pres_limit, pres_context);
assert!(result_verify.is_ok());
}

assert_eq!(state.used_presentations(), pres_limit.get());
assert_eq!(state.remaining_presentations(), 0);

let result = state.present(csrng);
assert!(result.is_err());
}

pub(crate) fn multiple_presentations<S: Suite>() {
let request_ctx = b"RequestContext";
let csrng = &mut ThreadRng::default();
let key = SecretKey::<S>::new(csrng);
let params = key.issuer_params().unwrap();

let (request, secrets) = CredentialRequest::new(csrng, request_ctx).unwrap();
let response = CredentialResponse::new(csrng, &key, request.clone()).unwrap();
let credential = Credential::new(&params, request, secrets, response).unwrap();

let photos_limit = 11.try_into().unwrap();
let photos_ctx = "photos.example.com".as_bytes();
let mut state_photos = credential
.presentation_state(photos_limit, photos_ctx)
.unwrap();
let photos1 = state_photos.present(csrng).unwrap();

let movies_limit = 10.try_into().unwrap();
let movies_ctx = "movies.example.com".as_bytes();
let mut state_movies = credential
.presentation_state(movies_limit, movies_ctx)
.unwrap();

let mut result = photos1.verify(&key, request_ctx, photos_limit, photos_ctx);
assert!(result.is_ok());

// Check mismatching of verification parameters.
let key2 = SecretKey::<S>::new(csrng);
result = photos1.verify(&key2, request_ctx, photos_limit, photos_ctx);
assert!(result.is_err(), "wrong key");

result = photos1.verify(&key, b"bad request context", photos_limit, photos_ctx);
assert!(result.is_err(), "wrong request context");

result = photos1.verify(&key, request_ctx, movies_limit, photos_ctx);
assert!(result.is_err(), "wrong presentation limit");

result = photos1.verify(&key, request_ctx, photos_limit, b"bad presentation context");
assert!(result.is_err(), "wrong presentation context");

// Check mismatching of presentation parameters.
let movies1 = state_movies.present(csrng).unwrap();
result = movies1.verify(&key, request_ctx, movies_limit, movies_ctx);
assert!(result.is_ok());

let movies2 = state_photos.present(csrng).unwrap();
result = movies2.verify(&key, request_ctx, movies_limit, movies_ctx);
assert!(result.is_err(), "must fail as generated with a wrong state");

// Single presentation.
let single_ctx = "single.example.com".as_bytes();
const ONE: NonZero<u32> = NonZero::<u32>::MIN;
let mut state_single = credential.presentation_state(ONE, single_ctx).unwrap();
let single_presentation = state_single.present(csrng).unwrap();
result = single_presentation.verify(&key, request_ctx, ONE, single_ctx);
assert!(result.is_ok());
}
25 changes: 25 additions & 0 deletions tests/test_protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2025 Cloudflare, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod protocol;

#[cfg(feature = "suite_p256")]
mod p256 {
crate::all_arc_tests!(arc::suites::P256);
}

#[cfg(feature = "suite_ristretto255")]
mod ristretto255 {
crate::all_arc_tests!(arc::suites::Ristretto255);
}
35 changes: 35 additions & 0 deletions tests/test_vectors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2025 Cloudflare, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

mod vectors;

#[cfg(feature = "suite_p256")]
mod p256 {
use crate::vectors::{SuiteVector, test_vectors_arc};

#[derive(serde::Deserialize)]
struct Vector {
#[serde(rename = "ARCV1-P256")]
suite: SuiteVector,
}

#[test]
fn test_vectors() {
let vectors_json = include_str!("../tests/vectors/arc_v01.json");
let v = serde_json::from_str::<Vector>(vectors_json)
.map_err(|e| format!("JSON parsing error: {e}"))
.unwrap();
test_vectors_arc::<arc::suites::P256>(v.suite)
}
}
69 changes: 69 additions & 0 deletions tests/vectors/arc_v01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"ARCV1-P256": {
"Credential": {
"U": "0318d861aacf3f42ad356c3c0ad90184a8717762274bcdf4faa1f9d237e0b056b4",
"U_prime": "02bed79f62edabf8833b105630f27a83c708304638ccf2b350654adb6cc7de8652",
"X1": "03913c27f2a2f50bfb0895d9f56aa583eb00be102f7a7ac662896fc0683a0223c3",
"m1": "141c4ca5e614af8e5e323eb47a7e76737de803fb5c0fc5386685ff1d369e2a06"
},
"CredentialRequest": {
"m1": "141c4ca5e614af8e5e323eb47a7e76737de803fb5c0fc5386685ff1d369e2a06",
"m1_enc": "0299cbf0f4c53787aebac181db3e2e93e3a3b82a48c25edd0e08db490578c4ba13",
"m2": "911fb315257d9ae29d47ecb48c6fa27074dee6860a0489f8db6ac9a486be6a3e",
"m2_enc": "03a586d0d40f4548015e9b12d054ff953a021737ea7c631ea1757590ef6876fc10",
"proof": "eb7dfe77ee80364ed4452cdf2993cb74ece96ea6710ab80dabcae115f852da40b5a17902677f864fc74a2efea001126e2b78d83de226029be276801404c06030b744f4c9d81281400f430f01d9e2404b6c54f8096fc5e8052de94da3717b2ca5432958bf729822a5dcd43644baf960b822ac6bec5db28ffc192f85c9088037f8d401b3d1ae84b8fa8885e4838e5ef4d9d3a11a939342f455c43fdd66f2faa3a9",
"r1": "5c183d2dea942eb2780afb90cfd9498387426c25ef582e0744c7ba0d65145379",
"r2": "044d4a5b5daf00dd1fb4444ca2f8c3f9da1f750850131b376056f1c1e86a76a0",
"request_context": "74657374207265717565737420636f6e74657874"
},
"CredentialResponse": {
"H_aux": "03e1e59f959c25774f3dfa5a23feac65243caf1faa716eab007ceeeb035f07414d",
"U": "0318d861aacf3f42ad356c3c0ad90184a8717762274bcdf4faa1f9d237e0b056b4",
"X0_aux": "03f1b707708c30fba247be2ca8aafa0873292f545d21389981533ef00d65300be8",
"X1_aux": "02567d76f0bd5015f6b8aa3307e03c3f71e70a323f3eb3badf386a2e8f9022342f",
"X2_aux": "034c380139f46b2ad96830f092f655a3d8a33f3760d1d9efb0459e397eb8b82854",
"b": "9ac9d836ef405f4c6c1de4de18d210c8496beca2fd39c4819c5d626a9aea1043",
"enc_U_prime": "037f41cad28206c524627829d9e9e8a64969c33ae6c6016314d986ff1e1634d763",
"proof": "2d8a68a4b7b74f3dc73f9a7340cc52d46b8c8131febb428675184eb6a2e0df3b6cf7804399c3592ed175e5fad01f4aec82b5b90c4a78e5db0cd6f2fe09e238312e8d407c1cce94f17b03a7354b7b6e8d46850282477793afef80eb18720852550678b87883070092a5b2858a2b413595e4ac3b947fc7d4b3fe546efe4d4d01973cfac7cd43e6e79ed8fe97a41b643c2451600acf3213ee65071b2f9549bc30e28390aea744983a22229618ff4abac9453e9245554584a6fea851d6809b1935ba4c0d00a751d9689793db574ee7204a5a3df4017e55681da53e62d289f63aafe6606febd903b267865a10b6ae454c0f42619a3c26b7928265b5a27f3cee4b050f"
},
"Presentation1": {
"D_0": "022b4db8497de8afd15c0263986ce3c993a001bdb12a2b7a2a2c0df54c55aa17dd",
"U": "0387a786795f7d8891d0f61e62f34e00937a1b40fcfe3dc9cfafc3d371d6d6c9bf",
"U_prime_commit": "02e0ea4889c2837d3ed2442e1cfb9e766f156ee1bdf7d21468d09ff262e7424303",
"a": "a3c469d2d55062b463b17f45acd2fb16ee659265fc900356e553487ea846e9e7",
"m1_commit": "039dc93a94798626512cddeb21b79991d2760672f1ef285bca87853bbf4b432a1a",
"nonce": "00",
"nonce_blinding": "ed8b643e3c8ba74cc417b5bbfc4f42be8b4e6197ad59bed42269dc781f679d66",
"nonce_commit": "022b4db8497de8afd15c0263986ce3c993a001bdb12a2b7a2a2c0df54c55aa17dd",
"presentation_context": "746573742070726573656e746174696f6e20636f6e74657874",
"proof": "022b4db8497de8afd15c0263986ce3c993a001bdb12a2b7a2a2c0df54c55aa17ddd146335145e067552bbc98042918c893fe1c9dc0a003c6aab72bc02cf4c87c11c814099ed225a8f456aad03f72108f14f16fdf19d0ad3452e1e9cbd77a78df696b76a67c5b1fa7ed45499a55bacb1aee3d0d7f323f6323211ef4702b6db894e5e7ee14c0d9bcbcbc8b4d4b9b5a40aaaea0597c6a3b1f4e572bb1a153277e55566bbdb55b649021bf2f777b9130c2e375f560eee4691d04bd38e9571d94512578273265a40e847145a13f430bfe3eae2a2beffd05f427e88587709ce713a16fc9acc4f2ba0d724f2bfd182d5df4d038e74b5c35cc7c4aa7622c2682e040877eeb9a7251a638a8246070fbda88656b25c244bf273ba82fe17f2d74f66601aaf7ad399c8dd24728e580d5d9691a90214fa3154365c47657e54cc7bb97eb008d2828",
"r": "54925f70e9ec2128114c6ae8bfc6e1a1bf7ee4e261cbf119c0d5447a2eb0e934",
"tag": "036c46e86eac84c68806dad845a5be5d007263285022e3c98e298f97205e831ead",
"z": "9ed3c3ddb1b5ff64e55b1d0a4f58eee5c7472f211e0e8ccf4ab17a2f784d287c"
},
"Presentation2": {
"D_0": "0383476a5a3d85aaa47e07023ce7ab6aa2ae775c3f6bed5e34924dfe9cc3deae14",
"U": "03e867857097728b99446da74095f6873142386996e8ce283479d1521ce94786e7",
"U_prime_commit": "028cd873d34082ca4952bb51c9a973a87348d44781a314506740d02baf144062ab",
"a": "ae14ddaf96907f2fee72069664e1883f00ef5c86e447ceac3a192fd3df7a44c1",
"m1_commit": "039c9d565204fc5d43ffd4c6eaa81172fffe5d34dadc2cf9cb9826e507dc96179f",
"nonce": "01",
"nonce_blinding": "90b8387fe4145c2d47a0f042c2611992bc4de48f81168a0d94b56c69ea0efff2",
"nonce_commit": "0383476a5a3d85aaa47e07023ce7ab6aa2ae775c3f6bed5e34924dfe9cc3deae14",
"presentation_context": "746573742070726573656e746174696f6e20636f6e74657874",
"proof": "0383476a5a3d85aaa47e07023ce7ab6aa2ae775c3f6bed5e34924dfe9cc3deae14bf818dad5dd87e9633e2384cd28774f8df1d839461291b880172b3c91f1630e711d4e62706768fec64f33d8d707cb11b9706c41c25de93be8a8233ab921733a387a45d3a857a5a556657745b1f86f15786d19917aeefcca8a0a9a3f8180abd29c0b5c0d8d65141618a71cc2bbf94c15dc3d7026aa011162aebf8f299d984dde281f6d4ceae8597deb73ea6379cd66934f54ff3c8973773bf9f6a9cf983d6b73c1edbc20ff037b3535191eca66ff05f913bebf530f560d41d530960eaea8f6a6c23316ae50ae38ef06d96b9865b0a6c268be1b27e23e6890be68f81fe7a4779ff74e5835492bb618a16463b97ec7beae9da0c03e79dad8b5addb41739077db27d320ec9e1019260efe2cbf6e9cba6871ffee3b5566d5e865c729c5e1d48529559",
"r": "f994bf66d0c7943ce97331da186e2311f8d2399bbbfe87f0eaaa7d50f7453630",
"tag": "03d790d0a88e92eb010554de4a327e0228773c8e0d865a86762f06a4900bc7d93b",
"z": "fa27efc5066bca91121642d629477eb10b5437eb07cd2783e9537c0ee7736dac"
},
"ServerKey": {
"X0": "02984e3a37e21054d2e54614acb7f95a624afc7eb22de66131bf458fa478e2f62b",
"X1": "03913c27f2a2f50bfb0895d9f56aa583eb00be102f7a7ac662896fc0683a0223c3",
"X2": "0340bf069b5aa61019d6c6d826e7f5dc6f6767a288c5bc1738a7a9fa5bc3cf3d5f",
"x0": "1008f2c706ae2157c75e41b2d75695c7571644a476f2fb1ecc13f7e2c30b80a5",
"x1": "526e009578f6f25fdec992343f09f5e61aeedef029d8f9d0194746a1da89e7b5",
"x2": "549075ccd3d1c36b3546725c43e71942978901a9171ef7e771c86ca7b1140906",
"xb": "7276533ce3c89f04a007c2e8aa7d2e3abe4cb92695630c7d4575d16c47071974"
}
}
}
Loading