use crate::poseidon::Poseidon;
use crate::{Error, Fr};
pub struct Duplex;
impl Duplex {
fn cipher_domain() -> Fr {
Poseidon::domain("eid-envelope/cipher/v1")
}
fn run(key: Fr, a: Fr, b: Fr, ctx: Fr, input: &[Fr], encrypt: bool) -> Result<Vec<Fr>, Error> {
let mut state = Poseidon::permute([key, a, b, Self::cipher_domain()]);
state[0] += ctx;
state = Poseidon::permute(state);
let mut out = Vec::with_capacity(input.len());
for block in input.chunks(3) {
for (j, x) in block.iter().enumerate() {
let c = if encrypt { *x + state[j] } else { *x };
out.push(if encrypt { c } else { *x - state[j] });
state[j] = c;
}
state = Poseidon::permute(state);
}
Ok(out)
}
pub fn seal(key: Fr, a: Fr, b: Fr, ctx: Fr, plaintext: &[Fr]) -> Result<Vec<Fr>, Error> {
Self::run(key, a, b, ctx, plaintext, true)
}
pub fn open(key: Fr, a: Fr, b: Fr, ctx: Fr, ciphertext: &[Fr]) -> Result<Vec<Fr>, Error> {
Self::run(key, a, b, ctx, ciphertext, false)
}
}