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
use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
use ff::PrimeField;

use crate::gadgets::constraint;

pub fn encode<Scalar, CS>(
    mut cs: CS,
    key: &AllocatedNum<Scalar>,
    value: &AllocatedNum<Scalar>,
) -> Result<AllocatedNum<Scalar>, SynthesisError>
where
    Scalar: PrimeField,
    CS: ConstraintSystem<Scalar>,
{
    constraint::add(cs.namespace(|| "encode_add"), key, value)
}

pub fn decode<Scalar, CS>(
    mut cs: CS,
    key: &AllocatedNum<Scalar>,
    value: &AllocatedNum<Scalar>,
) -> Result<AllocatedNum<Scalar>, SynthesisError>
where
    Scalar: PrimeField,
    CS: ConstraintSystem<Scalar>,
{
    constraint::sub(cs.namespace(|| "decode_sub"), value, key)
}