Skip to content

Commit

Permalink
Load circuit using sinsemilla_s_generators() from constants
Browse files Browse the repository at this point in the history
  • Loading branch information
therealyingtong committed May 5, 2021
1 parent 22b242f commit fc36949
Showing 1 changed file with 5 additions and 32 deletions.
37 changes: 5 additions & 32 deletions src/circuit/gadget/sinsemilla/chip/generator_table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::primitives::sinsemilla::{K, S_PERSONALIZATION};
use crate::primitives::sinsemilla::{sinsemilla_s_generators, K, S_PERSONALIZATION};
use halo2::{
arithmetic::{CurveAffine, CurveExt, FieldExt},
circuit::{Chip, Layouter},
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<C: CurveAffine> GeneratorTableChip<C> {
|| "generator_table",
|mut gate| {
// We generate the row values lazily (we only need them during keygen).
let mut rows = config.generate::<C>();
let mut rows = sinsemilla_s_generators::<C>();

for index in 0..(1 << K) {
let mut row = None;
Expand All @@ -113,20 +113,20 @@ impl<C: CurveAffine> GeneratorTableChip<C> {
index,
|| {
row = rows.next();
row.map(|(idx, _, _)| idx).ok_or(Error::SynthesisError)
Ok(C::Base::from_u64(index as u64))
},
)?;
gate.assign_fixed(
|| "table_x",
config.table_x,
index,
|| row.map(|(_, x, _)| x).ok_or(Error::SynthesisError),
|| row.map(|(x, _)| x).ok_or(Error::SynthesisError),
)?;
gate.assign_fixed(
|| "table_y",
config.table_y,
index,
|| row.map(|(_, _, y)| y).ok_or(Error::SynthesisError),
|| row.map(|(_, y)| y).ok_or(Error::SynthesisError),
)?;
}
Ok(())
Expand All @@ -135,33 +135,6 @@ impl<C: CurveAffine> GeneratorTableChip<C> {
}
}

impl GeneratorTableConfig {
// Generates S[0..2^k] as 2^k independent, verifiably random generators of the group.
// Loads these generators into a lookup table along with their indices.
// Uses SWU hash-to-curve.
fn generate<C: CurveAffine>(&self) -> impl Iterator<Item = (C::Base, C::Base, C::Base)> {
let init = get_s_by_idx::<C>(0).to_affine().coordinates().unwrap();

(1..=(1 << K)).scan(
(C::Base::default(), *init.x(), *init.y()),
move |(idx, x, y), i| {
// We computed this table row in the previous iteration.
let res = (*idx, *x, *y);

// i holds the zero-indexed row number for the next table row.
*idx = C::Base::from_u64(i as u64);

let new = get_s_by_idx::<C>(i).to_affine().coordinates().unwrap();

*x = *new.x();
*y = *new.y();

Some(res)
},
)
}
}

/// Get generator S by index
pub fn get_s_by_idx<C: CurveAffine>(idx: u32) -> C::Curve {
let hash = C::CurveExt::hash_to_curve(S_PERSONALIZATION);
Expand Down

0 comments on commit fc36949

Please sign in to comment.