-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzkfloat.nr
177 lines (142 loc) · 4.41 KB
/
zkfloat.nr
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Struct representing float numbers using sign, mantissa and exponent.
// When Noir language gets the update to support signed integers, the sign field will be removed
struct Float {
sign: Field,
mantissa: Field,
exponent: Field,
}
// Float number precision of mantissa
global precision : Field = 7;
// ReLU activation function used for neural network ML models
fn relu(x : Float) -> Float {
let mut res = x;
if x.sign as u64 == 1 {
res = Float { sign: 0, mantissa: 0, exponent: 100 };
}
res
}
// Truncate Float to "precision" number of digits, 5 in the example
fn truncate(num: Float) -> Float {
let lookup : [Field; 25] = [
1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000,
100000000000,
1000000000000,
10000000000000,
100000000000000,
1000000000000000,
10000000000000000,
100000000000000000,
1000000000000000000,
10000000000000000000,
100000000000000000000,
1000000000000000000000,
10000000000000000000000,
100000000000000000000000,
1000000000000000000000000,
];
let maxValue : Field = 10.pow_32(precision);
let mut decValue : Field = 1;
let mut logValue : Field = 0;
for i in 0..25 {
if num.mantissa as u64 >= lookup[i] as u64 {
decValue = lookup[i];
logValue = i;
}
}
decValue *= 10;
logValue += 1;
let mut res : Float = Float { sign: num.sign, mantissa: num.mantissa, exponent: num.exponent };
if logValue as u64 > precision as u64 {
let diff = (decValue / maxValue) as u64;
res = Float { sign: num.sign, mantissa: (num.mantissa as u64 / diff) as Field, exponent: num.exponent + (logValue - precision)}; //
}
if res.mantissa == 0 {
res = Float { sign: res.sign, mantissa: 0, exponent: 100 };
}
res
}
// Multiplication of Float numbers
fn mulFloats(x : Float, y : Float) -> Float {
let mant = x.mantissa * y.mantissa;
let exp = x.exponent + y.exponent - 100;
let mut sign : Field = 0;
if x.sign != y.sign {
sign = 1;
}
truncate(Float { sign: sign, mantissa: mant, exponent: exp })
}
// Dividing of Float numbers
fn divFloats(x : Float, y: Float) -> Float {
assert(y.mantissa > 0);
let mut exp1: Field = x.exponent;
let mut mant1: u64 = x.mantissa as u64;
let exp2: Field = y.exponent;
let mant2: Field = y.mantissa;
// Can't divide lower by higher number with same precision, result will be 0
// The lower must be multiplied by 10, it means at the same time exponent must be reduced by 1
if mant1 < mant2 as u64 {
mant1 *= 10;
exp1 -= 1;
}
let mut new_mant: u64 = 0;
for i in 0..7 {
let div = mant1 / mant2 as u64;
mant1 = (mant1 - mant2 as u64 * div) * 10;
// For precision N, the highest exponent is 10^(N-1)
let exp = precision - i - 1;
let pow = 10.pow_32(exp) as u64;
new_mant += div * pow;
}
let new_exp = 100 + exp1 - exp2 - precision + 1;
let mut new_sign : Field = 0;
if x.sign as u64 != y.sign as u64 {
new_sign = 1;
}
Float{sign: new_sign, mantissa: new_mant as Field, exponent: new_exp}
}
// Sumation of Float numbers
fn addFloats(x : Float, y : Float) -> Float {
let mut mant_1 = x.mantissa;
let mut mant_2 = y.mantissa;
let mut exp_1 = x.exponent;
let mut exp_2 = y.exponent;
let mut diff : Field = 0;
if exp_1 as u64 > exp_2 as u64 {
diff = exp_1 - exp_2;
} else {
diff = exp_2 - exp_1;
}
let mut pow10 : Field = 10.pow_32(diff);
if x.exponent as u64 < y.exponent as u64 {
mant_2 *= pow10;
exp_1 = x.exponent;
} else {
mant_1 *= pow10;
exp_1 = y.exponent;
}
let mut sum_mant = mant_1 + mant_2;
let mut sign = x.sign;
if x.sign != y.sign {
if mant_1 as u64 > mant_2 as u64 {
sum_mant = mant_1 - mant_2;
} else {
sum_mant = mant_2 - mant_1;
sign = y.sign;
}
}
truncate(Float { sign: sign, mantissa: sum_mant, exponent: exp_1 })
}
// Subtraction of float numbers
fn subFloats(x : Float, y : Float) -> Float {
addFloats(x, Float { sign: 1 - y.sign, mantissa: y.mantissa, exponent: y.exponent })
}