Skip to content
This repository was archived by the owner on Jul 5, 2024. It is now read-only.

Commit 603409a

Browse files
Remove biguint to field (#283)
* remove biguint to field * comment on the assumption
1 parent 0230b6a commit 603409a

13 files changed

+53
-63
lines changed

keccak256/src/arith_helpers.rs

-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use itertools::Itertools;
33
use num_bigint::BigUint;
44
use num_traits::Zero;
55
use pairing::arithmetic::FieldExt;
6-
use pairing::bn256::Fr as Fp;
76
use std::ops::{Index, IndexMut};
87

98
pub const B2: u8 = 2;
@@ -188,21 +187,6 @@ pub fn convert_b9_lane_to_b2_normal(x: Lane9) -> u64 {
188187
.unwrap_or(0)
189188
}
190189

191-
pub fn big_uint_to_field<F: FieldExt>(a: &BigUint) -> F {
192-
let mut b: [u64; 4] = [0; 4];
193-
let mut iter = a.iter_u64_digits();
194-
195-
for i in &mut b {
196-
*i = match &iter.next() {
197-
Some(x) => *x,
198-
None => 0u64,
199-
};
200-
}
201-
202-
// Workarround since `FieldExt` does not impl `from_raw`.
203-
F::from_bytes(&Fp::from_raw(b).to_bytes()).unwrap()
204-
}
205-
206190
/// This function allows us to inpect coefficients of big-numbers in different
207191
/// bases.
208192
pub fn inspect(x: BigUint, name: &str, base: u8) {

keccak256/src/gates/absorb.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::arith_helpers::*;
22
use crate::common::*;
3+
use crate::gates::gate_helpers::biguint_to_f;
34
use crate::keccak_arith::*;
45
use halo2::circuit::Cell;
56
use halo2::circuit::Layouter;
@@ -177,9 +178,8 @@ impl<F: FieldExt> AbsorbConfig<F> {
177178
state[(x, y)].clone().try_into().expect("Conversion err"),
178179
);
179180
next_biguint[(x, y)] = convert_b2_to_b9(next_input[x][y]);
180-
in_state[5 * x + y] = big_uint_to_field(&in_biguint[(x, y)]);
181-
in_next_input_25[5 * x + y] =
182-
big_uint_to_field(&next_biguint[(x, y)]);
181+
in_state[5 * x + y] = biguint_to_f(&in_biguint[(x, y)]);
182+
in_next_input_25[5 * x + y] = biguint_to_f(&next_biguint[(x, y)]);
183183
}
184184

185185
let mut in_next_input_17 = [Fp::zero(); ABSORB_NEXT_INPUTS];

keccak256/src/gates/base_conversion.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,7 @@ mod tests {
298298
let input = 12345678u64;
299299
let circuit = MyCircuit::<Fp> {
300300
input_b2_lane: Fp::from(input),
301-
output_b13_lane: biguint_to_f::<Fp>(&convert_b2_to_b13(input))
302-
.unwrap(),
301+
output_b13_lane: biguint_to_f::<Fp>(&convert_b2_to_b13(input)),
303302
};
304303
let k = 17;
305304

keccak256/src/gates/gate_helpers.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
use halo2::{circuit::Cell, plonk::Error};
1+
use halo2::circuit::Cell;
22
use num_bigint::BigUint;
33
use pairing::arithmetic::FieldExt;
4-
5-
#[derive(Debug, Clone)]
6-
pub struct Lane<F> {
7-
pub cell: Cell,
8-
pub value: F,
9-
}
4+
use std::convert::TryInto;
105

116
#[derive(Debug, Clone, Copy)]
127
pub struct BlockCount<F> {
@@ -16,17 +11,23 @@ pub struct BlockCount<F> {
1611

1712
pub type BlockCount2<F> = (BlockCount<F>, BlockCount<F>);
1813

19-
pub fn biguint_to_f<F: FieldExt>(x: &BigUint) -> Result<F, Error> {
20-
let mut word = [0; 32];
21-
let x_bytes = x.to_bytes_le();
22-
let len = x_bytes.len();
23-
assert!(len <= 32, "expect len <=32 but got {}", len);
24-
word[..len].clone_from_slice(&x_bytes[..len]);
25-
Option::from(F::from_bytes(&word)).ok_or(Error::Synthesis)
14+
/// Convert a bigUint value to FieldExt
15+
///
16+
/// We assume the input value is smaller than the field size
17+
pub fn biguint_to_f<F: FieldExt>(x: &BigUint) -> F {
18+
let mut x_bytes = x.to_bytes_le();
19+
assert!(
20+
x_bytes.len() <= 32,
21+
"expect len <=32 but got {}",
22+
x_bytes.len()
23+
);
24+
x_bytes.resize(32, 0);
25+
let x_bytes: [u8; 32] = x_bytes.try_into().unwrap();
26+
F::from_bytes(&x_bytes).unwrap()
2627
}
2728

28-
pub fn f_to_biguint<F: FieldExt>(x: F) -> Option<BigUint> {
29-
Option::from(BigUint::from_bytes_le(&x.to_bytes()[..]))
29+
pub fn f_to_biguint<F: FieldExt>(x: F) -> BigUint {
30+
BigUint::from_bytes_le(&x.to_bytes())
3031
}
3132

3233
pub fn biguint_mod(x: &BigUint, modulus: u8) -> u8 {

keccak256/src/gates/iota_b13.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ impl<F: FieldExt> IotaB13Config<F> {
205205
mod tests {
206206
use super::*;
207207
use crate::common::{PERMUTATION, ROUND_CONSTANTS};
208+
use crate::gates::gate_helpers::biguint_to_f;
208209
use halo2::circuit::Layouter;
209210
use halo2::plonk::{Advice, Column, ConstraintSystem, Error};
210211
use halo2::{circuit::SimpleFloorPlanner, dev::MockProver, plonk::Circuit};
@@ -321,7 +322,7 @@ mod tests {
321322

322323
let constants: Vec<Fp> = ROUND_CONSTANTS
323324
.iter()
324-
.map(|num| big_uint_to_field(&convert_b2_to_b13(*num)))
325+
.map(|num| biguint_to_f(&convert_b2_to_b13(*num)))
325326
.collect();
326327

327328
// With flag set to true, the gate should trigger as we Mix.

keccak256/src/gates/iota_b9.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::arith_helpers::*;
22
use crate::common::*;
3+
use crate::gates::gate_helpers::biguint_to_f;
34
use crate::keccak_arith::*;
45
use halo2::circuit::Cell;
56
use halo2::circuit::Layouter;
@@ -265,7 +266,7 @@ impl<F: FieldExt> IotaB9Config<F> {
265266
in_biguint[(x, y)] = convert_b2_to_b9(
266267
state[(x, y)].clone().try_into().expect("Conversion err"),
267268
);
268-
in_state[5 * x + y] = big_uint_to_field(&in_biguint[(x, y)]);
269+
in_state[5 * x + y] = biguint_to_f(&in_biguint[(x, y)]);
269270
}
270271

271272
// Compute out state
@@ -279,6 +280,7 @@ impl<F: FieldExt> IotaB9Config<F> {
279280
mod tests {
280281
use super::*;
281282
use crate::common::{PERMUTATION, ROUND_CONSTANTS};
283+
use crate::gates::gate_helpers::biguint_to_f;
282284
use halo2::circuit::Layouter;
283285
use halo2::plonk::{Advice, Column, ConstraintSystem, Error};
284286
use halo2::{circuit::SimpleFloorPlanner, dev::MockProver, plonk::Circuit};
@@ -399,7 +401,7 @@ mod tests {
399401

400402
let constants: Vec<Fp> = ROUND_CONSTANTS
401403
.iter()
402-
.map(|num| big_uint_to_field(&convert_b2_to_b9(*num)))
404+
.map(|num| biguint_to_f(&convert_b2_to_b9(*num)))
403405
.collect();
404406

405407
// (flag = 0) -> Out state is checked as constraints are applied.
@@ -551,7 +553,7 @@ mod tests {
551553

552554
for (x, y) in (0..5).cartesian_product(0..5) {
553555
in_biguint[(x, y)] = convert_b2_to_b9(input1[x][y]);
554-
in_state[5 * x + y] = big_uint_to_field(&in_biguint[(x, y)]);
556+
in_state[5 * x + y] = biguint_to_f(&in_biguint[(x, y)]);
555557
}
556558

557559
// Test for the 25 rounds
@@ -571,7 +573,7 @@ mod tests {
571573

572574
let constants: Vec<Fp> = ROUND_CONSTANTS
573575
.iter()
574-
.map(|num| big_uint_to_field(&convert_b2_to_b9(*num)))
576+
.map(|num| biguint_to_f(&convert_b2_to_b9(*num)))
575577
.collect();
576578

577579
let prover =

keccak256/src/gates/mixing.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ impl<F: FieldExt> MixingConfig<F> {
203203
mod tests {
204204
use super::*;
205205
use crate::common::{State, PERMUTATION, ROUND_CONSTANTS};
206+
use crate::gates::gate_helpers::biguint_to_f;
206207
use halo2::circuit::Layouter;
207208
use halo2::plonk::{ConstraintSystem, Error};
208209
use halo2::{circuit::SimpleFloorPlanner, dev::MockProver, plonk::Circuit};
@@ -325,12 +326,12 @@ mod tests {
325326

326327
let constants_b13: Vec<Fp> = ROUND_CONSTANTS
327328
.iter()
328-
.map(|num| big_uint_to_field(&convert_b2_to_b13(*num)))
329+
.map(|num| biguint_to_f(&convert_b2_to_b13(*num)))
329330
.collect();
330331

331332
let constants_b9: Vec<Fp> = ROUND_CONSTANTS
332333
.iter()
333-
.map(|num| big_uint_to_field(&convert_b2_to_b9(*num)))
334+
.map(|num| biguint_to_f(&convert_b2_to_b9(*num)))
334335
.collect();
335336

336337
// With flag set to false, we don't mix. And so we should obtain Absorb

keccak256/src/gates/rho.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,12 @@ mod tests {
233233
}
234234
let s0_arith = KeccakFArith::theta(&in_biguint);
235235
for (x, y) in (0..5).cartesian_product(0..5) {
236-
in_state[5 * x + y] = biguint_to_f(&s0_arith[(x, y)]).unwrap();
236+
in_state[5 * x + y] = biguint_to_f(&s0_arith[(x, y)]);
237237
}
238238
let s1_arith = KeccakFArith::rho(&s0_arith);
239239
let mut out_state: [Fp; 25] = [Fp::zero(); 25];
240240
for (x, y) in (0..5).cartesian_product(0..5) {
241-
out_state[5 * x + y] = biguint_to_f(&s1_arith[(x, y)]).unwrap();
241+
out_state[5 * x + y] = biguint_to_f(&s1_arith[(x, y)]);
242242
}
243243
let circuit = MyCircuit::<Fp> {
244244
in_state,

keccak256/src/gates/rho_checks.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ impl<F: FieldExt> LaneRotateConversionConfig<F> {
362362
region.constrain_equal(lane_base_13.0, cell)?;
363363

364364
let mut rv = RotatingVariables::from(
365-
f_to_biguint(lane_base_13.1).ok_or(Error::Synthesis)?,
365+
f_to_biguint(lane_base_13.1),
366366
self.rotation,
367367
)?;
368368
let all_block_counts: Result<Vec<BlockCount2<F>>, Error> = self
@@ -507,11 +507,11 @@ impl<F: FieldExt> ChunkRotateConversionConfig<F> {
507507
rv: &RotatingVariables,
508508
) -> Result<BlockCount2<F>, Error> {
509509
assert_eq!(
510-
biguint_to_f::<F>(&rv.input_power_of_base)?,
510+
biguint_to_f::<F>(&rv.input_power_of_base),
511511
self.power_of_b13
512512
);
513513
assert_eq!(
514-
biguint_to_f::<F>(&rv.output_power_of_base)?,
514+
biguint_to_f::<F>(&rv.output_power_of_base),
515515
self.power_of_b9
516516
);
517517
self.q_enable.enable(region, offset)?;
@@ -522,25 +522,25 @@ impl<F: FieldExt> ChunkRotateConversionConfig<F> {
522522
|| format!("Input Coef {}", self.chunk_idx),
523523
self.adv.input.coef,
524524
offset,
525-
|| biguint_to_f::<F>(&rv.input_coef),
525+
|| Ok(biguint_to_f::<F>(&rv.input_coef)),
526526
)?;
527527
region.assign_advice(
528528
|| "Input accumulator",
529529
self.adv.input.acc,
530530
offset,
531-
|| biguint_to_f::<F>(&rv.input_acc),
531+
|| Ok(biguint_to_f::<F>(&rv.input_acc)),
532532
)?;
533533
region.assign_advice(
534534
|| "Output Coef",
535535
self.adv.output.coef,
536536
offset,
537-
|| biguint_to_f::<F>(&rv.output_coef),
537+
|| Ok(biguint_to_f::<F>(&rv.output_coef)),
538538
)?;
539539
region.assign_advice(
540540
|| "Output accumulator",
541541
self.adv.output.acc,
542542
offset,
543-
|| biguint_to_f::<F>(&rv.output_acc),
543+
|| Ok(biguint_to_f::<F>(&rv.output_acc)),
544544
)?;
545545
let block_counts = self.block_count_acc_config.assign_region(
546546
region,
@@ -612,19 +612,19 @@ impl<F: FieldExt> SpecialChunkConfig<F> {
612612
|| "input_acc",
613613
self.base_13_acc,
614614
offset,
615-
|| biguint_to_f::<F>(&rv.input_acc),
615+
|| Ok(biguint_to_f::<F>(&rv.input_acc)),
616616
)?;
617617
region.assign_advice(
618618
|| "ouput_acc",
619619
self.base_9_acc,
620620
offset,
621-
|| biguint_to_f::<F>(&rv.output_acc),
621+
|| Ok(biguint_to_f::<F>(&rv.output_acc)),
622622
)?;
623623
region.assign_advice(
624624
|| "last_b9_coef",
625625
self.last_b9_coef,
626626
offset,
627-
|| biguint_to_f::<F>(&rv.output_coef),
627+
|| Ok(biguint_to_f::<F>(&rv.output_coef)),
628628
)?;
629629

630630
let rv_final = rv.finalize();
@@ -634,7 +634,7 @@ impl<F: FieldExt> SpecialChunkConfig<F> {
634634
offset + 1,
635635
|| Ok(F::zero()),
636636
)?;
637-
let value = biguint_to_f::<F>(&rv_final.output_acc)?;
637+
let value = biguint_to_f::<F>(&rv_final.output_acc);
638638
let cell = region.assign_advice(
639639
|| "input_acc",
640640
self.base_9_acc,

keccak256/src/gates/state_conversion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ mod tests {
218218
.unwrap();
219219
let out_state: [Fp; 25] = in_state_flat
220220
.iter()
221-
.map(|&x| biguint_to_f::<Fp>(&convert_b2_to_b13(*x)).unwrap())
221+
.map(|&x| biguint_to_f::<Fp>(&convert_b2_to_b13(*x)))
222222
.collect::<Vec<_>>()
223223
.try_into()
224224
.unwrap();

keccak256/src/gates/tables.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<F: FieldExt> BaseInfo<F> {
231231
) -> Result<(Vec<F>, Vec<F>, F), Error> {
232232
// big-endian
233233
let input_chunks: Vec<u8> = {
234-
let raw = f_to_biguint(input).ok_or(Error::Synthesis)?;
234+
let raw = f_to_biguint(input);
235235
let mut v = raw.to_radix_le(self.input_base.into());
236236
assert!(v.len() <= self.max_chunks);
237237
// fill 0 to max chunks

keccak256/src/gates/theta.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl<F: FieldExt> ThetaConfig<F> {
109109
mod tests {
110110
use super::*;
111111
use crate::common::*;
112+
use crate::gates::gate_helpers::biguint_to_f;
112113
use crate::keccak_arith::*;
113114
use halo2::{
114115
circuit::{Layouter, SimpleFloorPlanner},
@@ -202,12 +203,12 @@ mod tests {
202203

203204
for (x, y) in (0..5).cartesian_product(0..5) {
204205
in_biguint[(x, y)] = convert_b2_to_b13(input1[x][y]);
205-
in_state[5 * x + y] = big_uint_to_field(&in_biguint[(x, y)]);
206+
in_state[5 * x + y] = biguint_to_f(&in_biguint[(x, y)]);
206207
}
207208
let s1_arith = KeccakFArith::theta(&in_biguint);
208209
let mut out_state: [Fp; 25] = [Fp::zero(); 25];
209210
for (x, y) in (0..5).cartesian_product(0..5) {
210-
out_state[5 * x + y] = big_uint_to_field(&s1_arith[(x, y)]);
211+
out_state[5 * x + y] = biguint_to_f(&s1_arith[(x, y)]);
211212
}
212213

213214
let circuit = MyCircuit::<Fp> {

keccak256/src/gates/xi.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ mod tests {
108108
use super::*;
109109
use crate::arith_helpers::*;
110110
use crate::common::*;
111+
use crate::gates::gate_helpers::biguint_to_f;
111112
use crate::keccak_arith::*;
112113
use halo2::circuit::Layouter;
113114
use halo2::plonk::{Advice, Column, ConstraintSystem, Error};
@@ -208,12 +209,12 @@ mod tests {
208209

209210
for (x, y) in (0..5).cartesian_product(0..5) {
210211
in_biguint[(x, y)] = convert_b2_to_b9(input1[x][y]);
211-
in_state[5 * x + y] = big_uint_to_field(&in_biguint[(x, y)]);
212+
in_state[5 * x + y] = biguint_to_f(&in_biguint[(x, y)]);
212213
}
213214
let s1_arith = KeccakFArith::xi(&in_biguint);
214215
let mut out_state: [Fp; 25] = [Fp::zero(); 25];
215216
for (x, y) in (0..5).cartesian_product(0..5) {
216-
out_state[5 * x + y] = big_uint_to_field(&s1_arith[(x, y)]);
217+
out_state[5 * x + y] = biguint_to_f(&s1_arith[(x, y)]);
217218
}
218219
let circuit = MyCircuit::<Fp> {
219220
in_state,

0 commit comments

Comments
 (0)