Nova/src/commitments.rs
src/commitments.rsRaw
1use super::errors::NovaError;
2use super::traits::{CompressedGroup, Group};
3use core::fmt::Debug;
4use core::ops::{Add, AddAssign, Mul, MulAssign};
5use digest::{ExtendableOutput, Input};
6use merlin::Transcript;
7use sha3::Shake256;
8use std::io::Read;
9
10#[derive(Debug)]
11pub struct CommitGens<G: Group> {
12 gens: Vec<G>,
13}
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub struct Commitment<G: Group> {
17 comm: G,
18}
19
20#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct CompressedCommitment<C: CompressedGroup> {
22 comm: C,
23}
24
25impl<G: Group> CommitGens<G> {
26 pub fn new(label: &[u8], n: usize) -> Self {
27 let mut shake = Shake256::default();
28 shake.input(label);
29 let mut reader = shake.xof_result();
30 let mut gens: Vec<G> = Vec::new();
31 let mut uniform_bytes = [0u8; 64];
32 for _ in 0..n {
33 reader.read_exact(&mut uniform_bytes).unwrap();
34 gens.push(G::from_uniform_bytes(&uniform_bytes).unwrap());
35 }
36
37 CommitGens { gens }
38 }
39}
40
41impl<G: Group> Commitment<G> {
42 pub fn compress(&self) -> CompressedCommitment<G::CompressedGroupElement> {
43 CompressedCommitment {
44 comm: self.comm.compress(),
45 }
46 }
47}
48
49impl<C: CompressedGroup> CompressedCommitment<C> {
50 pub fn decompress(&self) -> Result<Commitment<C::GroupElement>, NovaError> {
51 let comm = self.comm.decompress();
52 if comm.is_none() {
53 return Err(NovaError::DecompressionError);
54 }
55 Ok(Commitment {
56 comm: comm.unwrap(),
57 })
58 }
59}
60
61pub trait CommitTrait<G: Group> {
62 fn commit(&self, gens: &CommitGens<G>) -> Commitment<G>;
63}
64
65impl<G: Group> CommitTrait<G> for [G::Scalar] {
66 fn commit(&self, gens: &CommitGens<G>) -> Commitment<G> {
67 assert_eq!(gens.gens.len(), self.len());
68 Commitment {
69 comm: G::vartime_multiscalar_mul(self, &gens.gens),
70 }
71 }
72}
73
74pub trait AppendToTranscriptTrait {
75 fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript);
76}
77
78impl<G: Group> AppendToTranscriptTrait for Commitment<G> {
79 fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript) {
80 transcript.append_message(label, self.comm.compress().as_bytes());
81 }
82}
83
84impl<C: CompressedGroup> AppendToTranscriptTrait for CompressedCommitment<C> {
85 fn append_to_transcript(&self, label: &'static [u8], transcript: &mut Transcript) {
86 transcript.append_message(label, self.comm.as_bytes());
87 }
88}
89
90impl<'b, G: Group> MulAssign<&'b G::Scalar> for Commitment<G> {
91 fn mul_assign(&mut self, scalar: &'b G::Scalar) {
92 let result = (self as &Commitment<G>).comm * scalar;
93 *self = Commitment { comm: result };
94 }
95}
96
97impl<G: Group> Mul<G::Scalar> for Commitment<G> {
98 type Output = Commitment<G>;
99 fn mul(self, scalar: G::Scalar) -> Commitment<G> {
100 Commitment {
101 comm: self.comm * scalar,
102 }
103 }
104}
105
106impl<'b, G: Group> AddAssign<&'b Commitment<G>> for Commitment<G> {
107 fn add_assign(&mut self, other: &'b Commitment<G>) {
108 let result = (self as &Commitment<G>).comm + other.comm;
109 *self = Commitment { comm: result };
110 }
111}
112
113impl<'a, 'b, G: Group> Add<&'b Commitment<G>> for &'a Commitment<G> {
114 type Output = Commitment<G>;
115 fn add(self, other: &'b Commitment<G>) -> Commitment<G> {
116 Commitment {
117 comm: self.comm + other.comm,
118 }
119 }
120}
121
122impl<G: Group> AddAssign<Commitment<G>> for Commitment<G> {
123 fn add_assign(&mut self, rhs: Commitment<G>) {
124 *self += &rhs;
125 }
126}
127
128impl<'b, G: Group> Add<&'b Commitment<G>> for Commitment<G> {
129 type Output = Commitment<G>;
130 fn add(self, rhs: &'b Commitment<G>) -> Commitment<G> {
131 &self + rhs
132 }
133}
134
135impl<'a, G: Group> Add<Commitment<G>> for &'a Commitment<G> {
136 type Output = Commitment<G>;
137 fn add(self, rhs: Commitment<G>) -> Commitment<G> {
138 self + &rhs
139 }
140}
141
142impl<G: Group> Add<Commitment<G>> for Commitment<G> {
143 type Output = Commitment<G>;
144 fn add(self, rhs: Commitment<G>) -> Commitment<G> {
145 &self + &rhs
146 }
147}