jasmin-lang / jasmin

Language for high-assurance and high-speed cryptography
MIT License
268 stars 55 forks source link

asmgen: (compile_arg) not compatible asm_arg #930

Open y4cer opened 1 week ago

y4cer commented 1 week ago

Good day!

I encountered the internal compilation error, which kindly asked to report issue here

"src/util.jazz", line 33 (2-36):                                                                                                                                                                            
internal compilation error in function dot_product:                                                                                                                                                         
  asmgen: (compile_arg) not compatible asm_arg                                                                                                                                                              
Please report at https://github.com/jasmin-lang/jasmin/issues                                                                                                                                               
make: *** [Makefile:18: build/jasmin/util.s] Error 1  

Here is the code snippet, the error happens in the second for loop, I also added comments there for better visibility.

export
fn dot_product(reg u64 l, reg u64 r) -> reg u64 {

    inline u64 rec, nwords;

    // wordwise dot product
    inline int i;
    reg u64 w;
    w = 0;
    nwords = n / 64;
    for i=0 to nwords {
        w ^= (u64)[l + i] & (u64)[r + i];
    }

    // bytewise dot product
    inline int start;
    start = nwords * 64;
    rec = n % 64;
    reg u64 b, tmp;
    b = 0;
    for i=0 to rec {
        tmp = (u64)[l + start + i] & 0xff;         // <- ERROR IS HERE
        b ^= tmp;
        tmp = (u64)[r + start + i] & 0xff;        // <- ERROR IS HERE
        b ^= tmp;
    }
    reg u64 res;
    res = 0;

    res ^= (w & 0xff);
    res ^= b;

    return b;
}

At the time of this issue resolution, is there a workaround for this problem?

vbgl commented 1 week ago

Thanks for the report. There is indeed a bug.

If you want to just load 8 bits, you can use an 8-bit load: tmp = (u8)[l + start + i];.

There is probably an other issue in your program there: w ^= (u64)[l + i] & (u64)[r + i]; You are trying to do two memory accesses, and two logic/arithmetic operations in a single instructions. That’s too much. You’ll have to split this into smaller steps.