Skip to content

Commit

Permalink
More uniform API
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaker committed Jan 30, 2024
1 parent e415ef8 commit 4a2c9d6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/arthur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
R: RngCore + CryptoRng,
{
#[inline(always)]
pub fn add(&mut self, input: &[U]) -> Result<(), IOPatternError> {
pub fn add_units(&mut self, input: &[U]) -> Result<(), IOPatternError> {
// let serialized = bincode::serialize(input).unwrap();
// self.arthur.sponge.absorb_unchecked(&serialized);
let old_len = self.transcript.len();
Expand Down Expand Up @@ -142,7 +142,7 @@ where
{
fn public_units(&mut self, input: &[U]) -> Result<(), IOPatternError> {
let len = self.transcript.len();
self.add(input)?;
self.add_units(input)?;
self.transcript.truncate(len);
Ok(())
}
Expand Down Expand Up @@ -172,6 +172,6 @@ where
{
#[inline(always)]
fn add_bytes(&mut self, input: &[u8]) -> Result<(), IOPatternError> {
self.add(input)
self.add_units(input)
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
//! let io = IOPattern::<Keccak>::new("example-protocol").absorb(1, "send").squeeze(16, "receive");
//! let mut arthur = io.to_arthur();
//! // the prover sends the byte 0x42.
//! arthur.add(&[0x42]).expect("Absorbing one byte");
//! arthur.add_bytes(&[0x42]).expect("Absorbing one byte");
//! // the prover receive a 128-bit challenge.
//! let mut chal = [0u8; 16];
//! arthur.fill_challenge_bytes(&mut chal).expect("Squeezing 128 bits");
Expand Down
12 changes: 6 additions & 6 deletions src/merlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ impl<'a, U: Unit, H: DuplexHash<U>> Merlin<'a, H, U> {
}

/// Read `input.len()` elements from the transcript.
#[inline(always)]
pub fn fill_next(&mut self, input: &mut [U]) -> Result<(), IOPatternError> {
#[inline]
pub fn fill_next_units(&mut self, input: &mut [U]) -> Result<(), IOPatternError> {
U::read(&mut self.transcript, input)?;
self.safe.absorb(input)?;
Ok(())
}

/// Signals the end of the statement.
#[inline(always)]
#[inline]
pub fn ratchet(&mut self) -> Result<(), IOPatternError> {
self.safe.ratchet()
}

/// Signals the end of the statement and returns the (compressed) sponge state.
#[inline(always)]
#[inline]
pub fn preprocess(self) -> Result<&'static [U], IOPatternError> {
self.safe.preprocess()
}
Expand All @@ -67,8 +67,8 @@ impl<'a, H: DuplexHash<U>, U: Unit> core::fmt::Debug for Merlin<'a, H, U> {
}

impl<'a, H: DuplexHash<u8>> ByteReader for Merlin<'a, H, u8> {
#[inline(always)]
#[inline]
fn fill_next_bytes(&mut self, input: &mut [u8]) -> Result<(), IOPatternError> {
self.fill_next(input)
self.fill_next_units(input)
}
}
4 changes: 2 additions & 2 deletions src/plugins/ark/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
let mut buf = vec![0u8; point_size];

for o in output.iter_mut() {
self.fill_next(&mut buf)?;
self.fill_next_units(&mut buf)?;
*o = G::deserialize_compressed(buf.as_slice())?;
}
Ok(())
Expand All @@ -48,7 +48,7 @@ where
H: DuplexHash<Fp<C, N>>,
{
fn fill_next_scalars(&mut self, output: &mut [Fp<C, N>]) -> crate::ProofResult<()> {
self.fill_next(output)?;
self.fill_next_units(output)?;
Ok(())
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ fn test_merlin() {
.squeeze(10, "bye bye");

let mut arthur = Arthur::<Keccak>::from(&io);
arthur.add(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).unwrap();
arthur.add_units(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).unwrap();
arthur.fill_challenge_bytes(&mut [0u8; 10]).unwrap();
let transcript = arthur.transcript();

let mut merlin = Merlin::<Keccak>::new(&io, transcript);
let mut input = [0u8; 5];
merlin.fill_next(&mut input).unwrap();
merlin.fill_next_units(&mut input).unwrap();
assert_eq!(input, [0, 1, 2, 3, 4]);
merlin.fill_next(&mut input).unwrap();
merlin.fill_next_units(&mut input).unwrap();
assert_eq!(input, [5, 6, 7, 8, 9]);
}

0 comments on commit 4a2c9d6

Please sign in to comment.