1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

//! zk app

use ark_ff::{BigInteger256, One, Zero};
use derive_more::{From, Into};
use once_cell::sync::OnceCell;
use proof_systems::{
    mina_hasher::{self, Fp, Hashable, Hasher, ROInput},
    ChunkedROInput, ToChunkedROInput,
};

/// FIXME: Doc
#[derive(Default, Debug, Clone, From, Into)]
pub struct ZkApp(());

impl<'a> ZkApp {
    /// Get a borrow of the default value
    pub fn borrow_default() -> &'a Self {
        static INSTANCE: OnceCell<ZkApp> = OnceCell::new();
        INSTANCE.get_or_init(Self::default)
    }
}

impl ToChunkedROInput for ZkApp {
    fn to_chunked_roinput(&self) -> ChunkedROInput {
        // FIXME: This is only for default hash
        // 12935064460869035604753254773225484359407575580289870070671311469994328713165
        const FP1: BigInteger256 = BigInteger256::new([
            17818364107179681741,
            14791875366151232437,
            6753084239224090698,
            2060674656258731539,
        ]);
        // 15836741414052211301983886193856353162526040956490609761139212467629447291325
        const FP2: BigInteger256 = BigInteger256::new([
            17064760232198382013,
            7053532856486746066,
            14503003448090529760,
            2522938464542289201,
        ]);
        ChunkedROInput::new()
            .append_field(FP1.into())
            .append_field(FP1.into())
            .append_field(FP1.into())
            .append_field(FP1.into())
            .append_field(FP1.into())
            .append_field(FP2.into())
            .append_field(Fp::zero())
            .append_field(Fp::zero())
            .append_field(Fp::zero())
            .append_field(Fp::zero())
            .append_field(Fp::zero())
            .append_field(Fp::zero())
            .append_field(Fp::zero())
            .append_field(Fp::zero())
            .append_packed(Fp::zero(), 1)
            .append_packed(Fp::zero(), 32)
            .append_packed(Fp::zero(), 32)
    }
}

/// Wrapper of [Option<ZkApp>] that implements [Hashable]
#[derive(Debug, Clone)]
pub struct ZkAppOptionHashableWrapper<'a>(pub &'a Option<ZkApp>);

impl<'a> Hashable for ZkAppOptionHashableWrapper<'a> {
    type D = ();

    fn to_roinput(&self) -> ROInput {
        if let Some(v) = self.0 {
            v
        } else {
            ZkApp::borrow_default()
        }
        .roinput()
    }

    fn domain_string(_: Self::D) -> Option<String> {
        Some("CodaZkappAccount".into())
    }
}

impl<'a> ToChunkedROInput for ZkAppOptionHashableWrapper<'a> {
    fn to_chunked_roinput(&self) -> ChunkedROInput {
        ChunkedROInput::new().append_field({
            let mut hasher = mina_hasher::create_kimchi(());
            hasher.hash(self)
        })
    }
}

/// FIXME: Doc
#[derive(Default, Debug, Clone, From, Into)]
pub struct ZkAppUri(());

impl<'a> ZkAppUri {
    /// Get a borrow of the default value
    pub fn borrow_default() -> &'a Self {
        static INSTANCE: OnceCell<ZkAppUri> = OnceCell::new();
        INSTANCE.get_or_init(Self::default)
    }
}

impl ToChunkedROInput for ZkAppUri {
    fn to_chunked_roinput(&self) -> ChunkedROInput {
        // FIXME: This is only for default hash
        ChunkedROInput::new().append_packed(Fp::one(), 1)
    }
}

/// Wrapper of [Option<ZkAppUri>] that implements [Hashable]
#[derive(Debug, Clone)]
pub struct ZkAppUriOptionHashableWrapper<'a>(pub &'a Option<ZkAppUri>);

impl<'a> Hashable for ZkAppUriOptionHashableWrapper<'a> {
    type D = ();

    fn to_roinput(&self) -> ROInput {
        if let Some(v) = self.0 {
            v
        } else {
            ZkAppUri::borrow_default()
        }
        .roinput()
    }

    fn domain_string(_: Self::D) -> Option<String> {
        Some("MinaZkappUri".into())
    }
}

impl<'a> ToChunkedROInput for ZkAppUriOptionHashableWrapper<'a> {
    fn to_chunked_roinput(&self) -> ChunkedROInput {
        ChunkedROInput::new().append_field({
            let mut hasher = mina_hasher::create_kimchi(());
            hasher.hash(&ZkAppUriOptionHashableWrapper(self.0))
        })
    }
}