Skip to content

Commit 7c44adb

Browse files
committed
Add more tests and documentation to increase coverage.
1 parent 5647935 commit 7c44adb

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

src/arthur.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ where
7171
}
7272
}
7373

74-
impl<U, H, D> From<D> for Arthur<H, U, DefaultRng>
74+
impl<U, H, B> From<B> for Arthur<H, U, DefaultRng>
7575
where
7676
U: Unit,
7777
H: DuplexHash<U>,
78-
D: core::ops::Deref<Target = IOPattern<H, U>>,
78+
B: core::borrow::Borrow<IOPattern<H, U>>,
7979
{
80-
fn from(pattern: D) -> Self {
81-
Arthur::new(pattern.deref(), DefaultRng::default())
80+
fn from(pattern: B) -> Self {
81+
Arthur::new(pattern.borrow(), DefaultRng::default())
8282
}
8383
}
8484

@@ -127,12 +127,12 @@ where
127127
// let serialized = bincode::serialize(input).unwrap();
128128
// self.arthur.sponge.absorb_unchecked(&serialized);
129129
let old_len = self.transcript.len();
130+
self.safe.absorb(input)?;
130131
// write never fails on Vec<u8>
131132
U::write(input, &mut self.transcript).unwrap();
132133
self.rng
133134
.sponge
134135
.absorb_unchecked(&self.transcript[old_len..]);
135-
self.safe.absorb(input)?;
136136

137137
Ok(())
138138
}

src/iopattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<H: DuplexHash<U>, U: Unit> IOPattern<H, U> {
183183

184184
/// Create an [`crate::Arthur`] instance from the IO Pattern.
185185
pub fn to_arthur(&self) -> crate::Arthur<H, U, crate::DefaultRng> {
186-
crate::Arthur::new(self, crate::DefaultRng::default())
186+
self.into()
187187
}
188188

189189
/// Create a [`crate::Merlin`] instance from the IO Pattern and the protocol transcript (bytes).

src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
//! easy an easier inspection of the Fiat-Shamir transform.
2727
//!
2828
//! ```
29-
//! use nimue::IOPattern;
30-
//! use nimue::hash::Keccak;
29+
//! use nimue::{IOPattern, DefaultHash};
3130
//!
32-
//! let io = IOPattern::<Keccak>::new("👩‍💻🥷🏻👨‍💻 building 🔐🔒🗝️")
31+
//! let io = IOPattern::<DefaultHash>::new("👩‍💻🥷🏻👨‍💻 building 🔐🔒🗝️")
3332
//! // this indicates the prover is sending 10 elements (bytes)
3433
//! .absorb(10, "first")
3534
//! // this indicates the verifier is sending 10 elements (bytes)
@@ -57,7 +56,6 @@
5756
//! build the protocol transcript, and seed the private randomness for the prover.
5857
//!
5958
//! ```
60-
//! use nimue::{IOPattern, Arthur};
6159
//! use nimue::*;
6260
//! use rand::Rng;
6361
//!

src/plugins/ark/poseidon/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! This code has been blatantly stolen from `ark-crypto-primitive::sponge``
1+
//! This code has been blatantly stolen from `ark-crypto-primitive::sponge`
22
//! from William Lin, with contributions from Pratyush Mishra, Weikeng Chen, Yuwen Zhang, Kristian Sosnin, Merlyn, Wilson Nguyen, Hossein Moghaddas, and others.
33
use ark_ff::PrimeField;
44

src/tests.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
1+
use rand::RngCore;
2+
13
use crate::hash::keccak::Keccak;
24
use crate::hash::legacy::DigestBridge;
3-
use crate::{Arthur, ByteChallenges, ByteWriter, DuplexHash, IOPattern, Safe};
5+
use crate::{Arthur, ByteChallenges, BytePublic, ByteWriter, DuplexHash, IOPattern, Safe};
46

57
type Sha2 = DigestBridge<sha2::Sha256>;
68
type Blake2b512 = DigestBridge<blake2::Blake2b512>;
79
type Blake2s256 = DigestBridge<blake2::Blake2s256>;
810

9-
/// How should a protocol without IOPattern be handled?
11+
/// How should a protocol without actual IO be handled?
1012
#[test]
1113
fn test_iopattern() {
1214
// test that the byte separator is always added
1315
let iop = IOPattern::<Keccak>::new("example.com");
1416
assert!(iop.as_bytes().starts_with(b"example.com"));
1517
}
1618

19+
20+
/// Test Arthur's rng is not doing completely stupid things.
21+
#[test]
22+
fn test_arthur_rng_basic() {
23+
let iop = IOPattern::<Keccak>::new("example.com");
24+
let mut arthur = iop.to_arthur();
25+
let rng = arthur.rng();
26+
27+
let mut random_bytes = [0u8; 32];
28+
rng.fill_bytes(&mut random_bytes);
29+
let random_u32 = rng.next_u32();
30+
let random_u64 = rng.next_u64();
31+
assert_ne!(random_bytes, [0u8; 32]);
32+
assert_ne!(random_u32, 0);
33+
assert_ne!(random_u64, 0);
34+
assert!(random_bytes.iter().any(|&x| x != random_bytes[0]));
35+
}
36+
37+
38+
#[test]
39+
fn test_arthur_add() {
40+
let iop = IOPattern::<Keccak>::new("example.com").absorb(1, "🥕");
41+
let mut arthur = iop.to_arthur();
42+
assert!(arthur.add_units(&[0u8]).is_ok());
43+
assert!(arthur.add_units(&[1u8]).is_err());
44+
assert_eq!(arthur.transcript(), b"\0", "Protocol Transcript survives errors");
45+
46+
let mut arthur = iop.to_arthur();
47+
assert!(arthur.public_bytes(&[0u8]).is_ok());
48+
assert_eq!(arthur.transcript(), b"");
49+
}
50+
1751
/// A protocol flow that does not match the IOPattern should fail.
1852
#[test]
1953
#[should_panic]

0 commit comments

Comments
 (0)