#[encrypted_library]
mod arcis_library {

    /// Owner for shared data between the owner of a public key and the MXE.
    ///
    /// Decrypting data owned by this owner requires participation from either:
    /// * the whole MXE
    /// * the public key owner.
    #[derive(Debug, PartialEq)]
    pub struct Shared {
        pub public_key: ArcisX25519Pubkey,
        pub(crate) nonce: u128,
    }
    impl Shared {
        /// Creates an owner for shared data between the MXE and the public key owner.
        pub fn new(public_key: ArcisX25519Pubkey) -> Self {
            let nonce = ArcisRNG::gen_public_integer_from_width(128);
            Self { public_key, nonce }
        }
    }
    /// Owner of secret data.
    /// Decrypting data owned by this owner requires participation from all nodes that comprise
    /// the MXE cluster.
    #[derive(Debug, PartialEq)]
    pub struct Mxe {
        pub(crate) nonce: u128,
    }
    impl Mxe {
        /// Generate a nonce for the MXE. Used to encrypt data which can only be decrypted by the
        /// nodes that comprise the cluster attached to the given MXE.
        pub fn get() -> Self {
            let nonce = ArcisRNG::gen_public_integer_from_width(128);
            Self { nonce }
        }
    }

    pub(crate) fn base58_to_32_uint8array(s: &[u8]) -> [u8; 32] {
        fn base58_to_u8(c: u8) -> u8 {
            #![allow(clippy::manual_range_contains)]
            if b'1' <= c && c <= b'9' {
                c - b'1'
            } else if b'A' <= c && c <= b'H' {
                c - b'A' + 9
            } else if b'J' <= c && c <= b'N' {
                c - b'J' + 17
            } else if b'P' <= c && c <= b'Z' {
                c - b'P' + 22
            } else if b'a' <= c && c <= b'k' {
                c - b'a' + 33
            } else if b'm' <= c && c <= b'z' {
                c - b'm' + 44
            } else {
                // Maybe some kind of assert or panic would be good.
                127
            }
        }
        fn op(a: &mut [u8; 32], factor: u8, carry: u8) {
            let mut carry = carry as u16;
            for a in a.iter_mut() {
                let num = *a as u16 * factor as u16 + carry;
                carry = num / 256;
                *a = (num % 256) as u8;
            }
        }
        let mut address = [0u8; 32];
        for &c in s {
            let b = base58_to_u8(c);
            op(&mut address, 58, b);
        }
        address.reverse();
        address
    }

    impl ArcisX25519Pubkey {
        /// Creates an Arcis public key based on the base58-encoding of the Montgomery x-coordinate.
        pub fn from_base58(s: &[u8]) -> Self {
            let arr = base58_to_32_uint8array(s);
            Self::from_uint8(&arr)
        }
        /// Creates an Arcis public key based on the uint8array-encoding of the Montgomery
        /// x-coordinate.
        pub fn from_uint8(s: &[u8]) -> Self {
            if s.len() > 32 {
                arcis_static_panic!("Length of arg in `from_uint8` is too big.");
            }
            let two_power_eight = BaseField25519::from_u64(256);
            let mut x = BaseField25519::from_u8(0);
            let mut factor = BaseField25519::from_u8(1);
            for &b in s {
                x += BaseField25519::from_u8(b) * factor;
                factor *= two_power_eight;
            }
            Self::new_from_x(x)
        }
    }

    impl<T: ArcisType> EncData<T> {
        /// This does the same thing as `.to_arcis()` on the matching `Enc`.
        ///
        /// This function exists for performance reasons: imagine you have an `#[instruction]`
        /// with inputs `t: Enc<Shared, T>` and `u: Enc<Shared, U>` that have the same pubkey.
        /// Arcis cannot know the pubkeys are equal and will have duplicate gates for each pubkey.
        /// You have two ways of fixing the performance hit:
        /// * Refactoring your `#[instruction]` inputs into `tu: Enc<Shared, (T, U)>`. But that may
        ///   require to change the smart contract and the whole way your app flows.
        /// * Refactoring your `#[instruction]` inputs into `key: ArcisX25519Pubkey, t_nonce: u128,
        ///   t: EncData<T>, u_nonce: u128, u: EncData<U>`. This change will only require to change
        ///   the caller and the callee.
        /// The first solution is slightly better in terms of performance if you can do it.
        pub fn to_arcis_with_pubkey_and_nonce(
            self,
            public_key: ArcisX25519Pubkey,
            nonce: u128,
        ) -> T {
            let owner = Shared { public_key, nonce };
            Enc { owner, data: self }.to_arcis()
        }
    }
}

