Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduced Hopwood optimized vbSM gadget #126

Merged
merged 26 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c6b4db6
(WIP) Introduced Hopwood optimized vbSM gadget. Handle internally exc…
DanieleDiBenedetto Sep 22, 2021
011015c
Bug fix
DanieleDiBenedetto Sep 22, 2021
74d9a82
Daira's double_and_add_interal: more docu, enforcing exceptional case…
UlrichHaboeck75 Sep 23, 2021
c929373
Daira's mul_bits: extra treatment of last bit with safe add. Added docu.
UlrichHaboeck75 Sep 23, 2021
db21144
copied the corrected code of mul_bits(), double_and_add_internal() to…
UlrichHaboeck75 Sep 23, 2021
e1d0235
translated the changes of double_and_add_internal(), mul_bits() to no…
UlrichHaboeck75 Sep 23, 2021
967e8e7
Added scalar_bits_to_constant_length gadget
DanieleDiBenedetto Sep 24, 2021
53e402f
corrected `scalar_bits_to_constant_length`, added comments.
UlrichHaboeck75 Sep 24, 2021
665619b
added TODO
UlrichHaboeck75 Sep 24, 2021
1715dae
Replaced non-native field gadgets by direct arguments.
UlrichHaboeck75 Sep 26, 2021
941dadd
Simplified from_bits() for non-natives by limb-wise packing via from_…
UlrichHaboeck75 Sep 26, 2021
f9b227b
Fixed from_bits test
DanieleDiBenedetto Sep 27, 2021
139d77a
Drop the assumption on the scalar MSB being 1 for mul_bits
DanieleDiBenedetto Sep 27, 2021
71ca963
Return exactly SimulationF::size_in_bits() bits from NonNativeFieldGa…
DanieleDiBenedetto Sep 27, 2021
dc56cd0
Merge branch 'rc/audit_chain' into hopwood_optimized_sm
DanieleDiBenedetto Sep 27, 2021
e00a772
Serialized some intensive tests
DanieleDiBenedetto Sep 27, 2021
60ea587
Fix CI
DanieleDiBenedetto Sep 27, 2021
f75ee3a
Additional secure add in `mul_bits()` native and non-native.
UlrichHaboeck75 Sep 27, 2021
4016f27
mul_bits() native and non-native: additional comments on the usage of…
UlrichHaboeck75 Sep 28, 2021
1531c6d
Revert "Serialized some intensive tests"
DanieleDiBenedetto Sep 28, 2021
64bb089
Added more CI stages to avoid timeout
DanieleDiBenedetto Sep 28, 2021
be7c743
separated the ci in multiple steps to optimize timing
lander86 Sep 28, 2021
f1c79c3
fixed typo
lander86 Sep 28, 2021
8e964e8
mul_bits() native: resolved TODOs on documenting completeness.
UlrichHaboeck75 Sep 28, 2021
b0340fb
fix typos
lander86 Sep 28, 2021
1adf934
Merge branch 'hopwood_optimized_sm' of github.com:HorizenOfficial/gin…
lander86 Sep 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion r1cs/gadgets/crypto/src/nizk/gm17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,9 @@ for Gm17VerifierGadget<PairingE, ConstraintF, P>
.enumerate()
{
let input_bits = input.to_bits(cs.ns(|| format!("Input {}", i)))?;
g_psi = b.mul_bits(cs.ns(|| format!("Mul {}", i)), &g_psi, input_bits.iter())?;
g_psi = b
.mul_bits(cs.ns(|| format!("Mul {}", i)), input_bits.iter())?
.add(cs.ns(|| format!("Add {}", i)), &g_psi)?;
input_len += 1;
}
// Check that the input and the query in the verification are of the
Expand Down
4 changes: 3 additions & 1 deletion r1cs/gadgets/crypto/src/nizk/groth16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ for Groth16VerifierGadget<PairingE, ConstraintF, P>
.enumerate()
{
let input_bits = input.to_bits(cs.ns(|| format!("Input {}", i)))?;
g_ic = b.mul_bits(cs.ns(|| format!("Mul {}", i)), &g_ic, input_bits.iter())?;
g_ic = b
.mul_bits(cs.ns(|| format!("Mul {}", i)), input_bits.iter())?
.add(cs.ns(|| format!("Add {}", i)), &g_ic)?;
input_len += 1;
}
// Check that the input and the query in the verification are of the
Expand Down
22 changes: 3 additions & 19 deletions r1cs/gadgets/crypto/src/signature/schnorr/field_based_schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::{
borrow::Borrow,
marker::PhantomData,
};
use rand::rngs::OsRng;
use primitives::signature::schnorr::field_based_schnorr::FieldBasedSchnorrPk;
use r1cs_std::alloc::ConstantGadget;

Expand Down Expand Up @@ -366,21 +365,7 @@ impl<ConstraintF, G, GG, H, HG> FieldBasedSchnorrSigVerificationGadget<Constrain
e_bits
};

// Random shift to avoid exceptional cases if add is incomplete.
// With overwhelming probability the circuit will be satisfiable,
// otherwise the prover can sample another shift by re-running
// the proof creation.
let shift = GG::alloc(cs.ns(|| "alloc random shift"), || {
let mut rng = OsRng::default();
Ok(loop {
let r = G::rand(&mut rng);
if !r.is_zero() { break(r) }
})
})?;

