snark_verifier/system/halo2/
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
//! Transcripts implemented with both `halo2_proofs::transcript` and
//! `crate::util::transcript`.
use crate::halo2_proofs;
use crate::{
    loader::native::{self, NativeLoader},
    util::{
        arithmetic::CurveAffine,
        transcript::{Transcript, TranscriptRead, TranscriptWrite},
    },
    Error,
};
use halo2_proofs::transcript::{Blake2bRead, Blake2bWrite, Challenge255};
use pairing::group::ff::FromUniformBytes;
use std::io::{Read, Write};

#[cfg(feature = "loader_evm")]
pub mod evm;

#[cfg(feature = "loader_halo2")]
pub mod halo2;

impl<C: CurveAffine, R: Read> Transcript<C, NativeLoader> for Blake2bRead<R, C, Challenge255<C>>
where
    C::Scalar: FromUniformBytes<64>,
{
    fn loader(&self) -> &NativeLoader {
        &native::LOADER
    }

    fn squeeze_challenge(&mut self) -> C::Scalar {
        *halo2_proofs::transcript::Transcript::squeeze_challenge_scalar::<C::Scalar>(self)
    }

    fn common_ec_point(&mut self, ec_point: &C) -> Result<(), Error> {
        halo2_proofs::transcript::Transcript::common_point(self, *ec_point)
            .map_err(|err| Error::Transcript(err.kind(), err.to_string()))
    }

    fn common_scalar(&mut self, scalar: &C::Scalar) -> Result<(), Error> {
        halo2_proofs::transcript::Transcript::common_scalar(self, *scalar)
            .map_err(|err| Error::Transcript(err.kind(), err.to_string()))
    }
}

impl<C: CurveAffine, R: Read> TranscriptRead<C, NativeLoader> for Blake2bRead<R, C, Challenge255<C>>
where
    C::Scalar: FromUniformBytes<64>,
{
    fn read_scalar(&mut self) -> Result<C::Scalar, Error> {
        halo2_proofs::transcript::TranscriptRead::read_scalar(self)
            .map_err(|err| Error::Transcript(err.kind(), err.to_string()))
    }

    fn read_ec_point(&mut self) -> Result<C, Error> {
        halo2_proofs::transcript::TranscriptRead::read_point(self)
            .map_err(|err| Error::Transcript(err.kind(), err.to_string()))
    }
}

impl<C: CurveAffine, W: Write> Transcript<C, NativeLoader> for Blake2bWrite<W, C, Challenge255<C>>
where
    C::Scalar: FromUniformBytes<64>,
{
    fn loader(&self) -> &NativeLoader {
        &native::LOADER
    }

    fn squeeze_challenge(&mut self) -> C::Scalar {
        *halo2_proofs::transcript::Transcript::squeeze_challenge_scalar::<C::Scalar>(self)
    }

    fn common_ec_point(&mut self, ec_point: &C) -> Result<(), Error> {
        halo2_proofs::transcript::Transcript::common_point(self, *ec_point)
            .map_err(|err| Error::Transcript(err.kind(), err.to_string()))
    }

    fn common_scalar(&mut self, scalar: &C::Scalar) -> Result<(), Error> {
        halo2_proofs::transcript::Transcript::common_scalar(self, *scalar)
            .map_err(|err| Error::Transcript(err.kind(), err.to_string()))
    }
}

impl<C: CurveAffine> TranscriptWrite<C> for Blake2bWrite<Vec<u8>, C, Challenge255<C>>
where
    C::Scalar: FromUniformBytes<64>,
{
    fn write_scalar(&mut self, scalar: C::Scalar) -> Result<(), Error> {
        halo2_proofs::transcript::TranscriptWrite::write_scalar(self, scalar)
            .map_err(|err| Error::Transcript(err.kind(), err.to_string()))
    }

    fn write_ec_point(&mut self, ec_point: C) -> Result<(), Error> {
        halo2_proofs::transcript::TranscriptWrite::write_point(self, ec_point)
            .map_err(|err| Error::Transcript(err.kind(), err.to_string()))
    }
}