1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use ff::Field;
use paired::Engine;
pub const DEFAULT_ROUNDS: usize = 1;
const SLOTH_V: [u64; 4] = [
3_689_348_813_023_923_405,
2_413_663_763_415_232_921,
16_233_882_818_423_549_954,
3_341_406_743_785_779_740,
];
const FIVE: [u64; 1] = [5];
#[inline]
pub fn encode<E: Engine>(key: &E::Fr, plaintext: &E::Fr, rounds: usize) -> E::Fr {
let mut ciphertext = *plaintext;
if rounds == 0 {
ciphertext.add_assign(key);
};
for _ in 0..rounds {
ciphertext.add_assign(key);
ciphertext = ciphertext.pow(&SLOTH_V);
}
ciphertext
}
#[inline]
pub fn decode<E: Engine>(key: &E::Fr, ciphertext: &E::Fr, rounds: usize) -> E::Fr {
let mut plaintext = *ciphertext;
for _ in 0..rounds {
plaintext = plaintext.pow(&FIVE);
plaintext.sub_assign(key);
}
if rounds == 0 {
plaintext.sub_assign(key);
}
plaintext
}
#[cfg(test)]
mod tests {
use super::*;
use ff::PrimeField;
use num_bigint::BigUint;
use paired::bls12_381::{Bls12, Fr, FrRepr};
use std::str::FromStr;
const MODULUS: [u64; 4] = [
0xffffffff00000001,
0x53bda402fffe5bfe,
0x3339d80809a1d805,
0x73eda753299d7d48,
];
const MODULUS_STR: &str =
&"52435875175126190479447740508185965837690552500527637822603658699938581184513";
const V_STR: &str =
&"20974350070050476191779096203274386335076221000211055129041463479975432473805";
#[test]
fn const_identities() {
let sloth_v = Fr::from_str(V_STR).unwrap();
assert_eq!(sloth_v, Fr::from_repr(FrRepr(SLOTH_V)).unwrap());
let v = BigUint::from_str(V_STR).unwrap();
let r = BigUint::from_str(MODULUS_STR).unwrap();
let one = BigUint::from(1u32);
let five = BigUint::from(5u32);
assert_eq!((v * five) % (r - &one), one);
}
#[test]
fn sloth_bls_12() {
sloth_bls_12_aux(0);
sloth_bls_12_aux(10);
}
fn sloth_bls_12_aux(rounds: usize) {
let key = Fr::from_str("11111111").unwrap();
let plaintext = Fr::from_str("123456789").unwrap();
let ciphertext = encode::<Bls12>(&key, &plaintext, rounds);
let decrypted = decode::<Bls12>(&key, &ciphertext, rounds);
assert_eq!(plaintext, decrypted);
assert_ne!(plaintext, ciphertext);
}
#[test]
fn sloth_bls_12_fake() {
sloth_bls_12_fake_aux(0);
sloth_bls_12_fake_aux(10);
}
fn sloth_bls_12_fake_aux(rounds: usize) {
let key = Fr::from_str("11111111").unwrap();
let key_fake = Fr::from_str("11111112").unwrap();
let plaintext = Fr::from_str("123456789").unwrap();
let ciphertext = encode::<Bls12>(&key, &plaintext, rounds);
let decrypted = decode::<Bls12>(&key_fake, &ciphertext, rounds);
assert_ne!(plaintext, decrypted);
}
prop_compose! {
fn arb_fr()(a in 0..MODULUS[0], b in 0..MODULUS[1], c in 0..MODULUS[2], d in 0..MODULUS[3]) -> Fr {
Fr::from_repr(FrRepr([a, b, c, d])).unwrap()
}
}
proptest! {
#[test]
fn sloth_bls_roundtrip(key in arb_fr(), plaintext in arb_fr()) {
let ciphertext = encode::<Bls12>(&key, &plaintext, 10);
assert_eq!(decode::<Bls12>(&key, &ciphertext, 10), plaintext);
}
}
}