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
use crate::{
fields::FpGadget,
utilities::{
alloc::AllocGadget,
boolean::{AllocatedBit, Boolean},
eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget},
select::CondSelectGadget,
ToBitsBEGadget,
ToBytesGadget,
},
};
use snarkvm_fields::{fp_parameters::FpParameters, traits::to_field_vec::ToConstraintField, Field, PrimeField};
use snarkvm_r1cs::{errors::SynthesisError, Assignment, ConstraintSystem, LinearCombination};
use snarkvm_utilities::bytes::ToBytes;
use core::borrow::Borrow;
use std::{cmp::Ordering, fmt::Debug};
uint_impl!(UInt8, u8, 8);
uint_impl!(UInt16, u16, 16);
uint_impl!(UInt32, u32, 32);
uint_impl!(UInt64, u64, 64);
pub trait UInt: Debug + Clone + PartialOrd + Eq + PartialEq {
fn negate(&self) -> Self;
fn is_constant(&self) -> bool;
fn result_is_constant(first: &Self, second: &Self) -> bool {
if !first.is_constant() {
return false;
}
second.is_constant()
}
fn to_bits_le(&self) -> Vec<Boolean>;
fn from_bits_le(bits: &[Boolean]) -> Self;
fn rotr(&self, by: usize) -> Self;
fn xor<F: Field, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, SynthesisError>;
fn addmany<F: PrimeField, CS: ConstraintSystem<F>>(cs: CS, operands: &[Self]) -> Result<Self, SynthesisError>;
fn sub<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, SynthesisError>;
fn sub_unsafe<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, SynthesisError>;
fn mul<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, SynthesisError>;
fn div<F: PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self) -> Result<Self, SynthesisError>;
fn pow<F: Field + PrimeField, CS: ConstraintSystem<F>>(&self, cs: CS, other: &Self)
-> Result<Self, SynthesisError>;
}
impl UInt8 {
pub fn constant_vec(values: &[u8]) -> Vec<Self> {
values.iter().copied().map(UInt8::constant).collect()
}
pub fn alloc_vec<F, CS, T>(mut cs: CS, values: &[T]) -> Result<Vec<Self>, SynthesisError>
where
F: Field,
CS: ConstraintSystem<F>,
T: Into<Option<u8>> + Copy,
{
let mut output_vec = Vec::with_capacity(values.len());
for (i, value) in values.iter().enumerate() {
let byte: Option<u8> = Into::into(*value);
let alloc_byte = Self::alloc(&mut cs.ns(|| format!("byte_{}", i)), || byte.get())?;
output_vec.push(alloc_byte);
}
Ok(output_vec)
}
pub fn alloc_input_vec_le<F, CS>(mut cs: CS, values: &[u8]) -> Result<Vec<Self>, SynthesisError>
where
F: PrimeField,
CS: ConstraintSystem<F>,
{
let values_len = values.len();
let field_elements: Vec<F> = ToConstraintField::<F>::to_field_elements(values).unwrap();
let max_size = 8 * (F::Parameters::CAPACITY / 8) as usize;
let mut allocated_bits = Vec::new();
for (i, field_element) in field_elements.into_iter().enumerate() {
let fe = FpGadget::alloc_input(&mut cs.ns(|| format!("Field element {}", i)), || Ok(field_element))?;
let mut fe_bits = fe.to_bits_be(cs.ns(|| format!("Convert fe to bits {}", i)))?;
fe_bits.reverse();
allocated_bits.extend_from_slice(&fe_bits[0..max_size]);
}
Ok(allocated_bits[0..8 * values_len]
.chunks(8)
.map(Self::from_bits_le)
.collect())
}
}