Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into threshold-circuit-use-aggregator-1
Browse files Browse the repository at this point in the history
  • Loading branch information
duguorong009 committed Sep 11, 2023
2 parents 765264a + eafb21c commit 3a14a9d
Show file tree
Hide file tree
Showing 20 changed files with 589 additions and 464 deletions.
2 changes: 1 addition & 1 deletion eigentrust-cli/assets/attestations.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
about,domain,value,message,sig_r,sig_s,rec_id
0x70997970c51812dc3a010c7d01b50e0d17dc7668,0x0000000000000000000000000000000000000000,8,0x0000000000000000000000000000000000000000000000000000000000000000,0x274c5c01d85bbc9bf69f498d08ecca54f692ba2111910c35fb68e89f6a6de2a1,0x06215a213b4bf6a5b9608a7ac23d7f01e0a6e29b088df7d5a50ebd8d2896d03a,0
0x70997970c51812dc3a010c7d01b50e0d17dc79c8,0x0000000000000000000000000000000000000000,5,0x0000000000000000000000000000000000000000000000000000000000000000,0x1cc60bf64279111e233842283422f63cc34235adb4622a905aa325b6c8c44c4f,0xefa0e00156ab25f681075542d005f82e4b53ecdedee9d776268c0d7e7f325a21,1
4 changes: 1 addition & 3 deletions eigentrust-cli/assets/scores.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
peer_address,score_fr,numerator,denominator,score
0x70997970c51812dc3a010c7d01b50e0d17dc7666,0x268ac16f8598e86f93c3c0323f9d890b116e644167ea4b476dcc06209c0f274a,55358500,59049,937
0x70997970c51812dc3a010c7d01b50e0d17dc7667,0x28aef48fc67971ad01291f460e61d68e24b76dd4fe8a94b62dec5ac09d96966f,59787100,59049,1012
0x70997970c51812dc3a010c7d01b50e0d17dc7668,0x118ee6e67650e636dbb3abf4b50351211a41fe7a8cfe0124ec0b8a46a65a4e01,62001400,59049,1049
0x70997970c51812dc3a010c7d01b50e0d17dc79c8,0x00000000000000000000000000000000000000000000000000000000000003e8,1000,1,1000
0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266,0x00000000000000000000000000000000000000000000000000000000000003e8,1000,1,1000
8 changes: 4 additions & 4 deletions eigentrust-zk/src/circuits/dynamic_sets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use std::marker::PhantomData;
pub struct EigenTrustSetConfig<F: FieldExt, H, S>
where
H: HasherChipset<F, HASHER_WIDTH>,
S: SpongeHasherChipset<F, HASHER_WIDTH>,
S: SpongeHasherChipset<F>,
{
common: CommonConfig,
main: MainConfig,
Expand Down Expand Up @@ -81,7 +81,7 @@ pub struct EigenTrustSet<
C::ScalarExt: FieldExt,
H: HasherChipset<N, HASHER_WIDTH>,
HN: Hasher<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N>,
{
// Attestation
attestations: Vec<Vec<UnassignedSignedAttestation<C, N, NUM_LIMBS, NUM_BITS, P>>>,
Expand Down Expand Up @@ -119,7 +119,7 @@ where
C::ScalarExt: FieldExt,
H: HasherChipset<N, HASHER_WIDTH>,
HN: Hasher<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N>,
{
/// Constructs a new EigenTrustSet circuit
pub fn new(
Expand Down Expand Up @@ -213,7 +213,7 @@ impl<
C::ScalarExt: FieldExt,
H: HasherChipset<N, HASHER_WIDTH>,
HN: Hasher<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N>,
{
type Config = EigenTrustSetConfig<N, H, SH>;
type FloorPlanner = SimpleFloorPlanner;
Expand Down
36 changes: 29 additions & 7 deletions eigentrust-zk/src/circuits/dynamic_sets/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
FieldExt, Hasher, SpongeHasher,
};
use halo2::halo2curves::CurveAffine;
use itertools::Itertools;
use num_bigint::{BigInt, ToBigInt};
use num_rational::BigRational;
use num_traits::{FromPrimitive, One, Zero};
Expand Down Expand Up @@ -51,6 +50,14 @@ where

Self { attestation, signature }
}

/// Constructs a new empty attestation with about
pub fn empty_with_about(about: N, domain: N) -> Self {
let attestation = Attestation::<N> { about, domain, ..Default::default() };
let signature = Signature { r: Integer::one(), s: Integer::one(), ..Default::default() };

Self { attestation, signature }
}
}

impl<C: CurveAffine, N: FieldExt, const NUM_LIMBS: usize, const NUM_BITS: usize, P> Default
Expand Down Expand Up @@ -195,13 +202,28 @@ impl<
&mut self, from: PublicKey<C, N, NUM_LIMBS, NUM_BITS, P, EC>,
op: Vec<Option<SignedAttestation<C, N, NUM_LIMBS, NUM_BITS, P>>>,
) -> N {
let empty_att = SignedAttestation::empty(self.domain);
let op_unwrapped = op.iter().map(|x| x.clone().unwrap_or(empty_att.clone())).collect_vec();
let op = Opinion::<NUM_NEIGHBOURS, C, N, NUM_LIMBS, NUM_BITS, P, EC, H, SH>::new(
from, op_unwrapped, self.domain,
// Get participant set addresses
let set: Vec<N> = self.set.iter().map(|&(addr, _)| addr).collect();

// Build the opinion group by unwrapping attestations and filling the empty ones with default values.
// Enumerating to keep track of the participant this opinion is about.
let opinion_group = op
.into_iter()
.enumerate()
.map(|(index, attestation)| {
attestation.unwrap_or_else(|| {
SignedAttestation::<C, N, NUM_LIMBS, NUM_BITS, P>::empty_with_about(
set[index], self.domain,
)
})
})
.collect();

// Build opinion from the opinion group and validate
let opinion = Opinion::<NUM_NEIGHBOURS, C, N, NUM_LIMBS, NUM_BITS, P, EC, H, SH>::new(
from, opinion_group, self.domain,
);
let set = self.set.iter().map(|&(addr, _)| addr).collect();
let (addr, scores, op_hash) = op.validate(set);
let (addr, scores, op_hash) = opinion.validate(set);

self.ops.insert(addr, scores);

Expand Down
4 changes: 2 additions & 2 deletions eigentrust-zk/src/circuits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
params::hasher::poseidon_bn254_5x5::Params,
poseidon::{
native::{sponge::PoseidonSponge, Poseidon},
sponge::PoseidonSpongeChipset,
sponge::StatefulSpongeChipset,
FullRoundChip, PartialRoundChip, PoseidonChipset,
},
};
Expand Down Expand Up @@ -39,6 +39,6 @@ pub type PartialRoundHasher = PartialRoundChip<Scalar, HASHER_WIDTH, Params>;
/// Full rounds of permuation chip
pub type FullRoundHasher = FullRoundChip<Scalar, HASHER_WIDTH, Params>;
/// Type alias for the poseidon spong chip with a width of 5 and bn254 params
pub type SpongeHasher = PoseidonSpongeChipset<Scalar, HASHER_WIDTH, Params>;
pub type SpongeHasher = StatefulSpongeChipset<Scalar, HASHER_WIDTH, Params>;
/// Type alias for Eddsa chip on BabyJubJub elliptic curve
pub type Eddsa = EddsaChipset<Scalar, BabyJubJub, Params>;
10 changes: 5 additions & 5 deletions eigentrust-zk/src/circuits/opinion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ where
pub struct OpinionConfig<F: FieldExt, H, S>
where
H: HasherChipset<F, HASHER_WIDTH>,
S: SpongeHasherChipset<F, HASHER_WIDTH>,
S: SpongeHasherChipset<F>,
{
ecdsa: EcdsaConfig,
main: MainConfig,
Expand All @@ -298,7 +298,7 @@ where
impl<F: FieldExt, H, S> OpinionConfig<F, H, S>
where
H: HasherChipset<F, HASHER_WIDTH>,
S: SpongeHasherChipset<F, HASHER_WIDTH>,
S: SpongeHasherChipset<F>,
{
/// Construct a new config
pub fn new(
Expand Down Expand Up @@ -327,7 +327,7 @@ pub struct OpinionChipset<
C::Base: FieldExt,
C::ScalarExt: FieldExt,
H: HasherChipset<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N>,
{
/// Domain of the attestations
domain: AssignedCell<N, N>,
Expand Down Expand Up @@ -362,7 +362,7 @@ where
C::Base: FieldExt,
C::ScalarExt: FieldExt,
H: HasherChipset<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N>,
{
/// Create a new chip.
pub fn new(
Expand Down Expand Up @@ -401,7 +401,7 @@ where
C::Base: FieldExt,
C::ScalarExt: FieldExt,
H: HasherChipset<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N, HASHER_WIDTH>,
SH: SpongeHasherChipset<N>,
{
type Config = OpinionConfig<N, H, SH>;
type Output = (Vec<AssignedCell<N, N>>, AssignedCell<N, N>);
Expand Down
4 changes: 2 additions & 2 deletions eigentrust-zk/src/circuits/threshold/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ mod tests {
let pub_ins = vec![target_addr, threshold, native_threshold_check];

// Prepare the Aggregator input
let NativeAggregator { svk, snarks, instances, as_proof } = {
let NativeAggregator { svk, snarks, instances, as_proof, .. } = {
let rng = &mut thread_rng();
let k = 20;
let params = generate_params::<Bn256>(k);
Expand Down Expand Up @@ -866,7 +866,7 @@ mod tests {
let pub_ins = vec![target_addr, threshold, native_threshold_check];

// Prepare the Aggregator input
let NativeAggregator { svk, snarks, instances, as_proof } = {
let NativeAggregator { svk, snarks, instances, as_proof, .. } = {
let rng = &mut thread_rng();
let k = 20;
let params = generate_params::<Bn256>(k);
Expand Down
11 changes: 2 additions & 9 deletions eigentrust-zk/src/ecdsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,14 +810,7 @@ mod test {
fn test_ecdsa() {
// Test Halo2 ECDSA verify
let rng = &mut rand::thread_rng();
let keypair = EcdsaKeypair::<
Secp256k1Affine,
N,
NUM_LIMBS,
NUM_BITS,
Secp256k1_4_68,
Secp256k1Params,
>::generate_keypair(rng);
let keypair = EcdsaKeypair::<C, N, NUM_LIMBS, NUM_BITS, P, EC>::generate_keypair(rng);
let public_key = keypair.public_key.clone();

let msg_hash = SecpScalar::from_u128(123456789);
Expand All @@ -828,7 +821,7 @@ mod test {
let s_inv = Integer::from_w(s_inv_fq);

let g = Secp256k1::generator().to_affine();
let g_as_ecpoint = EcPoint::<Secp256k1Affine, N, NUM_LIMBS, NUM_BITS, P, EC>::new(
let g_as_ecpoint = EcPoint::<C, N, NUM_LIMBS, NUM_BITS, P, EC>::new(
Integer::from_w(g.x),
Integer::from_w(g.y),
);
Expand Down
18 changes: 9 additions & 9 deletions eigentrust-zk/src/ecdsa/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ where
let rev_y: Vec<u8> = y.iter().rev().cloned().collect();
let pub_key = [rev_x, rev_y].concat();

// Hash and get first 20 bytes.
let hashed_public_key = Keccak256::digest(&pub_key);
let address_slice = &Keccak256::digest(&pub_key)[hashed_public_key.len() - 20..];
// Hash and get the last 20 bytes.
let pub_key_hash = Keccak256::digest(pub_key);
let address: &[u8] = &pub_key_hash[pub_key_hash.len() - 20..];

// Build fixed-size array.
let mut address = [0u8; 32];
address[..20].copy_from_slice(address_slice);
// Get little endian address
let le_address: Vec<u8> = address.iter().rev().cloned().collect();

let mut address_bytes = <N as PrimeField>::Repr::default();
address.as_ref().read_exact(address_bytes.as_mut()).unwrap();
// Build fixed-size array.
let mut address = [0u8; 64];
address[..20].copy_from_slice(&le_address);

N::from_repr(address_bytes).unwrap()
N::from_uniform_bytes(&address)
}
}

Expand Down
2 changes: 1 addition & 1 deletion eigentrust-zk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub trait HasherChipset<F: FieldExt, const WIDTH: usize>: Chipset<F> + Clone {
}

/// Sponge Hasher chipset trait
pub trait SpongeHasherChipset<F: FieldExt, const WIDTH: usize>: Clone {
pub trait SpongeHasherChipset<F: FieldExt>: Clone {
/// Config selectors for the sponge
type Config: Clone;
/// Creates a new sponge hasher chipset
Expand Down
2 changes: 1 addition & 1 deletion eigentrust-zk/src/poseidon/sponge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ where
default: AssignedCell<F, F>,
}

impl<F: FieldExt, const WIDTH: usize, P> SpongeHasherChipset<F, WIDTH>
impl<F: FieldExt, const WIDTH: usize, P> SpongeHasherChipset<F>
for StatefulSpongeChipset<F, WIDTH, P>
where
P: RoundParams<F, WIDTH>,
Expand Down
Loading

0 comments on commit 3a14a9d

Please sign in to comment.