@@ -4,13 +4,18 @@ use halo2::{arithmetic::CurveAffine, circuit::Layouter, plonk::Error};
4
4
use std:: fmt:: Debug ;
5
5
6
6
pub mod chip;
7
+ mod message;
8
+
7
9
pub use chip:: { SinsemillaChip , SinsemillaConfig } ;
8
10
9
11
/// The set of circuit instructions required to use the [`Sinsemilla`](https://zcash.github.io/halo2/design/gadgets/sinsemilla.html) gadget.
10
12
/// This trait is bounded on two constant parameters: `K`, the number of bits
11
13
/// in each word accepted by the Sinsemilla hash, and `MAX_WORDS`, the maximum
12
14
/// number of words that a single hash instance can process.
13
15
pub trait SinsemillaInstructions < C : CurveAffine , const K : usize , const MAX_WORDS : usize > {
16
+ /// A message composed of [`MessagePiece`]s.
17
+ type Message : From < Vec < Self :: MessagePiece > > ;
18
+
14
19
/// A piece in a message containing a number of `K`-bit words.
15
20
/// A [`MessagePiece`] fits in a single base field element,
16
21
/// which means it can only contain up to `N` words, where
@@ -20,9 +25,6 @@ pub trait SinsemillaInstructions<C: CurveAffine, const K: usize, const MAX_WORDS
20
25
/// up to `N = 25` words in a single base field element.
21
26
type MessagePiece ;
22
27
23
- /// A cell in the circuit with an optional assigned value.
24
- type CellValue ;
25
-
26
28
/// The x-coordinate of a point output of [`hash_to_point`].
27
29
type X ;
28
30
/// A point output of [`hash_to_point`].
@@ -51,7 +53,7 @@ pub trait SinsemillaInstructions<C: CurveAffine, const K: usize, const MAX_WORDS
51
53
layouter : impl Layouter < C :: Base > ,
52
54
message : Vec < Option < bool > > ,
53
55
num_words : usize ,
54
- ) -> Result < Vec < Self :: MessagePiece > , Error > ;
56
+ ) -> Result < Self :: Message , Error > ;
55
57
56
58
/// Witness a message piece given a bitstring. Returns a [`MessagePiece`]
57
59
/// encoding the given message.
@@ -84,17 +86,13 @@ pub trait SinsemillaInstructions<C: CurveAffine, const K: usize, const MAX_WORDS
84
86
num_words : usize ,
85
87
) -> Result < Self :: MessagePiece , Error > ;
86
88
87
- /// Prepare a message piece given a [`CellValue`] and the number of words
88
- /// encoded in the contained base field element.
89
- fn prepare_message_piece ( cell : & Self :: CellValue , num_words : usize ) -> Self :: MessagePiece ;
90
-
91
89
/// Hashes a message to an ECC curve point.
92
90
#[ allow( non_snake_case) ]
93
91
fn hash_to_point (
94
92
& self ,
95
93
layouter : impl Layouter < C :: Base > ,
96
94
Q : C ,
97
- message : Vec < Self :: MessagePiece > ,
95
+ message : Self :: Message ,
98
96
) -> Result < Self :: Point , Error > ;
99
97
100
98
/// Extracts the x-coordinate of the output of a Sinsemilla hash.
@@ -110,7 +108,7 @@ where
110
108
SinsemillaChip : SinsemillaInstructions < C , K , MAX_WORDS > + Clone + Debug + Eq ,
111
109
{
112
110
chip : SinsemillaChip ,
113
- inner : Vec < SinsemillaChip :: MessagePiece > ,
111
+ inner : SinsemillaChip :: Message ,
114
112
}
115
113
116
114
impl < C : CurveAffine , SinsemillaChip , const K : usize , const MAX_WORDS : usize >
@@ -132,24 +130,9 @@ where
132
130
fn from_pieces ( chip : SinsemillaChip , pieces : Vec < SinsemillaChip :: MessagePiece > ) -> Self {
133
131
Self {
134
132
chip,
135
- inner : pieces,
133
+ inner : pieces. into ( ) ,
136
134
}
137
135
}
138
-
139
- /// Return the `MessagePiece`s contained in this `Message`.
140
- fn pieces ( & self ) -> & [ SinsemillaChip :: MessagePiece ] {
141
- & self . inner
142
- }
143
-
144
- /// Construct a `MessagePiece` given a vector of `CellValue`s and the
145
- /// number of words encoded in the contained base field elements.
146
- fn new_piece (
147
- _chip : SinsemillaChip ,
148
- cell : & SinsemillaChip :: CellValue ,
149
- num_words : usize ,
150
- ) -> SinsemillaChip :: MessagePiece {
151
- SinsemillaChip :: prepare_message_piece ( cell, num_words)
152
- }
153
136
}
154
137
155
138
#[ allow( non_snake_case) ]
@@ -337,9 +320,12 @@ mod tests {
337
320
SinsemillaInstructions ,
338
321
} ;
339
322
340
- use crate :: circuit:: gadget:: ecc:: {
341
- chip:: { EccChip , EccConfig } ,
342
- ScalarFixed ,
323
+ use crate :: circuit:: gadget:: {
324
+ ecc:: {
325
+ chip:: { EccChip , EccConfig } ,
326
+ ScalarFixed ,
327
+ } ,
328
+ utilities:: Var ,
343
329
} ;
344
330
345
331
use std:: convert:: TryInto ;
@@ -432,7 +418,11 @@ mod tests {
432
418
) ?;
433
419
let left = merkle_crh. hash_to_point ( layouter. namespace ( || "left" ) , left) ?;
434
420
let left = left. extract_p ( ) ;
435
- Message :: new_piece ( chip1. clone ( ) , left. inner ( ) , 25 )
421
+ chip1. witness_message_piece_field (
422
+ layouter. namespace ( || "witness left piece" ) ,
423
+ left. inner ( ) . value ( ) ,
424
+ 25 ,
425
+ ) ?
436
426
} ;
437
427
438
428
// Right leaf
@@ -447,7 +437,11 @@ mod tests {
447
437
) ?;
448
438
let right = merkle_crh. hash_to_point ( layouter. namespace ( || "right" ) , right) ?;
449
439
let right = right. extract_p ( ) ;
450
- Message :: new_piece ( chip1. clone ( ) , right. inner ( ) , 25 )
440
+ chip1. witness_message_piece_field (
441
+ layouter. namespace ( || "witness left piece" ) ,
442
+ right. inner ( ) . value ( ) ,
443
+ 25 ,
444
+ ) ?
451
445
} ;
452
446
453
447
// Layer 0
0 commit comments