cairo_vm/hint_processor/builtin_hint_processor/vrf/
pack.rs

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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use num_bigint::BigInt;
use num_integer::Integer;
use num_traits::One;

use crate::hint_processor::builtin_hint_processor::secp::bigint_utils::BigInt3;
use crate::hint_processor::builtin_hint_processor::secp::secp_utils::SECP_P_V2;
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::math_utils::div_mod;
use crate::serde::deserialize_program::ApTracking;
use crate::stdlib::collections::HashMap;
use crate::stdlib::prelude::String;
use crate::types::exec_scope::ExecutionScopes;
use crate::vm::errors::hint_errors::HintError;
use crate::vm::vm_core::VirtualMachine;

/// Implements hint:
/// ```python
/// from starkware.cairo.common.cairo_secp.secp_utils import pack
/// SECP_P=2**255-19
///
/// x = pack(ids.x, PRIME) % SECP_P
/// ```
pub fn ed25519_is_zero_pack(
    vm: &mut VirtualMachine,
    exec_scopes: &mut ExecutionScopes,
    ids_data: &HashMap<String, HintReference>,
    ap_tracking: &ApTracking,
) -> Result<(), HintError> {
    let x = BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?.pack86();
    exec_scopes.insert_value("x", x.mod_floor(&SECP_P_V2));
    exec_scopes.insert_value("SECP_P", SECP_P_V2.clone());

    Ok(())
}

/// Implements hint:
/// ```python
/// from starkware.cairo.common.cairo_secp.secp_utils import pack
/// SECP_P=2**255-19
///
/// value = pack(ids.x, PRIME) % SECP_P
/// ```
pub fn ed25519_reduce(
    vm: &mut VirtualMachine,
    exec_scopes: &mut ExecutionScopes,
    ids_data: &HashMap<String, HintReference>,
    ap_tracking: &ApTracking,
) -> Result<(), HintError> {
    let x = BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?.pack86();
    exec_scopes.insert_value("value", x.mod_floor(&SECP_P_V2));
    exec_scopes.insert_value("SECP_P", SECP_P_V2.clone());

    Ok(())
}

/// Implements hint:
/// ```python
/// SECP_P=2**255-19
/// from starkware.python.math_utils import div_mod
///
/// value = x_inv = div_mod(1, x, SECP_P)
/// ```
pub fn ed25519_is_zero_assign_scope_vars(
    exec_scopes: &mut ExecutionScopes,
) -> Result<(), HintError> {
    let x = exec_scopes.get::<BigInt>("x")?;
    let x_inv = div_mod(&BigInt::one(), &x, &SECP_P_V2)?;
    exec_scopes.insert_value("x_inv", x_inv.clone());
    exec_scopes.insert_value("value", x_inv);
    exec_scopes.insert_value("SECP_P", SECP_P_V2.clone());

    Ok(())
}

#[cfg(test)]
mod test {
    use crate::any_box;
    use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
    use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
    use crate::hint_processor::builtin_hint_processor::hint_code;
    use crate::hint_processor::builtin_hint_processor::secp::secp_utils::SECP_P_V2;
    use crate::hint_processor::hint_processor_definition::HintProcessorLogic;
    use crate::hint_processor::hint_processor_definition::HintReference;
    use crate::stdlib::collections::HashMap;
    use crate::types::exec_scope::ExecutionScopes;
    use crate::utils::test_utils::*;
    use num_bigint::BigInt;
    use num_traits::One;
    use num_traits::Zero;

    static SECP_P_D0: i128 = 77371252455336267181195245_i128;
    static SECP_P_D1: i128 = 77371252455336267181195263_i128;
    static SECP_P_D2: i128 = 9671406556917033397649407_i128;

    fn assert_is_zero_pack_ed25519_equals(x_d0: i128, x_d1: i128, x_d2: i128, expected: BigInt) {
        let ids_data = non_continuous_ids_data![("x", 0)];

        let mut vm = vm!();
        vm.run_context.fp = 0;

        vm.segments = segments![((1, 0), x_d0), ((1, 1), x_d1), ((1, 2), x_d2)];

        let mut exec_scopes = scope![];
        assert!(run_hint!(
            vm,
            ids_data,
            hint_code::IS_ZERO_PACK_ED25519,
            &mut exec_scopes
        )
        .is_ok());

        check_scope!(
            &exec_scopes,
            [("x", expected), ("SECP_P", SECP_P_V2.clone())]
        );
    }

    fn assert_reduce_ed25519_equals(x_d0: i128, x_d1: i128, x_d2: i128, expected: BigInt) {
        let ids_data = non_continuous_ids_data![("x", 0)];

        let mut vm = vm!();
        vm.run_context.fp = 0;

        vm.segments = segments![((1, 0), x_d0), ((1, 1), x_d1), ((1, 2), x_d2)];

        let mut exec_scopes = scope![];

        assert!(run_hint!(vm, ids_data, hint_code::REDUCE_ED25519, &mut exec_scopes).is_ok());

        check_scope!(
            &exec_scopes,
            [("value", expected), ("SECP_P", SECP_P_V2.clone())]
        );
    }

    #[test]
    fn test_is_zero_pack_ed25519_with_zero() {
        assert_is_zero_pack_ed25519_equals(0, 0, 0, BigInt::zero());
    }

    #[test]
    fn test_is_zero_pack_ed25519_with_secp_prime_minus_one() {
        assert_is_zero_pack_ed25519_equals(
            SECP_P_D0 - 1,
            SECP_P_D1,
            SECP_P_D2,
            SECP_P_V2.clone() - 1,
        );
    }

    #[test]
    fn test_is_zero_pack_ed25519_with_secp_prime() {
        assert_is_zero_pack_ed25519_equals(SECP_P_D0, SECP_P_D1, SECP_P_D2, BigInt::zero());
    }

    #[test]
    fn test_reduce_ed25519_with_zero() {
        assert_reduce_ed25519_equals(0, 0, 0, BigInt::zero());
    }

    #[test]
    fn test_reduce_ed25519_with_prime_minus_one() {
        assert_reduce_ed25519_equals(SECP_P_D0 - 1, SECP_P_D1, SECP_P_D2, SECP_P_V2.clone() - 1);
    }

    #[test]
    fn test_reduce_ed25519_with_prime() {
        assert_reduce_ed25519_equals(SECP_P_D0, SECP_P_D1, SECP_P_D2, BigInt::zero());
    }

    #[test]
    fn test_is_zero_assign_scope_vars_ed25519_with_one() {
        let mut vm = vm!();
        vm.run_context.fp = 0;

        let mut exec_scopes = scope![("x", BigInt::one())];

        assert!(run_hint!(
            vm,
            HashMap::default(),
            hint_code::IS_ZERO_ASSIGN_SCOPE_VARS_ED25519,
            &mut exec_scopes
        )
        .is_ok());

        check_scope!(
            &exec_scopes,
            [
                ("x_inv", BigInt::one()),
                ("value", BigInt::one()),
                ("SECP_P", SECP_P_V2.clone())
            ]
        );
    }
}