MCPcopy Create free account
hub / github.com/BitVM/BitVM / hinted_check_add

Method hinted_check_add

bitvm/src/bn254/g1.rs:234–332  ·  view source on GitHub ↗
(t: ark_bn254::G1Affine, q: ark_bn254::G1Affine)

Source from the content-addressed store, hash-verified

232 }
233
234 pub fn hinted_check_add(t: ark_bn254::G1Affine, q: ark_bn254::G1Affine) -> (Script, Vec<Hint>) {
235 let mut hints = vec![];
236
237 let (alpha, bias) = if t.is_zero() || q.is_zero() {
238 // no hint, dummy zero
239 (ark_bn254::Fq::ZERO, ark_bn254::Fq::ZERO)
240 } else if t == -q {
241 // no hint, dummy zero
242 (ark_bn254::Fq::ZERO, ark_bn254::Fq::ZERO)
243 } else if t == q {
244 // doubling hints
245 let alpha = (t.x.square() + t.x.square() + t.x.square()) / (t.y + t.y);
246 let bias = t.y - alpha * t.x;
247 (alpha, bias)
248 } else {
249 // adding hints
250 let alpha = (t.y - q.y) / (t.x - q.x);
251 let bias = t.y - alpha * t.x;
252 (alpha, bias)
253 };
254
255 let (hinted_script1, hint1) = Self::hinted_check_chord_line(t, q, alpha);
256 let (hinted_script2, hint2) = Self::hinted_add(t.x, q.x, alpha);
257 let (hinted_script3, hint3) = Self::hinted_check_tangent_line(t, alpha);
258
259 let script = script! { // tx ty qx qy
260 { G1Affine::is_zero_keep_element() }
261 OP_IF
262 { G1Affine::drop() }
263 OP_ELSE
264 { G1Affine::roll(2) }
265 { G1Affine::is_zero_keep_element() }
266 OP_IF
267 { G1Affine::drop() }
268 OP_ELSE // qx qy tx ty
269 { Fq::equal_keep_elements(3, 1) } // t == q or t == -q
270 OP_DUP
271 OP_TOALTSTACK
272 OP_TOALTSTACK
273 { Fq::equal_keep_elements(2, 0) }
274 OP_NOT
275 OP_FROMALTSTACK
276 OP_BOOLAND
277 OP_IF // case: t == -q
278 OP_FROMALTSTACK OP_DROP
279 { G1Affine::drop() }
280 { G1Affine::drop() }
281 { G1Affine::identity() }
282 OP_ELSE // case: t != -q
283 for _ in 0..Fq::N_LIMBS {
284 OP_DEPTH OP_1SUB OP_ROLL
285 }
286 { Fq::check_validity_and_keep_element() }
287 for _ in 0..Fq::N_LIMBS {
288 OP_DEPTH OP_1SUB OP_ROLL
289 } // qx qy tx ty c3 c4
290 { Fq::check_validity_and_keep_element() }
291 { Fq::copy(1) } // qx qy tx ty c3 c4 c3

Callers

nothing calls this directly

Calls 5

hinted_check_chord_lineFunction · 0.85
FqClass · 0.85
is_zeroMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected