admin管理员组

文章数量:1391925

I'm working on a Circom circuit to calculate the BMI (Body Mass Index) and check if a person is healthy based on their height and weight. However, I'm currently using a custom div function for division and implementing a BMI check that isn't fully optimized. Below is the code I've written so far:

pragma circom 2.0.0;

function div(a, b) {
    var r = a;
    var q = 0;
    while (r >= b) {
        r = r - b;
        q = q + 1;
    }
    return q;
}

template BMIChecker() {

    // Declaration of signals.
    signal input height; // in cm(centimeters)
    signal input weight; // in kg(kilograms) * 10000000(10^7)
    signal output isHealthy; // 1 if healthy, 0 if not healthy
    var max = 25000;
    var min = 18499;
    
    signal height_squared <== height * height;

    var bmi = div(weight, height_squared);

    signal output inv <-- bmi > min && bmi < max ? 1 : 0;

    log("isHealthy: ", inv);

    isHealthy <== inv;
    isHealthy === 1;
}

component main = BMIChecker();

Questions:

  1. Optimization: The custom div function I've written performs repeated subtraction, but it seems inefficient for larger inputs. Is there a more efficient or optimized way to handle division in Circom?

  2. BMI Calculation: The BMI is calculated as weight / (height^2). In my code, I manually calculate height squared and use a custom division function. Are there better ways to handle this within Circom, such as using specific circuits or built-in functions?

  3. Bounds Checking: In the current implementation, I am using a conditional expression to check if the BMI is between a defined range. Would this approach be the most efficient in Circom, or is there a better way to implement the check for a healthy BMI?

Any help with optimizing this BMI circuit for better performance or more efficient computation would be greatly appreciated!

本文标签: What is the best way to implement a custom div function and BMI check in CircomStack Overflow