let neg_e_times_pk = public_key
.mul_bits(cs.ns(|| "pk * e + shift"), &shift, e_bits.as_slice().iter().rev())?
.negate(cs.ns(|| "- (pk * e + shift)"))?;
let neg_e_times_pk = public_key.mul_bits(cs.ns(|| "pk * e"), e_bits.as_slice().iter().rev())?;

//Enforce s * G and R' = s*G - e*pk
let mut s_bits = {
Expand Down Expand Up @@ -409,12 +394,11 @@ impl<ConstraintF, G, GG, H, HG> FieldBasedSchnorrSigVerificationGadget<Constrain
let g = GG::from_value(cs.ns(|| "hardcode generator"), &G::prime_subgroup_generator());
let r_prime = GG::mul_bits_fixed_base(
&g.get_constant(),
cs.ns(|| "(s * G + shift)"),
&shift,
cs.ns(|| "(s * G)"),
s_bits.as_slice()
)?
// If add is incomplete, and s * G - e * pk = 0, the circuit of the add won't be satisfiable
.add(cs.ns(|| "s * G - e * pk "), &neg_e_times_pk)?;
.sub(cs.ns(|| "s * G - e * pk "), &neg_e_times_pk)?;

let r_prime_coords = r_prime.to_field_gadget_elements(cs.ns(|| "r_prime to fes"))?;

Expand Down
12 changes: 7 additions & 5 deletions r1cs/gadgets/crypto/src/signature/schnorr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ for SchnorrRandomizePkGadget<G, ConstraintF, GG>
.iter()
.flat_map(|b| b.into_bits_le())
.collect::<Vec<_>>();
let rand_pk = base.mul_bits(
&mut cs.ns(|| "Compute Randomizer"),
&public_key.pub_key,
randomness.iter(),
)?;
let rand_pk = {
let base_pow_rand = base.mul_bits(
&mut cs.ns(|| "Compute randomizer"),
randomness.iter(),
)?;
public_key.pub_key.add(cs.ns(|| "Randomize pk"), &base_pow_rand)
}?;
Ok(SchnorrSigGadgetPk {
pub_key: rand_pk,
_group: PhantomData,
Expand Down
32 changes: 8 additions & 24 deletions r1cs/gadgets/crypto/src/vrf/ecvrf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::{
marker::PhantomData,
borrow::Borrow,
};
use rand::rngs::OsRng;
use primitives::vrf::ecvrf::FieldBasedEcVrfPk;
use r1cs_std::bits::boolean::Boolean;

Expand Down Expand Up @@ -346,42 +345,27 @@ for FieldBasedEcVrfProofVerificationGadget<ConstraintF, G, GG, FH, FHG, GH, GHG>
&G::prime_subgroup_generator()
);

// Random shift to avoid exceptional cases if add is incomplete.
// With overwhelming probability the circuit will be satisfiable,
// otherwise the prover can sample another shift by re-running
// the proof creation.
let shift = GG::alloc(cs.ns(|| "alloc random shift"), || {
let mut rng = OsRng::default();
Ok(loop {
let r = G::rand(&mut rng);
if !r.is_zero() { break(r) }
})
})?;

//Check u = g^s - pk^c
let u =
{
let neg_c_times_pk = public_key.pk
.mul_bits(cs.ns(|| "pk * c + shift"), &shift, c_bits.as_slice().iter().rev())?
.negate(cs.ns(|| "- (c * pk + shift)"))?;
let c_times_pk = public_key.pk
.mul_bits(cs.ns(|| "pk * c"), c_bits.as_slice().iter().rev())?;
GG::mul_bits_fixed_base(&g.get_constant(),
cs.ns(|| "(s * G + shift)"),
&shift,
cs.ns(|| "s * G"),
s_bits.as_slice())?
// If add is incomplete, and s * G - c * pk = 0, the circuit of the add won't be satisfiable
.add(cs.ns(|| "(s * G) - (c * pk)"), &neg_c_times_pk)?
.sub(cs.ns(|| "(s * G) - (c * pk)"), &c_times_pk)?
};

//Check v = mh^s - gamma^c
let v =
{
let neg_c_times_gamma = proof.gamma
.mul_bits(cs.ns(|| "c * gamma + shift"), &shift, c_bits.as_slice().iter().rev())?
.negate(cs.ns(|| "- (c * gamma + shift)"))?;
let c_times_gamma = proof.gamma
.mul_bits(cs.ns(|| "c * gamma"), c_bits.as_slice().iter().rev())?;
message_on_curve
.mul_bits(cs.ns(|| "(s * mh + shift)"), &shift, s_bits.as_slice().iter())?
.mul_bits(cs.ns(|| "s * mh"), s_bits.as_slice().iter())?
// If add is incomplete, and s * mh - c * gamma = 0, the circuit of the add won't be satisfiable
.add(cs.ns(|| "(s * mh) - (c * gamma"), &neg_c_times_gamma)?
.sub(cs.ns(|| "(s * mh) - (c * gamma"), &c_times_gamma)?
};

// Check c' = H(m||pk.x||u.x||v.x)
Expand Down
2 changes: 1 addition & 1 deletion r1cs/gadgets/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ density-optimized = []
paste = "1.0"
rand = { version = "0.8.4" }
rand_xorshift = { version = "0.3.0" }
r1cs-std = { path = "../std", features = ["bls12_381", "jubjub", "tweedle", "secp256k1", "bn_382", "nonnative"] }
r1cs-std = { path = "../std", features = ["bls12_381", "jubjub", "tweedle", "secp256k1", "bn_382", "nonnative", "density-optimized"] }
Loading