Просмотр исходного кода

halo2/utilities: Add missing lebs2ip function

parazyd 5 лет назад
Родитель
Сommit
0edaa5d29a

+ 2 - 1
examples/halo2/src/circuit/gadget/utilities/lookup_range_check.rs

@@ -1,7 +1,6 @@
 //! Make use of a K-bit lookup table to decompose a field element into K-bit
 //! Make use of a K-bit lookup table to decompose a field element into K-bit
 //! words.
 //! words.
 
 
-use crate::spec::lebs2ip;
 use halo2::{
 use halo2::{
     circuit::{Layouter, Region},
     circuit::{Layouter, Region},
     plonk::{Advice, Column, ConstraintSystem, Error, Selector, TableColumn},
     plonk::{Advice, Column, ConstraintSystem, Error, Selector, TableColumn},
@@ -11,6 +10,8 @@ use std::{convert::TryInto, marker::PhantomData};
 
 
 use ff::PrimeFieldBits;
 use ff::PrimeFieldBits;
 
 
+use crate::spec::lebs2ip;
+
 use super::*;
 use super::*;
 
 
 /// The running sum $[z_0, ..., z_W]$. If created in strict mode, $z_W = 0$.
 /// The running sum $[z_0, ..., z_W]$. If created in strict mode, $z_W = 0$.

+ 2 - 0
examples/halo2/src/lib.rs

@@ -1,2 +1,4 @@
 pub mod circuit;
 pub mod circuit;
+pub mod constants;
 pub mod primitives;
 pub mod primitives;
+pub mod spec;

+ 1 - 0
examples/halo2/src/primitives.rs

@@ -1 +1,2 @@
 pub mod poseidon;
 pub mod poseidon;
+pub mod sinsemilla;

+ 12 - 1
examples/halo2/src/spec.rs

@@ -5,7 +5,6 @@ use halo2::{
 };
 };
 use subtle::CtOption;
 use subtle::CtOption;
 
 
-use crate::circuit::gadget::utilities::lookup_range_check::lebs2ip;
 use crate::constants::util::gen_const_array;
 use crate::constants::util::gen_const_array;
 
 
 /// Coordinate extractor for Pallas.
 /// Coordinate extractor for Pallas.
@@ -36,6 +35,18 @@ pub fn lebs2ip_field<F: FieldExt, const L: usize>(bits: &[bool; L]) -> F {
     F::from_u64(lebs2ip::<L>(bits))
     F::from_u64(lebs2ip::<L>(bits))
 }
 }
 
 
+/// The u64 integer represented by an L-bit little-endian bitstring.
+///
+/// # Panics
+///
+/// Panics if the bitstring is longer than 64 bits.
+pub fn lebs2ip<const L: usize>(bits: &[bool; L]) -> u64 {
+    assert!(L <= 64);
+    bits.iter()
+        .enumerate()
+        .fold(0u64, |acc, (i, b)| acc + if *b { 1 << i } else { 0 })
+}
+
 /// The sequence of bits representing a u64 in little-endian order.
 /// The sequence of bits representing a u64 in little-endian order.
 ///
 ///
 /// # Panics
 /// # Panics