Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zudo committed Jun 16, 2023
1 parent 3c801e0 commit 3cd1294
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 52 deletions.
18 changes: 8 additions & 10 deletions src/blsag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,22 @@ impl BLSAG {
let image = image::<Hash>(secret);
let secret_index = rng.gen_range(0..=ring.len());
ring.insert(secret_index, secret * RISTRETTO_BASEPOINT_POINT);
let ring_size = ring.len();
let x = ring.len();
let hash = Hash::new().chain_update(data);
let mut hashes = (0..ring_size).map(|_| hash.clone()).collect::<Vec<_>>();
let mut current_index = (secret_index + 1) % ring_size;
let mut hashes = (0..x).map(|_| hash.clone()).collect::<Vec<_>>();
let mut current_index = (secret_index + 1) % x;
let r = scalar_random(rng);
hashes[current_index].update((r * RISTRETTO_BASEPOINT_POINT).compress().as_bytes());
hashes[current_index].update(
(r * point_hash::<Hash>(ring[secret_index]))
.compress()
.as_bytes(),
);
let mut challenges = vec![scalar_zero(); ring_size];
let mut challenges = vec![scalar_zero(); x];
challenges[current_index] = scalar_from_hash(hashes[current_index].clone());
let mut response = (0..ring_size)
.map(|_| scalar_random(rng))
.collect::<Vec<_>>();
let mut response = (0..x).map(|_| scalar_random(rng)).collect::<Vec<_>>();
loop {
let next_index = (current_index + 1) % ring_size;
let next_index = (current_index + 1) % x;
hashes[next_index].update(
RistrettoPoint::multiscalar_mul(
&[response[current_index], challenges[current_index]],
Expand All @@ -67,8 +65,8 @@ impl BLSAG {
.as_bytes(),
);
challenges[next_index] = scalar_from_hash(hashes[next_index].clone());
if (secret_index >= 1 && current_index == (secret_index - 1) % ring_size)
|| (secret_index == 0 && current_index == ring_size - 1)
if (secret_index >= 1 && current_index == (secret_index - 1) % x)
|| (secret_index == 0 && current_index == x - 1)
{
break;
}
Expand Down
64 changes: 32 additions & 32 deletions src/mlsag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,64 +28,64 @@ impl MLSAG {
mut rings: Vec<Vec<RistrettoPoint>>,
message: impl AsRef<[u8]>,
) -> Option<MLSAG> {
let nr = rings.len() + 1;
let nc = rings[0].len();
let x = rings.len() + 1;
let y = rings[0].len();
let k_points = secrets
.iter()
.map(|scalar| scalar * RISTRETTO_BASEPOINT_POINT)
.collect::<Vec<_>>();
let images = MLSAG::image::<Hash>(secrets);
let secret_index = rng.gen_range(0..nr);
let secret_index = rng.gen_range(0..x);
rings.insert(secret_index, k_points.clone());
let a: Vec<Scalar> = (0..nc).map(|_| scalar_random(rng)).collect();
let mut responses = (0..nr)
.map(|_| (0..nc).map(|_| scalar_random(rng)).collect())
let a: Vec<Scalar> = (0..y).map(|_| scalar_random(rng)).collect();
let mut responses = (0..x)
.map(|_| (0..y).map(|_| scalar_random(rng)).collect())
.collect::<Vec<Vec<_>>>();
let mut challenges: Vec<Scalar> = (0..nr).map(|_| scalar_zero()).collect();
let mut challenges: Vec<Scalar> = (0..x).map(|_| scalar_zero()).collect();
let mut hash = Hash::new();
hash.update(message);
let mut hashes: Vec<Hash> = (0..nr).map(|_| hash.clone()).collect();
for j in 0..nc {
hashes[(secret_index + 1) % nr]
let mut hashes: Vec<Hash> = (0..x).map(|_| hash.clone()).collect();
for j in 0..y {
hashes[(secret_index + 1) % x]
.update((a[j] * RISTRETTO_BASEPOINT_POINT).compress().as_bytes());
hashes[(secret_index + 1) % nr].update(
hashes[(secret_index + 1) % x].update(
(a[j] * point_hash::<Hash>(k_points[j]))
.compress()
.as_bytes(),
);
}
challenges[(secret_index + 1) % nr] =
scalar_from_hash(hashes[(secret_index + 1) % nr].clone());
let mut i = (secret_index + 1) % nr;
challenges[(secret_index + 1) % x] =
scalar_from_hash(hashes[(secret_index + 1) % x].clone());
let mut i = (secret_index + 1) % x;
loop {
for j in 0..nc {
hashes[(i + 1) % nr].update(
for j in 0..y {
hashes[(i + 1) % x].update(
RistrettoPoint::multiscalar_mul(
&[responses[i % nr][j], challenges[i % nr]],
&[RISTRETTO_BASEPOINT_POINT, rings[i % nr][j]],
&[responses[i % x][j], challenges[i % x]],
&[RISTRETTO_BASEPOINT_POINT, rings[i % x][j]],
)
.compress()
.as_bytes(),
);
hashes[(i + 1) % nr].update(
hashes[(i + 1) % x].update(
RistrettoPoint::multiscalar_mul(
&[responses[i % nr][j], challenges[i % nr]],
&[point_hash::<Hash>(rings[i % nr][j]), images[j]],
&[responses[i % x][j], challenges[i % x]],
&[point_hash::<Hash>(rings[i % x][j]), images[j]],
)
.compress()
.as_bytes(),
);
}
challenges[(i + 1) % nr] = scalar_from_hash(hashes[(i + 1) % nr].clone());
if secret_index >= 1 && i % nr == (secret_index - 1) % nr {
challenges[(i + 1) % x] = scalar_from_hash(hashes[(i + 1) % x].clone());
if secret_index >= 1 && i % x == (secret_index - 1) % x {
break;
} else if secret_index == 0 && i % nr == nr - 1 {
} else if secret_index == 0 && i % x == x - 1 {
break;
} else {
i = (i + 1) % nr;
i = (i + 1) % x;
}
}
for j in 0..nc {
for j in 0..y {
responses[secret_index][j] = a[j] - (challenges[secret_index] * secrets[j]);
}
Some(MLSAG {
Expand Down Expand Up @@ -131,12 +131,12 @@ impl MLSAG {
.collect::<Option<Vec<Vec<_>>>>()?;
let challenge_0 = scalar_from_canonical(self.challenge)?;
let mut challenge_1 = challenge_0;
let nr = self.rings.len();
let nc = self.rings[0].len();
for i in 0..nr {
let x = self.rings.len();
let y = self.rings[0].len();
for i in 0..x {
let mut hash = Hash::new();
hash.update(&data);
for j in 0..nc {
for j in 0..y {
hash.update(
RistrettoPoint::multiscalar_mul(
&[responses[i][j], challenge_1],
Expand All @@ -163,12 +163,12 @@ impl MLSAG {
}
}
pub fn image<Hash: Digest<OutputSize = U64>>(secrets: &[Scalar]) -> Vec<RistrettoPoint> {
let nc = secrets.len();
let x = secrets.len();
let publics = secrets
.iter()
.map(|scalar| scalar * RISTRETTO_BASEPOINT_POINT)
.collect::<Vec<_>>();
(0..nc)
(0..x)
.map(|i| secrets[i] * point_hash::<Hash>(publics[i]))
.collect()
}
Expand Down
18 changes: 8 additions & 10 deletions src/sag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,21 @@ impl SAG {
) -> Option<SAG> {
let secret_index = rng.gen_range(0..=ring.len());
ring.insert(secret_index, secret * RISTRETTO_BASEPOINT_POINT);
let ring_size = ring.len();
let x = ring.len();
let hash = Hash::new().chain_update(data);
let mut hashes = (0..ring_size).map(|_| hash.clone()).collect::<Vec<_>>();
let mut current_index = (secret_index + 1) % ring_size;
let mut hashes = (0..x).map(|_| hash.clone()).collect::<Vec<_>>();
let mut current_index = (secret_index + 1) % x;
let secret_scalar_1 = scalar_random(rng);
hashes[current_index].update(
(secret_scalar_1 * RISTRETTO_BASEPOINT_POINT)
.compress()
.as_bytes(),
);
let mut challenges = vec![scalar_zero(); ring_size];
let mut challenges = vec![scalar_zero(); x];
challenges[current_index] = scalar_from_hash(hashes[current_index].clone());
let mut response = (0..ring_size)
.map(|_| scalar_random(rng))
.collect::<Vec<_>>();
let mut response = (0..x).map(|_| scalar_random(rng)).collect::<Vec<_>>();
loop {
let next_index = (current_index + 1) % ring_size;
let next_index = (current_index + 1) % x;
hashes[next_index].update(
RistrettoPoint::multiscalar_mul(
&[response[current_index], challenges[current_index]],
Expand All @@ -54,8 +52,8 @@ impl SAG {
.as_bytes(),
);
challenges[next_index] = scalar_from_hash(hashes[next_index].clone());
if (secret_index >= 1 && current_index == (secret_index - 1) % ring_size)
|| (secret_index == 0 && current_index == ring_size - 1)
if (secret_index >= 1 && current_index == (secret_index - 1) % x)
|| (secret_index == 0 && current_index == x - 1)
{
break;
}
Expand Down

0 comments on commit 3cd1294

Please sign in to comment.