flexible_transcript/
tests.rs

1use crate::Transcript;
2
3/// Test the sanity of a transcript.
4///
5/// This will panic if sanity checks fail.
6pub fn test_transcript<T: Transcript<Challenge: PartialEq>>() {
7  // Ensure distinct names cause distinct challenges
8  {
9    let mut t1 = T::new(b"1");
10    let mut t2 = T::new(b"2");
11    assert!(t1.challenge(b"c") != t2.challenge(b"c"));
12  }
13
14  // Ensure names can't lead into labels
15  {
16    let mut t1 = T::new(b"12");
17    let c1 = t1.challenge(b"c");
18    let mut t2 = T::new(b"1");
19    let c2 = t2.challenge(b"2c");
20    assert!(c1 != c2);
21  }
22
23  let t = || T::new(b"name");
24  let c = |mut t: T| t.challenge(b"c");
25
26  // Ensure domain separators do something
27  {
28    let mut t1 = t();
29    t1.domain_separate(b"d");
30    assert!(c(t1) != c(t()));
31  }
32
33  // Ensure distinct domain separators create distinct challenges
34  {
35    let mut t1 = t();
36    let mut t2 = t();
37    t1.domain_separate(b"d1");
38    t2.domain_separate(b"d2");
39    assert!(c(t1) != c(t2));
40  }
41
42  // Ensure distinct messages create distinct challenges
43  {
44    // By label
45    {
46      let mut t1 = t();
47      let mut t2 = t();
48      t1.append_message(b"msg", b"a");
49      t2.append_message(b"msg", b"b");
50      assert!(c(t1) != c(t2));
51    }
52
53    // By value
54    {
55      let mut t1 = t();
56      let mut t2 = t();
57      t1.append_message(b"a", b"val");
58      t2.append_message(b"b", b"val");
59      assert!(c(t1) != c(t2));
60    }
61  }
62
63  // Ensure challenges advance the transcript
64  {
65    let mut t = t();
66    let c1 = t.challenge(b"c");
67    let c2 = t.challenge(b"c");
68    assert!(c1 != c2);
69  }
70
71  // Ensure distinct challenge labels produce distinct challenges
72  assert!(t().challenge(b"a") != t().challenge(b"b"));
73
74  // Ensure RNG seed calls advance the transcript
75  {
76    let mut t = t();
77    let s1 = t.rng_seed(b"s");
78    let s2 = t.rng_seed(b"s");
79    assert!(s1 != s2);
80  }
81
82  // Ensure distinct RNG seed labels produce distinct seeds
83  assert!(t().rng_seed(b"a") != t().rng_seed(b"b"));
84}
85
86#[test]
87fn test_digest() {
88  test_transcript::<crate::DigestTranscript<sha2::Sha256>>();
89  test_transcript::<crate::DigestTranscript<blake2::Blake2b512>>();
90}
91
92#[cfg(feature = "recommended")]
93#[test]
94fn test_recommended() {
95  test_transcript::<crate::RecommendedTranscript>();
96}
97
98#[cfg(feature = "merlin")]
99#[test]
100fn test_merlin() {
101  test_transcript::<crate::MerlinTranscript>();
102}