-
Notifications
You must be signed in to change notification settings - Fork 2
/
transcript.rs
192 lines (174 loc) · 5.33 KB
/
transcript.rs
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use halo2_proofs::{
halo2curves::{ff::PrimeField, Coordinates, CurveAffine},
transcript::{
EncodedChallenge, Transcript, TranscriptRead, TranscriptReadBuffer, TranscriptWrite,
TranscriptWriterBuffer,
},
};
use itertools::{chain, Itertools};
use ruint::aliases::U256;
use sha3::{Digest, Keccak256};
use std::{
io::{self, Read, Write},
marker::PhantomData,
mem,
};
/// Transcript using Keccak256 as hash function in Fiat-Shamir transformation.
#[derive(Debug, Default)]
pub struct Keccak256Transcript<C, S> {
stream: S,
buf: Vec<u8>,
_marker: PhantomData<C>,
}
impl<C, S> Keccak256Transcript<C, S> {
/// Return a `Keccak256Transcript` with empty buffer.
pub fn new(stream: S) -> Self {
Self {
stream,
buf: Vec::new(),
_marker: PhantomData,
}
}
}
#[derive(Debug)]
pub struct ChallengeEvm<C>(C::Scalar)
where
C: CurveAffine,
C::Scalar: PrimeField<Repr = [u8; 0x20]>;
impl<C> EncodedChallenge<C> for ChallengeEvm<C>
where
C: CurveAffine,
C::Scalar: PrimeField<Repr = [u8; 0x20]>,
{
type Input = [u8; 0x20];
fn new(challenge_input: &[u8; 0x20]) -> Self {
ChallengeEvm(u256_to_fe(U256::from_be_bytes(*challenge_input)))
}
fn get_scalar(&self) -> C::Scalar {
self.0
}
}
impl<C, S> Transcript<C, ChallengeEvm<C>> for Keccak256Transcript<C, S>
where
C: CurveAffine,
C::Scalar: PrimeField<Repr = [u8; 0x20]>,
{
fn squeeze_challenge(&mut self) -> ChallengeEvm<C> {
let buf_len = self.buf.len();
let data = chain![
mem::take(&mut self.buf),
if buf_len == 0x20 { Some(1) } else { None }
]
.collect_vec();
let hash: [u8; 0x20] = Keccak256::digest(data).into();
self.buf = hash.to_vec();
ChallengeEvm::new(&hash)
}
fn common_point(&mut self, ec_point: C) -> io::Result<()> {
let coords: Coordinates<C> = Option::from(ec_point.coordinates()).ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
"Invalid elliptic curve point".to_string(),
)
})?;
[coords.x(), coords.y()].map(|coordinate| {
self.buf
.extend(coordinate.to_repr().as_ref().iter().rev().cloned());
});
Ok(())
}
fn common_scalar(&mut self, scalar: C::Scalar) -> io::Result<()> {
self.buf.extend(scalar.to_repr().as_ref().iter().rev());
Ok(())
}
}
impl<C, R: Read> TranscriptRead<C, ChallengeEvm<C>> for Keccak256Transcript<C, R>
where
C: CurveAffine,
C::Scalar: PrimeField<Repr = [u8; 0x20]>,
{
fn read_point(&mut self) -> io::Result<C> {
let mut reprs = [<C::Base as PrimeField>::Repr::default(); 2];
for repr in &mut reprs {
self.stream.read_exact(repr.as_mut())?;
repr.as_mut().reverse();
}
let [x, y] = reprs.map(|repr| Option::from(C::Base::from_repr(repr)));
let ec_point = x
.zip(y)
.and_then(|(x, y)| Option::from(C::from_xy(x, y)))
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
"Invalid elliptic curve point".to_string(),
)
})?;
self.common_point(ec_point)?;
Ok(ec_point)
}
fn read_scalar(&mut self) -> io::Result<C::Scalar> {
let mut data = [0; 0x20];
self.stream.read_exact(data.as_mut())?;
data.reverse();
let scalar = C::Scalar::from_repr_vartime(data)
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Invalid scalar".to_string()))?;
Transcript::<C, ChallengeEvm<C>>::common_scalar(self, scalar)?;
Ok(scalar)
}
}
impl<C, R: Read> TranscriptReadBuffer<R, C, ChallengeEvm<C>> for Keccak256Transcript<C, R>
where
C: CurveAffine,
C::Scalar: PrimeField<Repr = [u8; 0x20]>,
{
fn init(reader: R) -> Self {
Keccak256Transcript::new(reader)
}
}
impl<C, W: Write> TranscriptWrite<C, ChallengeEvm<C>> for Keccak256Transcript<C, W>
where
C: CurveAffine,
C::Scalar: PrimeField<Repr = [u8; 0x20]>,
{
fn write_point(&mut self, ec_point: C) -> io::Result<()> {
self.common_point(ec_point)?;
let coords = ec_point.coordinates().unwrap();
for coord in [coords.x(), coords.y()] {
let mut repr = coord.to_repr();
repr.as_mut().reverse();
self.stream.write_all(repr.as_ref())?;
}
Ok(())
}
fn write_scalar(&mut self, scalar: C::Scalar) -> io::Result<()> {
Transcript::<C, ChallengeEvm<C>>::common_scalar(self, scalar)?;
let mut data = scalar.to_repr();
data.as_mut().reverse();
self.stream.write_all(data.as_ref())
}
}
impl<C, W: Write> TranscriptWriterBuffer<W, C, ChallengeEvm<C>> for Keccak256Transcript<C, W>
where
C: CurveAffine,
C::Scalar: PrimeField<Repr = [u8; 0x20]>,
{
fn init(writer: W) -> Self {
Keccak256Transcript::new(writer)
}
fn finalize(self) -> W {
self.stream
}
}
fn u256_to_fe<F>(value: U256) -> F
where
F: PrimeField<Repr = [u8; 0x20]>,
{
let value = value % modulus::<F>();
F::from_repr(value.to_le_bytes::<0x20>()).unwrap()
}
fn modulus<F>() -> U256
where
F: PrimeField<Repr = [u8; 0x20]>,
{
U256::from_le_bytes((-F::ONE).to_repr()) + U256::from(1)
}