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
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::prelude::{Deserialize, DeserializeOwned, Serialize};
use snarkvm_curves::{
    bls12_377::Bls12_377,
    edwards_bls12::{EdwardsAffine, EdwardsParameters},
    AffineCurve,
    MontgomeryParameters,
    PairingEngine,
    ProjectiveCurve,
    TwistedEdwardsParameters,
};
use snarkvm_fields::{PrimeField, SquareRootField};
use snarkvm_utilities::BigInteger;

use core::{fmt::Debug, hash::Hash};

pub trait Environment:
    'static + Copy + Clone + Debug + PartialEq + Eq + Hash + Serialize + DeserializeOwned + Send + Sync
{
    type Affine: AffineCurve<
            Projective = Self::Projective,
            BaseField = Self::Field,
            ScalarField = Self::Scalar,
            Coordinates = (Self::Field, Self::Field),
        >;
    type BigInteger: BigInteger;
    type Field: PrimeField<BigInteger = Self::BigInteger> + SquareRootField + Copy;
    type PairingCurve: PairingEngine<Fr = Self::Field>;
    type Projective: ProjectiveCurve<Affine = Self::Affine, BaseField = Self::Field, ScalarField = Self::Scalar>;
    type Scalar: PrimeField<BigInteger = Self::BigInteger> + Copy;

    /// The coefficient `A` of the twisted Edwards curve.
    const EDWARDS_A: Self::Field;
    /// The coefficient `D` of the twisted Edwards curve.
    const EDWARDS_D: Self::Field;

    /// The coefficient `A` of the Montgomery curve.
    const MONTGOMERY_A: Self::Field;
    /// The coefficient `B` of the Montgomery curve.
    const MONTGOMERY_B: Self::Field;

    /// The maximum number of bytes allowed in a string.
    const MAX_STRING_BYTES: u32 = u8::MAX as u32;

    /// Halts the program from further synthesis, evaluation, and execution in the current environment.
    fn halt<S: Into<String>, T>(message: S) -> T {
        panic!("{}", message.into())
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Console;

impl Environment for Console {
    type Affine = EdwardsAffine;
    type BigInteger = <Self::Field as PrimeField>::BigInteger;
    type Field = <Self::Affine as AffineCurve>::BaseField;
    type PairingCurve = Bls12_377;
    type Projective = <Self::Affine as AffineCurve>::Projective;
    type Scalar = <Self::Affine as AffineCurve>::ScalarField;

    /// The coefficient `A` of the twisted Edwards curve.
    const EDWARDS_A: Self::Field = <EdwardsParameters as TwistedEdwardsParameters>::EDWARDS_A;
    /// The coefficient `D` of the twisted Edwards curve.
    const EDWARDS_D: Self::Field = <EdwardsParameters as TwistedEdwardsParameters>::EDWARDS_D;
    /// The coefficient `A` of the Montgomery curve.
    const MONTGOMERY_A: Self::Field = <EdwardsParameters as MontgomeryParameters>::MONTGOMERY_A;
    /// The coefficient `B` of the Montgomery curve.
    const MONTGOMERY_B: Self::Field = <EdwardsParameters as MontgomeryParameters>::MONTGOMERY_B;
}