NEWTON

NEWTON


Popular tags

    How to make math operation with Field Elements (felts) in Cairo lang?

    Asked

    3 months ago

    124

    views


    0

    Hello! This is day 8 of the 17 days of the Cairo Challenge. And I have no idea how to solve the playground exercise “Field Elements”. Perhaps you can help me?

    // Unlike most systems where the primitive type is a 32 or 64-bit integer,
    // the primitive type in Cairo is "felt", which stands for "field element".
    // When we say "a field element" we mean an integer in the range -P / 2 < x < P / 2,
    // where P is a very large (prime) number.
    // Currently we use a 252-bit number (76 decimal digits).
    // When we add, subtract or multiply and the result is outside the range above,
    // there is an overflow.
    //
    // The most important difference between integers and field elements is division:
    // Division of field elements (and therefore division in Cairo) is not the
    // integer division you may be used to.
    // In the next challenge, you'll see how to use unsigned_div_rem()
    // for truncated (i.e., rounding down) integer division.
    //
    // Make the code below output the results of the following expressions:
    // 1. 1 + 100, 1 - 100, 9 * 9.
    // 2. pow(10, 74), pow(10, 75), pow(10, 76)
    // 3. 12 / 3, (-12) / 3
    // 4. 13 / 3 (This is an example of division in the field)
    
    %builtins output
    
    from starkware.cairo.common.serialize import serialize_word
    
    // Computes base^exp.
    func pow(base: felt, exp: felt) -> (res: felt) {
        if (exp == 0) {
            return (res=1);
        }
    
        let (res) = pow(base=base, exp=exp - 1);
        return (res=res * base);
    }
    
    func main{output_ptr: felt*}() {
        serialize_word(1 + 100);
        // Add your code here.
    
        return ();
    }
    

    Answers to this question are a part of the ✨ 17 days of Cairo Lang with Playground & Newton. ✨

    Vote for your favorite answer - the best answer will win a $10 award. A new day – a new reward! During the next 17 days, our goal is to attract more developers to the Cairo language and to systematize the knowledge of Cairo lang. Read rules

      #17daysOfCairocairo-beginnerscairoplayground

    Newton

    asked

    3 months ago


    2 answers

    1

    Accepted answer

    Solution

    %builtins output
    
    from starkware.cairo.common.serialize import serialize_word
    
    // Computes base^exp.
    func pow(base: felt, exp: felt) -> (res: felt) {
        if (exp == 0) {
            return (res=1);
        }
    
        let (res) = pow(base=base, exp=exp - 1);
        return (res=res * base);
    }
    // 1. 1 + 100, 1 - 100, 9 * 9.
    // 2. pow(10, 74), pow(10, 75), pow(10, 76)
    // 3. 12 / 3, (-12) / 3
    // 4. 13 / 3 (This is an example of division in the field)
    
    func main{output_ptr: felt*}() {
    alloc_locals;
    //Addition and subtraction
    serialize_word(1 + 100);
    serialize_word(1-100);
    serialize_word(9*9);
    //power
    let(result)=pow(10,74);
    serialize_word(result);
    
    let(result2)=pow(10,75);
    serialize_word(result2);
    //out of range
    let(result3)=pow(10,76);
    serialize_word(result3);
    //Division
    serialize_word(12/3);
    serialize_word(-12/3);
    //overflow will occur
    serialize_word(13/3);
    
    return ();
    }
    

    Output

    Program output:
      101
      -99
      81
      100000000000000000000000000000000000000000000000000000000000000000000000000
      1000000000000000000000000000000000000000000000000000000000000000000000000000
      -855508365998393641091968349285210316869321645994790099919276168407616061443
      4
      -4
      1206167596222043737899107594365023368541035738443865566657697352045290673498
    
    Number of steps: 1422
    Program hash: 0x06ae6cf8429a50cc2acf3ddca91271df8c4ac533750824ce0a13934991245e51
    

    Division in Cairo

    Unlike regular CPUs where integer division x / y is defined as ⌊ x / y ⌋ (so 7 / 2 = 3 ) and it may or may not satisfy the equation (x / y) * y == x , in Cairo, the result of x / y is defined to always satisfy the equation (x / y) * y == x .

    https://www.cairo-lang.org/docs/hello_cairo/intro.html#the-primitive-type-field-element-felt

    Ishita Rastogi

    answered

    3 months ago

    0

    This is my solution for this challenge:

    %builtins output
    
    from starkware.cairo.common.alloc import alloc
    from starkware.cairo.common.serialize import serialize_word
    
    // Computes base^exp.
    func pow(base: felt, exp: felt) -> (res: felt) {
        if (exp == 0) {
            return (res=1);
        }
    
        let (res) = pow(base=base, exp=exp - 1);
        return (res=res * base);
    }
    
    func main{output_ptr: felt*}() {
        alloc_locals;
        let (res1:felt) = pow(10,74);
        let (res2:felt) = pow(10,75);
        let (res3:felt) = pow(10,76);
    
        serialize_word(1 + 100);
        // Add your code here.
        serialize_word(100-1);
        serialize_word(9*9);
        serialize_word(res1);
        serialize_word(res2);
        serialize_word(res3);
        serialize_word(12/3);
        serialize_word(-12/3);
        serialize_word(13/3);
    
        
        
    
        return ();
    }
    

    And this is the output:

    101 99 81 100000000000000000000000000000000000000000000000000000000000000000000000000 1000000000000000000000000000000000000000000000000000000000000000000000000000 -855508365998393641091968349285210316869321645994790099919276168407616061443 4 -4 1206167596222043737899107594365023368541035738443865566657697352045290673498 Number of steps: 1418 Program hash: 0x035cc12e58b157f112f80b514355324089313c3543d346f2d90a49b0a6511e6d~~~~

    answered

    3 months ago

    Your answer

    NEWTON

    NEWTON