Skip to main content

midnight_circuits/instructions/
map.rs

1// This file is part of MIDNIGHT-ZK.
2// Copyright (C) Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14//! Map instructions interface.
15//!
16//! It provides (in-circuit) functions for creating a map from a specified input
17//! type into another.
18
19use midnight_proofs::{
20    circuit::{Layouter, Value},
21    plonk::Error,
22};
23
24use crate::{
25    types::{AssignedNative, InnerValue},
26    CircuitField,
27};
28
29/// The set of off-circuit instructions for mapping operations.
30pub trait MapCPU<F, Key, Value> {
31    /// Initializes a new map where all keys point to `default`.
32    fn new(default: &Value) -> Self;
33
34    /// A (cryptographically unequivocal) succinct representation of the map.
35    fn succinct_repr(&self) -> F;
36
37    /// Inserts a new key -> value entry into the map.
38    fn insert(&mut self, key: &Key, value: &Value);
39
40    /// Returns the value associated to a given key.
41    /// Unlike a standard `HashMap` every single key has a value in this
42    /// structure (possibly the default value it was created with).
43    fn get(&self, key: &Key) -> Value;
44}
45
46/// The set of circuit instructions for mapping operations.
47pub trait MapInstructions<F, AssignedKey, AssignedValue>
48where
49    F: CircuitField,
50    AssignedKey: InnerValue,
51    AssignedValue: InnerValue,
52{
53    /// The CPU version of the map.
54    type MapCPU: MapCPU<F, AssignedKey::Element, AssignedValue::Element>;
55
56    /// Initializes a new in-circuit map from the given off-circuit map.
57    fn init(
58        &mut self,
59        layouter: &mut impl Layouter<F>,
60        map: Value<Self::MapCPU>,
61    ) -> Result<(), Error>;
62
63    /// A (cryptographically unequivocal) succinct representation of the map.
64    fn succinct_repr(&self) -> AssignedNative<F>;
65
66    /// Inserts a new key -> value entry into the map.
67    /// This call introduces in-circuit constraints that guarantee that the
68    /// insertion was done correctly.
69    fn insert(
70        &mut self,
71        layouter: &mut impl Layouter<F>,
72        key: &AssignedKey,
73        value: &AssignedValue,
74    ) -> Result<(), Error>;
75
76    /// Returns the value associated to a given key.
77    /// Unlike a standard `HashMap` every single key has a value in this
78    /// structure (possibly the default value it was created with).
79    /// This call introduces in-circuit constraints that guarantee that the
80    /// returned value is correct.
81    fn get(
82        &self,
83        layouter: &mut impl Layouter<F>,
84        key: &AssignedKey,
85    ) -> Result<AssignedValue, Error>;
86}