common.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //! Basic types for the DORAM implementation.
  2. use communicator::Error as CommunicationError;
  3. use ff::PrimeField;
  4. /// Type of an access operation.
  5. pub enum Operation {
  6. /// Read from the memory.
  7. Read,
  8. /// Write to the memory.
  9. Write,
  10. }
  11. impl Operation {
  12. /// Encode an access operation as field element.
  13. ///
  14. /// Read is encoded as 0, and Write is encoded as 1.
  15. pub fn encode<F: PrimeField>(&self) -> F {
  16. match self {
  17. Self::Read => F::ZERO,
  18. Self::Write => F::ONE,
  19. }
  20. }
  21. /// Decode an encoded operation again.
  22. pub fn decode<F: PrimeField>(encoded_op: F) -> Self {
  23. if encoded_op == F::ZERO {
  24. Self::Read
  25. } else if encoded_op == F::ONE {
  26. Self::Write
  27. } else {
  28. panic!("invalid value")
  29. }
  30. }
  31. }
  32. /// Define an additive share of an access instruction.
  33. #[derive(Clone, Copy, Debug, Default)]
  34. pub struct InstructionShare<F: PrimeField> {
  35. /// Whether it is a read (0) or a write (1).
  36. pub operation: F,
  37. /// The address to access.
  38. pub address: F,
  39. /// The value that should (possibly) be written into memory.
  40. pub value: F,
  41. }
  42. /// Custom error type used in this library.
  43. #[derive(Debug)]
  44. pub enum Error {
  45. /// Wrap a [`communicator::Error`].
  46. CommunicationError(CommunicationError),
  47. }
  48. impl From<CommunicationError> for Error {
  49. fn from(e: CommunicationError) -> Self {
  50. Error::CommunicationError(e)
  51. }
  52. }